vest-utils 1.4.0 → 1.5.0

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 (157) hide show
  1. package/README.md +2 -7
  2. package/dist/cjs/minifyObject.development.js +217 -0
  3. package/dist/cjs/minifyObject.development.js.map +1 -0
  4. package/dist/cjs/minifyObject.js +6 -0
  5. package/dist/cjs/minifyObject.production.js +2 -0
  6. package/dist/cjs/minifyObject.production.js.map +1 -0
  7. package/dist/cjs/package.json +1 -0
  8. package/dist/cjs/vest-utils.development.js +378 -0
  9. package/dist/cjs/vest-utils.development.js.map +1 -0
  10. package/dist/cjs/vest-utils.js +6 -0
  11. package/dist/cjs/vest-utils.production.js +2 -0
  12. package/dist/cjs/vest-utils.production.js.map +1 -0
  13. package/dist/es/minifyObject.development.js +214 -0
  14. package/dist/es/minifyObject.development.js.map +1 -0
  15. package/dist/es/minifyObject.production.js +2 -0
  16. package/dist/es/minifyObject.production.js.map +1 -0
  17. package/dist/es/package.json +1 -0
  18. package/dist/es/vest-utils.development.js +330 -0
  19. package/dist/es/vest-utils.development.js.map +1 -0
  20. package/dist/es/vest-utils.production.js +2 -0
  21. package/dist/es/vest-utils.production.js.map +1 -0
  22. package/dist/umd/minifyObject.development.js +223 -0
  23. package/dist/umd/minifyObject.development.js.map +1 -0
  24. package/dist/umd/minifyObject.production.js +2 -0
  25. package/dist/umd/minifyObject.production.js.map +1 -0
  26. package/dist/umd/vest-utils.development.js +384 -0
  27. package/dist/umd/vest-utils.development.js.map +1 -0
  28. package/dist/umd/vest-utils.production.js +2 -0
  29. package/dist/umd/vest-utils.production.js.map +1 -0
  30. package/minifyObject/package.json +8 -12
  31. package/package.json +58 -43
  32. package/types/{exports/minifyObject.d.cts → minifyObject.d.ts} +2 -4
  33. package/types/minifyObject.d.ts.map +1 -0
  34. package/types/vest-utils.d.ts +143 -245
  35. package/types/vest-utils.d.ts.map +1 -0
  36. package/vitest.config.ts +45 -9
  37. package/dist/chunk-CLMFDpHK.mjs +0 -18
  38. package/dist/exports/minifyObject.cjs +0 -114
  39. package/dist/exports/minifyObject.cjs.map +0 -1
  40. package/dist/exports/minifyObject.mjs +0 -113
  41. package/dist/exports/minifyObject.mjs.map +0 -1
  42. package/dist/exports/standardSchemaSpec.cjs +0 -0
  43. package/dist/exports/standardSchemaSpec.mjs +0 -1
  44. package/dist/isEmpty-BBxAFjjm.mjs +0 -103
  45. package/dist/isEmpty-BBxAFjjm.mjs.map +0 -1
  46. package/dist/isEmpty-BuEa-96Q.cjs +0 -235
  47. package/dist/isEmpty-BuEa-96Q.cjs.map +0 -1
  48. package/dist/vest-utils.cjs +0 -510
  49. package/dist/vest-utils.cjs.map +0 -1
  50. package/dist/vest-utils.mjs +0 -421
  51. package/dist/vest-utils.mjs.map +0 -1
  52. package/src/Brand.ts +0 -9
  53. package/src/IO.ts +0 -2
  54. package/src/Predicates.ts +0 -13
  55. package/src/Result.ts +0 -121
  56. package/src/SimpleStateMachine.ts +0 -157
  57. package/src/StringObject.ts +0 -6
  58. package/src/__tests__/Architecture.test.ts +0 -69
  59. package/src/__tests__/Predicates.test.ts +0 -118
  60. package/src/__tests__/Result.test.ts +0 -284
  61. package/src/__tests__/SimpleStateMachine.test.ts +0 -425
  62. package/src/__tests__/StringObject.test.ts +0 -18
  63. package/src/__tests__/asArray.test.ts +0 -14
  64. package/src/__tests__/bindNot.test.ts +0 -39
  65. package/src/__tests__/bus.test.ts +0 -135
  66. package/src/__tests__/cache.test.ts +0 -139
  67. package/src/__tests__/callEach.test.ts +0 -20
  68. package/src/__tests__/defaultTo.test.ts +0 -52
  69. package/src/__tests__/deferThrow.test.ts +0 -26
  70. package/src/__tests__/either.test.ts +0 -17
  71. package/src/__tests__/freezeAssign.test.ts +0 -24
  72. package/src/__tests__/greaterThan.test.ts +0 -68
  73. package/src/__tests__/invariant.test.ts +0 -47
  74. package/src/__tests__/isArray.test.ts +0 -16
  75. package/src/__tests__/isBoolean.test.ts +0 -16
  76. package/src/__tests__/isEmpty.test.ts +0 -55
  77. package/src/__tests__/isEmptySet.test.ts +0 -22
  78. package/src/__tests__/isNull.test.ts +0 -26
  79. package/src/__tests__/isNumeric.test.ts +0 -27
  80. package/src/__tests__/isPositive.test.ts +0 -38
  81. package/src/__tests__/isPromise.test.ts +0 -17
  82. package/src/__tests__/isString.test.ts +0 -13
  83. package/src/__tests__/isUndefined.test.ts +0 -27
  84. package/src/__tests__/isUnsafeKey.test.ts +0 -22
  85. package/src/__tests__/lengthEquals.test.ts +0 -58
  86. package/src/__tests__/longerThan.test.ts +0 -58
  87. package/src/__tests__/mapFirst.test.ts +0 -31
  88. package/src/__tests__/nonnullish.test.ts +0 -25
  89. package/src/__tests__/noop.test.ts +0 -12
  90. package/src/__tests__/numberEquals.test.ts +0 -67
  91. package/src/__tests__/optionalFunctionValue.test.ts +0 -29
  92. package/src/__tests__/seq.test.ts +0 -29
  93. package/src/__tests__/text.test.ts +0 -41
  94. package/src/__tests__/tinyState.test.ts +0 -68
  95. package/src/__tests__/toNumber.test.ts +0 -39
  96. package/src/__tests__/vest-utils.test.ts +0 -13
  97. package/src/__tests__/withCatch.test.ts +0 -17
  98. package/src/__tests__/withResolvers.test.ts +0 -45
  99. package/src/asArray.ts +0 -3
  100. package/src/assign.ts +0 -1
  101. package/src/bindNot.ts +0 -3
  102. package/src/bus.ts +0 -52
  103. package/src/cache.ts +0 -68
  104. package/src/callEach.ts +0 -5
  105. package/src/defaultTo.ts +0 -9
  106. package/src/deferThrow.ts +0 -7
  107. package/src/dynamicValue.ts +0 -9
  108. package/src/either.ts +0 -3
  109. package/src/exports/__tests__/minifyObject.security.test.ts +0 -65
  110. package/src/exports/__tests__/minifyObject.test.ts +0 -281
  111. package/src/exports/minifyObject.ts +0 -198
  112. package/src/exports/standardSchemaSpec.ts +0 -70
  113. package/src/freezeAssign.ts +0 -5
  114. package/src/globals.d.ts +0 -3
  115. package/src/greaterThan.ts +0 -8
  116. package/src/hasOwnProperty.ts +0 -9
  117. package/src/invariant.ts +0 -19
  118. package/src/isArrayValue.ts +0 -11
  119. package/src/isBooleanValue.ts +0 -3
  120. package/src/isEmpty.ts +0 -18
  121. package/src/isEmptySet.ts +0 -15
  122. package/src/isFunction.ts +0 -5
  123. package/src/isNull.ts +0 -7
  124. package/src/isNullish.ts +0 -10
  125. package/src/isNumeric.ts +0 -11
  126. package/src/isPositive.ts +0 -5
  127. package/src/isPromise.ts +0 -5
  128. package/src/isStringValue.ts +0 -3
  129. package/src/isUndefined.ts +0 -7
  130. package/src/isUnsafeKey.ts +0 -3
  131. package/src/lengthEquals.ts +0 -11
  132. package/src/longerThan.ts +0 -8
  133. package/src/mapFirst.ts +0 -25
  134. package/src/nonnullish.ts +0 -9
  135. package/src/noop.ts +0 -1
  136. package/src/numberEquals.ts +0 -11
  137. package/src/seq.ts +0 -16
  138. package/src/text.ts +0 -20
  139. package/src/tinyState.ts +0 -28
  140. package/src/toNumber.ts +0 -11
  141. package/src/utilityTypes.ts +0 -25
  142. package/src/valueIsObject.ts +0 -5
  143. package/src/vest-utils.ts +0 -73
  144. package/src/withCatch.ts +0 -11
  145. package/src/withResolvers.ts +0 -33
  146. package/standardSchemaSpec/package.json +0 -14
  147. package/types/exports/minifyObject.d.cts.map +0 -1
  148. package/types/exports/minifyObject.d.mts +0 -7
  149. package/types/exports/minifyObject.d.mts.map +0 -1
  150. package/types/exports/standardSchemaSpec.d.cts +0 -59
  151. package/types/exports/standardSchemaSpec.d.cts.map +0 -1
  152. package/types/exports/standardSchemaSpec.d.mts +0 -59
  153. package/types/exports/standardSchemaSpec.d.mts.map +0 -1
  154. package/types/vest-utils.d.cts +0 -296
  155. package/types/vest-utils.d.cts.map +0 -1
  156. package/types/vest-utils.d.mts +0 -295
  157. package/types/vest-utils.d.mts.map +0 -1
@@ -1,425 +0,0 @@
1
- import { describe, test, it, expect } from 'vitest';
2
-
3
- import { unwrap, isFailure, isSuccess } from '../Result';
4
- import { StateMachine } from '../SimpleStateMachine';
5
-
6
- describe('SimpleStateMachine', () => {
7
- test('sample', () => {
8
- const machine = StateMachine({
9
- initial: 'idle',
10
- states: {
11
- error: {},
12
- idle: {
13
- click: 'loading',
14
- },
15
- loading: {
16
- success: 'success',
17
- error: 'error',
18
- },
19
- success: {},
20
- },
21
- });
22
-
23
- expect(machine.getState()).toBe('idle');
24
-
25
- unwrap(machine.transition('click'));
26
-
27
- expect(machine.getState()).toBe('loading');
28
-
29
- unwrap(machine.transition('success'));
30
-
31
- expect(machine.getState()).toBe('success');
32
-
33
- expect(isFailure(machine.transition('click'))).toBe(true);
34
-
35
- expect(machine.getState()).toBe('success');
36
- });
37
-
38
- test('sample with conditional', () => {
39
- const machine = StateMachine({
40
- initial: 'idle',
41
- states: {
42
- error: {},
43
- idle: {
44
- click: ['loading', () => false],
45
- },
46
- loading: {
47
- success: 'success',
48
- error: 'error',
49
- },
50
- success: {},
51
- },
52
- });
53
-
54
- expect(machine.getState()).toBe('idle');
55
-
56
- expect(isFailure(machine.transition('click'))).toBe(true);
57
-
58
- expect(machine.getState()).toBe('idle');
59
-
60
- expect(isFailure(machine.transition('success'))).toBe(true);
61
-
62
- expect(machine.getState()).toBe('idle');
63
-
64
- expect(isFailure(machine.transition('click'))).toBe(true);
65
-
66
- expect(machine.getState()).toBe('idle');
67
- });
68
-
69
- describe('sample with conditional and payload', () => {
70
- const machine = StateMachine({
71
- initial: 'idle',
72
- states: {
73
- error: {},
74
- idle: {
75
- click: ['loading', (payload: number) => payload > 0],
76
- },
77
- loading: {
78
- success: 'success',
79
- error: 'error',
80
- },
81
- success: {},
82
- },
83
- });
84
-
85
- test('should not transition if payload is falsy', () => {
86
- expect(machine.getState()).toBe('idle');
87
-
88
- expect(isFailure(machine.transition('click', 0))).toBe(true);
89
-
90
- expect(machine.getState()).toBe('idle');
91
- });
92
-
93
- test('should transition if payload is truthy', () => {
94
- expect(machine.getState()).toBe('idle');
95
-
96
- unwrap(machine.transition('click', 1));
97
-
98
- expect(machine.getState()).toBe('loading');
99
- });
100
- });
101
-
102
- describe('invalid transitions', () => {
103
- test('should not transition if action is invalid', () => {
104
- const machine = StateMachine({
105
- initial: 'idle',
106
- states: {
107
- error: {},
108
- idle: {
109
- click: 'loading',
110
- },
111
- loading: {
112
- success: 'success',
113
- error: 'error',
114
- },
115
- success: {},
116
- },
117
- });
118
- expect(machine.getState()).toBe('idle');
119
-
120
- unwrap(machine.transition('click'));
121
-
122
- expect(machine.getState()).toBe('loading');
123
-
124
- expect(isFailure(machine.transition('click'))).toBe(true);
125
-
126
- expect(machine.getState()).toBe('loading');
127
- });
128
-
129
- test('should not transition if target state is invalid', () => {
130
- const machine = StateMachine({
131
- initial: 'idle',
132
- states: {
133
- error: {},
134
- idle: {
135
- click: 'loading',
136
- },
137
- loading: {
138
- success: 'success',
139
- error: 'error',
140
- },
141
- success: {},
142
- },
143
- });
144
- expect(machine.getState()).toBe('idle');
145
-
146
- unwrap(machine.transition('click'));
147
-
148
- expect(machine.getState()).toBe('loading');
149
-
150
- // @ts-expect-error - Testing invalid transition
151
- expect(isFailure(machine.transition('finish'))).toBe(true);
152
-
153
- expect(machine.getState()).toBe('loading');
154
- });
155
- });
156
-
157
- describe('Catchall state', () => {
158
- it('When a valid transition does not exist, should search in the search all state', () => {
159
- const machine = StateMachine({
160
- initial: 'idle',
161
- states: {
162
- '*': {
163
- terminate: 'x_x',
164
- },
165
- error: {},
166
- idle: {
167
- click: 'loading',
168
- },
169
- loading: {
170
- success: 'success',
171
- error: 'error',
172
- },
173
- success: {},
174
- },
175
- });
176
- expect(machine.getState()).toBe('idle');
177
-
178
- unwrap(machine.transition('click'));
179
-
180
- expect(machine.getState()).toBe('loading');
181
-
182
- unwrap(machine.transition('terminate'));
183
-
184
- expect(machine.getState()).toBe('x_x');
185
- });
186
- });
187
-
188
- describe('transition output value', () => {
189
- describe('when transition is valid', () => {
190
- it('Should return Success', () => {
191
- const machine = StateMachine({
192
- initial: 'idle',
193
- states: {
194
- error: {},
195
- idle: {
196
- click: 'loading',
197
- },
198
- loading: {
199
- success: 'success',
200
- error: 'error',
201
- },
202
- success: {},
203
- },
204
- });
205
- expect(machine.getState()).toBe('idle');
206
-
207
- expect(isSuccess(machine.transition('click'))).toBe(true);
208
- });
209
- });
210
-
211
- describe('When transitioning to the same state', () => {
212
- it('Should return Success if transition is valid', () => {
213
- const machine = StateMachine({
214
- initial: 'idle',
215
- states: {
216
- idle: {
217
- click: 'idle',
218
- },
219
- },
220
- });
221
- expect(machine.getState()).toBe('idle');
222
-
223
- expect(isSuccess(machine.transition('click'))).toBe(true);
224
- expect(machine.getState()).toBe('idle');
225
- });
226
- });
227
-
228
- describe('When target state does not exist', () => {
229
- it('Should return Failure', () => {
230
- const machine = StateMachine({
231
- initial: 'idle',
232
- states: {
233
- error: {},
234
- idle: {
235
- click: 'loading',
236
- },
237
- loading: {
238
- success: 'success',
239
- error: 'error',
240
- },
241
- success: {},
242
- },
243
- });
244
- expect(machine.getState()).toBe('idle');
245
-
246
- unwrap(machine.transition('click'));
247
- // @ts-expect-error - Testing invalid transition
248
- expect(isFailure(machine.transition('finish'))).toBe(true);
249
- });
250
- });
251
-
252
- describe('When transition is invalid', () => {
253
- it('Should return Failure', () => {
254
- const machine = StateMachine({
255
- initial: 'idle',
256
- states: {
257
- error: {},
258
- idle: {
259
- click: 'loading',
260
- },
261
- loading: {
262
- success: 'success',
263
- error: 'error',
264
- },
265
- success: {},
266
- },
267
- });
268
- expect(machine.getState()).toBe('idle');
269
-
270
- unwrap(machine.transition('click'));
271
- expect(isFailure(machine.transition('click'))).toBe(true);
272
- });
273
- });
274
-
275
- describe('When the transition is disallowed by a conditional', () => {
276
- it('Should return Failure', () => {
277
- const machine = StateMachine({
278
- initial: 'idle',
279
- states: {
280
- error: {},
281
- idle: {
282
- click: ['loading', () => false],
283
- },
284
- loading: {
285
- success: 'success',
286
- error: 'error',
287
- },
288
- success: {},
289
- },
290
- });
291
- expect(machine.getState()).toBe('idle');
292
-
293
- expect(isFailure(machine.transition('click'))).toBe(true);
294
- });
295
- });
296
- });
297
-
298
- describe('staticTransition', () => {
299
- describe('When the transition is valid', () => {
300
- it('Should return the new state', () => {
301
- const machine = StateMachine({
302
- initial: 'idle',
303
- states: {
304
- error: {},
305
- idle: {
306
- click: 'loading',
307
- },
308
- loading: {
309
- success: 'success',
310
- error: 'error',
311
- },
312
- success: {},
313
- },
314
- });
315
- expect(machine.getState()).toBe('idle');
316
- expect(machine.staticTransition('idle', 'click')).toBe('loading');
317
- });
318
-
319
- it('Should not modify the state', () => {
320
- const machine = StateMachine({
321
- initial: 'idle',
322
- states: {
323
- error: {},
324
- idle: {
325
- click: 'loading',
326
- },
327
- loading: {
328
- success: 'success',
329
- error: 'error',
330
- },
331
- success: {},
332
- },
333
- });
334
- expect(machine.getState()).toBe('idle');
335
- machine.staticTransition('idle', 'click');
336
- expect(machine.getState()).toBe('idle');
337
- });
338
- });
339
-
340
- describe('When the transition is invalid', () => {
341
- it('Should return the previous state', () => {
342
- const machine = StateMachine({
343
- initial: 'idle',
344
- states: {
345
- error: {},
346
- idle: {
347
- click: 'loading',
348
- },
349
- loading: {
350
- success: 'success',
351
- error: 'error',
352
- },
353
- success: {},
354
- },
355
- });
356
- expect(machine.getState()).toBe('idle');
357
- // @ts-expect-error - Testing invalid transition
358
- expect(machine.staticTransition('idle', 'finish')).toBe('idle');
359
- });
360
- });
361
-
362
- describe('When the transition is disallowed by a conditional', () => {
363
- it('Should return the previous state', () => {
364
- const machine = StateMachine({
365
- initial: 'idle',
366
- states: {
367
- error: {},
368
- idle: {
369
- click: ['loading', () => false],
370
- },
371
- loading: {
372
- success: 'success',
373
- error: 'error',
374
- },
375
- success: {},
376
- },
377
- });
378
- expect(machine.getState()).toBe('idle');
379
- expect(machine.staticTransition('idle', 'click')).toBe('idle');
380
- });
381
- });
382
-
383
- describe('When the transition is allowed by a conditional', () => {
384
- it('Should return the new state', () => {
385
- const machine = StateMachine({
386
- initial: 'idle',
387
- states: {
388
- error: {},
389
- idle: {
390
- click: ['loading', () => true],
391
- },
392
- loading: {
393
- success: 'success',
394
- error: 'error',
395
- },
396
- success: {},
397
- },
398
- });
399
- expect(machine.getState()).toBe('idle');
400
- expect(machine.staticTransition('idle', 'click')).toBe('loading');
401
- });
402
- });
403
- });
404
-
405
- describe('iniitial', () => {
406
- it('Should return the initial state', () => {
407
- const machine = StateMachine({
408
- initial: 'idle',
409
- states: {
410
- error: {},
411
- idle: {
412
- click: 'loading',
413
- },
414
- loading: {
415
- success: 'success',
416
- error: 'error',
417
- },
418
- success: {},
419
- },
420
- });
421
- expect(machine.getState()).toBe('idle');
422
- expect(machine.initial()).toBe('idle');
423
- });
424
- });
425
- });
@@ -1,18 +0,0 @@
1
- import { describe, test, expect, it } from 'vitest';
2
-
3
- import { StringObject } from '../StringObject';
4
-
5
- describe('StringObject', () => {
6
- test('returns an instance of String', () => {
7
- const str = StringObject('hello');
8
- expect(str).toBeInstanceOf(String);
9
- });
10
-
11
- describe('When the passed value is a function that returns a string', () => {
12
- it('should return a string object with the value of the function', () => {
13
- const str = StringObject(() => 'hello');
14
- expect(str).toBeInstanceOf(String);
15
- expect(str.toString()).toBe('hello');
16
- });
17
- });
18
- });
@@ -1,14 +0,0 @@
1
- import asArray from '../asArray';
2
- import { describe, it, expect } from 'vitest';
3
-
4
- describe('asArray', () => {
5
- it('should return an array', () => {
6
- expect(asArray('test')).toEqual(['test']);
7
- expect(asArray(['test'])).toEqual(['test']);
8
- });
9
-
10
- it('Should create a shallow copy of the array', () => {
11
- const arr = ['test'];
12
- expect(asArray(arr)).not.toBe(arr);
13
- });
14
- });
@@ -1,39 +0,0 @@
1
- import { describe, test, expect, vi, it } from 'vitest';
2
-
3
- import { bindNot } from '../vest-utils';
4
-
5
- describe('bindNot', () => {
6
- it('Should return return a function', () => {
7
- expect(typeof bindNot(vi.fn())).toBe('function');
8
- });
9
-
10
- test('calling returned function runs accepted function', () => {
11
- const fn = vi.fn();
12
-
13
- expect(fn).not.toHaveBeenCalled();
14
- const not = bindNot(fn);
15
- expect(fn).not.toHaveBeenCalled();
16
- not();
17
- expect(fn).toHaveBeenCalledTimes(1);
18
- });
19
-
20
- it('Should pass arguments to accepted function', () => {
21
- const fn = vi.fn();
22
-
23
- const not = bindNot(fn);
24
- not(1, 2, 3, 4);
25
- expect(fn).toHaveBeenCalledWith(1, 2, 3, 4);
26
- });
27
-
28
- it('Should return the boolean negation of the original function', () => {
29
- expect(bindNot(() => true)()).toBe(false);
30
- expect(bindNot(() => false)()).toBe(true);
31
- expect(bindNot(() => 'string')()).toBe(false);
32
- expect(bindNot(() => [])()).toBe(false);
33
- expect(bindNot(() => '')()).toBe(true);
34
- expect(bindNot(() => 0)()).toBe(true);
35
- expect(bindNot(() => NaN)()).toBe(true);
36
- expect(bindNot(() => null)()).toBe(true);
37
- expect(bindNot(() => undefined)()).toBe(true);
38
- });
39
- });
@@ -1,135 +0,0 @@
1
- import { describe, it, expect, vi, test } from 'vitest';
2
-
3
- import { createBus } from '../bus';
4
-
5
- type TestEvents = {
6
- test: any;
7
- t1: void;
8
- t2: void;
9
- };
10
-
11
- describe('bus', () => {
12
- it('should be a function', () => {
13
- expect(createBus).toBeInstanceOf(Function);
14
- });
15
-
16
- it('should return a bus', () => {
17
- const bus = createBus<TestEvents>();
18
- expect(bus).toBeInstanceOf(Object);
19
- expect(bus.emit).toBeInstanceOf(Function);
20
- expect(bus.on).toBeInstanceOf(Function);
21
- });
22
-
23
- it('should emit events', () => {
24
- const bus = createBus<TestEvents>();
25
- const spy = vi.fn();
26
- bus.on('test', spy);
27
- bus.emit('test');
28
- expect(spy).toHaveBeenCalled();
29
- });
30
-
31
- it('should emit events with data', () => {
32
- const bus = createBus<TestEvents>();
33
- const spy = vi.fn();
34
- bus.on('test', spy);
35
- bus.emit('test', 'testData');
36
- expect(spy).toHaveBeenCalledWith('testData');
37
- });
38
-
39
- it('should emit events with multiple listeners', () => {
40
- const bus = createBus<TestEvents>();
41
- const spy1 = vi.fn();
42
- const spy2 = vi.fn();
43
- bus.on('test', spy1);
44
- bus.on('test', spy2);
45
- bus.emit('test');
46
- expect(spy1).toHaveBeenCalled();
47
- expect(spy2).toHaveBeenCalled();
48
- });
49
-
50
- it('should emit events with multiple listeners and data', () => {
51
- const bus = createBus<TestEvents>();
52
- const spy1 = vi.fn();
53
- const spy2 = vi.fn();
54
- bus.on('test', spy1);
55
- bus.on('test', spy2);
56
- bus.emit('test', 'test1');
57
- expect(spy1).toHaveBeenCalledWith('test1');
58
- expect(spy2).toHaveBeenCalledWith('test1');
59
- });
60
-
61
- test('on returns an object with an `off` function', () => {
62
- const bus = createBus<TestEvents>();
63
- const spy = vi.fn();
64
- const off = bus.on('test', spy);
65
- expect(off).toBeInstanceOf(Object);
66
- expect(off.off).toBeInstanceOf(Function);
67
- });
68
-
69
- test('off should remove a listener', () => {
70
- const bus = createBus<TestEvents>();
71
- const spy = vi.fn();
72
- const off = bus.on('test', spy);
73
- off.off();
74
- bus.emit('test');
75
- expect(spy).not.toHaveBeenCalled();
76
- });
77
-
78
- test('off should only remove specific handler', () => {
79
- const bus = createBus<TestEvents>();
80
- const spy1 = vi.fn();
81
- const spy2 = vi.fn();
82
- const off = bus.on('test', spy1);
83
- bus.on('test', spy2);
84
- off.off();
85
- bus.emit('test');
86
- expect(spy1).not.toHaveBeenCalled();
87
- expect(spy2).toHaveBeenCalled();
88
- });
89
-
90
- describe('"ANY" wildcard (*)', () => {
91
- it('Should run the wildcard handler on any event', () => {
92
- const bus = createBus<TestEvents>();
93
- const spy1 = vi.fn();
94
- const spy2 = vi.fn();
95
- const spy3 = vi.fn();
96
- bus.on('t1', spy1);
97
- bus.on('t2', spy2);
98
- bus.on('*', spy3);
99
- expect(spy1).not.toHaveBeenCalled();
100
- expect(spy2).not.toHaveBeenCalled();
101
- expect(spy3).not.toHaveBeenCalled();
102
- bus.emit('t1');
103
- expect(spy1).toHaveBeenCalledTimes(1);
104
- expect(spy2).toHaveBeenCalledTimes(0);
105
- expect(spy3).toHaveBeenCalledTimes(1);
106
- bus.emit('t2');
107
- expect(spy1).toHaveBeenCalledTimes(1);
108
- expect(spy2).toHaveBeenCalledTimes(1);
109
- expect(spy3).toHaveBeenCalledTimes(2);
110
- });
111
-
112
- it('Should call the wildcard last, regardless of when it was defined', () => {
113
- const bus = createBus<TestEvents>();
114
- const spy1 = vi.fn();
115
- const spy2 = vi.fn();
116
- const spy3 = vi.fn();
117
- const spy4 = vi.fn();
118
- bus.on('t1', spy1);
119
- bus.on('*', spy4);
120
- bus.on('t1', spy2);
121
- bus.on('t1', spy3);
122
- bus.emit('t1');
123
- const invocations = [spy1, spy2, spy3, spy4]
124
- .map(i => i.mock.invocationCallOrder[0])
125
- .sort();
126
-
127
- expect(invocations).toEqual([
128
- spy1.mock.invocationCallOrder[0],
129
- spy2.mock.invocationCallOrder[0],
130
- spy3.mock.invocationCallOrder[0],
131
- spy4.mock.invocationCallOrder[0],
132
- ]);
133
- });
134
- });
135
- });