-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
/
NativeSelectInput.test.js
155 lines (131 loc) · 4.65 KB
/
NativeSelectInput.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import * as React from 'react';
import { expect } from 'chai';
import { spy } from 'sinon';
import { describeConformance, createRenderer, fireEvent } from 'test/utils';
import { createTheme, ThemeProvider } from '@mui/material/styles';
import NativeSelectInput from './NativeSelectInput';
import nativeSelectClasses from './nativeSelectClasses';
describe('<NativeSelectInput />', () => {
const { render } = createRenderer();
describeConformance(<NativeSelectInput IconComponent="div" />, () => ({
only: ['refForwarding'],
refInstanceof: window.HTMLSelectElement,
muiName: 'MuiNativeSelectInput',
}));
it('should render a native select', () => {
const { container } = render(
<NativeSelectInput IconComponent="div" onChange={() => {}} value={10}>
<option value={10}>Ten</option>
<option value={20}>Twenty</option>
<option value={30}>Thirty</option>
</NativeSelectInput>,
);
expect(container.firstChild.value).to.equal('10');
});
it('should respond to update event', () => {
const handleChange = spy();
render(
<NativeSelectInput defaultValue={10} IconComponent="div" onChange={handleChange}>
<option value={10}>Ten</option>
<option value={20}>Twenty</option>
<option value={30}>Thirty</option>
</NativeSelectInput>,
);
fireEvent.change(document.querySelector('select'), { target: { value: 20 } });
expect(handleChange.callCount).to.equal(1);
expect(handleChange.args[0][0].target.value).to.equal('20');
});
it('should apply outlined class', () => {
const { container } = render(
<NativeSelectInput
IconComponent="div"
variant="outlined"
classes={{ outlined: 'outlined' }}
/>,
);
expect(container.firstChild).to.have.class(nativeSelectClasses.outlined);
});
it('should apply filled class', () => {
const { container } = render(
<NativeSelectInput IconComponent="div" variant="filled" classes={{ filled: 'filled' }} />,
);
expect(container.firstChild).to.have.class(nativeSelectClasses.filled);
});
it('should apply multiple class to `select` slot', () => {
const { container } = render(<NativeSelectInput IconComponent="div" multiple />);
expect(container.firstChild).to.have.class(nativeSelectClasses.multiple);
});
describe('prop: multiple', () => {
it('should be able to override `multiple` rule name in `select` slot', function test() {
if (/jsdom/.test(window.navigator.userAgent)) {
this.skip();
}
const selectStyle = {
marginLeft: '10px',
marginTop: '10px',
};
const multipleStyle = {
marginTop: '14px',
};
const theme = createTheme({
components: {
MuiNativeSelect: {
styleOverrides: {
select: selectStyle,
multiple: multipleStyle,
},
},
},
});
const { container } = render(
<ThemeProvider theme={theme}>
<NativeSelectInput IconComponent="div" multiple>
<option value={'first'}>First</option>
<option value={'second'}>Second</option>
</NativeSelectInput>
</ThemeProvider>,
);
const combinedStyle = { ...selectStyle, ...multipleStyle };
expect(
container.getElementsByClassName(nativeSelectClasses.select)[0],
).to.toHaveComputedStyle(combinedStyle);
});
});
describe('theme styleOverrides:', () => {
it('should override with error style when `select` has `error` state', function test() {
if (/jsdom/.test(window.navigator.userAgent)) {
this.skip();
}
const iconStyle = { color: 'rgb(255, 0, 0)' };
const selectStyle = { color: 'rgb(255, 192, 203)' };
const theme = createTheme({
components: {
MuiNativeSelect: {
styleOverrides: {
icon: (props) => ({
...(props.ownerState.error && iconStyle),
}),
select: (props) => ({
...(props.ownerState.error && selectStyle),
}),
},
},
},
});
const { container } = render(
<ThemeProvider theme={theme}>
<NativeSelectInput error IconComponent="div">
<option value={'first'}>First</option>
<option value={'second'}>Second</option>
</NativeSelectInput>
</ThemeProvider>,
);
expect(container.querySelector(`.${nativeSelectClasses.select}`)).toHaveComputedStyle(
selectStyle,
);
expect(container.querySelector(`.${nativeSelectClasses.icon}`)).toHaveComputedStyle(
iconStyle,
);
});
});
});