-
Notifications
You must be signed in to change notification settings - Fork 285
Expand file tree
/
Copy pathButton.cy.tsx
More file actions
637 lines (491 loc) · 14.9 KB
/
Copy pathButton.cy.tsx
File metadata and controls
637 lines (491 loc) · 14.9 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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
import { setNoConflict } from "@ui5/webcomponents-base/dist/config/NoConflict.js";
import Avatar from "../../src/Avatar.js";
import Button from "../../src/Button.js";
import Label from "../../src/Label.js";
import ButtonBadge from "../../src/ButtonBadge.js";
import download from "@ui5/webcomponents-icons/dist/download.js";
import employee from "@ui5/webcomponents-icons/dist/employee.js";
import {
BUTTON_ARIA_TYPE_EMPHASIZED,
} from "../../src/generated/i18n/i18n-defaults.js";
describe("Button general interaction", () => {
it("tests button's text rendering", () => {
cy.mount(<Button icon={download} design="Negative">Action Bar Button</Button>);
cy.get<Button>("[ui5-button]")
.shadow()
.find(".ui5-button-text>bdi>slot")
.should("have.length", 1, "Button text is not rendered");
});
it("tests button's icon rendering", () => {
cy.mount(<Button icon={download} design="Emphasized">Action Bar Button</Button>);
cy.get("[ui5-button]")
.as("button");
cy.get("@button")
.then($button => {
$button.attr("icon", "add");
});
cy.get("@button")
.shadow()
.find(".ui5-button-icon")
.should("exist", "icon is present");
cy.get("@button")
.then($button => {
$button.attr("icon", "");
});
cy.get("@button")
.shadow()
.find(".ui5-button-icon")
.should("not.exist", "icon is not present");
});
it("tests button's endIon rendering", () => {
cy.mount(<Button>Action Bar Button</Button>);
cy.get("[ui5-button]")
.as("button");
cy.get("@button")
.then($button => {
$button.attr("end-icon", "add");
});
cy.get("@button")
.shadow()
.find(".ui5-button-end-icon")
.should("exist", "endIon is present");
cy.get("@button")
.then($button => {
$button.attr("end-icon", "");
});
cy.get("@button")
.shadow()
.find(".ui5-button-end-icon")
.should("not.exist", "endIon is not present");
});
it("tests click event", () => {
cy.mount(<Button icon="home" design="Emphasized">Action Bar Button</Button>);
cy.get("[ui5-button]")
.as("button");
cy.get("@button")
.then(button => {
button.get(0).addEventListener("click", cy.stub().as("clicked"));
});
cy.get("@button")
.realClick();
cy.realPress("Space");
cy.realPress("Enter");
cy.get("@clicked")
.should("have.been.calledThrice");
});
it("tests keyboard shortcuts used to prevent a click event", () => {
cy.mount(<Button>Text</Button>);
cy.get("[ui5-button]")
.as("button");
cy.get("@button")
.realClick();
cy.get("@button")
.then(button => {
button.get(0).addEventListener("click", cy.stub().as("clicked"));
});
cy.realPress(["Space", "Shift"]);
cy.realPress(["Space", "Escape"]);
cy.get("@clicked")
.should("not.been.called");
});
it("tests button's icon only rendering", () => {
cy.mount(<Button icon="home"></Button>);
cy.get("[ui5-button]")
.should("have.attr", "icon-only");
});
it("tests button's icon only rendering", () => {
cy.mount(<Button icon="text"> </Button>);
cy.get("[ui5-button]")
.should("have.attr", "icon-only");
});
it("tests button's slot rendering", () => {
cy.mount(
<Button>
<Avatar id="btnImage" size="XS">
<img src="https://sdk.openui5.org/test-resources/sap/f/images/Woman_avatar_01.png" />
</Avatar>
</Button>
);
cy.get("[ui5-button]")
.should("be.visible", "Btn image is rendered");
});
it("tests clicking on disabled button", () => {
cy.mount(<Button disabled>Inactive</Button>);
cy.get("[ui5-button]")
.as("button");
cy.get("@button")
.then(button => {
button.get(0).addEventListener("click", cy.stub().as("clicked"));
});
// don't test space and enter, as wdio always fires a click but the browser not.
// await button.keys("Space");
// await button.keys("Enter");
cy.get("@button")
.realClick();
cy.get("@clicked")
.should("not.called");
cy.get("@button")
.shadow()
.find("button")
.as("nativeButton");
cy.get("@nativeButton")
.should("have.attr", "disabled");
cy.get("@nativeButton")
.should("not.have.attr", "tabindex");
});
it("tests clicking on disabled button with Icon", () => {
cy.mount(<Button icon={employee} disabled></Button>);
cy.get("[ui5-button]")
.as("button");
cy.get("@button")
.then(button => {
button.get(0).addEventListener("click", cy.stub().as("clicked"));
});
cy.get("@button")
.scrollIntoView();
cy.get("@button")
.realClick();
cy.get("@clicked")
.should("not.called");
cy.get("@button")
.shadow()
.find("[ui5-icon]")
.should("be.visible")
.realClick();
cy.get("@clicked")
.should("not.called");
});
it("tests clicking on button with busy indicator", () => {
cy.mount(<Button loading></Button>);
cy.get("[ui5-button]")
.as("button");
cy.get("@button")
.then(button => {
button.get(0).addEventListener("click", cy.stub().as("clicked"));
});
cy.get("@button")
.shadow()
.find("[ui5-busy-indicator]")
.should("be.visible");
cy.get("@button")
.realClick();
cy.get("@clicked")
.should("not.called");
});
it("tests button with text icon role", () => {
cy.mount(<Button design="Attention" icon="message-warning">Warning</Button>);
cy.get("[ui5-button]")
.as("button");
cy.get("@button")
.shadow()
.find("[ui5-icon]")
.should("have.attr", "mode", "Decorative");
});
it("Prevents native behavior when click event is prevented", () => {
cy.mount(<a href="#navigation">
<Button onClick={e => e.preventDefault()}>Click me</Button>
</a>);
cy.location("hash")
.should("not.eq", "#navigation");
cy.get("[ui5-button]")
.realClick();
cy.location("hash")
.should("not.eq", "#navigation");
});
it("Allows native behavior when click event is not prevented", () => {
cy.mount(<a href="#navigation">
<Button>Click me</Button>
</a>);
cy.location("hash")
.should("not.eq", "#navigation");
cy.get("[ui5-button]")
.realClick();
cy.location("hash")
.should("eq", "#navigation");
});
it("Native event is always fired", () => {
cy.mount(<div onClick={cy.stub().as("nativeClick")}>
<Button>Click me</Button>
</div>);
cy.wrap({ setNoConflict })
.invoke("setNoConflict", true)
cy.get("[ui5-button]")
.realClick();
cy.get("@nativeClick")
.should("have.been.calledOnce")
.and("be.calledWithMatch", {
type: "click"
});
cy.get('@nativeClick')
.invoke('resetHistory')
cy.wrap({ setNoConflict })
.invoke("setNoConflict", false)
cy.get("[ui5-button]")
.realClick();
cy.get("@nativeClick")
.should("have.been.calledOnce")
.and("be.calledWithMatch", {
type: "click"
});
});
});
describe("Accessibility", () => {
it("setting tooltip on the host is reflected on the button tag", () => {
cy.mount(<Button icon="message-information" tooltip="Go home"></Button>);
cy.get("[ui5-button]")
.shadow()
.find("button")
.as("button");
cy.get("@button")
.should("have.attr", "title", "Go home");
});
it("tooltip from inner icon is propagated", () => {
cy.mount(<Button icon="download" accessibleName="Download application"></Button>);
cy.get("[ui5-button]")
.shadow()
.find("button")
.as("button");
cy.get("@button")
.should("have.attr", "title", "Download");
});
it("tooltip not displayed when there is a text", () => {
cy.mount(<Button icon="home">Action</Button>);
cy.get("[ui5-button]")
.should("not.have.attr", "title");
});
it("aria-label is properly applied on the button tag", () => {
cy.mount(<Button design="Emphasized">Action</Button>);
cy.get("[ui5-button]")
.shadow()
.find("button")
.as("button");
cy.get("@button")
.should("have.attr", "aria-label", "Action");
cy.get("@button")
.should("have.attr", "aria-description", Button.i18nBundle.getText(BUTTON_ARIA_TYPE_EMPHASIZED));
});
it("aria-label uses accessibleName when both text and accessibleName are provided", () => {
cy.mount(<Button accessibleName="Custom Action Label">Button Text</Button>);
cy.get("[ui5-button]")
.shadow()
.find("button")
.as("button");
cy.get("@button")
.should("have.attr", "aria-label", "Custom Action Label");
});
it("aria-expanded is properly applied on the button tag", () => {
cy.mount(<Button icon="home" design="Emphasized">Action Bar Button</Button>);
cy.get("[ui5-button]")
.as("button");
cy.get<Button>("@button")
.then($el => {
$el.get(0).accessibilityAttributes = {
expanded: "true",
};
});
cy.get("@button")
.shadow()
.find("button")
.should("have.attr", "aria-expanded", "true");
cy.get<Button>("@button")
.then($el => {
$el.get(0).accessibilityAttributes = {
expanded: "false",
};
});
cy.get("@button")
.shadow()
.find("button")
.should("have.attr", "aria-expanded", "false");
});
it("setting accessible-role on the host is reflected on the button tag", () => {
cy.mount(<Button accessibleRole="Link"> Navigation Button </Button>);
cy.get("[ui5-button]")
.shadow()
.find("button")
.as("button");
cy.get("@button")
.should("have.attr", "role", "link");
});
it("not setting accessible-role on the host keeps the correct role on the button tag", () => {
cy.mount(<Button icon="home" design="Emphasized">Action Bar Button</Button>);
cy.get("[ui5-button]")
.shadow()
.find("button")
.as("button");
cy.get("@button")
.should("have.attr", "role", "button");
});
it("accessibleDescription in combination with design property applied on the button tag", () => {
cy.mount(<Button design="Negative" accessibleDescription="Decline">Content</Button>);
cy.get("[ui5-button]")
.shadow()
.find("button")
.as("button");
cy.get("@button")
.should("have.attr", "aria-description", "Decline Negative Action");
});
it("accessibleName when the button has a ui5-button-badge with more than 1 items", () => {
cy.mount(
<Button accessibleName="Download">
<ButtonBadge design="OverlayText" text="999+" slot="badge"></ButtonBadge>
</Button>
);
cy.get("[ui5-button]")
.shadow()
.find("button")
.as("button");
cy.get("@button")
.should("have.attr", "aria-label", `Download 999+ items`);
});
it("accessibleName when the button has a ui5-button-badge with 1 item", () => {
cy.mount(
<Button design="Emphasized" accessibleName="Download">
<ButtonBadge text="1" design="InlineText" slot="badge"></ButtonBadge>
</Button>
);
cy.get("[ui5-button]")
.shadow()
.find("button")
.as("button");
cy.get("@button")
.should("have.attr", "aria-label", `Download 1 item`);
cy.get("@button")
.should("have.attr", "aria-description", Button.i18nBundle.getText(BUTTON_ARIA_TYPE_EMPHASIZED));
});
it("setting accessible-name-ref on the host is reflected on the button tag", () => {
cy.mount(
<>
<Button icon="download" accessibleName="Help me" accessibleNameRef="1download-text"></Button>
<Label id="1download-text">Download Application</Label>
</>
);
cy.get("[ui5-button]")
.shadow()
.find("button")
.as("button");
cy.get("@button")
.should("have.attr", "aria-label", "Download Application");
});
it("aria-haspopup and aria-controls are properly applied on the button tag", () => {
cy.mount(<Button>Show Registration Dialog</Button>);
cy.get("[ui5-button]")
.as("button");
cy.get<Button>("@button")
.then($el => {
$el.get(0).accessibilityAttributes = {
hasPopup: "dialog",
controls: "registration-dialog",
};
});
cy.get("@button")
.shadow()
.find("button")
.should("have.attr", "aria-haspopup", "dialog");
cy.get("@button")
.shadow()
.find("button")
.should("have.attr", "aria-controls", "registration-dialog");
});
it("aria-label and aria-keyshortcuts are properly applied on the button tag", () => {
cy.mount(<Button accessibilityAttributes={{ariaKeyShortcuts: "Alt+G", ariaLabel: "Some text"}}>Show Registration Dialog</Button>);
cy.get("[ui5-button]")
.as("button");
cy.get<Button>("@button")
.then($el => {
$el.get(0).accessibilityAttributes = {
ariaKeyShortcuts: "Alt+G",
ariaLabel: "Some text",
};
});
cy.get("@button")
.shadow()
.find("button")
.should("have.attr", "aria-label", "Some text");
cy.get("@button")
.shadow()
.find("button")
.should("have.attr", "aria-keyshortcuts", "Alt+G");
});
it("aria-busy is properly applied on the button with busy indicator", () => {
cy.mount(<Button loading></Button>);
cy.get("[ui5-button]")
.as("button");
cy.get("@button")
.shadow()
.find("button")
.should("have.attr", "aria-busy", "true");
});
it("setting accessible-description is applied to button tag", () => {
cy.mount(<Button accessibleDescription="A long description."></Button>);
cy.get("[ui5-button]")
.shadow()
.find("button")
.as("button");
cy.get("@button")
.should("have.attr", "aria-description", "A long description.");
});
it("button with a badge", () => {
cy.mount(
<Button design="Emphasized" icon={employee}>Emphasized
<ButtonBadge design="OverlayText" text="999+" slot="badge"></ButtonBadge>
</Button>
);
cy.get("[ui5-button]")
.find("ui5-button-badge")
.as("badge");
cy.get("@badge")
.shadow()
.find("ui5-tag")
.as("tag");
cy.get("@tag")
.should("have.attr", "design", "Critical");
cy.get("@tag")
.should("have.text", "999+");
});
it("accessibilityInfo returns correct properties for different button states", () => {
cy.mount(<Button design="Emphasized" accessibleDescription="Submit form">Submit</Button>);
cy.get<Button>("[ui5-button]")
.then($button => {
const button = $button.get(0);
const info = button.accessibilityInfo;
expect(info.description).to.include("Submit form");
expect(info.description).to.include(Button.i18nBundle.getText(BUTTON_ARIA_TYPE_EMPHASIZED));
expect(info.role).to.equal("button");
expect(info.disabled).to.be.false;
});
});
it("accessibilityInfo reflects custom role and disabled state", () => {
cy.mount(<Button accessibleRole="Link" disabled>Navigate</Button>);
cy.get<Button>("[ui5-button]")
.then($button => {
const button = $button.get(0);
const info = button.accessibilityInfo;
expect(info.role).to.equal("link");
expect(info.disabled).to.be.true;
expect(info.description).to.be.undefined;
});
});
it("accessibilityInfo updates when properties change", () => {
cy.mount(<Button>Click me</Button>);
cy.get<Button>("[ui5-button]")
.as("button");
cy.get<Button>("@button")
.then($button => {
const button = $button.get(0);
expect(button.accessibilityInfo.disabled).to.be.false;
expect(button.accessibilityInfo.role).to.equal("button");
button.disabled = true;
button.accessibleRole = "Link";
button.design = "Negative";
});
cy.get<Button>("@button")
.then($button => {
const button = $button.get(0);
const info = button.accessibilityInfo;
expect(info.disabled).to.be.true;
expect(info.role).to.equal("link");
expect(info.description).to.include("Negative Action");
});
});
});