Skip to content

Commit 474a352

Browse files
authored
test: use vaadin testing helpers
vaadin/vaadin-button#165
1 parent 1529ac4 commit 474a352

File tree

2 files changed

+57
-34
lines changed

2 files changed

+57
-34
lines changed

packages/vaadin-button/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,9 @@
6363
"devDependencies": {
6464
"@esm-bundle/chai": "^4.1.5",
6565
"@open-wc/rollup-plugin-html": "^1.2.5",
66-
"@open-wc/testing-helpers": "^1.8.12",
6766
"@polymer/iron-component-page": "^4.0.0",
6867
"@polymer/iron-icon": "^3.0.0",
69-
"@polymer/iron-test-helpers": "^3.0.0",
68+
"@vaadin/testing-helpers": "^0.1.1",
7069
"@web/dev-server": "^0.1.5",
7170
"@web/test-runner": "^0.12.6",
7271
"@web/test-runner-saucelabs": "^0.4.0",
@@ -83,6 +82,7 @@
8382
"rimraf": "^3.0.2",
8483
"rollup": "^2.34.1",
8584
"rollup-plugin-terser": "^7.0.2",
85+
"sinon": "^9.2.4",
8686
"stylelint": "^13.8.0",
8787
"stylelint-config-prettier": "^8.0.2",
8888
"stylelint-config-vaadin": "^0.2.10"

packages/vaadin-button/test/button.test.js

Lines changed: 55 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
import { 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';
416
import { FlattenedNodesObserver } from '@polymer/polymer/lib/utils/flattened-nodes-observer.js';
517
import '../vaadin-button.js';
618

719
describe('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

Comments
 (0)