-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpower-select-test.ts
322 lines (283 loc) · 11 KB
/
power-select-test.ts
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
import { hbs } from 'ember-cli-htmlbars';
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { setupIntl } from 'ember-intl/test-support';
import { render, setupOnerror, findAll, click, typeIn, scrollTo } from '@ember/test-helpers';
import sinon from 'sinon';
module('Integration | Component | o-s-s/power-select', function (hooks) {
setupRenderingTest(hooks);
setupIntl(hooks);
const onSearch = sinon.stub();
const onChange = sinon.stub();
hooks.beforeEach(function () {
this.selectedItems = [];
this.items = [];
this.onSearch = onSearch;
});
module('it renders', () => {
test('with all required named blocks', async function (assert) {
await render(hbs`
<OSS::PowerSelect @selectedItems={{this.selectedItems}} @items={{this.items}}
@onSearch={{this.onSearch}}>
<:selected-item as |selectedItem|>
{{selectedItem}}
</:selected-item>
<:option-item as |item|>
{{item}}
</:option-item>
</OSS::PowerSelect>
`);
assert.dom('.upf-power-select').exists({ count: 1 });
assert.dom('.upf-power-select__array-container').exists({ count: 1 });
assert.dom('.upf-power-select').hasNoAttribute('open');
await click('.upf-power-select__array-container');
assert.dom('.upf-power-select').hasAttribute('open');
});
test('it renders without a border when @borderless is true', async function (assert) {
await render(hbs`
<OSS::PowerSelect @selectedItems={{this.selectedItems}} @items={{this.items}}
@onSearch={{this.onSearch}} @borderless={{true}}>
<:selected-item as |selectedItem|>
{{selectedItem}}
</:selected-item>
<:option-item as |item|>
{{item}}
</:option-item>
<:empty-state>
<div class="foobar">custom empty state</div>
</:empty-state>
</OSS::PowerSelect>
`);
assert.dom('.upf-power-select').hasClass('borderless');
});
test('custom empty state is properly rendered', async function (assert) {
await render(hbs`
<OSS::PowerSelect @selectedItems={{this.selectedItems}} @items={{this.items}}
@onSearch={{this.onSearch}}>
<:selected-item as |selectedItem|>
{{selectedItem}}
</:selected-item>
<:option-item as |item|>
{{item}}
</:option-item>
<:empty-state>
<div class="foobar">custom empty state</div>
</:empty-state>
</OSS::PowerSelect>
`);
assert.dom('.upf-power-select').exists({ count: 1 });
assert.dom('.upf-power-select__array-container').exists({ count: 1 });
assert.dom('.upf-power-select').hasNoAttribute('open');
await click('.upf-power-select__array-container');
assert.dom('.upf-power-select').hasAttribute('open');
assert.dom('.foobar').exists();
assert.dom('.foobar').hasText('custom empty state');
});
});
module('with @selectedItems', () => {
test('Passing @selectedItems parameter displays the items', async function (assert) {
this.selectedItems = ['value1', 'value2'];
await render(hbs`
<OSS::PowerSelect @selectedItems={{this.selectedItems}} @items={{this.items}}
@onSearch={{this.onSearch}}>
<:selected-item as |selectedItem|>
<span>{{selectedItem}}</span>
</:selected-item>
<:option-item as |item|>
{{item}}
</:option-item>
</OSS::PowerSelect>
`);
const domTags = findAll('.array-input-container span');
assert.dom(domTags[0]).hasText('value1');
assert.dom(domTags[1]).hasText('value2');
});
test('Passing empty @selectedItems parameter displays nothing', async function (assert) {
await render(hbs`
<OSS::PowerSelect @selectedItems={{this.selectedItems}} @items={{this.items}}
@onSearch={{this.onSearch}}>
<:selected-item as |selectedItem|>
<span id="selectedItemTest">{{selectedItem}}</span>
</:selected-item>
<:option-item as |item|>
{{item}}
</:option-item>
</OSS::PowerSelect>
`);
assert.dom('.array-input-container #selectedItemTest').doesNotExist();
});
test('Passing empty @selectedItems and @placeholder parameters displays placeholder', async function (assert) {
await render(hbs`
<OSS::PowerSelect @selectedItems={{this.selectedItems}} @items={{this.items}}
@onSearch={{this.onSearch}} @placeholder="placeholder">
<:selected-item as |selectedItem|>
{{selectedItem}}
</:selected-item>
<:option-item as |item|>
{{item}}
</:option-item>
</OSS::PowerSelect>
`);
assert.dom('.array-input-container').hasText('placeholder');
});
});
module('with @items', (hooks) => {
hooks.beforeEach(function () {
this.items = ['value1', 'value2'];
});
test('Passing @items parameter displays the items in InfiniteSelect', async function (assert) {
await render(hbs`
<OSS::PowerSelect @selectedItems={{this.selectedItems}} @items={{this.items}}
@onSearch={{this.onSearch}}>
<:selected-item as |selectedItem|>
{{selectedItem}}
</:selected-item>
<:option-item as |item|>
{{item}}
</:option-item>
</OSS::PowerSelect>
`);
await click('.upf-power-select__array-container');
const domTags = findAll('.upf-infinite-select__item');
assert.dom(domTags[0]).hasText('value1');
assert.dom(domTags[1]).hasText('value2');
});
});
module('with @onChange', (hooks) => {
hooks.beforeEach(function () {
this.selectedItems = ['value1', 'value2'];
this.items = ['value1', 'value2'];
this.onChange = onChange;
});
test('selecting item triggers onChange with selection operation', async function (assert) {
await render(hbs`
<OSS::PowerSelect @selectedItems={{this.selectedItems}} @items={{this.items}}
@onSearch={{this.onSearch}} @onChange={{this.onChange}}>
<:selected-item as |selectedItem|>
{{selectedItem}}
</:selected-item>
<:option-item as |item|>
{{item}}
</:option-item>
</OSS::PowerSelect>
`);
await click('.upf-power-select__array-container');
await click('.upf-infinite-select__item:nth-child(1)');
assert.ok(this.onChange.calledWith('value1', 'selection'));
});
});
module('with @searchPlaceholder', () => {
test('Passing @items parameter displays the items in InfiniteSelect', async function (assert) {
await render(hbs`
<OSS::PowerSelect @selectedItems={{this.selectedItems}} @items={{this.items}}
@onSearch={{this.onSearch}} @searchPlaceholder='searchPlaceholder'>
<:selected-item as |selectedItem|>
{{selectedItem}}
</:selected-item>
<:option-item as |item|>
{{item}}
</:option-item>
</OSS::PowerSelect>
`);
await click('.upf-power-select__array-container');
assert.dom('.upf-infinite-select input').hasAttribute('placeholder', 'searchPlaceholder');
});
});
module('with @onSearch', () => {
test('search is correctly called', async function (assert) {
await render(hbs`
<OSS::PowerSelect @selectedItems={{this.selectedItems}} @items={{this.items}}
@onSearch={{this.onSearch}}>
<:selected-item as |selectedItem|>
{{selectedItem}}
</:selected-item>
<:option-item as |item|>
{{item}}
</:option-item>
</OSS::PowerSelect>
`);
await click('.upf-power-select__array-container');
await typeIn('.upf-infinite-select input', 's');
assert.ok(this.onSearch.calledWith('s'));
});
});
module('with @onBottomReached', (hooks) => {
hooks.beforeEach(function () {
this.items = ['value1', 'value2', 'value3'];
this.loadingMore = false;
this.onBottomReached = sinon.stub().callsFake(() => {
this.set('loadingMore', true);
});
});
test('search is correctly call', async function (assert) {
await render(hbs`
<div style="height:150px">
<OSS::PowerSelect @selectedItems={{this.selectedItems}} @items={{this.items}} @loadingMore={{this.loadingMore}}
@onSearch={{this.onSearch}} @onBottomReached={{this.onBottomReached}}>
<:selected-item as |selectedItem|>
{{selectedItem}}
</:selected-item>
<:option-item as |item|>
{{item}}
</:option-item>
</OSS::PowerSelect>
</div>
`);
await click('.upf-power-select__array-container');
await scrollTo('.upf-infinite-select__items-container', 0, 1500);
this.set('loadingMore', false);
await scrollTo('.upf-infinite-select__items-container', 0, 1500);
assert.ok(this.onBottomReached.calledTwice);
});
});
module('with @addressableAs', (hooks) => {
hooks.beforeEach(function () {
this.items = ['value1', 'value2', 'value3'];
});
test('the dropdown has the right class assigned to it', async function (assert) {
await render(hbs`
<div style="height:150px">
<OSS::PowerSelect @selectedItems={{this.selectedItems}} @items={{this.items}} @onSearch={{this.onSearch}} @addressableAs="foobar-select">
<:selected-item as |selectedItem|>
{{selectedItem}}
</:selected-item>
<:option-item as |item|>
{{item}}
</:option-item>
</OSS::PowerSelect>
</div>
`);
await click('.upf-power-select__array-container');
assert.dom('.upf-infinite-select').exists();
assert.dom('.upf-infinite-select').hasClass('foobar-select__dropdown');
});
});
module('Error management', () => {
test('without selected-item named block', async function (assert) {
setupOnerror((err: any) => {
assert.equal(
err.message,
'Assertion Failed: [component][OSS::PowerSelect] You must pass selected-item named block'
);
});
await render(hbs`
<OSS::PowerSelect @onSearch={{this.onSearch}}/>
`);
});
test('without option-item named block', async function (assert) {
setupOnerror((err: any) => {
assert.equal(
err.message,
'Assertion Failed: [component][OSS::PowerSelect] You must pass option-item named block'
);
});
await render(hbs`
<OSS::PowerSelect @onSearch={{this.onSearch}}>
<:selected-item as |selectedItem|>
{{selectedItem.name}}
</:selected-item>
</OSS::PowerSelect>
`);
});
});
});