@khanacademy/wonder-blocks-testing 10.1.1 → 11.0.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 (87) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/dist/es/index.js +25 -386
  3. package/dist/gql/types.d.ts +1 -2
  4. package/dist/harness/adapters/data.d.ts +1 -1
  5. package/dist/harness/adapters/index.d.ts +41 -0
  6. package/dist/harness/adapters/ssr.d.ts +1 -1
  7. package/dist/index.d.ts +8 -13
  8. package/dist/index.js +61 -395
  9. package/package.json +8 -6
  10. package/src/gql/__tests__/mock-gql-fetch.test.tsx +1 -1
  11. package/src/gql/__tests__/types.typestest.ts +1 -1
  12. package/src/gql/__tests__/wb-data-integration.test.tsx +1 -1
  13. package/src/gql/mock-gql-fetch.ts +1 -1
  14. package/src/gql/types.ts +4 -2
  15. package/src/harness/adapters/__tests__/ssr.test.tsx +1 -1
  16. package/src/harness/adapters/data.tsx +1 -1
  17. package/src/harness/adapters/{adapters.ts → index.ts} +10 -11
  18. package/src/harness/adapters/ssr.tsx +1 -1
  19. package/src/index.ts +32 -13
  20. package/tsconfig-build.json +1 -0
  21. package/tsconfig-build.tsbuildinfo +1 -1
  22. package/dist/fetch/fetch-request-matches-mock.d.ts +0 -5
  23. package/dist/fetch/mock-fetch.d.ts +0 -5
  24. package/dist/fetch/types.d.ts +0 -9
  25. package/dist/fixtures/fixtures.basic.stories.d.ts +0 -13
  26. package/dist/fixtures/fixtures.d.ts +0 -13
  27. package/dist/fixtures/fixtures.defaultwrapper.stories.d.ts +0 -9
  28. package/dist/fixtures/types.d.ts +0 -36
  29. package/dist/harness/adapt.d.ts +0 -17
  30. package/dist/harness/adapters/adapters.d.ts +0 -36
  31. package/dist/harness/adapters/css.d.ts +0 -12
  32. package/dist/harness/adapters/portal.d.ts +0 -12
  33. package/dist/harness/adapters/router.d.ts +0 -94
  34. package/dist/harness/get-named-adapter-component.d.ts +0 -16
  35. package/dist/harness/hook-harness.d.ts +0 -13
  36. package/dist/harness/make-hook-harness.d.ts +0 -17
  37. package/dist/harness/make-test-harness.d.ts +0 -15
  38. package/dist/harness/test-harness.d.ts +0 -33
  39. package/dist/harness/types.d.ts +0 -36
  40. package/dist/mock-requester.d.ts +0 -5
  41. package/dist/respond-with.d.ts +0 -75
  42. package/dist/response-impl.d.ts +0 -1
  43. package/dist/settle-controller.d.ts +0 -19
  44. package/dist/settle-signal.d.ts +0 -18
  45. package/dist/types.d.ts +0 -25
  46. package/src/__tests__/mock-requester.test.ts +0 -212
  47. package/src/__tests__/respond-with.test.ts +0 -524
  48. package/src/__tests__/response-impl.test.js +0 -47
  49. package/src/__tests__/settle-controller.test.ts +0 -28
  50. package/src/__tests__/settle-signal.test.ts +0 -104
  51. package/src/fetch/__tests__/__snapshots__/mock-fetch.test.ts.snap +0 -29
  52. package/src/fetch/__tests__/fetch-request-matches-mock.test.ts +0 -98
  53. package/src/fetch/__tests__/mock-fetch.test.ts +0 -83
  54. package/src/fetch/fetch-request-matches-mock.ts +0 -42
  55. package/src/fetch/mock-fetch.ts +0 -20
  56. package/src/fetch/types.ts +0 -14
  57. package/src/fixtures/__tests__/fixtures.test.tsx +0 -147
  58. package/src/fixtures/fixtures.basic.stories.tsx +0 -62
  59. package/src/fixtures/fixtures.defaultwrapper.stories.tsx +0 -49
  60. package/src/fixtures/fixtures.tsx +0 -72
  61. package/src/fixtures/types.ts +0 -42
  62. package/src/harness/__tests__/adapt.test.tsx +0 -248
  63. package/src/harness/__tests__/hook-harness.test.ts +0 -73
  64. package/src/harness/__tests__/make-hook-harness.test.tsx +0 -93
  65. package/src/harness/__tests__/make-test-harness.test.tsx +0 -195
  66. package/src/harness/__tests__/test-harness.test.ts +0 -75
  67. package/src/harness/__tests__/types.typestest.tsx +0 -103
  68. package/src/harness/adapt.tsx +0 -41
  69. package/src/harness/adapters/__tests__/__snapshots__/router.test.tsx.snap +0 -5
  70. package/src/harness/adapters/__tests__/css.test.tsx +0 -95
  71. package/src/harness/adapters/__tests__/portal.test.tsx +0 -30
  72. package/src/harness/adapters/__tests__/router.test.tsx +0 -252
  73. package/src/harness/adapters/css.tsx +0 -66
  74. package/src/harness/adapters/portal.tsx +0 -25
  75. package/src/harness/adapters/router.tsx +0 -205
  76. package/src/harness/get-named-adapter-component.tsx +0 -36
  77. package/src/harness/hook-harness.ts +0 -22
  78. package/src/harness/make-hook-harness.tsx +0 -40
  79. package/src/harness/make-test-harness.tsx +0 -60
  80. package/src/harness/test-harness.ts +0 -13
  81. package/src/harness/types.ts +0 -47
  82. package/src/mock-requester.ts +0 -68
  83. package/src/respond-with.ts +0 -263
  84. package/src/response-impl.ts +0 -8
  85. package/src/settle-controller.ts +0 -34
  86. package/src/settle-signal.ts +0 -42
  87. package/src/types.ts +0 -40
@@ -1,524 +0,0 @@
1
- import {SettleController} from "../settle-controller";
2
- import {RespondWith} from "../respond-with";
3
-
4
- describe("RespondWith", () => {
5
- describe("#text.toPromise", () => {
6
- it("should respond with the given text", async () => {
7
- // Arrange
8
-
9
- // Act
10
- const response = await RespondWith.text("SOME TEXT").toPromise();
11
- const result = await response.text();
12
-
13
- // Assert
14
- expect(result).toBe("SOME TEXT");
15
- });
16
-
17
- it("should respond with the given status code", async () => {
18
- // Arrange
19
-
20
- // Act
21
- const result = await RespondWith.text("SOME TEXT", 204).toPromise();
22
-
23
- // Assert
24
- expect(result).toHaveProperty("status", 204);
25
- });
26
-
27
- it("should not settle if the signal is not raised", async () => {
28
- // Arrange
29
- const settleController = new SettleController();
30
- const settleableResponse = RespondWith.text(
31
- "SIGNALLED",
32
- 200,
33
- settleController.signal,
34
- ).toPromise();
35
- const otherResponse = RespondWith.text("NO SIGNAL").toPromise();
36
-
37
- // Act
38
- const firstResponse = await Promise.race([
39
- settleableResponse,
40
- otherResponse,
41
- ]);
42
- const result = await firstResponse.text();
43
-
44
- // Assert
45
- expect(result).toBe("NO SIGNAL");
46
- });
47
-
48
- it("should settle if the signal is raised", async () => {
49
- // Arrange
50
- const settleController = new SettleController();
51
- const settleableResponse = RespondWith.text(
52
- "SIGNALLED",
53
- 200,
54
- settleController.signal,
55
- ).toPromise();
56
- const otherResponse = RespondWith.text("NO SIGNAL").toPromise();
57
-
58
- // Act
59
- settleController.settle();
60
- const firstResponse = await Promise.race([
61
- settleableResponse,
62
- otherResponse,
63
- ]);
64
- const result = await firstResponse.text();
65
-
66
- // Assert
67
- expect(result).toBe("SIGNALLED");
68
- });
69
- });
70
-
71
- describe("#json.toPromise", () => {
72
- it("should respond with the given json", async () => {
73
- // Arrange
74
-
75
- // Act
76
- const response = await RespondWith.json({
77
- some: "json",
78
- }).toPromise();
79
- const result = await response.json();
80
-
81
- // Assert
82
- expect(result).toStrictEqual({some: "json"});
83
- });
84
-
85
- it("should not settle if the signal is not raised", async () => {
86
- // Arrange
87
- const settleController = new SettleController();
88
- const settleableResponse = RespondWith.json(
89
- {result: "SIGNALLED"},
90
- settleController.signal,
91
- ).toPromise();
92
- const otherResponse = RespondWith.text("NO SIGNAL").toPromise();
93
-
94
- // Act
95
- const firstResponse = await Promise.race([
96
- settleableResponse,
97
- otherResponse,
98
- ]);
99
- const result = await firstResponse.text();
100
-
101
- // Assert
102
- expect(result).toBe("NO SIGNAL");
103
- });
104
-
105
- it("should settle if the signal is raised", async () => {
106
- // Arrange
107
- const settleController = new SettleController();
108
- const settleableResponse = RespondWith.json(
109
- {result: "SIGNALLED"},
110
- settleController.signal,
111
- ).toPromise();
112
- const otherResponse = RespondWith.text("NO SIGNAL").toPromise();
113
-
114
- // Act
115
- settleController.settle();
116
- const firstResponse = await Promise.race([
117
- settleableResponse,
118
- otherResponse,
119
- ]);
120
- const result = await firstResponse.json();
121
-
122
- // Assert
123
- expect(result).toStrictEqual({result: "SIGNALLED"});
124
- });
125
- });
126
-
127
- describe("#graphQLData", () => {
128
- it("should respond with the given GraphQL data", async () => {
129
- // Arrange
130
-
131
- // Act
132
- const response = await RespondWith.graphQLData({
133
- some: "json",
134
- }).toPromise();
135
- const result = await response.json();
136
-
137
- // Assert
138
- expect(result).toStrictEqual({data: {some: "json"}});
139
- });
140
-
141
- it("should not settle if the signal is not raised", async () => {
142
- // Arrange
143
- const settleController = new SettleController();
144
- const settleableResponse = RespondWith.graphQLData(
145
- {result: "SIGNALLED"},
146
- settleController.signal,
147
- ).toPromise();
148
- const otherResponse = RespondWith.text("NO SIGNAL").toPromise();
149
-
150
- // Act
151
- const firstResponse = await Promise.race([
152
- settleableResponse,
153
- otherResponse,
154
- ]);
155
- const result = await firstResponse.text();
156
-
157
- // Assert
158
- expect(result).toBe("NO SIGNAL");
159
- });
160
-
161
- it("should settle if the signal is raised", async () => {
162
- // Arrange
163
- const settleController = new SettleController();
164
- const settleableResponse = RespondWith.graphQLData(
165
- {result: "SIGNALLED"},
166
- settleController.signal,
167
- ).toPromise();
168
- const otherResponse = RespondWith.text("NO SIGNAL").toPromise();
169
-
170
- // Act
171
- settleController.settle();
172
- const firstResponse = await Promise.race([
173
- settleableResponse,
174
- otherResponse,
175
- ]);
176
- const result = await firstResponse.json();
177
-
178
- // Assert
179
- expect(result).toStrictEqual({data: {result: "SIGNALLED"}});
180
- });
181
- });
182
-
183
- describe("#unparseableBody", () => {
184
- it("should reject JSON as unparseable", async () => {
185
- // Arrange
186
-
187
- // Act
188
- const response = await RespondWith.unparseableBody().toPromise();
189
- const act = response.json();
190
-
191
- // Assert
192
- await expect(act).rejects.toThrowErrorMatchingInlineSnapshot(
193
- `"invalid json response body at reason: Unexpected token 'I', "INVALID JSON" is not valid JSON"`,
194
- );
195
- });
196
-
197
- it("should not settle if the signal is not raised", async () => {
198
- // Arrange
199
- const settleController = new SettleController();
200
- const settleableResponse = RespondWith.unparseableBody(
201
- settleController.signal,
202
- ).toPromise();
203
- const otherResponse = RespondWith.text("NO SIGNAL").toPromise();
204
-
205
- // Act
206
- const firstResponse = await Promise.race([
207
- settleableResponse,
208
- otherResponse,
209
- ]);
210
- const result = await firstResponse.text();
211
-
212
- // Assert
213
- expect(result).toBe("NO SIGNAL");
214
- });
215
-
216
- it("should settle if the signal is raised", async () => {
217
- // Arrange
218
- const settleController = new SettleController();
219
- const settleableResponse = RespondWith.unparseableBody(
220
- settleController.signal,
221
- ).toPromise();
222
- const otherResponse = RespondWith.text("NO SIGNAL").toPromise();
223
-
224
- // Act
225
- settleController.settle();
226
- const firstResponse = await Promise.race([
227
- settleableResponse,
228
- otherResponse,
229
- ]);
230
- const act = firstResponse.json();
231
-
232
- // Assert
233
- await expect(act).rejects.toThrowError();
234
- });
235
- });
236
-
237
- describe("#abortedRequest", () => {
238
- it("should reject with AbortError", async () => {
239
- // Arrange
240
-
241
- // Act
242
- const act = RespondWith.abortedRequest().toPromise();
243
-
244
- // Assert
245
- await expect(act).rejects.toThrowErrorMatchingInlineSnapshot(
246
- `"Mock request aborted"`,
247
- );
248
- });
249
-
250
- it("should not settle if the signal is not raised", async () => {
251
- // Arrange
252
- const settleController = new SettleController();
253
- const settleableResponse = RespondWith.abortedRequest(
254
- settleController.signal,
255
- ).toPromise();
256
- const otherResponse = RespondWith.text("NO SIGNAL").toPromise();
257
-
258
- // Act
259
- const act = Promise.race([settleableResponse, otherResponse]);
260
-
261
- // Assert
262
- await expect(act).resolves;
263
- });
264
-
265
- it("should settle if the signal is raised", async () => {
266
- // Arrange
267
- const settleController = new SettleController();
268
- const settleableResponse = RespondWith.abortedRequest(
269
- settleController.signal,
270
- ).toPromise();
271
- const otherResponse = RespondWith.text("NO SIGNAL").toPromise();
272
-
273
- // Act
274
- settleController.settle();
275
- const act = Promise.race([settleableResponse, otherResponse]);
276
-
277
- // Assert
278
- await expect(act).rejects.toThrowError();
279
- });
280
- });
281
-
282
- describe("#reject", () => {
283
- it("should reject with AbortError", async () => {
284
- // Arrange
285
-
286
- // Act
287
- const act = RespondWith.reject(new Error("BOOM!")).toPromise();
288
-
289
- // Assert
290
- await expect(act).rejects.toThrowErrorMatchingInlineSnapshot(
291
- `"BOOM!"`,
292
- );
293
- });
294
-
295
- it("should not settle if the signal is not raised", async () => {
296
- // Arrange
297
- const settleController = new SettleController();
298
- const settleableResponse = RespondWith.reject(
299
- new Error("BOOM!"),
300
- settleController.signal,
301
- ).toPromise();
302
- const otherResponse = RespondWith.text("NO SIGNAL").toPromise();
303
-
304
- // Act
305
- const act = Promise.race([settleableResponse, otherResponse]);
306
-
307
- // Assert
308
- await expect(act).resolves;
309
- });
310
-
311
- it("should settle if the signal is raised", async () => {
312
- // Arrange
313
- const settleController = new SettleController();
314
- const settleableResponse = RespondWith.reject(
315
- new Error("BOOM!"),
316
- settleController.signal,
317
- ).toPromise();
318
- const otherResponse = RespondWith.text("NO SIGNAL").toPromise();
319
-
320
- // Act
321
- settleController.settle();
322
- const act = Promise.race([settleableResponse, otherResponse]);
323
-
324
- // Assert
325
- await expect(act).rejects.toThrowError();
326
- });
327
- });
328
-
329
- describe("#errorStatusCode", () => {
330
- it("should throw if the status code represents success", () => {
331
- // Arrange
332
-
333
- // Act
334
- const result = () => RespondWith.errorStatusCode(200);
335
-
336
- // Assert
337
- expect(result).toThrowErrorMatchingInlineSnapshot(
338
- `"200 is not a valid error status code"`,
339
- );
340
- });
341
-
342
- it("should respond with the given status code", async () => {
343
- // Arrange
344
-
345
- // Act
346
- const result = await RespondWith.errorStatusCode(400).toPromise();
347
-
348
- // Assert
349
- expect(result).toHaveProperty("status", 400);
350
- });
351
-
352
- it("should not settle if the signal is not raised", async () => {
353
- // Arrange
354
- const settleController = new SettleController();
355
- const settleableResponse = RespondWith.errorStatusCode(
356
- 500,
357
- settleController.signal,
358
- ).toPromise();
359
- const otherResponse = RespondWith.text("NO SIGNAL").toPromise();
360
-
361
- // Act
362
- const firstResponse = await Promise.race([
363
- settleableResponse,
364
- otherResponse,
365
- ]);
366
- const result = await firstResponse.text();
367
-
368
- // Assert
369
- expect(result).toBe("NO SIGNAL");
370
- });
371
-
372
- it("should settle if the signal is raised", async () => {
373
- // Arrange
374
- const settleController = new SettleController();
375
- const settleableResponse = RespondWith.errorStatusCode(
376
- 500,
377
- settleController.signal,
378
- ).toPromise();
379
- const otherResponse = RespondWith.text("NO SIGNAL").toPromise();
380
-
381
- // Act
382
- settleController.settle();
383
- const result = await Promise.race([
384
- settleableResponse,
385
- otherResponse,
386
- ]);
387
-
388
- // Assert
389
- expect(result).toHaveProperty("status", 500);
390
- });
391
- });
392
-
393
- describe("#nonGraphQLBody", () => {
394
- it("should respond with valid json", async () => {
395
- // Arrange
396
-
397
- // Act
398
- const response = await RespondWith.nonGraphQLBody().toPromise();
399
- const act = response.json();
400
-
401
- // Assert
402
- await expect(act).resolves.not.toThrow();
403
- });
404
-
405
- it("should respond with JSON that is not a valid GraphQL response", async () => {
406
- // Arrange
407
-
408
- // Act
409
- const response = await RespondWith.nonGraphQLBody().toPromise();
410
- const result = await response.json();
411
-
412
- // Assert
413
- expect(result).toMatchInlineSnapshot(`
414
- {
415
- "that": "is not a valid graphql response",
416
- "valid": "json",
417
- }
418
- `);
419
- });
420
-
421
- it("should not settle if the signal is not raised", async () => {
422
- // Arrange
423
- const settleController = new SettleController();
424
- const settleableResponse = RespondWith.nonGraphQLBody(
425
- settleController.signal,
426
- ).toPromise();
427
- const otherResponse = RespondWith.text("NO SIGNAL").toPromise();
428
-
429
- // Act
430
- const firstResponse = await Promise.race([
431
- settleableResponse,
432
- otherResponse,
433
- ]);
434
- const result = await firstResponse.text();
435
-
436
- // Assert
437
- expect(result).toBe("NO SIGNAL");
438
- });
439
-
440
- it("should settle if the signal is raised", async () => {
441
- // Arrange
442
- const settleController = new SettleController();
443
- const settleableResponse = RespondWith.nonGraphQLBody(
444
- settleController.signal,
445
- ).toPromise();
446
- const otherResponse = RespondWith.text("NO SIGNAL").toPromise();
447
-
448
- // Act
449
- settleController.settle();
450
- const firstResponse = await Promise.race([
451
- settleableResponse,
452
- otherResponse,
453
- ]);
454
- const result = await firstResponse.json();
455
-
456
- // Assert
457
- expect(result).toStrictEqual({
458
- valid: "json",
459
- that: "is not a valid graphql response",
460
- });
461
- });
462
- });
463
-
464
- describe("#graphQLErrors", () => {
465
- it("should respond with the given GraphQL errors", async () => {
466
- // Arrange
467
-
468
- // Act
469
- const response = await RespondWith.graphQLErrors([
470
- "BOOM!",
471
- "BANG!",
472
- ]).toPromise();
473
- const result = await response.json();
474
-
475
- // Assert
476
- expect(result).toStrictEqual({
477
- errors: [{message: "BOOM!"}, {message: "BANG!"}],
478
- });
479
- });
480
-
481
- it("should not settle if the signal is not raised", async () => {
482
- // Arrange
483
- const settleController = new SettleController();
484
- const settleableResponse = RespondWith.graphQLErrors(
485
- ["BOOM!", "BANG!"],
486
- settleController.signal,
487
- ).toPromise();
488
- const otherResponse = RespondWith.text("NO SIGNAL").toPromise();
489
-
490
- // Act
491
- const firstResponse = await Promise.race([
492
- settleableResponse,
493
- otherResponse,
494
- ]);
495
- const result = await firstResponse.text();
496
-
497
- // Assert
498
- expect(result).toBe("NO SIGNAL");
499
- });
500
-
501
- it("should settle if the signal is raised", async () => {
502
- // Arrange
503
- const settleController = new SettleController();
504
- const settleableResponse = RespondWith.graphQLErrors(
505
- ["SIGNALLED"],
506
- settleController.signal,
507
- ).toPromise();
508
- const otherResponse = RespondWith.text("NO SIGNAL").toPromise();
509
-
510
- // Act
511
- settleController.settle();
512
- const firstResponse = await Promise.race([
513
- settleableResponse,
514
- otherResponse,
515
- ]);
516
- const result = await firstResponse.json();
517
-
518
- // Assert
519
- expect(result).toStrictEqual({
520
- errors: [{message: "SIGNALLED"}],
521
- });
522
- });
523
- });
524
- });
@@ -1,47 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-var-requires */
2
- import * as wst from "@khanacademy/wonder-stuff-testing";
3
-
4
- describe("ResponseImpl", () => {
5
- const globalResponse = globalThis.Response;
6
-
7
- beforeEach(() => {
8
- if (globalResponse) {
9
- delete globalThis.Response;
10
- }
11
- });
12
-
13
- afterEach(() => {
14
- if (globalResponse) {
15
- globalThis.Response = globalResponse;
16
- } else {
17
- delete globalThis.Response;
18
- }
19
- });
20
-
21
- it("should use Response from node-fetch if Response does not exist", () => {
22
- // Arrange
23
-
24
- // Act
25
- const {ResponseImpl: result, NodeFetchResponse} =
26
- wst.jest.isolateModules(() => ({
27
- ResponseImpl: require("../response-impl").ResponseImpl,
28
- NodeFetchResponse: require("node-fetch").Response,
29
- }));
30
-
31
- // Assert
32
- expect(result).toBe(NodeFetchResponse);
33
- });
34
-
35
- it("should return the existing Response type if it exists", () => {
36
- // Arrange
37
- globalThis.Response = class CustomResponse {};
38
-
39
- // Act
40
- const result = wst.jest.isolateModules(
41
- () => require("../response-impl").ResponseImpl,
42
- );
43
-
44
- // Assert
45
- expect(result).toBe(globalThis.Response);
46
- });
47
- });
@@ -1,28 +0,0 @@
1
- import {SettleController} from "../settle-controller";
2
- import {SettleSignal} from "../settle-signal";
3
-
4
- describe("SettleController", () => {
5
- it("should have a signal", () => {
6
- // Arrange
7
-
8
- // Act
9
- const result = new SettleController();
10
-
11
- // Assert
12
- expect(result).toHaveProperty("signal", expect.any(SettleSignal));
13
- });
14
-
15
- describe("#settle", () => {
16
- it("should settle the signal", () => {
17
- // Arrange
18
- const controller = new SettleController();
19
- const signal = controller.signal;
20
-
21
- // Act
22
- controller.settle();
23
-
24
- // Assert
25
- expect(signal.settled).toBe(true);
26
- });
27
- });
28
- });
@@ -1,104 +0,0 @@
1
- import {SettleSignal} from "../settle-signal";
2
-
3
- describe("SettleSignal", () => {
4
- it("should extend EventTarget", () => {
5
- // Arrange
6
-
7
- // Act
8
- const result = new SettleSignal();
9
-
10
- // Assert
11
- expect(result).toBeInstanceOf(EventTarget);
12
- });
13
-
14
- it("should start with settled = false", () => {
15
- // Arrange
16
-
17
- // Act
18
- const result = new SettleSignal();
19
-
20
- // Assert
21
- expect(result).toHaveProperty("settled", false);
22
- });
23
-
24
- it("should invoke the passed function with a function", () => {
25
- // Arrange
26
- const setSettleFn = jest.fn();
27
-
28
- // Act
29
- // eslint-disable-next-line no-new
30
- new SettleSignal(setSettleFn);
31
-
32
- // Assert
33
- expect(setSettleFn).toHaveBeenCalledWith(expect.any(Function));
34
- });
35
-
36
- describe("setSettleFn argument", () => {
37
- it("should set settled to true", () => {
38
- // Arrange
39
- const setSettleFn = jest.fn();
40
- const signal = new SettleSignal(setSettleFn);
41
- const settle = setSettleFn.mock.calls[0][0];
42
-
43
- // Act
44
- settle();
45
-
46
- // Assert
47
- expect(signal.settled).toBe(true);
48
- });
49
-
50
- it("should raise the settled event", () => {
51
- // Arrange
52
- const setSettleFn = jest.fn();
53
- const signal = new SettleSignal(setSettleFn);
54
- const settle = setSettleFn.mock.calls[0][0];
55
- const handler = jest.fn();
56
- signal.addEventListener("settled", handler);
57
-
58
- // Act
59
- settle();
60
-
61
- // Assert
62
- expect(handler).toHaveBeenCalled();
63
- });
64
-
65
- it("should throw if the signal has already been settled", () => {
66
- // Arrange
67
- const setSettleFn = jest.fn();
68
- // eslint-disable-next-line no-new
69
- new SettleSignal(setSettleFn);
70
- const settle = setSettleFn.mock.calls[0][0];
71
- settle();
72
-
73
- // Act
74
- const result = () => settle();
75
-
76
- // Assert
77
- expect(result).toThrowErrorMatchingInlineSnapshot(
78
- `"SettleSignal already settled"`,
79
- );
80
- });
81
- });
82
-
83
- describe("#SettleSignal.settle", () => {
84
- it("should return a SettleSignal", () => {
85
- // Arrange
86
-
87
- // Act
88
- const result = SettleSignal.settle();
89
-
90
- // Assert
91
- expect(result).toBeInstanceOf(SettleSignal);
92
- });
93
-
94
- it("should return a SettleSignal that is already settled", () => {
95
- // Arrange
96
-
97
- // Act
98
- const result = SettleSignal.settle();
99
-
100
- // Assert
101
- expect(result).toHaveProperty("settled", true);
102
- });
103
- });
104
- });