generated from adobe/aem-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 174
/
merch-quantity-select.test.html.js
199 lines (179 loc) · 7.77 KB
/
merch-quantity-select.test.html.js
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
import { runTests } from '@web/test-runner-mocha';
import { expect } from '@esm-bundle/chai';
import { mockLana } from './mocks/lana.js';
import { mockFetch } from './mocks/fetch.js';
import { mockConfig } from './mocks/config.js';
import '../src/merch-quantity-select.js';
import { appendMiloStyles, delay } from './utils.js';
import { ARROW_DOWN, ARROW_UP } from '../src/focus.js';
import { withWcs } from './mocks/wcs.js';
import { withLiterals } from './mocks/literals.js';
import mas from './mas.js';
const skipTests = sessionStorage.getItem('skipTests');
runTests(async () => {
appendMiloStyles();
if (skipTests === null) {
mockLana();
await mockFetch(withWcs, withLiterals);
await mas();
describe('merch-quantity-selector web component', () => {
const quantitySelect = document.querySelector(
'merch-quantity-select',
);
const pickerButton =
quantitySelect.shadowRoot.querySelector('.picker-button');
const popOver = quantitySelect.shadowRoot.querySelector('.popover');
it('has the required properties set up correctly', async () => {
expect(quantitySelect).to.exist;
expect(quantitySelect.title).to.equal('Select a quantity');
expect(quantitySelect.min).to.equal(1);
expect(quantitySelect.max).to.equal(10);
expect(quantitySelect.step).to.equal(1);
});
it('should open and close the menu', async () => {
expect(popOver.classList.contains('closed')).to.be.true;
pickerButton.click();
await delay();
expect(popOver.classList.contains('open')).to.be.true;
pickerButton.click();
await delay();
expect(popOver.classList.contains('closed')).to.be.true;
});
it('should set default value as 2 ', async () => {
expect(quantitySelect.highlightedIndex).to.equal(1);
expect(quantitySelect.selectedValue).to.equal(2);
});
it('should navigate up by the items ', async () => {
quantitySelect.defaultValue = undefined;
await delay();
pickerButton.click();
await delay();
expect(popOver.classList.contains('open')).to.be.true;
expect(quantitySelect.highlightedIndex).to.equal(0);
quantitySelect.handleKeydown({
key: ARROW_UP,
preventDefault: () => {},
stopPropagation: () => {},
composedPath() {
return [];
},
});
await delay();
expect(quantitySelect.highlightedIndex).to.equal(9);
});
it('should navigate down by the items ', async () => {
quantitySelect.handleKeydown({
key: ARROW_DOWN,
preventDefault: () => {},
stopPropagation: () => {},
composedPath() {
return [];
},
});
await delay();
expect(quantitySelect.highlightedIndex).to.equal(0);
});
it('should navigate and select with enter', async () => {
quantitySelect.handleKeydown({
key: ARROW_DOWN,
preventDefault: () => {},
stopPropagation: () => {},
composedPath() {
return [];
},
});
await delay();
quantitySelect.handleKeydown({
key: 'Enter',
preventDefault: () => {},
stopPropagation: () => {},
composedPath() {
return [];
},
});
await delay();
expect(quantitySelect.highlightedIndex).to.equal(1);
expect(quantitySelect.selectedValue).to.equal(2);
});
it('generates the correct options array', () => {
const options = quantitySelect.generateOptionsArray();
expect(options).to.eql([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
});
it('last option has + in the end', async () => {
const options =
quantitySelect.shadowRoot.querySelectorAll('.item');
const lastOption = options[options.length - 1];
expect(lastOption.textContent.trim()).to.equal('10+');
});
it('select item when click it', async () => {
const options =
quantitySelect.shadowRoot.querySelectorAll('.item');
options[3].click();
await delay();
expect(quantitySelect.selectedValue).to.equal(4);
});
it('key up event', async () => {
const inputField =
quantitySelect.shadowRoot.querySelector(
'.text-field-input',
);
inputField.value = '3';
const event = new KeyboardEvent('keyup', {
key: '3',
bubbles: true,
});
event.composedPath = () => [quantitySelect];
inputField.dispatchEvent(event);
await delay();
expect(quantitySelect.selectedValue).to.equal(3);
expect(popOver.classList.contains('closed')).to.be.true;
});
it('key up event and invalid value', async () => {
const inputField =
quantitySelect.shadowRoot.querySelector(
'.text-field-input',
);
inputField.value = '-11';
const event = new KeyboardEvent('keyup', {
key: '-11',
bubbles: true,
});
event.composedPath = () => [quantitySelect];
inputField.dispatchEvent(event);
await delay();
expect(quantitySelect.selectedValue).to.equal(3);
expect(popOver.classList.contains('closed')).to.be.true;
});
it('should handle mouse enter event and update highlighted index', async () => {
const options =
quantitySelect.shadowRoot.querySelectorAll('.item');
options[4].dispatchEvent(new MouseEvent('mouseenter'));
await delay();
expect(quantitySelect.highlightedIndex).to.equal(4);
});
it('handle click outside', async () => {
pickerButton.click();
await delay();
expect(popOver.classList.contains('open')).to.be.true;
quantitySelect.handleClickOutside({ composedPath: () => [] });
await delay();
expect(popOver.classList.contains('closed')).to.be.true;
});
it('should adjust to maxInput value', async () => {
const inputField =
quantitySelect.shadowRoot.querySelector(
'.text-field-input',
);
inputField.value = '300';
const event = new KeyboardEvent('keyup', {
key: '300',
bubbles: true,
});
event.composedPath = () => [quantitySelect];
inputField.dispatchEvent(event);
await delay();
expect(quantitySelect.selectedValue).to.equal(250);
});
});
}
});