-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
/
styled.test.js
120 lines (105 loc) · 3.36 KB
/
styled.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import React from 'react';
import { assert } from 'chai';
import PropTypes from 'prop-types';
import styled from './styled';
import { SheetsRegistry } from 'jss';
import { createMount } from '@material-ui/core/test-utils';
import { createGenerateClassName } from '@material-ui/styles';
import consoleErrorMock from 'test/utils/consoleErrorMock';
import StylesProvider from '../StylesProvider';
describe('styled', () => {
let mount;
let StyledButton;
before(() => {
// StrictModeViolation: uses makeStyles
mount = createMount({ strict: false });
StyledButton = styled('button')({
background: 'linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)',
borderRadius: 3,
border: 0,
color: 'white',
height: 48,
padding: '0 30px',
boxShadow: '0 3px 5px 2px rgba(255, 105, 135, .3)',
});
});
after(() => {
mount.cleanUp();
});
it('should work as expected', () => {
const sheetsRegistry = new SheetsRegistry();
const generateClassName = createGenerateClassName();
mount(
<StylesProvider sheetsRegistry={sheetsRegistry} generateClassName={generateClassName}>
<StyledButton>Styled Components</StyledButton>
</StylesProvider>,
);
assert.strictEqual(sheetsRegistry.registry.length, 1);
assert.deepEqual(sheetsRegistry.registry[0].classes, { root: 'button-root-1' });
});
describe('prop: clone', () => {
let wrapper;
before(() => {
wrapper = mount(
<StyledButton clone data-test="enzyme">
<div>Styled Components</div>
</StyledButton>,
);
});
it('should be able to pass props to cloned element', () => {
assert.strictEqual(wrapper.find('div').props()['data-test'], 'enzyme');
});
it('should be able to clone the child element', () => {
assert.strictEqual(wrapper.getDOMNode().nodeName, 'DIV');
wrapper.setProps({
clone: false,
});
assert.strictEqual(wrapper.getDOMNode().nodeName, 'BUTTON');
});
});
it('should filter some props', () => {
const style = props => ({
background: props.color,
borderRadius: 3,
border: 0,
color: 'white',
height: 48,
padding: '0 30px',
boxShadow: '0 3px 5px 2px rgba(255, 105, 135, .3)',
});
style.filterProps = ['color'];
style.propTypes = {};
const StyledDiv = styled('div')(style);
const wrapper = mount(
<StyledDiv data-test="enzyme" color="blue">
Styled Components
</StyledDiv>,
);
assert.strictEqual(wrapper.find('div').props().color, undefined);
assert.strictEqual(wrapper.find('div').props()['data-test'], 'enzyme');
});
describe('warnings', () => {
beforeEach(() => {
consoleErrorMock.spy();
});
afterEach(() => {
consoleErrorMock.reset();
PropTypes.resetWarningCache();
});
it('warns if it cant detect the secondary action properly', () => {
mount(
<StyledButton clone component="div">
<div>Styled Components</div>
</StyledButton>,
);
assert.strictEqual(consoleErrorMock.callCount(), 1);
assert.include(
consoleErrorMock.args()[0][0],
'You can not use the clone and component prop at the same time',
);
});
});
it('should accept a child function', () => {
mount(<StyledButton>{props => <div {...props}>Styled Components</div>}</StyledButton>);
});
});