11import { expect } from '@esm-bundle/chai' ;
2- import { fixtureSync } from '@open-wc/testing-helpers' ;
3- import { downAndUp , keyDownOn , keyUpOn } from '@polymer/iron-test-helpers/mock-interactions.js' ;
2+ import sinon from 'sinon' ;
3+ import {
4+ arrowDownKeyDown ,
5+ enterKeyDown ,
6+ enterKeyUp ,
7+ fixtureSync ,
8+ isIOS ,
9+ mousedown ,
10+ mouseup ,
11+ spaceKeyDown ,
12+ spaceKeyUp ,
13+ touchstart ,
14+ touchend
15+ } from '@vaadin/testing-helpers' ;
416import { FlattenedNodesObserver } from '@polymer/polymer/lib/utils/flattened-nodes-observer.js' ;
517import '../vaadin-button.js' ;
618
719describe ( 'vaadin-button' , ( ) => {
8- const down = ( node ) => {
9- node . dispatchEvent ( new CustomEvent ( 'down' ) ) ;
10- } ;
11-
12- const up = ( node ) => {
13- node . dispatchEvent ( new CustomEvent ( 'up' ) ) ;
14- } ;
15-
1620 let vaadinButton , nativeButton , label ;
1721
1822 beforeEach ( ( ) => {
@@ -36,11 +40,19 @@ describe('vaadin-button', () => {
3640 expect ( nativeButton . hasAttribute ( 'disabled' ) ) . to . be . eql ( true ) ;
3741 } ) ;
3842
39- it ( 'fires click event' , ( done ) => {
40- vaadinButton . addEventListener ( 'click' , ( ) => {
41- done ( ) ;
42- } ) ;
43- downAndUp ( vaadinButton ) ;
43+ it ( 'should fire click event' , ( ) => {
44+ const spy = sinon . spy ( ) ;
45+ vaadinButton . addEventListener ( 'click' , spy ) ;
46+ vaadinButton . click ( ) ;
47+ expect ( spy . calledOnce ) . to . be . true ;
48+ } ) ;
49+
50+ it ( 'should not fire click event when disabled' , ( ) => {
51+ const spy = sinon . spy ( ) ;
52+ vaadinButton . addEventListener ( 'click' , spy ) ;
53+ vaadinButton . disabled = true ;
54+ vaadinButton . click ( ) ;
55+ expect ( spy . called ) . to . be . false ;
4456 } ) ;
4557
4658 it ( 'host should have the `button` role' , ( ) => {
@@ -55,60 +67,71 @@ describe('vaadin-button', () => {
5567 expect ( nativeButton . getAttribute ( 'role' ) ) . to . be . eql ( 'presentation' ) ;
5668 } ) ;
5769
58- it ( 'should have active attribute on down' , ( ) => {
59- down ( vaadinButton ) ;
70+ ( isIOS ? it . skip : it ) ( 'should have active attribute on mousedown' , ( ) => {
71+ mousedown ( vaadinButton ) ;
72+ expect ( vaadinButton . hasAttribute ( 'active' ) ) . to . be . true ;
73+ } ) ;
74+
75+ ( isIOS ? it . skip : it ) ( 'should not have active attribute after mouseup' , ( ) => {
76+ mousedown ( vaadinButton ) ;
77+ mouseup ( vaadinButton ) ;
78+ expect ( vaadinButton . hasAttribute ( 'active' ) ) . to . be . false ;
79+ } ) ;
80+
81+ it ( 'should have active attribute on touchstart' , ( ) => {
82+ touchstart ( vaadinButton ) ;
6083 expect ( vaadinButton . hasAttribute ( 'active' ) ) . to . be . true ;
6184 } ) ;
6285
63- it ( 'should not have active attribute after up ' , ( ) => {
64- down ( vaadinButton ) ;
65- up ( vaadinButton ) ;
86+ it ( 'should not have active attribute after touchend ' , ( ) => {
87+ touchstart ( vaadinButton ) ;
88+ touchend ( vaadinButton ) ;
6689 expect ( vaadinButton . hasAttribute ( 'active' ) ) . to . be . false ;
6790 } ) ;
6891
6992 it ( 'should have active attribute on enter' , ( ) => {
70- keyDownOn ( vaadinButton , 13 ) ;
93+ enterKeyDown ( vaadinButton ) ;
7194 expect ( vaadinButton . hasAttribute ( 'active' ) ) . to . be . true ;
7295 } ) ;
7396
7497 it ( 'should not have active attribute after enter' , ( ) => {
75- keyDownOn ( vaadinButton , 13 ) ;
76- keyUpOn ( vaadinButton , 13 ) ;
98+ enterKeyDown ( vaadinButton ) ;
99+ enterKeyUp ( vaadinButton ) ;
77100 expect ( vaadinButton . hasAttribute ( 'active' ) ) . to . be . false ;
78101 } ) ;
79102
80103 it ( 'should have active attribute on space' , ( ) => {
81- keyDownOn ( vaadinButton , 32 ) ;
104+ spaceKeyDown ( vaadinButton ) ;
82105 expect ( vaadinButton . hasAttribute ( 'active' ) ) . to . be . true ;
83106 } ) ;
84107
85108 it ( 'should not have active attribute after space' , ( ) => {
86- keyDownOn ( vaadinButton , 32 ) ;
87- keyUpOn ( vaadinButton , 32 ) ;
109+ spaceKeyDown ( vaadinButton ) ;
110+ spaceKeyUp ( vaadinButton ) ;
88111 expect ( vaadinButton . hasAttribute ( 'active' ) ) . to . be . false ;
89112 } ) ;
90113
91114 it ( 'should not have active attribute on arrow key' , ( ) => {
92- keyDownOn ( vaadinButton , 37 ) ;
115+ arrowDownKeyDown ( vaadinButton ) ;
93116 expect ( vaadinButton . hasAttribute ( 'active' ) ) . to . be . false ;
94117 } ) ;
95118
96119 it ( 'should not have active attribute when disabled' , ( ) => {
97120 vaadinButton . disabled = true ;
98- down ( vaadinButton ) ;
99- keyDownOn ( vaadinButton , 13 ) ;
100- keyDownOn ( vaadinButton , 32 ) ;
121+ mousedown ( vaadinButton ) ;
122+ enterKeyDown ( vaadinButton ) ;
123+ spaceKeyDown ( vaadinButton ) ;
101124 expect ( vaadinButton . hasAttribute ( 'active' ) ) . to . be . false ;
102125 } ) ;
103126
104127 it ( 'should not have active attribute when disconnected from the DOM' , ( ) => {
105- keyDownOn ( vaadinButton , 32 ) ;
128+ spaceKeyDown ( vaadinButton ) ;
106129 vaadinButton . parentNode . removeChild ( vaadinButton ) ;
107130 expect ( vaadinButton . hasAttribute ( 'active' ) ) . to . be . false ;
108131 } ) ;
109132
110133 it ( 'should not have active attribute after blur' , ( ) => {
111- keyDownOn ( vaadinButton , 32 ) ;
134+ spaceKeyDown ( vaadinButton ) ;
112135 vaadinButton . dispatchEvent ( new CustomEvent ( 'blur' ) ) ;
113136 expect ( vaadinButton . hasAttribute ( 'active' ) ) . to . be . false ;
114137 } ) ;
0 commit comments