-
-
Notifications
You must be signed in to change notification settings - Fork 213
Expand file tree
/
Copy pathSelectNative.spec.tsx
More file actions
75 lines (69 loc) · 2.31 KB
/
Copy pathSelectNative.spec.tsx
File metadata and controls
75 lines (69 loc) · 2.31 KB
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
// Bsased on https://github.com/mui-org/material-ui
import { fireEvent, screen } from '@testing-library/react';
import React from 'react';
import { renderWithTheme } from '../../test/utils';
import { SelectOption } from './Select.types';
import { SelectNative } from './SelectNative';
const options: SelectOption<string>[] = [
{ label: 'ten', value: '10' },
{ label: 'twenty', value: '20' },
{ label: 'thirty', value: '30' }
];
describe('<SelectNative />', () => {
describe('prop: native', () => {
it('renders a <select />', () => {
const { container } = renderWithTheme(<SelectNative options={options} />);
expect(container.querySelector('select')).toBeInTheDocument();
});
it('renders uses values for labels', () => {
const optionsWithoutLabels = options.map(({ value }) => ({ value }));
renderWithTheme(
<SelectNative options={optionsWithoutLabels} data-testid='select' />
);
expect(screen.getByTestId('select')).toHaveTextContent('10');
});
it('calls onChange if not disabled or readOnly', () => {
const handleChange = jest.fn();
renderWithTheme(
<>
<SelectNative
options={options}
data-testid='selectEnabled'
onChange={handleChange}
/>
<SelectNative
options={options}
data-testid='selectDisabled'
disabled
onChange={handleChange}
/>
<SelectNative
options={options}
data-testid='selectReadOnly'
onChange={handleChange}
readOnly
/>
</>
);
fireEvent.change(screen.getByTestId('selectEnabled'));
fireEvent.change(screen.getByTestId('selectDisabled'));
fireEvent.change(screen.getByTestId('selectReadOnly'));
expect(handleChange).toHaveBeenCalledTimes(1);
expect(handleChange).toHaveBeenCalledWith(options[0], {
fromEvent: expect.objectContaining({ type: 'change' })
});
});
it('can be labelled with a <label />', () => {
renderWithTheme(
<>
<label htmlFor='select'>A select</label>
<SelectNative id='select' options={options} />
</>
);
expect(screen.getByLabelText('A select')).toHaveProperty(
'tagName',
'SELECT'
);
});
});
});