@enact/core 3.4.9-experimental-3 → 3.4.9-experimental-7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (113) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/{build/dispatcher → dispatcher}/dispatcher.d.ts +0 -0
  3. package/dispatcher/dispatcher.js +76 -57
  4. package/dispatcher/listeners.js +31 -19
  5. package/dispatcher/tests/dispatcher-specs.js +75 -100
  6. package/{build/handle → handle}/handle.d.ts +0 -0
  7. package/handle/handle.js +260 -212
  8. package/handle/tests/handle-specs.js +476 -601
  9. package/{build/hoc → hoc}/hoc.d.ts +0 -0
  10. package/hoc/hoc.js +47 -33
  11. package/hoc/tests/hoc-specs.js +85 -126
  12. package/{build/index.d.ts → index.d.ts} +0 -0
  13. package/index.js +1 -0
  14. package/{build/internal → internal}/ApiDecorator/ApiDecorator.d.ts +0 -0
  15. package/internal/ApiDecorator/ApiDecorator.js +107 -61
  16. package/internal/ApiDecorator/tests/ApiDecorator-specs.js +106 -97
  17. package/{build/internal → internal}/Registry/Registry.d.ts +0 -0
  18. package/internal/Registry/Registry.js +68 -51
  19. package/internal/Registry/tests/Registry-specs.js +201 -143
  20. package/{build/internal → internal}/deprecate/deprecate.d.ts +0 -0
  21. package/internal/deprecate/deprecate.js +85 -60
  22. package/{build/internal → internal}/prop-types/prop-types.d.ts +0 -0
  23. package/internal/prop-types/prop-types.js +68 -61
  24. package/{build/keymap → keymap}/index.d.ts +0 -0
  25. package/keymap/index.js +27 -15
  26. package/keymap/keymap.js +67 -52
  27. package/keymap/tests/keymap-specs.js +75 -104
  28. package/kind/computed.js +29 -16
  29. package/{build/kind → kind}/kind.d.ts +0 -0
  30. package/kind/kind.js +150 -114
  31. package/kind/styles.js +62 -47
  32. package/kind/tests/computed-specs.js +87 -86
  33. package/kind/tests/kind-specs.js +216 -214
  34. package/kind/tests/styles-specs.js +170 -223
  35. package/kind/util.js +15 -7
  36. package/package.json +1 -1
  37. package/{build/platform → platform}/platform.d.ts +0 -0
  38. package/platform/platform.js +263 -169
  39. package/platform/tests/platform-specs.js +92 -90
  40. package/{build/snapshot → snapshot}/snapshot.d.ts +0 -0
  41. package/snapshot/snapshot.js +34 -27
  42. package/useChainRefs/tests/useChainRefs-specs.js +46 -30
  43. package/{build/useChainRefs → useChainRefs}/useChainRefs.d.ts +0 -0
  44. package/useChainRefs/useChainRefs.js +49 -29
  45. package/useClass/tests/useClass-specs.js +51 -46
  46. package/{build/useClass → useClass}/useClass.d.ts +0 -0
  47. package/useClass/useClass.js +27 -9
  48. package/useHandlers/Handlers.js +55 -30
  49. package/useHandlers/tests/useHandlers-specs.js +79 -95
  50. package/{build/useHandlers → useHandlers}/useHandlers.d.ts +0 -0
  51. package/useHandlers/useHandlers.js +18 -11
  52. package/util/Job.js +203 -241
  53. package/util/tests/Job-specs.js +216 -204
  54. package/util/tests/util-specs.js +123 -146
  55. package/{build/util → util}/util.d.ts +0 -0
  56. package/util/util.js +146 -103
  57. package/build/CHANGELOG.md +0 -699
  58. package/build/README.md +0 -33
  59. package/build/dispatcher/dispatcher.js +0 -160
  60. package/build/dispatcher/listeners.js +0 -37
  61. package/build/dispatcher/package.json +0 -3
  62. package/build/dispatcher/tests/dispatcher-specs.js +0 -75
  63. package/build/handle/handle.js +0 -846
  64. package/build/handle/package.json +0 -3
  65. package/build/handle/tests/handle-specs.js +0 -476
  66. package/build/hoc/hoc.js +0 -102
  67. package/build/hoc/package.json +0 -3
  68. package/build/hoc/tests/hoc-specs.js +0 -90
  69. package/build/index.js +0 -2
  70. package/build/internal/ApiDecorator/ApiDecorator.js +0 -143
  71. package/build/internal/ApiDecorator/package.json +0 -3
  72. package/build/internal/ApiDecorator/tests/ApiDecorator-specs.js +0 -106
  73. package/build/internal/Registry/Registry.js +0 -76
  74. package/build/internal/Registry/package.json +0 -3
  75. package/build/internal/Registry/tests/Registry-specs.js +0 -201
  76. package/build/internal/deprecate/deprecate.js +0 -116
  77. package/build/internal/deprecate/package.json +0 -3
  78. package/build/internal/prop-types/package.json +0 -3
  79. package/build/internal/prop-types/prop-types.js +0 -90
  80. package/build/keymap/index.js +0 -54
  81. package/build/keymap/keymap.js +0 -201
  82. package/build/keymap/package.json +0 -3
  83. package/build/keymap/tests/keymap-specs.js +0 -75
  84. package/build/kind/computed.js +0 -60
  85. package/build/kind/kind.js +0 -266
  86. package/build/kind/package.json +0 -3
  87. package/build/kind/styles.js +0 -105
  88. package/build/kind/tests/computed-specs.js +0 -87
  89. package/build/kind/tests/kind-specs.js +0 -216
  90. package/build/kind/tests/styles-specs.js +0 -170
  91. package/build/kind/util.js +0 -17
  92. package/build/package-lock.json +0 -206
  93. package/build/package.json +0 -38
  94. package/build/platform/package.json +0 -3
  95. package/build/platform/platform.js +0 -306
  96. package/build/platform/tests/platform-specs.js +0 -92
  97. package/build/snapshot/package.json +0 -3
  98. package/build/snapshot/snapshot.js +0 -89
  99. package/build/useChainRefs/package.json +0 -3
  100. package/build/useChainRefs/tests/useChainRefs-specs.js +0 -57
  101. package/build/useChainRefs/useChainRefs.js +0 -78
  102. package/build/useClass/package.json +0 -3
  103. package/build/useClass/tests/useClass-specs.js +0 -51
  104. package/build/useClass/useClass.js +0 -59
  105. package/build/useHandlers/Handlers.js +0 -55
  106. package/build/useHandlers/package.json +0 -3
  107. package/build/useHandlers/tests/useHandlers-specs.js +0 -84
  108. package/build/useHandlers/useHandlers.js +0 -36
  109. package/build/util/Job.js +0 -211
  110. package/build/util/package.json +0 -3
  111. package/build/util/tests/Job-specs.js +0 -216
  112. package/build/util/tests/util-specs.js +0 -123
  113. package/build/util/util.js +0 -322
@@ -1,601 +1,476 @@
1
- import {
2
- handle,
3
- adaptEvent,
4
- call,
5
- callOnEvent,
6
- forEventProp,
7
- forKeyCode,
8
- forProp,
9
- forward,
10
- forwardCustom,
11
- forwardWithPrevent,
12
- oneOf,
13
- preventDefault,
14
- stop
15
- } from '../handle';
16
-
17
- describe('handle', () => {
18
-
19
- const makeEvent = (payload) => ({
20
- preventDefault: jest.fn(),
21
- stopPropagation: jest.fn(),
22
- ...payload
23
- });
24
-
25
- const returnsTrue = () => true;
26
- const returnsFalse = () => false;
27
-
28
- test('should call only handler', () => {
29
- const handler = jest.fn(returnsTrue);
30
- const callback = handle(handler);
31
-
32
- callback(makeEvent());
33
-
34
- const expected = 1;
35
- const actual = handler.mock.calls.length;
36
-
37
- expect(actual).toBe(expected);
38
- });
39
-
40
- test('should call multiple handlers', () => {
41
- const handler1 = jest.fn(returnsTrue);
42
- const handler2 = jest.fn(returnsTrue);
43
-
44
- const callback = handle(handler1, handler2);
45
-
46
- callback(makeEvent());
47
-
48
- const expected = true;
49
- const actual = handler1.mock.calls.length === 1 &&
50
- handler2.mock.calls.length === 1;
51
-
52
- expect(actual).toBe(expected);
53
- });
54
-
55
- test('should skip non-function handlers', () => {
56
- const handler = jest.fn(returnsTrue);
57
- const callback = handle(null, void 0, 0, 'purple', handler);
58
-
59
- callback(makeEvent());
60
-
61
- const expected = 1;
62
- const actual = handler.mock.calls.length;
63
-
64
- expect(actual).toBe(expected);
65
- });
66
-
67
- test('should not call handlers after one that returns false', () => {
68
- const handler1 = jest.fn(returnsTrue);
69
- const handler2 = jest.fn(returnsTrue);
70
-
71
- const callback = handle(handler1, returnsFalse, handler2);
72
-
73
- callback(makeEvent());
74
-
75
- const expected = 0;
76
- const actual = handler2.mock.calls.length;
77
-
78
- expect(actual).toBe(expected);
79
- });
80
-
81
- test('should call stopPropagation on event', () => {
82
- const callback = handle(stop);
83
- const ev = makeEvent();
84
- callback(ev);
85
-
86
- const expected = 1;
87
- const actual = ev.stopPropagation.mock.calls.length;
88
-
89
- expect(actual).toBe(expected);
90
- });
91
-
92
- test('should call preventDefault on event', () => {
93
- const callback = handle(preventDefault);
94
- const ev = makeEvent();
95
- callback(ev);
96
-
97
- const expected = 1;
98
- const actual = ev.preventDefault.mock.calls.length;
99
-
100
- expect(actual).toBe(expected);
101
- });
102
-
103
- test('should call any method on event', () => {
104
- const callback = handle(callOnEvent('customMethod'));
105
- const ev = makeEvent({
106
- customMethod: jest.fn()
107
- });
108
- callback(ev);
109
-
110
- const expected = 1;
111
- const actual = ev.customMethod.mock.calls.length;
112
-
113
- expect(actual).toBe(expected);
114
- });
115
-
116
- test('should only call handler for specified keyCode', () => {
117
- const keyCode = 13;
118
- const handler = jest.fn();
119
- const callback = handle(forKeyCode(keyCode), handler);
120
-
121
- callback(makeEvent());
122
- expect(handler).not.toHaveBeenCalled();
123
-
124
- callback(makeEvent({keyCode}));
125
- expect(handler).toHaveBeenCalled();
126
- });
127
-
128
- test('should only call handler for specified event prop', () => {
129
- const prop = 'index';
130
- const value = 0;
131
- const handler = jest.fn();
132
- const callback = handle(forEventProp(prop, value), handler);
133
-
134
- // undefined shouldn't pass
135
- callback(makeEvent());
136
- expect(handler).not.toHaveBeenCalled();
137
-
138
- // == check shouldn't pass
139
- callback(makeEvent({
140
- [prop]: false
141
- }));
142
- expect(handler).not.toHaveBeenCalled();
143
-
144
- // === should pass
145
- callback(makeEvent({
146
- [prop]: value
147
- }));
148
- expect(handler).toHaveBeenCalled();
149
- });
150
-
151
- test('should only call handler for specified prop', () => {
152
- const handler = jest.fn();
153
- const callback = handle(forProp('checked', true), handler);
154
-
155
- // undefined shouldn't pass
156
- callback({}, {});
157
- expect(handler).not.toHaveBeenCalled();
158
-
159
- // == check shouldn't pass
160
- callback({}, {checked: 1});
161
- expect(handler).not.toHaveBeenCalled();
162
-
163
- // === should pass
164
- callback({}, {checked: true});
165
- expect(handler).toHaveBeenCalled();
166
- });
167
-
168
- test(
169
- 'should forward events to function specified in provided props',
170
- () => {
171
- const event = 'onMyClick';
172
- const prop = 'index';
173
- const propValue = 0;
174
- const spy = jest.fn();
175
-
176
- const props = {
177
- [event]: spy
178
- };
179
- const payload = {
180
- [prop]: propValue
181
- };
182
-
183
- handle(forward(event))(payload, props);
184
-
185
- const expected = true;
186
- const actual = spy.mock.calls[0][0][prop] === propValue;
187
-
188
- expect(actual).toBe(expected);
189
- }
190
- );
191
-
192
- test(
193
- 'should forwardWithPrevent events to function specified in provided props when preventDefault() hasn\'t been called',
194
- () => {
195
- const event = 'onMyClick';
196
- const handler = jest.fn();
197
-
198
- const callback = handle(forwardWithPrevent(event), handler);
199
-
200
- callback();
201
- expect(handler).toHaveBeenCalledTimes(1);
202
- }
203
- );
204
-
205
- test(
206
- 'should not forwardWithPrevent events to function specified in provided props when preventDefault() has been called',
207
- () => {
208
- const event = 'onMyClick';
209
- const handler = jest.fn();
210
-
211
- const callback = handle(forwardWithPrevent(event), handler);
212
-
213
- // should stop chain when `preventDefault()` has been called
214
- callback({}, {
215
- 'onMyClick': (ev) => ev.preventDefault()
216
- });
217
- expect(handler).not.toHaveBeenCalled();
218
- }
219
- );
220
-
221
- test('should include object props as second arg when bound', () => {
222
- const componentInstance = {
223
- context: {},
224
- props: {
225
- value: 1
226
- }
227
- };
228
- const handler = jest.fn();
229
- const h = handle.bind(componentInstance);
230
- const callback = h(handler);
231
- callback();
232
-
233
- const expected = 1;
234
- const actual = handler.mock.calls[0][1].value;
235
-
236
- expect(actual).toBe(expected);
237
- });
238
-
239
- test('should include object context as third arg when bound', () => {
240
- const componentInstance = {
241
- context: {
242
- value: 1
243
- },
244
- props: {}
245
- };
246
- const handler = jest.fn();
247
- const h = handle.bind(componentInstance);
248
- const callback = h(handler);
249
- callback();
250
-
251
- const expected = 1;
252
- const actual = handler.mock.calls[0][2].value;
253
-
254
- expect(actual).toBe(expected);
255
- });
256
-
257
- describe('finally', () => {
258
- test('should call the finally callback when handle returns true', () => {
259
- const finallyCallback = jest.fn();
260
- const callback = handle(returnsTrue).finally(finallyCallback);
261
-
262
- callback(makeEvent());
263
-
264
- const expected = 1;
265
- const actual = finallyCallback.mock.calls.length;
266
-
267
- expect(actual).toBe(expected);
268
- });
269
-
270
- test(
271
- 'should call the finally callback when handle returns false',
272
- () => {
273
- const finallyCallback = jest.fn();
274
- const callback = handle(returnsFalse).finally(finallyCallback);
275
-
276
- callback(makeEvent());
277
-
278
- const expected = 1;
279
- const actual = finallyCallback.mock.calls.length;
280
-
281
- expect(actual).toBe(expected);
282
- }
283
- );
284
-
285
- test(
286
- 'should call the finally callback when handle throws an error',
287
- () => {
288
- const finallyCallback = jest.fn();
289
- const callback = handle(() => {
290
- throw new Error('Something has gone awry ...');
291
- }).finally(finallyCallback);
292
-
293
- try {
294
- callback(makeEvent());
295
- } catch (e) {
296
- // we don't want the error to interrupt the test
297
- }
298
-
299
- const expected = 1;
300
- const actual = finallyCallback.mock.calls.length;
301
-
302
- expect(actual).toBe(expected);
303
- }
304
- );
305
- });
306
-
307
- describe('#oneOf', () => {
308
- test('should call each handler until one passes', () => {
309
- const handler = jest.fn(returnsTrue);
310
- const h1 = [
311
- returnsFalse,
312
- handler
313
- ];
314
- const h2 = [
315
- returnsTrue,
316
- handler
317
- ];
318
- const callback = oneOf(h1, h1, h2);
319
- callback();
320
-
321
- const expected = 1;
322
- const actual = handler.mock.calls.length;
323
-
324
- expect(actual).toBe(expected);
325
- });
326
-
327
- test('should stop if the first handler passes', () => {
328
- const handler = jest.fn(returnsTrue);
329
- const callback = oneOf(
330
- [returnsTrue, handler],
331
- [returnsTrue, handler],
332
- [returnsTrue, handler]
333
- );
334
- callback();
335
-
336
- const expected = 1;
337
- const actual = handler.mock.calls.length;
338
-
339
- expect(actual).toBe(expected);
340
- });
341
-
342
- test('should pass args to condition', () => {
343
- const handler = jest.fn(returnsTrue);
344
- const callback = oneOf(
345
- [handler, returnsTrue]
346
- );
347
- const ev = {value: 1};
348
- callback(ev);
349
-
350
- const expected = ev;
351
- const actual = handler.mock.calls[0][0];
352
-
353
- expect(actual).toBe(expected);
354
- });
355
-
356
- test('should pass args to handlers', () => {
357
- const handler = jest.fn(returnsTrue);
358
- const callback = oneOf(
359
- [returnsTrue, handler]
360
- );
361
- const ev = {value: 1};
362
- callback(ev);
363
-
364
- const expected = ev;
365
- const actual = handler.mock.calls[0][0];
366
-
367
- expect(actual).toBe(expected);
368
- });
369
-
370
- test(
371
- 'should return true when the passed condition branch returns a truthy value',
372
- () => {
373
- const callback = oneOf(
374
- [returnsTrue, () => 'ok']
375
- );
376
-
377
- const expected = true;
378
- const actual = callback();
379
-
380
- expect(actual).toBe(expected);
381
- }
382
- );
383
-
384
- test(
385
- 'should return false when the passed condition branch returns a falsy value',
386
- () => {
387
- const callback = oneOf(
388
- [returnsTrue, () => null]
389
- );
390
-
391
- const expected = false;
392
- const actual = callback();
393
-
394
- expect(actual).toBe(expected);
395
- }
396
- );
397
-
398
- test('should return false when no conditions pass', () => {
399
- const callback = oneOf(
400
- [returnsFalse, returnsTrue],
401
- [returnsFalse, returnsTrue]
402
- );
403
-
404
- const expected = false;
405
- const actual = callback();
406
-
407
- expect(actual).toBe(expected);
408
- });
409
-
410
- test('should support bound handlers', () => {
411
- const componentInstance = {
412
- props: {},
413
- context: {
414
- value: 1
415
- }
416
- };
417
- const handler = jest.fn();
418
- const h = handle.bind(componentInstance);
419
- const callback = oneOf(
420
- [returnsTrue, h(handler)]
421
- );
422
- callback();
423
-
424
- const expected = 1;
425
- const actual = handler.mock.calls[0][2].value;
426
-
427
- expect(actual).toBe(expected);
428
- });
429
-
430
- test('should include object props as second arg when bound', () => {
431
- const componentInstance = {
432
- props: {
433
- value: 1
434
- },
435
- context: {}
436
- };
437
- const handler = jest.fn();
438
- const o = oneOf.bind(componentInstance);
439
- const callback = o(
440
- [returnsTrue, handler]
441
- );
442
- callback();
443
-
444
- const expected = 1;
445
- const actual = handler.mock.calls[0][1].value;
446
-
447
- expect(actual).toBe(expected);
448
- });
449
-
450
- test('should include object context as third arg when bound', () => {
451
- const componentInstance = {
452
- props: {},
453
- context: {
454
- value: 1
455
- }
456
- };
457
- const handler = jest.fn();
458
- const o = oneOf.bind(componentInstance);
459
- const callback = o(
460
- [returnsTrue, handler]
461
- );
462
- callback();
463
-
464
- const expected = 1;
465
- const actual = handler.mock.calls[0][2].value;
466
-
467
- expect(actual).toBe(expected);
468
- });
469
-
470
- test('should support finally callback', () => {
471
- const handler = jest.fn();
472
- const callback = oneOf(
473
- [returnsFalse, returnsTrue],
474
- [returnsFalse, returnsTrue]
475
- ).finally(handler);
476
-
477
- callback();
478
-
479
- const expected = 1;
480
- const actual = handler.mock.calls.length;
481
-
482
- expect(actual).toBe(expected);
483
- });
484
- });
485
-
486
- describe('#adaptEvent', () => {
487
- test('should pass the adapted event payload to the provided handler', () => {
488
- const handler = jest.fn();
489
- const onlyValue = ({value}) => ({value});
490
- const ev = {
491
- value: 1,
492
- message: 'ok'
493
- };
494
-
495
- adaptEvent(onlyValue, handler)(ev);
496
-
497
- const expected = {value: 1};
498
- const actual = handler.mock.calls[0][0];
499
-
500
- expect(actual).toEqual(expected);
501
- });
502
-
503
- test('should pass additional arguments to the provided handler', () => {
504
- const handler = jest.fn();
505
- const returnOne = () => 1;
506
- adaptEvent(returnOne, handler)(0, 2, 3);
507
-
508
- const expected = [1, 2, 3];
509
- const actual = handler.mock.calls[0];
510
-
511
- expect(actual).toEqual(expected);
512
- });
513
-
514
- test('should support bound adapter function', () => {
515
- const obj = {
516
- adapt: () => 1
517
- };
518
- const handler = jest.fn();
519
- const fn = adaptEvent(call('adapt'), handler).bind(obj);
520
-
521
- fn(0, 2, 3);
522
-
523
- const expected = [1, 2, 3];
524
- const actual = handler.mock.calls[0];
525
-
526
- expect(actual).toEqual(expected);
527
- });
528
-
529
- test('should support bound handler function', () => {
530
- const obj = {
531
- handler: jest.fn()
532
- };
533
- const returnOne = () => 1;
534
- const fn = adaptEvent(returnOne, call('handler')).bind(obj);
535
-
536
- fn(0, 2, 3);
537
-
538
- const expected = [1, 2, 3];
539
- const actual = obj.handler.mock.calls[0];
540
-
541
- expect(actual).toEqual(expected);
542
- });
543
- });
544
-
545
- describe('#forwardCustom', () => {
546
- test('should pass an object with `type` when no adapter is provided', () => {
547
- const handler = jest.fn();
548
-
549
- forwardCustom('onCustomEvent')(null, {onCustomEvent: handler});
550
-
551
- const expected = {
552
- type: 'onCustomEvent'
553
- };
554
- const actual = handler.mock.calls[0][0];
555
-
556
- expect(actual).toEqual(expected);
557
- });
558
-
559
- test('should add `type` to object returned by adapter', () => {
560
- const handler = jest.fn();
561
- const adapter = () => ({index: 0});
562
- forwardCustom('onCustomEvent', adapter)(null, {onCustomEvent: handler});
563
-
564
- const expected = {
565
- type: 'onCustomEvent',
566
- index: 0
567
- };
568
- const actual = handler.mock.calls[0][0];
569
-
570
- expect(actual).toEqual(expected);
571
- });
572
-
573
- test('should create an event payload if the adapter returns nothing', () => {
574
- const handler = jest.fn();
575
- const adapter = () => null;
576
- forwardCustom('onCustomEvent', adapter)(null, {onCustomEvent: handler});
577
-
578
- const expected = {
579
- type: 'onCustomEvent'
580
- };
581
- const actual = handler.mock.calls[0][0];
582
-
583
- expect(actual).toEqual(expected);
584
- });
585
-
586
- test('should pass event, props, and context args to adapter', () => {
587
- const adapter = jest.fn();
588
- const args = [
589
- 1, // ev,
590
- 2, // props,
591
- 3 // context
592
- ];
593
- forwardCustom('onCustomEvent', adapter)(...args);
594
-
595
- const expected = args;
596
- const actual = adapter.mock.calls[0];
597
-
598
- expect(actual).toEqual(expected);
599
- });
600
- });
601
- });
1
+ "use strict";
2
+
3
+ var _handle = require("../handle");
4
+
5
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
6
+
7
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
8
+
9
+ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
10
+
11
+ describe('handle', function () {
12
+ var makeEvent = function makeEvent(payload) {
13
+ return _objectSpread({
14
+ preventDefault: jest.fn(),
15
+ stopPropagation: jest.fn()
16
+ }, payload);
17
+ };
18
+
19
+ var returnsTrue = function returnsTrue() {
20
+ return true;
21
+ };
22
+
23
+ var returnsFalse = function returnsFalse() {
24
+ return false;
25
+ };
26
+
27
+ test('should call only handler', function () {
28
+ var handler = jest.fn(returnsTrue);
29
+ var callback = (0, _handle.handle)(handler);
30
+ callback(makeEvent());
31
+ var expected = 1;
32
+ var actual = handler.mock.calls.length;
33
+ expect(actual).toBe(expected);
34
+ });
35
+ test('should call multiple handlers', function () {
36
+ var handler1 = jest.fn(returnsTrue);
37
+ var handler2 = jest.fn(returnsTrue);
38
+ var callback = (0, _handle.handle)(handler1, handler2);
39
+ callback(makeEvent());
40
+ var expected = true;
41
+ var actual = handler1.mock.calls.length === 1 && handler2.mock.calls.length === 1;
42
+ expect(actual).toBe(expected);
43
+ });
44
+ test('should skip non-function handlers', function () {
45
+ var handler = jest.fn(returnsTrue);
46
+ var callback = (0, _handle.handle)(null, void 0, 0, 'purple', handler);
47
+ callback(makeEvent());
48
+ var expected = 1;
49
+ var actual = handler.mock.calls.length;
50
+ expect(actual).toBe(expected);
51
+ });
52
+ test('should not call handlers after one that returns false', function () {
53
+ var handler1 = jest.fn(returnsTrue);
54
+ var handler2 = jest.fn(returnsTrue);
55
+ var callback = (0, _handle.handle)(handler1, returnsFalse, handler2);
56
+ callback(makeEvent());
57
+ var expected = 0;
58
+ var actual = handler2.mock.calls.length;
59
+ expect(actual).toBe(expected);
60
+ });
61
+ test('should call stopPropagation on event', function () {
62
+ var callback = (0, _handle.handle)(_handle.stop);
63
+ var ev = makeEvent();
64
+ callback(ev);
65
+ var expected = 1;
66
+ var actual = ev.stopPropagation.mock.calls.length;
67
+ expect(actual).toBe(expected);
68
+ });
69
+ test('should call preventDefault on event', function () {
70
+ var callback = (0, _handle.handle)(_handle.preventDefault);
71
+ var ev = makeEvent();
72
+ callback(ev);
73
+ var expected = 1;
74
+ var actual = ev.preventDefault.mock.calls.length;
75
+ expect(actual).toBe(expected);
76
+ });
77
+ test('should call any method on event', function () {
78
+ var callback = (0, _handle.handle)((0, _handle.callOnEvent)('customMethod'));
79
+ var ev = makeEvent({
80
+ customMethod: jest.fn()
81
+ });
82
+ callback(ev);
83
+ var expected = 1;
84
+ var actual = ev.customMethod.mock.calls.length;
85
+ expect(actual).toBe(expected);
86
+ });
87
+ test('should only call handler for specified keyCode', function () {
88
+ var keyCode = 13;
89
+ var handler = jest.fn();
90
+ var callback = (0, _handle.handle)((0, _handle.forKeyCode)(keyCode), handler);
91
+ callback(makeEvent());
92
+ expect(handler).not.toHaveBeenCalled();
93
+ callback(makeEvent({
94
+ keyCode: keyCode
95
+ }));
96
+ expect(handler).toHaveBeenCalled();
97
+ });
98
+ test('should only call handler for specified event prop', function () {
99
+ var prop = 'index';
100
+ var value = 0;
101
+ var handler = jest.fn();
102
+ var callback = (0, _handle.handle)((0, _handle.forEventProp)(prop, value), handler); // undefined shouldn't pass
103
+
104
+ callback(makeEvent());
105
+ expect(handler).not.toHaveBeenCalled(); // == check shouldn't pass
106
+
107
+ callback(makeEvent(_defineProperty({}, prop, false)));
108
+ expect(handler).not.toHaveBeenCalled(); // === should pass
109
+
110
+ callback(makeEvent(_defineProperty({}, prop, value)));
111
+ expect(handler).toHaveBeenCalled();
112
+ });
113
+ test('should only call handler for specified prop', function () {
114
+ var handler = jest.fn();
115
+ var callback = (0, _handle.handle)((0, _handle.forProp)('checked', true), handler); // undefined shouldn't pass
116
+
117
+ callback({}, {});
118
+ expect(handler).not.toHaveBeenCalled(); // == check shouldn't pass
119
+
120
+ callback({}, {
121
+ checked: 1
122
+ });
123
+ expect(handler).not.toHaveBeenCalled(); // === should pass
124
+
125
+ callback({}, {
126
+ checked: true
127
+ });
128
+ expect(handler).toHaveBeenCalled();
129
+ });
130
+ test('should forward events to function specified in provided props', function () {
131
+ var event = 'onMyClick';
132
+ var prop = 'index';
133
+ var propValue = 0;
134
+ var spy = jest.fn();
135
+
136
+ var props = _defineProperty({}, event, spy);
137
+
138
+ var payload = _defineProperty({}, prop, propValue);
139
+
140
+ (0, _handle.handle)((0, _handle.forward)(event))(payload, props);
141
+ var expected = true;
142
+ var actual = spy.mock.calls[0][0][prop] === propValue;
143
+ expect(actual).toBe(expected);
144
+ });
145
+ test('should forwardWithPrevent events to function specified in provided props when preventDefault() hasn\'t been called', function () {
146
+ var event = 'onMyClick';
147
+ var handler = jest.fn();
148
+ var callback = (0, _handle.handle)((0, _handle.forwardWithPrevent)(event), handler);
149
+ callback();
150
+ expect(handler).toHaveBeenCalledTimes(1);
151
+ });
152
+ test('should not forwardWithPrevent events to function specified in provided props when preventDefault() has been called', function () {
153
+ var event = 'onMyClick';
154
+ var handler = jest.fn();
155
+ var callback = (0, _handle.handle)((0, _handle.forwardWithPrevent)(event), handler); // should stop chain when `preventDefault()` has been called
156
+
157
+ callback({}, {
158
+ 'onMyClick': function onMyClick(ev) {
159
+ return ev.preventDefault();
160
+ }
161
+ });
162
+ expect(handler).not.toHaveBeenCalled();
163
+ });
164
+ test('should include object props as second arg when bound', function () {
165
+ var componentInstance = {
166
+ context: {},
167
+ props: {
168
+ value: 1
169
+ }
170
+ };
171
+ var handler = jest.fn();
172
+
173
+ var h = _handle.handle.bind(componentInstance);
174
+
175
+ var callback = h(handler);
176
+ callback();
177
+ var expected = 1;
178
+ var actual = handler.mock.calls[0][1].value;
179
+ expect(actual).toBe(expected);
180
+ });
181
+ test('should include object context as third arg when bound', function () {
182
+ var componentInstance = {
183
+ context: {
184
+ value: 1
185
+ },
186
+ props: {}
187
+ };
188
+ var handler = jest.fn();
189
+
190
+ var h = _handle.handle.bind(componentInstance);
191
+
192
+ var callback = h(handler);
193
+ callback();
194
+ var expected = 1;
195
+ var actual = handler.mock.calls[0][2].value;
196
+ expect(actual).toBe(expected);
197
+ });
198
+ describe('finally', function () {
199
+ test('should call the finally callback when handle returns true', function () {
200
+ var finallyCallback = jest.fn();
201
+ var callback = (0, _handle.handle)(returnsTrue)["finally"](finallyCallback);
202
+ callback(makeEvent());
203
+ var expected = 1;
204
+ var actual = finallyCallback.mock.calls.length;
205
+ expect(actual).toBe(expected);
206
+ });
207
+ test('should call the finally callback when handle returns false', function () {
208
+ var finallyCallback = jest.fn();
209
+ var callback = (0, _handle.handle)(returnsFalse)["finally"](finallyCallback);
210
+ callback(makeEvent());
211
+ var expected = 1;
212
+ var actual = finallyCallback.mock.calls.length;
213
+ expect(actual).toBe(expected);
214
+ });
215
+ test('should call the finally callback when handle throws an error', function () {
216
+ var finallyCallback = jest.fn();
217
+ var callback = (0, _handle.handle)(function () {
218
+ throw new Error('Something has gone awry ...');
219
+ })["finally"](finallyCallback);
220
+
221
+ try {
222
+ callback(makeEvent());
223
+ } catch (e) {// we don't want the error to interrupt the test
224
+ }
225
+
226
+ var expected = 1;
227
+ var actual = finallyCallback.mock.calls.length;
228
+ expect(actual).toBe(expected);
229
+ });
230
+ });
231
+ describe('#oneOf', function () {
232
+ test('should call each handler until one passes', function () {
233
+ var handler = jest.fn(returnsTrue);
234
+ var h1 = [returnsFalse, handler];
235
+ var h2 = [returnsTrue, handler];
236
+ var callback = (0, _handle.oneOf)(h1, h1, h2);
237
+ callback();
238
+ var expected = 1;
239
+ var actual = handler.mock.calls.length;
240
+ expect(actual).toBe(expected);
241
+ });
242
+ test('should stop if the first handler passes', function () {
243
+ var handler = jest.fn(returnsTrue);
244
+ var callback = (0, _handle.oneOf)([returnsTrue, handler], [returnsTrue, handler], [returnsTrue, handler]);
245
+ callback();
246
+ var expected = 1;
247
+ var actual = handler.mock.calls.length;
248
+ expect(actual).toBe(expected);
249
+ });
250
+ test('should pass args to condition', function () {
251
+ var handler = jest.fn(returnsTrue);
252
+ var callback = (0, _handle.oneOf)([handler, returnsTrue]);
253
+ var ev = {
254
+ value: 1
255
+ };
256
+ callback(ev);
257
+ var expected = ev;
258
+ var actual = handler.mock.calls[0][0];
259
+ expect(actual).toBe(expected);
260
+ });
261
+ test('should pass args to handlers', function () {
262
+ var handler = jest.fn(returnsTrue);
263
+ var callback = (0, _handle.oneOf)([returnsTrue, handler]);
264
+ var ev = {
265
+ value: 1
266
+ };
267
+ callback(ev);
268
+ var expected = ev;
269
+ var actual = handler.mock.calls[0][0];
270
+ expect(actual).toBe(expected);
271
+ });
272
+ test('should return true when the passed condition branch returns a truthy value', function () {
273
+ var callback = (0, _handle.oneOf)([returnsTrue, function () {
274
+ return 'ok';
275
+ }]);
276
+ var expected = true;
277
+ var actual = callback();
278
+ expect(actual).toBe(expected);
279
+ });
280
+ test('should return false when the passed condition branch returns a falsy value', function () {
281
+ var callback = (0, _handle.oneOf)([returnsTrue, function () {
282
+ return null;
283
+ }]);
284
+ var expected = false;
285
+ var actual = callback();
286
+ expect(actual).toBe(expected);
287
+ });
288
+ test('should return false when no conditions pass', function () {
289
+ var callback = (0, _handle.oneOf)([returnsFalse, returnsTrue], [returnsFalse, returnsTrue]);
290
+ var expected = false;
291
+ var actual = callback();
292
+ expect(actual).toBe(expected);
293
+ });
294
+ test('should support bound handlers', function () {
295
+ var componentInstance = {
296
+ props: {},
297
+ context: {
298
+ value: 1
299
+ }
300
+ };
301
+ var handler = jest.fn();
302
+
303
+ var h = _handle.handle.bind(componentInstance);
304
+
305
+ var callback = (0, _handle.oneOf)([returnsTrue, h(handler)]);
306
+ callback();
307
+ var expected = 1;
308
+ var actual = handler.mock.calls[0][2].value;
309
+ expect(actual).toBe(expected);
310
+ });
311
+ test('should include object props as second arg when bound', function () {
312
+ var componentInstance = {
313
+ props: {
314
+ value: 1
315
+ },
316
+ context: {}
317
+ };
318
+ var handler = jest.fn();
319
+
320
+ var o = _handle.oneOf.bind(componentInstance);
321
+
322
+ var callback = o([returnsTrue, handler]);
323
+ callback();
324
+ var expected = 1;
325
+ var actual = handler.mock.calls[0][1].value;
326
+ expect(actual).toBe(expected);
327
+ });
328
+ test('should include object context as third arg when bound', function () {
329
+ var componentInstance = {
330
+ props: {},
331
+ context: {
332
+ value: 1
333
+ }
334
+ };
335
+ var handler = jest.fn();
336
+
337
+ var o = _handle.oneOf.bind(componentInstance);
338
+
339
+ var callback = o([returnsTrue, handler]);
340
+ callback();
341
+ var expected = 1;
342
+ var actual = handler.mock.calls[0][2].value;
343
+ expect(actual).toBe(expected);
344
+ });
345
+ test('should support finally callback', function () {
346
+ var handler = jest.fn();
347
+ var callback = (0, _handle.oneOf)([returnsFalse, returnsTrue], [returnsFalse, returnsTrue])["finally"](handler);
348
+ callback();
349
+ var expected = 1;
350
+ var actual = handler.mock.calls.length;
351
+ expect(actual).toBe(expected);
352
+ });
353
+ });
354
+ describe('#adaptEvent', function () {
355
+ test('should pass the adapted event payload to the provided handler', function () {
356
+ var handler = jest.fn();
357
+
358
+ var onlyValue = function onlyValue(_ref) {
359
+ var value = _ref.value;
360
+ return {
361
+ value: value
362
+ };
363
+ };
364
+
365
+ var ev = {
366
+ value: 1,
367
+ message: 'ok'
368
+ };
369
+ (0, _handle.adaptEvent)(onlyValue, handler)(ev);
370
+ var expected = {
371
+ value: 1
372
+ };
373
+ var actual = handler.mock.calls[0][0];
374
+ expect(actual).toEqual(expected);
375
+ });
376
+ test('should pass additional arguments to the provided handler', function () {
377
+ var handler = jest.fn();
378
+
379
+ var returnOne = function returnOne() {
380
+ return 1;
381
+ };
382
+
383
+ (0, _handle.adaptEvent)(returnOne, handler)(0, 2, 3);
384
+ var expected = [1, 2, 3];
385
+ var actual = handler.mock.calls[0];
386
+ expect(actual).toEqual(expected);
387
+ });
388
+ test('should support bound adapter function', function () {
389
+ var obj = {
390
+ adapt: function adapt() {
391
+ return 1;
392
+ }
393
+ };
394
+ var handler = jest.fn();
395
+ var fn = (0, _handle.adaptEvent)((0, _handle.call)('adapt'), handler).bind(obj);
396
+ fn(0, 2, 3);
397
+ var expected = [1, 2, 3];
398
+ var actual = handler.mock.calls[0];
399
+ expect(actual).toEqual(expected);
400
+ });
401
+ test('should support bound handler function', function () {
402
+ var obj = {
403
+ handler: jest.fn()
404
+ };
405
+
406
+ var returnOne = function returnOne() {
407
+ return 1;
408
+ };
409
+
410
+ var fn = (0, _handle.adaptEvent)(returnOne, (0, _handle.call)('handler')).bind(obj);
411
+ fn(0, 2, 3);
412
+ var expected = [1, 2, 3];
413
+ var actual = obj.handler.mock.calls[0];
414
+ expect(actual).toEqual(expected);
415
+ });
416
+ });
417
+ describe('#forwardCustom', function () {
418
+ test('should pass an object with `type` when no adapter is provided', function () {
419
+ var handler = jest.fn();
420
+ (0, _handle.forwardCustom)('onCustomEvent')(null, {
421
+ onCustomEvent: handler
422
+ });
423
+ var expected = {
424
+ type: 'onCustomEvent'
425
+ };
426
+ var actual = handler.mock.calls[0][0];
427
+ expect(actual).toEqual(expected);
428
+ });
429
+ test('should add `type` to object returned by adapter', function () {
430
+ var handler = jest.fn();
431
+
432
+ var adapter = function adapter() {
433
+ return {
434
+ index: 0
435
+ };
436
+ };
437
+
438
+ (0, _handle.forwardCustom)('onCustomEvent', adapter)(null, {
439
+ onCustomEvent: handler
440
+ });
441
+ var expected = {
442
+ type: 'onCustomEvent',
443
+ index: 0
444
+ };
445
+ var actual = handler.mock.calls[0][0];
446
+ expect(actual).toEqual(expected);
447
+ });
448
+ test('should create an event payload if the adapter returns nothing', function () {
449
+ var handler = jest.fn();
450
+
451
+ var adapter = function adapter() {
452
+ return null;
453
+ };
454
+
455
+ (0, _handle.forwardCustom)('onCustomEvent', adapter)(null, {
456
+ onCustomEvent: handler
457
+ });
458
+ var expected = {
459
+ type: 'onCustomEvent'
460
+ };
461
+ var actual = handler.mock.calls[0][0];
462
+ expect(actual).toEqual(expected);
463
+ });
464
+ test('should pass event, props, and context args to adapter', function () {
465
+ var adapter = jest.fn();
466
+ var args = [1, // ev,
467
+ 2, // props,
468
+ 3 // context
469
+ ];
470
+ (0, _handle.forwardCustom)('onCustomEvent', adapter).apply(void 0, args);
471
+ var expected = args;
472
+ var actual = adapter.mock.calls[0];
473
+ expect(actual).toEqual(expected);
474
+ });
475
+ });
476
+ });