-
-
Notifications
You must be signed in to change notification settings - Fork 147
/
select-value-observer.spec.ts
335 lines (256 loc) · 10.7 KB
/
select-value-observer.spec.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
323
324
325
326
327
328
329
330
331
332
333
334
335
import { LifecycleFlags as LF } from '@aurelia/runtime';
import { SelectValueObserver } from '@aurelia/runtime-html';
import { h, HTMLTestContext, TestContext, verifyEqual, assert, createSpy } from '@aurelia/testing';
type Anything = any;
// TODO: need many more tests here, this is just preliminary
describe('SelectValueObserver', function () {
function createFixture(initialValue: Anything = '', options = [], multiple = false) {
const ctx = TestContext.createHTMLTestContext();
const { dom, lifecycle, observerLocator } = ctx;
const optionElements = options.map(o => `<option value="${o}">${o}</option>`).join('\n');
const markup = `<select ${multiple ? 'multiple' : ''}>\n${optionElements}\n</select>`;
const el = ctx.createElementFromMarkup(markup) as HTMLSelectElement;
const sut = observerLocator.getObserver(LF.none, el, 'value') as SelectValueObserver;
sut.setValue(initialValue, LF.fromBind);
return { ctx, lifecycle, el, sut, dom };
}
describe('setValue()', function () {
const valuesArr = [['', 'foo', 'bar']];
const initialArr = ['', 'foo', 'bar'];
const nextArr = ['', 'foo', 'bar'];
for (const values of valuesArr) {
for (const initial of initialArr) {
for (const next of nextArr) {
it(`sets 'value' from "${initial}" to "${next}"`, function () {
const { lifecycle, el, sut } = createFixture(initial, values);
assert.strictEqual(el.value, initial, `el.value`);
sut.bind(LF.none);
el.options.item(values.indexOf(next)).selected = true;
lifecycle.processRAFQueue(LF.none);
assert.strictEqual(el.value, next, `el.value`);
sut.unbind(LF.none);
});
}
}
}
});
describe('bind()', function () {
if (typeof MutationObserver !== 'undefined') {
it('uses private method handleNodeChange as callback', async function () {
for (const isMultiple of [true, false]) {
const { ctx, el, sut } = createFixture([], [], isMultiple);
const callbackSpy = createSpy(sut, 'handleNodeChange', true);
sut.bind(LF.none);
el.appendChild(ctx.createElement('option'));
await Promise.resolve();
assert.strictEqual(callbackSpy.calls.length, 1, 'callbackSpy.calls.length');
sut.unbind(LF.none);
}
});
}
});
describe('unbind()', function () {
it('disconnect node observer', function () {
for (const isMultiple of [true, false]) {
const { sut } = createFixture([], [], isMultiple);
let count = 0;
const nodeObserver: any = { disconnect() {
count++;
} };
sut['nodeObserver'] = nodeObserver;
sut.unbind(LF.none);
assert.strictEqual(count, 1, `count`);
assert.strictEqual(sut['nodeObserver'], null, `sut['nodeObserver']`);
}
});
it('unsubscribes array observer', function () {
for (const isMultiple of [true, false]) {
const { sut } = createFixture([], [], isMultiple);
let count = 0;
const nodeObserver: any = { disconnect() {
return;
} };
const arrayObserver: any = {
unsubscribeFromCollection(observer: Anything) {
assert.strictEqual(observer, sut, 'It should have unsubscribe with right observer.');
count++;
}
};
sut['nodeObserver'] = nodeObserver;
sut['arrayObserver'] = arrayObserver;
sut.unbind(LF.none);
assert.strictEqual(count, 1, `count`);
assert.strictEqual(sut['arrayObserver'], null, `sut['arrayObserver']`);
}
});
});
describe('synchronizeOptions', function () {
return;
});
describe('synchronizeValue()', function () {
describe('<select />', function () {
return;
});
// There is subtle difference in behavior of synchronization for SelectObserver
// When synchronzing value without synchronizing Options prior
// the behavior is different, as such, if currentValue is an array
// 1. With synchronizeOptions: source => target => source. Or selected <option/> are based on value array
// 2. Without synchronizeOptions: target => source. Or selected values are based on selected <option/>
describe('<select multiple="true" />', function () {
it('synchronizes with array', function () {
const { sut } = createMutiSelectSut([], [
option({ text: 'A', selected: true }),
option({ text: 'B', selected: true }),
option({ text: 'C' })
]);
sut.bind(LF.none);
const currentValue = sut.currentValue as any[];
assert.instanceOf(currentValue, Array);
assert.strictEqual(currentValue['length'], 0, `currentValue['length']`);
sut.synchronizeValue();
assert.strictEqual(currentValue, sut.currentValue, `currentValue`);
assert.strictEqual(currentValue['length'], 2, `currentValue['length']`);
sut.unbind(LF.none);
});
it('synchronizes with null', function () {
const { sut } = createMutiSelectSut(null, [
option({ text: 'A', selected: true }),
option({ text: 'B', selected: true }),
option({ text: 'C' })
]);
sut.bind(LF.none);
const currentValue = sut.currentValue as any;
assert.strictEqual(currentValue, null, `currentValue`);
sut.synchronizeValue();
assert.strictEqual(currentValue, sut.currentValue, `currentValue`);
sut.unbind(LF.none);
});
it('synchronizes with undefined', function () {
const { sut } = createMutiSelectSut(undefined, [
option({ text: 'A', selected: true }),
option({ text: 'B', selected: true }),
option({ text: 'C' })
]);
sut.bind(LF.none);
const currentValue = sut.currentValue as any;
assert.strictEqual(currentValue, undefined, `currentValue`);
sut.synchronizeValue();
assert.strictEqual(currentValue, sut.currentValue, `currentValue`);
sut.unbind(LF.none);
});
it('synchronizes with array (2)', function () {
const { sut } = createMutiSelectSut([], [
option({ text: 'A', _model: { id: 1, name: 'select 1' }, selected: true }),
option({ text: 'B', _model: { id: 2, name: 'select 2' }, selected: true }),
option({ text: 'C' })
]);
sut.bind(LF.none);
const currentValue = sut.currentValue as any[];
sut.synchronizeValue();
assert.strictEqual(currentValue, sut.currentValue, `currentValue`);
assert.strictEqual(currentValue['length'], 2, `currentValue['length']`);
verifyEqual(
currentValue,
[
{ id: 1, name: 'select 1' },
{ id: 2, name: 'select 2' }
]
);
sut.unbind(LF.none);
});
it('synchronizes with array (3): disregard "value" when there is model', function () {
const { sut } = createMutiSelectSut([], [
option({ text: 'A', value: 'AA', _model: { id: 1, name: 'select 1' }, selected: true }),
option({ text: 'B', value: 'BB', _model: { id: 2, name: 'select 2' }, selected: true }),
option({ text: 'C', value: 'CC' })
]);
sut.bind(LF.none);
const currentValue = sut.currentValue as any[];
sut.synchronizeValue();
assert.strictEqual(currentValue, sut.currentValue, `currentValue`);
assert.strictEqual(currentValue['length'], 2, `currentValue['length']`);
verifyEqual(
currentValue,
[
{ id: 1, name: 'select 1' },
{ id: 2, name: 'select 2' }
]
);
sut.unbind(LF.none);
});
it('synchronize regardless disabled state of <option/>', function () {
const { sut } = createMutiSelectSut([], [
option({ text: 'A', value: 'AA', _model: { id: 1, name: 'select 1' }, selected: true }),
option({ text: 'B', value: 'BB', disabled: true, _model: { id: 2, name: 'select 2' }, selected: true }),
option({ text: 'C', value: 'CC', disabled: true, selected: true })
]);
sut.bind(LF.none);
const currentValue = sut.currentValue as any[];
sut.synchronizeValue();
assert.strictEqual(currentValue, sut.currentValue, `currentValue`);
verifyEqual(
currentValue,
[
{ id: 1, name: 'select 1' },
{ id: 2, name: 'select 2' },
'CC'
]
);
sut.unbind(LF.none);
});
describe('with <optgroup>', function () {
it('synchronizes with array', function () {
const { sut } = createMutiSelectSut([], [
optgroup(
{},
option({ text: 'A', _model: { id: 1, name: 'select 1' }, selected: true }),
option({ text: 'B', _model: { id: 2, name: 'select 2' }, selected: true }),
),
option({ text: 'C', value: 'CC' })
]);
sut.bind(LF.none);
const currentValue = sut.currentValue as any[];
sut.synchronizeValue();
assert.strictEqual(currentValue, sut.currentValue, `currentValue`);
assert.strictEqual(currentValue['length'], 2, `currentValue['length']`);
verifyEqual(
currentValue,
[
{ id: 1, name: 'select 1' },
{ id: 2, name: 'select 2' }
]
);
sut.unbind(LF.none);
});
});
type SelectValidChild = HTMLOptionElement | HTMLOptGroupElement;
function createMutiSelectSut(initialValue: Anything[], optionFactories: ((ctx: HTMLTestContext) => SelectValidChild)[]) {
const ctx = TestContext.createHTMLTestContext();
const { observerLocator } = ctx;
const el = select(...optionFactories.map(create => create(ctx)))(ctx);
const sut = observerLocator.getObserver(LF.none, el, 'value') as SelectValueObserver;
sut.oldValue = sut.currentValue = initialValue;
return { ctx, el, sut };
}
function select(...options: SelectValidChild[]): (ctx: HTMLTestContext) => HTMLSelectElement {
return function(ctx: HTMLTestContext) {
return h(
'select',
{ multiple: true },
...options
);
};
}
});
});
function option(attributes: Record<string, any>) {
return function(ctx: HTMLTestContext) {
return h('option', attributes);
};
}
function optgroup(attributes: Record<string, any>, ...optionFactories: ((ctx: HTMLTestContext) => HTMLOptionElement)[]) {
return function(ctx: HTMLTestContext) {
return h('optgroup', attributes, ...optionFactories.map(create => create(ctx)));
};
}
});