1+ import Button from "../../src/Button.js" ;
2+ import Label from "../../src/Label.js" ;
3+ import Switch from "../../src/Switch.js" ;
4+
5+ describe ( "General events interactions" , ( ) => {
6+ it ( "Should fire change event" , ( ) => {
7+ cy . mount ( < Switch onChange = { cy . stub ( ) . as ( "changed" ) } > Click me</ Switch > ) ;
8+
9+ cy . get ( "[ui5-switch]" )
10+ . as ( "switch" )
11+
12+ cy . get ( "@switch" )
13+ . realClick ( ) ;
14+
15+ cy . get ( "@changed" )
16+ . should ( "have.been.calledOnce" ) ;
17+
18+ cy . get ( "@switch" )
19+ . should ( "have.attr" , "checked" ) ;
20+ } ) ;
21+
22+ it ( "Should not change checked property" , ( ) => {
23+ cy . mount ( < Switch > Click me</ Switch > ) ;
24+
25+ cy . get ( "[ui5-switch]" )
26+ . as ( "switch" )
27+ . then ( $switch => {
28+ $switch . get ( 0 ) . addEventListener ( "ui5-change" , ( e ) => e . preventDefault ( ) ) ;
29+ $switch . get ( 0 ) . addEventListener ( "ui5-change" , cy . stub ( ) . as ( "changed" ) ) ;
30+ } ) ;
31+
32+ cy . get ( "@switch" )
33+ . realClick ( ) ;
34+
35+ cy . get ( "@changed" )
36+ . should ( "have.been.calledOnce" ) ;
37+
38+ cy . get ( "@switch" )
39+ . should ( "not.have.attr" , "checked" ) ;
40+ } ) ;
41+ } ) ;
42+
43+ describe ( "General accesibility attributes" , ( ) => {
44+ const geoLocationDescr = "Geographical location" ;
45+ const gpsLocationDescr = "Use GPS location" ;
46+
47+ it ( "Should set correct 'accessible-name' attribute on the root" , ( ) => {
48+ cy . mount ( < Switch accessible-name = { geoLocationDescr } text-on = "Yes" text-off = "No" > </ Switch > ) ;
49+
50+ cy . get ( "[ui5-switch]" ) . as ( "switch" )
51+ . ui5SwitchCheckAttributeInShadowDomRoot ( "role" , "switch" ) ;
52+
53+ cy . get ( "@switch" )
54+ . ui5SwitchCheckAttributeInShadowDomRoot ( "aria-label" , `${ geoLocationDescr } No` ) ;
55+
56+ } ) ;
57+
58+ it ( "Should set correct 'accessible-name-ref' attribute on the root" , ( ) => {
59+ cy . mount (
60+ < >
61+ < Label id = "descriptionText" > { gpsLocationDescr } </ Label >
62+ < Switch accessible-name-ref = "descriptionText" text-on = "Yes" text-off = "No" > </ Switch >
63+ </ >
64+ ) ;
65+
66+ cy . get ( "[ui5-switch]" ) . as ( "switch" )
67+ . ui5SwitchCheckAttributeInShadowDomRoot ( "role" , "switch" ) ;
68+
69+ cy . get ( "@switch" )
70+ . ui5SwitchCheckAttributeInShadowDomRoot ( "aria-label" , `${ gpsLocationDescr } No` ) ;
71+ } ) ;
72+
73+ it ( "Should set correct correct tooltip on the root" , ( ) => {
74+ cy . mount ( < Switch tooltip = { gpsLocationDescr } text-on = "Yes" text-off = "No" > </ Switch > ) ;
75+
76+ cy . get ( "[ui5-switch]" )
77+ . ui5SwitchCheckAttributeInShadowDomRoot ( "title" , gpsLocationDescr ) ;
78+ } ) ;
79+
80+ it ( "Should set correct attribute 'aria-label' when 'text-on' and 'text-off' attributes aren't set" , ( ) => {
81+ cy . mount ( < Switch > </ Switch > ) ;
82+
83+ cy . get ( "[ui5-switch]" )
84+ . ui5SwitchCheckAttributeInShadowDomRoot ( "aria-label" , "" ) ;
85+ } ) ;
86+
87+ it ( "Should propagate 'required' attribute properly on the root" , ( ) => {
88+ cy . mount ( < Switch required > </ Switch > ) ;
89+
90+ cy . get ( "[ui5-switch]" )
91+ . ui5SwitchCheckAttributeInShadowDomRoot ( "aria-required" , "true" ) ;
92+ } ) ;
93+
94+ it ( "Should not propagate 'required' attribute on the root" , ( ) => {
95+ cy . mount ( < Switch > </ Switch > ) ;
96+
97+ cy . get ( "[ui5-switch]" )
98+ . ui5SwitchCheckAttributeInShadowDomRoot ( "aria-required" , "false" ) ;
99+ } ) ;
100+ } ) ;
101+
102+ describe ( "General interactions in form" , ( ) => {
103+ it ( "Should submit form only when 'required' switch is checked" , ( ) => {
104+ cy . mount (
105+ < form id = "switchForm" >
106+ < Switch checked > </ Switch >
107+ < Switch id = "requiredTestSwitch" required > </ Switch >
108+ < Button id = "switchSubmit" type = "Submit" > Submit</ Button >
109+ </ form >
110+ ) ;
111+
112+ cy . get < HTMLFormElement > ( "#switchForm" ) . should ( ( $form ) => {
113+ expect ( $form [ 0 ] . checkValidity ( ) ) . to . be . false ;
114+ } ) ;
115+
116+ cy . get ( "#requiredTestSwitch" )
117+ . realClick ( ) ;
118+
119+ cy . get < HTMLFormElement > ( "#switchForm" ) . should ( ( $form ) => {
120+ expect ( $form [ 0 ] . checkValidity ( ) ) . to . be . true ;
121+ } ) ;
122+ } ) ;
123+ } ) ;
0 commit comments