-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Expand file tree
/
Copy pathindex.tsx
More file actions
197 lines (171 loc) · 5.08 KB
/
index.tsx
File metadata and controls
197 lines (171 loc) · 5.08 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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/**
* External dependencies
*/
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
/**
* Internal dependencies
*/
import TabPanel from '..';
const setupUser = () =>
userEvent.setup( {
advanceTimers: jest.advanceTimersByTime,
} );
const TABS = [
{
name: 'alpha',
title: 'Alpha',
className: 'alpha-class',
},
{
name: 'beta',
title: 'Beta',
className: 'beta-class',
},
{
name: 'gamma',
title: 'Gamma',
className: 'gamma-class',
},
];
const getSelectedTab = () => screen.getByRole( 'tab', { selected: true } );
describe( 'TabPanel', () => {
it( 'should render a tabpanel, and clicking should change tabs', async () => {
const user = setupUser();
const panelRenderFunction = jest.fn();
const mockOnSelect = jest.fn();
render(
<TabPanel
tabs={ TABS }
children={ panelRenderFunction }
onSelect={ mockOnSelect }
/>
);
expect( getSelectedTab() ).toHaveTextContent( 'Alpha' );
expect(
screen.getByRole( 'tabpanel', { name: 'Alpha' } )
).toBeInTheDocument();
expect( panelRenderFunction ).toHaveBeenLastCalledWith( TABS[ 0 ] );
expect( mockOnSelect ).toHaveBeenLastCalledWith( 'alpha' );
await user.click( screen.getByRole( 'tab', { name: 'Beta' } ) );
expect( getSelectedTab() ).toHaveTextContent( 'Beta' );
expect(
screen.getByRole( 'tabpanel', { name: 'Beta' } )
).toBeInTheDocument();
expect( panelRenderFunction ).toHaveBeenLastCalledWith( TABS[ 1 ] );
expect( mockOnSelect ).toHaveBeenLastCalledWith( 'beta' );
await user.click( screen.getByRole( 'tab', { name: 'Alpha' } ) );
expect( getSelectedTab() ).toHaveTextContent( 'Alpha' );
expect(
screen.getByRole( 'tabpanel', { name: 'Alpha' } )
).toBeInTheDocument();
expect( panelRenderFunction ).toHaveBeenLastCalledWith( TABS[ 0 ] );
expect( mockOnSelect ).toHaveBeenLastCalledWith( 'alpha' );
} );
it( 'should render with a tab initially selected by prop initialTabIndex', () => {
render(
<TabPanel
initialTabName="beta"
tabs={ TABS }
children={ () => undefined }
/>
);
const selectedTab = screen.getByRole( 'tab', { selected: true } );
expect( selectedTab ).toHaveTextContent( 'Beta' );
} );
it( 'should apply the `activeClass` to the selected tab', async () => {
const user = setupUser();
const activeClass = 'my-active-tab';
render(
<TabPanel
activeClass={ activeClass }
tabs={ TABS }
children={ () => undefined }
/>
);
expect( getSelectedTab() ).toHaveClass( activeClass );
screen
.getAllByRole( 'tab', { selected: false } )
.forEach( ( unselectedTab ) => {
expect( unselectedTab ).not.toHaveClass( activeClass );
} );
await user.click( screen.getByRole( 'tab', { name: 'Beta' } ) );
expect( getSelectedTab() ).toHaveClass( activeClass );
screen
.getAllByRole( 'tab', { selected: false } )
.forEach( ( unselectedTab ) => {
expect( unselectedTab ).not.toHaveClass( activeClass );
} );
} );
it( "should apply the tab's `className` to the tab button", () => {
render( <TabPanel tabs={ TABS } children={ () => undefined } /> );
expect( screen.getByRole( 'tab', { name: 'Alpha' } ) ).toHaveClass(
'alpha-class'
);
expect( screen.getByRole( 'tab', { name: 'Beta' } ) ).toHaveClass(
'beta-class'
);
expect( screen.getByRole( 'tab', { name: 'Gamma' } ) ).toHaveClass(
'gamma-class'
);
} );
it( 'should select `initialTabname` if defined', () => {
const mockOnSelect = jest.fn();
render(
<TabPanel
tabs={ TABS }
initialTabName="beta"
children={ () => undefined }
onSelect={ mockOnSelect }
/>
);
expect( getSelectedTab() ).toHaveTextContent( 'Beta' );
expect( mockOnSelect ).toHaveBeenLastCalledWith( 'beta' );
} );
describe( 'fallbacks when new tab list invalidates current selection', () => {
it( 'should select `initialTabName` if defined', async () => {
const user = setupUser();
const mockOnSelect = jest.fn();
const { rerender } = render(
<TabPanel
tabs={ TABS }
initialTabName="gamma"
children={ () => undefined }
onSelect={ mockOnSelect }
/>
);
await user.click( screen.getByRole( 'tab', { name: 'Alpha' } ) );
rerender(
<TabPanel
tabs={ TABS.slice( 1 ) /* remove alpha */ }
initialTabName="gamma"
children={ () => undefined }
onSelect={ mockOnSelect }
/>
);
expect( getSelectedTab() ).toHaveTextContent( 'Gamma' );
expect( mockOnSelect ).toHaveBeenLastCalledWith( 'gamma' );
} );
it( 'should select first tab if `initialTabName` not defined', async () => {
const user = setupUser();
const mockOnSelect = jest.fn();
const { rerender } = render(
<TabPanel
tabs={ TABS }
children={ () => undefined }
onSelect={ mockOnSelect }
/>
);
await user.click( screen.getByRole( 'tab', { name: 'Alpha' } ) );
rerender(
<TabPanel
tabs={ TABS.slice( 1 ) /* remove alpha */ }
children={ () => undefined }
onSelect={ mockOnSelect }
/>
);
expect( getSelectedTab() ).toHaveTextContent( 'Beta' );
expect( mockOnSelect ).toHaveBeenLastCalledWith( 'beta' );
} );
} );
} );