1
1
import { render } from "@testing-library/svelte" ;
2
- import TestRenderer from "../../test-utils/TestRenderer.svelte" ;
3
2
import { Switch , SwitchDescription , SwitchGroup , SwitchLabel } from "." ;
4
3
import {
5
4
assertActiveElement ,
@@ -15,9 +14,9 @@ jest.mock("../../hooks/use-id");
15
14
16
15
describe ( "Safe guards" , ( ) => {
17
16
it ( "should be possible to render a Switch without crashing" , ( ) => {
18
- render ( TestRenderer , {
19
- allProps : [ Switch , { checked : false , onChange : console . log } ] ,
20
- } ) ;
17
+ render ( svelte `
18
+ < Switch checked={ false} on:change={ console.log} />
19
+ ` ) ;
21
20
} ) ;
22
21
} ) ;
23
22
@@ -43,16 +42,16 @@ describe("Rendering", () => {
43
42
} )
44
43
45
44
it ( "should be possible to render an (on) Switch using an `as` prop" , ( ) => {
46
- render ( TestRenderer , {
47
- allProps : [ Switch , { as : "span" , checked : true , onChange : console . log } ] ,
48
- } ) ;
45
+ render ( svelte `
46
+ < Switch as={ "span"} checked={ true} on:change={ console.log} />
47
+ ` ) ;
49
48
assertSwitch ( { state : SwitchState . On , tag : "span" } ) ;
50
49
} ) ;
51
50
52
51
it ( "should be possible to render an (off) Switch using an `as` prop" , ( ) => {
53
- render ( TestRenderer , {
54
- allProps : [ Switch , { as : "span" , checked : false , onChange : console . log } ] ,
55
- } ) ;
52
+ render ( svelte `
53
+ < Switch as={ "span"} checked={ false} on:change={ console.log} />
54
+ ` ) ;
56
55
assertSwitch ( { state : SwitchState . Off , tag : "span" } ) ;
57
56
} ) ;
58
57
@@ -67,37 +66,25 @@ describe("Rendering", () => {
67
66
68
67
describe ( "`type` attribute" , ( ) => {
69
68
it ( 'should set the `type` to "button" by default' , async ( ) => {
70
- render ( TestRenderer , {
71
- allProps : [
72
- Switch ,
73
- { checked : false , onChange : console . log } ,
74
- "Trigger" ,
75
- ] ,
76
- } ) ;
69
+ render ( svelte `
70
+ <Switch checked={false} on:change={console.log}>Trigger</Switch>
71
+ ` ) ;
77
72
78
73
expect ( getSwitch ( ) ) . toHaveAttribute ( "type" , "button" ) ;
79
74
} ) ;
80
75
81
76
it ( 'should not set the `type` to "button" if it already contains a `type`' , async ( ) => {
82
- render ( TestRenderer , {
83
- allProps : [
84
- Switch ,
85
- { checked : false , onChange : console . log , type : "submit" } ,
86
- "Trigger" ,
87
- ] ,
88
- } ) ;
77
+ render ( svelte `
78
+ <Switch checked={false} on:change={console.log} type={"submit"}>Trigger</Switch>
79
+ ` ) ;
89
80
90
81
expect ( getSwitch ( ) ) . toHaveAttribute ( "type" , "submit" ) ;
91
82
} ) ;
92
83
93
84
it ( 'should not set the type if the "as" prop is not a "button"' , async ( ) => {
94
- render ( TestRenderer , {
95
- allProps : [
96
- Switch ,
97
- { checked : false , onChange : console . log , as : "div" } ,
98
- "Trigger" ,
99
- ] ,
100
- } ) ;
85
+ render ( svelte `
86
+ <Switch checked={false} on:change={console.log} as={"div"}>Trigger</Switch>
87
+ ` ) ;
101
88
102
89
expect ( getSwitch ( ) ) . not . toHaveAttribute ( "type" ) ;
103
90
} ) ;
@@ -106,31 +93,23 @@ describe("Rendering", () => {
106
93
107
94
describe ( "Render composition" , ( ) => {
108
95
it ( "should be possible to render a Switch.Group, Switch and Switch.Label" , ( ) => {
109
- render ( TestRenderer , {
110
- allProps : [
111
- SwitchGroup ,
112
- { } ,
113
- [
114
- [ Switch , { checked : false , onChange : console . log } ] ,
115
- [ SwitchLabel , { } , "Enable notifications" ] ,
116
- ] ,
117
- ] ,
118
- } ) ;
96
+ render ( svelte `
97
+ <SwitchGroup>
98
+ <Switch checked={false} on:change={console.log} />
99
+ <SwitchLabel>Enable notifications</SwitchLabel>
100
+ </SwitchGroup>
101
+ ` ) ;
119
102
120
103
assertSwitch ( { state : SwitchState . Off , label : "Enable notifications" } ) ;
121
104
} ) ;
122
105
123
106
it ( "should be possible to render a Switch.Group, Switch and Switch.Label (before the Switch)" , ( ) => {
124
- render ( TestRenderer , {
125
- allProps : [
126
- SwitchGroup ,
127
- { } ,
128
- [
129
- [ SwitchLabel , { } , "Label B" ] ,
130
- [ Switch , { checked : false , onChange : console . log } , "Label A" ] ,
131
- ] ,
132
- ] ,
133
- } ) ;
107
+ render ( svelte `
108
+ <SwitchGroup>
109
+ <SwitchLabel>Label B</SwitchLabel>
110
+ <Switch checked={false} on:change={console.log}>Label A</Switch>
111
+ </SwitchGroup>
112
+ ` ) ;
134
113
135
114
// Warning! Using aria-label or aria-labelledby will hide any descendant content from assistive
136
115
// technologies.
@@ -140,16 +119,12 @@ describe("Render composition", () => {
140
119
} ) ;
141
120
142
121
it ( "should be possible to render a Switch.Group, Switch and Switch.Label (after the Switch)" , ( ) => {
143
- render ( TestRenderer , {
144
- allProps : [
145
- SwitchGroup ,
146
- { } ,
147
- [
148
- [ Switch , { checked : false , onChange : console . log } , "Label A" ] ,
149
- [ SwitchLabel , { } , "Label B" ] ,
150
- ] ,
151
- ] ,
152
- } ) ;
122
+ render ( svelte `
123
+ <SwitchGroup>
124
+ <Switch checked={false} on:change={console.log}>Label A</Switch>
125
+ <SwitchLabel>Label B</SwitchLabel>
126
+ </SwitchGroup>
127
+ ` ) ;
153
128
154
129
// Warning! Using aria-label or aria-labelledby will hide any descendant content from assistive
155
130
// technologies.
@@ -159,16 +134,12 @@ describe("Render composition", () => {
159
134
} ) ;
160
135
161
136
it ( "should be possible to render a Switch.Group, Switch and Switch.Description (before the Switch)" , async ( ) => {
162
- render ( TestRenderer , {
163
- allProps : [
164
- SwitchGroup ,
165
- { } ,
166
- [
167
- [ SwitchDescription , { } , "This is an important feature" ] ,
168
- [ Switch , { checked : false , onChange : console . log } ] ,
169
- ] ,
170
- ] ,
171
- } ) ;
137
+ render ( svelte `
138
+ <SwitchGroup>
139
+ <SwitchDescription>This is an important feature</SwitchDescription>
140
+ <Switch checked={false} on:change={console.log} />
141
+ </SwitchGroup>
142
+ ` ) ;
172
143
173
144
assertSwitch ( {
174
145
state : SwitchState . Off ,
@@ -177,16 +148,12 @@ describe("Render composition", () => {
177
148
} ) ;
178
149
179
150
it ( "should be possible to render a Switch.Group, Switch and Switch.Description (after the Switch)" , ( ) => {
180
- render ( TestRenderer , {
181
- allProps : [
182
- SwitchGroup ,
183
- { } ,
184
- [
185
- [ Switch , { checked : false , onChange : console . log } ] ,
186
- [ SwitchDescription , { } , "This is an important feature" ] ,
187
- ] ,
188
- ] ,
189
- } ) ;
151
+ render ( svelte `
152
+ <SwitchGroup>
153
+ <Switch checked={false} on:change={console.log} />
154
+ <SwitchDescription>This is an important feature</SwitchDescription>
155
+ </SwitchGroup>
156
+ ` ) ;
190
157
191
158
assertSwitch ( {
192
159
state : SwitchState . Off ,
@@ -195,17 +162,13 @@ describe("Render composition", () => {
195
162
} ) ;
196
163
197
164
it ( "should be possible to render a Switch.Group, Switch, Switch.Label and Switch.Description" , ( ) => {
198
- render ( TestRenderer , {
199
- allProps : [
200
- SwitchGroup ,
201
- { } ,
202
- [
203
- [ SwitchLabel , { } , "Label A" ] ,
204
- [ Switch , { checked : false , onChange : console . log } ] ,
205
- [ SwitchDescription , { } , "This is an important feature" ] ,
206
- ] ,
207
- ] ,
208
- } ) ;
165
+ render ( svelte `
166
+ <SwitchGroup>
167
+ <SwitchLabel>Label A</SwitchLabel>
168
+ <Switch checked={false} on:change={console.log} />
169
+ <SwitchDescription>This is an important feature</SwitchDescription>
170
+ </SwitchGroup>
171
+ ` ) ;
209
172
210
173
assertSwitch ( {
211
174
state : SwitchState . Off ,
@@ -218,9 +181,9 @@ describe("Render composition", () => {
218
181
describe ( "Keyboard interactions" , ( ) => {
219
182
describe ( "`Space` key" , ( ) => {
220
183
it ( "should be possible to toggle the Switch with Space" , async ( ) => {
221
- render ( TestRenderer , {
222
- allProps : [ ManagedSwitch , { } ] ,
223
- } ) ;
184
+ render ( svelte `
185
+ < ManagedSwitch />
186
+ ` ) ;
224
187
225
188
// Ensure checkbox is off
226
189
assertSwitch ( { state : SwitchState . Off } ) ;
@@ -245,9 +208,9 @@ describe("Keyboard interactions", () => {
245
208
describe ( "`Enter` key" , ( ) => {
246
209
it ( "should not be possible to use Enter to toggle the Switch" , async ( ) => {
247
210
let handleChange = jest . fn ( ) ;
248
- render ( TestRenderer , {
249
- allProps : [ ManagedSwitch , { onChange : handleChange } ] ,
250
- } ) ;
211
+ render ( svelte `
212
+ < ManagedSwitch onChange={ handleChange}/>
213
+ ` ) ;
251
214
252
215
// Ensure checkbox is off
253
216
assertSwitch ( { state : SwitchState . Off } ) ;
@@ -291,9 +254,9 @@ describe("Keyboard interactions", () => {
291
254
292
255
describe ( "Mouse interactions" , ( ) => {
293
256
it ( "should be possible to toggle the Switch with a click" , async ( ) => {
294
- render ( TestRenderer , {
295
- allProps : [ ManagedSwitch , { } ] ,
296
- } ) ;
257
+ render ( svelte `
258
+ < ManagedSwitch />
259
+ ` ) ;
297
260
298
261
// Ensure checkbox is off
299
262
assertSwitch ( { state : SwitchState . Off } ) ;
@@ -312,16 +275,12 @@ describe("Mouse interactions", () => {
312
275
} ) ;
313
276
314
277
it ( "should be possible to toggle the Switch with a click on the Label" , async ( ) => {
315
- render ( TestRenderer , {
316
- allProps : [
317
- SwitchGroup ,
318
- { } ,
319
- [
320
- [ ManagedSwitch , { } ] ,
321
- [ SwitchLabel , { } , "The label" ] ,
322
- ] ,
323
- ] ,
324
- } ) ;
278
+ render ( svelte `
279
+ <SwitchGroup>
280
+ <ManagedSwitch />
281
+ <SwitchLabel>The label</SwitchLabel>
282
+ </SwitchGroup>
283
+ ` ) ;
325
284
326
285
// Ensure checkbox is off
327
286
assertSwitch ( { state : SwitchState . Off } ) ;
@@ -346,16 +305,12 @@ describe("Mouse interactions", () => {
346
305
} ) ;
347
306
348
307
it ( "should not be possible to toggle the Switch with a click on the Label (passive)" , async ( ) => {
349
- render ( TestRenderer , {
350
- allProps : [
351
- SwitchGroup ,
352
- { } ,
353
- [
354
- [ ManagedSwitch , { } ] ,
355
- [ SwitchLabel , { passive : true } , "The label" ] ,
356
- ] ,
357
- ] ,
358
- } ) ;
308
+ render ( svelte `
309
+ <SwitchGroup>
310
+ <ManagedSwitch />
311
+ <SwitchLabel passive={true}>The label</SwitchLabel>
312
+ </SwitchGroup>
313
+ ` ) ;
359
314
360
315
// Ensure checkbox is off
361
316
assertSwitch ( { state : SwitchState . Off } ) ;
0 commit comments