-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathindex.spec.js
More file actions
392 lines (315 loc) · 15.3 KB
/
index.spec.js
File metadata and controls
392 lines (315 loc) · 15.3 KB
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
import { createElement, setFeatureFlagForTest } from 'lwc';
import AdapterConsumer from 'x/adapterConsumer';
import { EchoWireAdapter } from 'x/echoAdapter';
import BroadcastConsumer from 'x/broadcastConsumer';
import { BroadcastAdapter } from 'x/broadcastAdapter';
import InheritedMethods from 'x/inheritedMethods';
import ContextAwareConsumer from 'x/contextAwareConsumer';
import { ContextAwareWireAdapter } from 'x/contextAwareAdapter';
const ComponentClass = AdapterConsumer;
const AdapterId = EchoWireAdapter;
const ContextLog = ContextAwareWireAdapter;
function filterCalls(echoAdapterSpy, methodType) {
return echoAdapterSpy.filter((call) => call.method === methodType);
}
describe('wiring', () => {
describe('component lifecycle and wire adapter', () => {
it('should call a connect when component is connected', async () => {
const spy = [];
const elm = createElement('x-echo-adapter-consumer', { is: ComponentClass });
AdapterId.setSpy(spy);
document.body.appendChild(elm);
expect(filterCalls(spy, 'connect').length).toBe(1);
});
it('should call a disconnect when component is disconnected', async () => {
const spy = [];
AdapterId.setSpy(spy);
const elm = createElement('x-echo-adapter-consumer', { is: ComponentClass });
document.body.appendChild(elm);
document.body.removeChild(elm);
expect(filterCalls(spy, 'disconnect').length).toBe(1);
});
it('should call a connect and disconnect when component is connected, disconnected twice', async () => {
const spy = [];
const elm = createElement('x-echo-adapter-consumer', { is: ComponentClass });
AdapterId.setSpy(spy);
document.body.appendChild(elm);
document.body.removeChild(elm);
document.body.appendChild(elm);
document.body.removeChild(elm);
const connectCalls = filterCalls(spy, 'connect');
const disconnectCalls = filterCalls(spy, 'disconnect');
expect(connectCalls.length).toBe(2);
expect(disconnectCalls.length).toBe(2);
});
});
describe('update method on wire adapter', () => {
it('should be called in same tick when component with wire no dynamic params is created', async () => {
const spy = [];
AdapterId.setSpy(spy);
expect(spy.length).toBe(0);
const elm = createElement('x-echo-adapter-consumer', { is: InheritedMethods });
document.body.appendChild(elm);
const updateCalls = filterCalls(spy, 'update');
expect(updateCalls).toHaveSize(3);
expect(updateCalls[0].method).toBe('update'); // parentMethod
expect(updateCalls[1].method).toBe('update'); // childMethod
expect(updateCalls[2].method).toBe('update'); // overriddenInChild
});
describe('with ENABLE_WIRE_SYNC_EMIT=true', () => {
beforeEach(() => {
setFeatureFlagForTest('ENABLE_WIRE_SYNC_EMIT', true);
});
afterEach(() => {
setFeatureFlagForTest('ENABLE_WIRE_SYNC_EMIT', false);
});
it('should be called synchronously after connect when a component with wire that has dynamic params is created', async () => {
const spy = [];
AdapterId.setSpy(spy);
expect(spy.length).toBe(0);
const elm = createElement('x-echo-adapter-consumer', { is: ComponentClass });
document.body.appendChild(elm);
expect(spy).toHaveSize(2);
expect(spy[0].method).toBe('connect');
expect(spy[1].method).toBe('update');
});
it('should call synchronously update only once when the component is created and a wire dynamic param is modified', async () => {
const spy = [];
AdapterId.setSpy(spy);
expect(spy.length).toBe(0);
const elm = createElement('x-echo-adapter-consumer', { is: ComponentClass });
elm.setDynamicParamSource(1);
document.body.appendChild(elm);
// in the old wire protocol, there is only one call because
// on the same tick the config was modified
const updateCalls = filterCalls(spy, 'update');
expect(updateCalls).toHaveSize(1);
});
});
it('should be called next tick when the component with wire that has dynamic params is created', async () => {
const spy = [];
AdapterId.setSpy(spy);
expect(spy.length).toBe(0);
const elm = createElement('x-echo-adapter-consumer', { is: ComponentClass });
document.body.appendChild(elm);
expect(spy).toHaveSize(1);
expect(spy[0].method).toBe('connect');
return Promise.resolve().then(() => {
expect(spy).toHaveSize(2);
expect(spy[0].method).toBe('connect');
expect(spy[1].method).toBe('update');
});
});
it('should call update only once when the component is created and a wire dynamic param is modified in the same tick', async () => {
const spy = [];
AdapterId.setSpy(spy);
expect(spy.length).toBe(0);
const elm = createElement('x-echo-adapter-consumer', { is: ComponentClass });
elm.setDynamicParamSource(1);
document.body.appendChild(elm);
return Promise.resolve().then(() => {
// in the old wire protocol, there is only one call because
// on the same tick the config was modified
const updateCalls = filterCalls(spy, 'update');
expect(updateCalls).toHaveSize(1);
});
});
it('should be called only once during multiple renders when the wire config does not change', async () => {
const spy = [];
AdapterId.setSpy(spy);
const elm = createElement('x-echo-adapter-consumer', { is: ComponentClass });
document.body.appendChild(elm);
return Promise.resolve()
.then(() => {
expect(filterCalls(spy, 'update')).toHaveSize(1);
elm.forceRerender();
return Promise.resolve();
})
.then(() => {
expect(filterCalls(spy, 'update')).toHaveSize(1);
});
});
it('should be called when the wire parameters change its value.', async () => {
const spy = [];
AdapterId.setSpy(spy);
const elm = createElement('x-echo-adapter-consumer', { is: ComponentClass });
document.body.appendChild(elm);
return Promise.resolve()
.then(() => {
expect(filterCalls(spy, 'update')).toHaveSize(1);
elm.setDynamicParamSource('simpleParam modified');
return Promise.resolve();
})
.then(() => {
expect(filterCalls(spy, 'update')).toHaveSize(2);
const wireResult = elm.getWiredProp();
expect(wireResult.simpleParam).toBe('simpleParam modified');
});
});
it('should be called for common parameter when shared among wires', async () => {
const spy = [];
AdapterId.setSpy(spy);
const elm = createElement('x-bc-consumer', { is: BroadcastConsumer });
document.body.appendChild(elm);
return Promise.resolve().then(() => {
expect(filterCalls(spy, 'update')).toHaveSize(2);
elm.setCommonParameter('modified');
return Promise.resolve().then(() => {
expect(filterCalls(spy, 'update')).toHaveSize(4);
const wireResult1 = elm.getEchoWiredProp1();
const wireResult2 = elm.getEchoWiredProp2();
expect(wireResult1.id).toBe('echoWire1');
expect(wireResult1.common).toBe('modified');
expect(wireResult2.id).toBe('echoWire2');
expect(wireResult2.common).toBe('modified');
});
});
});
it('should not update when setting parameter with same value', async () => {
const spy = [];
const elm = createElement('x-echo-adapter-consumer', { is: ComponentClass });
document.body.appendChild(elm);
AdapterId.setSpy(spy);
const expected = 'expected value';
elm.setDynamicParamSource(expected);
return Promise.resolve()
.then(() => {
expect(spy.length).toBe(1); // update,connected
const wireResult = elm.getWiredProp();
expect(wireResult.simpleParam).toBe(expected);
elm.setDynamicParamSource(expected);
return Promise.resolve();
})
.then(() => {
expect(spy.length).toBe(1); // update,connected
});
});
it('should trigger component rerender when field is updated', async () => {
const elm = createElement('x-echo-adapter-consumer', { is: ComponentClass });
document.body.appendChild(elm);
await Promise.resolve();
await Promise.resolve(); // In this tick, the config is injected.
// Now the component has re-rendered.
const staticValue = elm.shadowRoot.querySelector('.static');
const dynamicValue = elm.shadowRoot.querySelector('.dynamic');
expect(staticValue.textContent).toBe('1,2,3');
expect(dynamicValue.textContent).toBe('');
elm.setDynamicParamSource('modified value');
await new Promise((resolve) => setTimeout(resolve, 5));
expect(staticValue.textContent).toBe('1,2,3');
expect(dynamicValue.textContent).toBe('modified value');
});
it('should not call update when component is disconnected.', async () => {
const spy = [];
AdapterId.setSpy(spy);
const elm = createElement('x-echo-adapter-consumer', { is: ComponentClass });
document.body.appendChild(elm);
return Promise.resolve()
.then(() => {
expect(filterCalls(spy, 'update')).toHaveSize(1);
document.body.removeChild(elm);
elm.setDynamicParamSource('simpleParam modified');
})
.then(() => {
expect(filterCalls(spy, 'update')).toHaveSize(1);
});
});
it('should call update when component is re-connected.', async () => {
const spy = [];
AdapterId.setSpy(spy);
const elm = createElement('x-echo-adapter-consumer', { is: ComponentClass });
document.body.appendChild(elm);
return Promise.resolve()
.then(() => {
expect(filterCalls(spy, 'update')).toHaveSize(1);
document.body.removeChild(elm);
elm.setDynamicParamSource('simpleParam modified');
})
.then(() => {
expect(filterCalls(spy, 'update')).toHaveSize(1);
const wireResult = elm.getWiredProp();
expect(wireResult.simpleParam).not.toBeDefined();
document.body.appendChild(elm);
})
.then(() => {
expect(filterCalls(spy, 'update')).toHaveSize(2);
const wireResult = elm.getWiredProp();
expect(wireResult.simpleParam).toBe('simpleParam modified');
});
});
});
});
describe('wired fields', () => {
it('should rerender component when adapter pushes data', async () => {
BroadcastAdapter.clearInstances();
const elm = createElement('x-bc-consumer', { is: BroadcastConsumer });
document.body.appendChild(elm);
BroadcastAdapter.broadcastData('expected value');
return Promise.resolve()
.then(() => {
const staticValue = elm.shadowRoot.querySelector('span');
expect(staticValue.textContent).toBe('expected value');
BroadcastAdapter.broadcastData('modified value');
return Promise.resolve();
})
.then(() => {
const staticValue = elm.shadowRoot.querySelector('span');
expect(staticValue.textContent).toBe('modified value');
});
});
it('should rerender component when wired field is mutated from within the component', async () => {
BroadcastAdapter.clearInstances();
const elm = createElement('x-bc-consumer', { is: BroadcastConsumer });
document.body.appendChild(elm);
BroadcastAdapter.broadcastData('expected value');
return Promise.resolve()
.then(() => {
const staticValue = elm.shadowRoot.querySelector('span');
expect(staticValue.textContent).toBe('expected value');
elm.setWiredPropToValue('modified value');
})
.then(() => {
const staticValue = elm.shadowRoot.querySelector('span');
expect(staticValue.textContent).toBe('modified value');
});
});
});
describe('wired methods', () => {
it('should call component method when wired to a method', async () => {
BroadcastAdapter.clearInstances();
const elm = createElement('x-bc-consumer', { is: BroadcastConsumer });
document.body.appendChild(elm);
BroadcastAdapter.broadcastData('expected value');
return Promise.resolve().then(() => {
const actual = elm.getWiredMethodArgument();
expect(actual).toBe('expected value');
});
});
it('should support method override', async () => {
const spy = [];
EchoWireAdapter.setSpy(spy);
const elm = createElement('x-inherited-methods', { is: InheritedMethods });
document.body.appendChild(elm);
// No need to wait for next tick, the wire only has static config.
const calls = filterCalls(spy, 'update');
const getCallByName = (name) => {
return calls.filter((call) => name === call.args[0].name)[0];
};
expect(calls.length).toBe(3);
expect(getCallByName('overriddenInChild').args[0].child).toBe(true);
expect(getCallByName('childMethod').args[0].child).toBe(true);
expect(getCallByName('parentMethod').args[0].parent).toBe(true);
});
});
describe('context aware', () => {
it('should receive the source element tag name when adapter is constructed', async () => {
const spy = [];
ContextLog.setSpy(spy);
const hostTagNameA = 'x-context-aware-a';
const hostTagNameB = 'x-context-aware-b';
createElement(hostTagNameA, { is: ContextAwareConsumer });
createElement(hostTagNameB, { is: ContextAwareConsumer });
expect(spy[0].hostContext.tagName).toBe(hostTagNameA);
expect(spy[1].hostContext.tagName).toBe(hostTagNameB);
});
});