-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathsegmented.test.tsx
More file actions
129 lines (117 loc) · 3.3 KB
/
Copy pathsegmented.test.tsx
File metadata and controls
129 lines (117 loc) · 3.3 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
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
import React from 'react';
import { render, fireEvent } from '@testing-library/react';
import Segmented from '../index';
describe('<Segmented />', () => {
const options = [
{ label: 'Daily', value: 'daily' },
{ label: 'Weekly', value: 'weekly' },
{ label: 'Monthly', value: 'monthly' },
];
it('should match the snapshot', () => {
const { asFragment } = render(<Segmented options={options} />);
expect(asFragment()).toMatchSnapshot();
});
it('should render correctly', () => {
const { container } = render(
<Segmented
options={[
{ label: 'A', value: 'a' },
{ label: 'B', value: 'b' },
{ label: 'C', value: 'c' },
]}
/>
);
expect(container.firstChild).toHaveClass('ty-segmented');
});
it('should render options', () => {
const { getByText } = render(
<Segmented
options={[
{ label: 'Foo', value: 'foo' },
{ label: 'Bar', value: 'bar' },
]}
/>
);
expect(getByText('Foo')).toBeInTheDocument();
expect(getByText('Bar')).toBeInTheDocument();
});
it('should select default value', () => {
const { container } = render(
<Segmented
options={[
{ label: 'A', value: 'a' },
{ label: 'B', value: 'b' },
{ label: 'C', value: 'c' },
]}
defaultValue="b"
/>
);
const active = container.querySelector('.ty-segmented__item_active');
expect(active).toBeTruthy();
expect(active!).toHaveTextContent('B');
});
it('should not select any option by default', () => {
const { container } = render(<Segmented options={options} />);
expect(container.querySelector('.ty-segmented__item_active')).toBeNull();
});
it('should handle onChange', () => {
const onChange = jest.fn();
const { getByLabelText } = render(
<Segmented
options={[
{ label: 'A', value: 'a' },
{ label: 'B', value: 'b' },
]}
onChange={onChange}
/>
);
fireEvent.click(getByLabelText('B'));
expect(onChange).toHaveBeenCalledWith(
'b',
{ label: 'B', value: 'b' },
expect.any(Object)
);
});
it('should support block mode', () => {
const { container } = render(
<Segmented
options={[
{ label: 'A', value: 'a' },
{ label: 'B', value: 'b' },
]}
block
/>
);
expect(container.firstChild).toHaveClass('ty-segmented_block');
});
it('should support disabled', () => {
const onChange = jest.fn();
const { getByLabelText } = render(
<Segmented
options={[
{ label: 'A', value: 'a' },
{ label: 'B', value: 'b' },
]}
disabled
onChange={onChange}
/>
);
fireEvent.click(getByLabelText('B'));
expect(onChange).not.toHaveBeenCalled();
});
it('should reset uncontrolled selection when option is removed', () => {
const { container, rerender } = render(
<Segmented options={options} defaultValue="weekly" />
);
rerender(
<Segmented
options={[
{ label: 'Daily', value: 'daily' },
{ label: 'Monthly', value: 'monthly' },
]}
defaultValue="weekly"
/>
);
expect(container.querySelector('.ty-segmented__item_active')).toBeNull();
});
});