|
1 | 1 | import { ResultAsync, err, ok } from "neverthrow";
|
2 |
| -import { describe, expect, it, vi, beforeEach, afterEach } from "vitest"; |
| 2 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
3 | 3 | import { debounceResultAsync } from "../debounce-results.js";
|
4 | 4 |
|
5 | 5 | describe("debounceResultAsync", () => {
|
6 |
| - beforeEach(() => { |
7 |
| - vi.useFakeTimers(); |
8 |
| - }); |
9 |
| - |
10 |
| - afterEach(() => { |
11 |
| - vi.restoreAllMocks(); |
12 |
| - }); |
13 |
| - |
14 |
| - it("should debounce function calls", async () => { |
15 |
| - const mockFn = vi.fn().mockImplementation((value: number) => |
16 |
| - ResultAsync.fromPromise( |
17 |
| - Promise.resolve(value * 2), |
18 |
| - error => error as Error |
19 |
| - ) |
20 |
| - ); |
21 |
| - |
22 |
| - const debounced = debounceResultAsync(mockFn, 1000); |
23 |
| - |
24 |
| - // Multiple calls in quick succession |
25 |
| - debounced(1); |
26 |
| - debounced(2); |
27 |
| - debounced(3); |
28 |
| - |
29 |
| - // Only the last one should be executed |
30 |
| - vi.advanceTimersByTime(1000); |
31 |
| - await vi.runAllTimersAsync(); |
32 |
| - |
33 |
| - expect(mockFn).toHaveBeenCalledTimes(1); |
34 |
| - expect(mockFn).toHaveBeenCalledWith(3); |
35 |
| - }); |
36 |
| - |
37 |
| - it("should return the same promise for calls during the wait time", async () => { |
38 |
| - const mockFn = vi.fn().mockImplementation((value: number) => |
39 |
| - ResultAsync.fromPromise( |
40 |
| - Promise.resolve(value * 2), |
41 |
| - error => error as Error |
42 |
| - ) |
43 |
| - ); |
44 |
| - |
45 |
| - const debounced = debounceResultAsync(mockFn, 1000); |
46 |
| - |
47 |
| - const promise1 = debounced(1); |
48 |
| - const promise2 = debounced(2); |
49 |
| - |
50 |
| - expect(promise1).toBe(promise2); |
51 |
| - }); |
52 |
| - |
53 |
| - it("should return the correct result", async () => { |
54 |
| - const mockFn = vi.fn().mockImplementation((value: number) => |
55 |
| - ResultAsync.fromPromise( |
56 |
| - Promise.resolve(value * 2), |
57 |
| - error => error as Error |
58 |
| - ) |
59 |
| - ); |
60 |
| - |
61 |
| - const debounced = debounceResultAsync(mockFn, 1000); |
62 |
| - |
63 |
| - const resultPromise = debounced(5); |
64 |
| - vi.advanceTimersByTime(1000); |
65 |
| - await vi.runAllTimersAsync(); |
66 |
| - |
67 |
| - const result = await resultPromise; |
68 |
| - expect(result.isOk()).toBe(true); |
69 |
| - expect(result._unsafeUnwrap()).toBe(10); |
70 |
| - }); |
71 |
| - |
72 |
| - it("should handle errors correctly", async () => { |
73 |
| - const testError = new Error("Test error"); |
74 |
| - const mockFn = vi.fn().mockImplementation(() => |
75 |
| - ResultAsync.fromPromise( |
76 |
| - Promise.reject(testError), |
77 |
| - error => error as Error |
78 |
| - ) |
79 |
| - ); |
80 |
| - |
81 |
| - const debounced = debounceResultAsync(mockFn, 1000); |
82 |
| - |
83 |
| - const resultPromise = debounced(); |
84 |
| - vi.advanceTimersByTime(1000); |
85 |
| - await vi.runAllTimersAsync(); |
86 |
| - |
87 |
| - const result = await resultPromise; |
88 |
| - expect(result.isErr()).toBe(true); |
89 |
| - expect(result._unsafeUnwrapErr()).toBe(testError); |
90 |
| - }); |
91 |
| - |
92 |
| - it("should reset after a successful call and allow new calls", async () => { |
93 |
| - const mockFn = vi.fn() |
94 |
| - .mockImplementationOnce((value: number) => |
95 |
| - ResultAsync.fromPromise( |
96 |
| - Promise.resolve(value * 2), |
97 |
| - error => error as Error |
98 |
| - ) |
99 |
| - ) |
100 |
| - .mockImplementationOnce((value: number) => |
101 |
| - ResultAsync.fromPromise( |
102 |
| - Promise.resolve(value * 3), |
103 |
| - error => error as Error |
104 |
| - ) |
105 |
| - ); |
106 |
| - |
107 |
| - const debounced = debounceResultAsync(mockFn, 1000); |
108 |
| - |
109 |
| - // First call |
110 |
| - const resultPromise1 = debounced(5); |
111 |
| - vi.advanceTimersByTime(1000); |
112 |
| - await vi.runAllTimersAsync(); |
113 |
| - |
114 |
| - const result1 = await resultPromise1; |
115 |
| - expect(result1._unsafeUnwrap()).toBe(10); |
116 |
| - |
117 |
| - // Second call should use a new promise |
118 |
| - const resultPromise2 = debounced(5); |
119 |
| - vi.advanceTimersByTime(1000); |
120 |
| - await vi.runAllTimersAsync(); |
121 |
| - |
122 |
| - const result2 = await resultPromise2; |
123 |
| - expect(result2._unsafeUnwrap()).toBe(15); |
124 |
| - |
125 |
| - expect(mockFn).toHaveBeenCalledTimes(2); |
126 |
| - }); |
127 |
| - |
128 |
| - it("should work with functions taking multiple arguments", async () => { |
129 |
| - const mockFn = vi.fn().mockImplementation((a: number, b: string, c: boolean) => |
130 |
| - ResultAsync.fromPromise( |
131 |
| - Promise.resolve(`${a}-${b}-${c}`), |
132 |
| - error => error as Error |
133 |
| - ) |
134 |
| - ); |
135 |
| - |
136 |
| - const debounced = debounceResultAsync(mockFn, 1000); |
137 |
| - |
138 |
| - const resultPromise = debounced(1, "test", true); |
139 |
| - vi.advanceTimersByTime(1000); |
140 |
| - await vi.runAllTimersAsync(); |
141 |
| - |
142 |
| - const result = await resultPromise; |
143 |
| - expect(result._unsafeUnwrap()).toBe("1-test-true"); |
144 |
| - }); |
| 6 | + beforeEach(() => { |
| 7 | + vi.useFakeTimers(); |
| 8 | + }); |
| 9 | + |
| 10 | + afterEach(() => { |
| 11 | + vi.restoreAllMocks(); |
| 12 | + }); |
| 13 | + |
| 14 | + it("should debounce function calls", async () => { |
| 15 | + const mockFn = vi |
| 16 | + .fn() |
| 17 | + .mockImplementation((value: number) => |
| 18 | + ResultAsync.fromPromise(Promise.resolve(value * 2), (error) => error as Error), |
| 19 | + ); |
| 20 | + |
| 21 | + const debounced = debounceResultAsync(mockFn, 1000); |
| 22 | + |
| 23 | + // Multiple calls in quick succession |
| 24 | + debounced(1); |
| 25 | + debounced(2); |
| 26 | + debounced(3); |
| 27 | + |
| 28 | + // Only the last one should be executed |
| 29 | + vi.advanceTimersByTime(1000); |
| 30 | + await vi.runAllTimersAsync(); |
| 31 | + |
| 32 | + expect(mockFn).toHaveBeenCalledTimes(1); |
| 33 | + expect(mockFn).toHaveBeenCalledWith(3); |
| 34 | + }); |
| 35 | + |
| 36 | + it("should return the same promise for calls during the wait time", async () => { |
| 37 | + const mockFn = vi |
| 38 | + .fn() |
| 39 | + .mockImplementation((value: number) => |
| 40 | + ResultAsync.fromPromise(Promise.resolve(value * 2), (error) => error as Error), |
| 41 | + ); |
| 42 | + |
| 43 | + const debounced = debounceResultAsync(mockFn, 1000); |
| 44 | + |
| 45 | + const promise1 = debounced(1); |
| 46 | + const promise2 = debounced(2); |
| 47 | + |
| 48 | + expect(promise1).toBe(promise2); |
| 49 | + }); |
| 50 | + |
| 51 | + it("should return the correct result", async () => { |
| 52 | + const mockFn = vi |
| 53 | + .fn() |
| 54 | + .mockImplementation((value: number) => |
| 55 | + ResultAsync.fromPromise(Promise.resolve(value * 2), (error) => error as Error), |
| 56 | + ); |
| 57 | + |
| 58 | + const debounced = debounceResultAsync(mockFn, 1000); |
| 59 | + |
| 60 | + const resultPromise = debounced(5); |
| 61 | + vi.advanceTimersByTime(1000); |
| 62 | + await vi.runAllTimersAsync(); |
| 63 | + |
| 64 | + const result = await resultPromise; |
| 65 | + expect(result.isOk()).toBe(true); |
| 66 | + expect(result._unsafeUnwrap()).toBe(10); |
| 67 | + }); |
| 68 | + |
| 69 | + it("should handle errors correctly", async () => { |
| 70 | + const testError = new Error("Test error"); |
| 71 | + const mockFn = vi |
| 72 | + .fn() |
| 73 | + .mockImplementation(() => |
| 74 | + ResultAsync.fromPromise(Promise.reject(testError), (error) => error as Error), |
| 75 | + ); |
| 76 | + |
| 77 | + const debounced = debounceResultAsync(mockFn, 1000); |
| 78 | + |
| 79 | + const resultPromise = debounced(); |
| 80 | + vi.advanceTimersByTime(1000); |
| 81 | + await vi.runAllTimersAsync(); |
| 82 | + |
| 83 | + const result = await resultPromise; |
| 84 | + expect(result.isErr()).toBe(true); |
| 85 | + expect(result._unsafeUnwrapErr()).toBe(testError); |
| 86 | + }); |
| 87 | + |
| 88 | + it("should reset after a successful call and allow new calls", async () => { |
| 89 | + const mockFn = vi |
| 90 | + .fn() |
| 91 | + .mockImplementationOnce((value: number) => |
| 92 | + ResultAsync.fromPromise(Promise.resolve(value * 2), (error) => error as Error), |
| 93 | + ) |
| 94 | + .mockImplementationOnce((value: number) => |
| 95 | + ResultAsync.fromPromise(Promise.resolve(value * 3), (error) => error as Error), |
| 96 | + ); |
| 97 | + |
| 98 | + const debounced = debounceResultAsync(mockFn, 1000); |
| 99 | + |
| 100 | + // First call |
| 101 | + const resultPromise1 = debounced(5); |
| 102 | + vi.advanceTimersByTime(1000); |
| 103 | + await vi.runAllTimersAsync(); |
| 104 | + |
| 105 | + const result1 = await resultPromise1; |
| 106 | + expect(result1._unsafeUnwrap()).toBe(10); |
| 107 | + |
| 108 | + // Second call should use a new promise |
| 109 | + const resultPromise2 = debounced(5); |
| 110 | + vi.advanceTimersByTime(1000); |
| 111 | + await vi.runAllTimersAsync(); |
| 112 | + |
| 113 | + const result2 = await resultPromise2; |
| 114 | + expect(result2._unsafeUnwrap()).toBe(15); |
| 115 | + |
| 116 | + expect(mockFn).toHaveBeenCalledTimes(2); |
| 117 | + }); |
| 118 | + |
| 119 | + it("should work with functions taking multiple arguments", async () => { |
| 120 | + const mockFn = vi |
| 121 | + .fn() |
| 122 | + .mockImplementation((a: number, b: string, c: boolean) => |
| 123 | + ResultAsync.fromPromise(Promise.resolve(`${a}-${b}-${c}`), (error) => error as Error), |
| 124 | + ); |
| 125 | + |
| 126 | + const debounced = debounceResultAsync(mockFn, 1000); |
| 127 | + |
| 128 | + const resultPromise = debounced(1, "test", true); |
| 129 | + vi.advanceTimersByTime(1000); |
| 130 | + await vi.runAllTimersAsync(); |
| 131 | + |
| 132 | + const result = await resultPromise; |
| 133 | + expect(result._unsafeUnwrap()).toBe("1-test-true"); |
| 134 | + }); |
145 | 135 | });
|
0 commit comments