-
Notifications
You must be signed in to change notification settings - Fork 112
/
index.spec.tsx
238 lines (215 loc) · 8.48 KB
/
index.spec.tsx
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import * as React from 'react';
import {render, screen, fireEvent, waitFor} from '@testing-library/react';
import {Theme} from '@twilio-paste/theme';
import {Box} from '@twilio-paste/box';
import {CustomizationProvider} from '@twilio-paste/customization';
import {
Sidebar,
SidebarCollapseButton,
SidebarCollapseButtonWrapper,
SidebarPushContentWrapper,
SidebarOverlayContentWrapper,
} from '../src';
import type {SidebarProps} from '../src';
const MockPushSidebar = ({
collapsed,
variant = 'default',
}: {
collapsed?: boolean;
variant?: SidebarProps['variant'];
}): JSX.Element => {
return (
<Theme.Provider theme="twilio">
<Sidebar aria-label="main" collapsed={collapsed} variant={variant}>
<Box color="colorTextInverse">Sidebar header</Box>
<SidebarCollapseButtonWrapper>
<SidebarCollapseButton i18nCollapseLabel="Close sidebar" i18nExpandLabel="Open sidebar" />
</SidebarCollapseButtonWrapper>
</Sidebar>
<SidebarPushContentWrapper collapsed={collapsed} variant={variant}>
<div>Content area</div>
</SidebarPushContentWrapper>
</Theme.Provider>
);
};
const MockOverlaySidebar = ({
collapsed,
variant = 'default',
}: {
collapsed?: boolean;
variant?: SidebarProps['variant'];
}): JSX.Element => {
return (
<Theme.Provider theme="twilio">
<Sidebar aria-label="main" collapsed={collapsed} variant={variant}>
<Box color="colorTextInverse">Sidebar header</Box>
<SidebarCollapseButtonWrapper>
<SidebarCollapseButton i18nCollapseLabel="Close sidebar" i18nExpandLabel="Open sidebar" />
</SidebarCollapseButtonWrapper>
</Sidebar>
<SidebarOverlayContentWrapper collapsed={collapsed} variant={variant}>
<div>Content area</div>
</SidebarOverlayContentWrapper>
</Theme.Provider>
);
};
describe('Sidebar', () => {
/**
* PUSH
*/
describe('Push Sidebar', () => {
it('should have an id', () => {
render(<MockPushSidebar collapsed />);
const nav = screen.getByRole('navigation');
expect(nav).toHaveAttribute('id');
});
it('should render expanded', () => {
render(<MockPushSidebar collapsed={false} />);
const nav = screen.getByRole('navigation');
expect(nav.style.width).toBe('15rem');
});
it('should render expanded by default', () => {
render(<MockPushSidebar />);
const nav = screen.getByRole('navigation');
expect(nav.style.width).toBe('15rem');
});
it('should render compact width', () => {
render(<MockPushSidebar collapsed={true} variant="compact" />);
const nav = screen.getByRole('navigation');
expect(nav.style.width).toBe('4.75rem');
});
});
/**
* OVERLAY
*/
describe('Overlay Sidebar', () => {
it('should have an id', () => {
render(<MockOverlaySidebar collapsed />);
const nav = screen.getByRole('navigation');
expect(nav).toHaveAttribute('id');
});
it('should render expanded', () => {
render(<MockOverlaySidebar collapsed={false} />);
const nav = screen.getByRole('navigation');
expect(nav.style.width).toBe('15rem');
});
it('should render expanded by default', () => {
render(<MockOverlaySidebar />);
const nav = screen.getByRole('navigation');
expect(nav.style.width).toBe('15rem');
});
it('should render compact width', () => {
render(<MockOverlaySidebar collapsed={true} variant="compact" />);
const nav = screen.getByRole('navigation');
expect(nav.style.width).toBe('4.75rem');
});
});
/**
* SIDEBAR COLLAPSE BUTTON
*/
describe('Sidebar Collapse Button', () => {
it('should have aria-expanded and aria-controls set correctly when collapsed', async () => {
render(<MockOverlaySidebar collapsed />);
const toggleButton = screen.getByRole('button');
const nav = screen.getByRole('navigation');
expect(toggleButton.getAttribute('aria-controls')).toEqual(nav.getAttribute('id'));
expect(toggleButton.getAttribute('aria-expanded')).toEqual('false');
expect(toggleButton.textContent).toBe('Open sidebar');
});
it('should have aria-expanded and aria-controls set correctly when expanded', async () => {
render(<MockOverlaySidebar collapsed={false} />);
const toggleButton = screen.getByRole('button');
const nav = screen.getByRole('navigation');
expect(toggleButton.getAttribute('aria-controls')).toEqual(nav.getAttribute('id'));
expect(toggleButton.getAttribute('aria-expanded')).toEqual('true');
expect(toggleButton.textContent).toBe('Close sidebar');
});
});
/**
* Customization
*/
describe('Customization', () => {
it('should work with default element values', () => {
render(
<CustomizationProvider
baseTheme="default"
theme={TestTheme}
elements={{
SIDEBAR: {backgroundColor: 'colorBackgroundPrimary', margin: 'space50'},
SIDEBAR_COLLAPSE_BUTTON: {
padding: 'space40',
},
SIDEBAR_COLLAPSE_BUTTON_WRAPPER: {
padding: 'space40',
},
SIDEBAR_PUSH_CONTENT_WRAPPER: {backgroundColor: 'colorBackgroundPrimary', margin: 'space50'},
}}
>
<Sidebar aria-label="main" variant="compact" data-testid="aaa">
<Box color="colorTextInverse">Sidebar header</Box>
<SidebarCollapseButtonWrapper data-testid="collapseButtonWrapper">
<SidebarCollapseButton i18nCollapseLabel="Close sidebar" i18nExpandLabel="Open sidebar" />
</SidebarCollapseButtonWrapper>
</Sidebar>
{/* Must wrap content area */}
<SidebarPushContentWrapper variant="compact" data-testid="contentwrapper">
<div>Content area</div>
</SidebarPushContentWrapper>
</CustomizationProvider>
);
const nav = screen.getByRole('navigation');
expect(nav).toHaveStyleRule('margin', '1rem');
expect(nav).toHaveStyleRule('background-color', 'rgb(2, 99, 224)');
const sidebarButton = screen.getByRole('button');
expect(sidebarButton).toHaveStyleRule('padding', '0.75rem');
const sidebarButtonWrapper = screen.getByTestId('collapseButtonWrapper');
expect(sidebarButtonWrapper).toHaveStyleRule('padding', '0.75rem');
const contentWrapper = screen.getByTestId('contentwrapper');
expect(contentWrapper).toHaveStyleRule('margin', '1rem');
expect(contentWrapper).toHaveStyleRule('background-color', 'rgb(2, 99, 224)');
});
it('should work with custom element values', () => {
render(
<CustomizationProvider
baseTheme="default"
theme={TestTheme}
elements={{
XSIDE: {backgroundColor: 'colorBackgroundPrimary', margin: 'space50'},
XSIDE_WRAPPER: {backgroundColor: 'colorBackgroundPrimary', margin: 'space50'},
XSIDE_COLLAPSE_BUTTON: {
padding: 'space40',
},
XSIDE_COLLAPSE_BUTTON_WRAPPER: {
padding: 'space40',
},
}}
>
<Sidebar aria-label="main" variant="compact" element="XSIDE">
<Box color="colorTextInverse">Sidebar header</Box>
<SidebarCollapseButtonWrapper element="XSIDE_COLLAPSE_BUTTON_WRAPPER" data-testid="collapseButtonWrapper">
<SidebarCollapseButton
element="XSIDE_COLLAPSE_BUTTON"
i18nCollapseLabel="Close sidebar"
i18nExpandLabel="Open sidebar"
/>
</SidebarCollapseButtonWrapper>
</Sidebar>
{/* Must wrap content area */}
<SidebarPushContentWrapper variant="compact" element="XSIDE_WRAPPER" data-testid="contentwrapper">
<div>Content area</div>
</SidebarPushContentWrapper>
</CustomizationProvider>
);
const nav = screen.getByRole('navigation');
expect(nav).toHaveStyleRule('margin', '1rem');
expect(nav).toHaveStyleRule('background-color', 'rgb(2, 99, 224)');
const sidebarButton = screen.getByRole('button');
expect(sidebarButton).toHaveStyleRule('padding', '0.75rem');
const sidebarButtonWrapper = screen.getByTestId('collapseButtonWrapper');
expect(sidebarButtonWrapper).toHaveStyleRule('padding', '0.75rem');
const contentWrapper = screen.getByTestId('contentwrapper');
expect(contentWrapper).toHaveStyleRule('margin', '1rem');
expect(contentWrapper).toHaveStyleRule('background-color', 'rgb(2, 99, 224)');
});
});
});