| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import {debounce} from '../debounce'; |
| b69ab31 | | | 9 | |
| b69ab31 | | | 10 | describe('debounce', () => { |
| b69ab31 | | | 11 | let func1: jest.Mock<[number, string]>; |
| b69ab31 | | | 12 | const BUFFER = 10; |
| b69ab31 | | | 13 | |
| b69ab31 | | | 14 | beforeEach(() => { |
| b69ab31 | | | 15 | jest.resetModules(); |
| b69ab31 | | | 16 | func1 = jest.fn(); |
| b69ab31 | | | 17 | jest.useFakeTimers(); |
| b69ab31 | | | 18 | }); |
| b69ab31 | | | 19 | |
| b69ab31 | | | 20 | function argsEquivalent(args1: Array<unknown>, args2: Array<unknown>) { |
| b69ab31 | | | 21 | for (let i = 0; i < Math.max(args1.length, args2.length); i++) { |
| b69ab31 | | | 22 | if (args1[i] != args2[i]) { |
| b69ab31 | | | 23 | return false; |
| b69ab31 | | | 24 | } |
| b69ab31 | | | 25 | } |
| b69ab31 | | | 26 | return true; |
| b69ab31 | | | 27 | } |
| b69ab31 | | | 28 | |
| b69ab31 | | | 29 | function assertCalledWith(...origargs: Array<unknown>) { |
| b69ab31 | | | 30 | const args = [].slice.call(origargs); |
| b69ab31 | | | 31 | expect(func1.mock.calls.some(call => argsEquivalent(args, call))).toBeTruthy(); |
| b69ab31 | | | 32 | } |
| b69ab31 | | | 33 | |
| b69ab31 | | | 34 | it('should not call until the wait is over', () => { |
| b69ab31 | | | 35 | const wait = 200; |
| b69ab31 | | | 36 | const debounced = debounce(func1, wait); |
| b69ab31 | | | 37 | debounced(1, 'a'); |
| b69ab31 | | | 38 | expect(func1).not.toBeCalled(); |
| b69ab31 | | | 39 | |
| b69ab31 | | | 40 | jest.advanceTimersByTime(wait + BUFFER); |
| b69ab31 | | | 41 | assertCalledWith(1, 'a'); |
| b69ab31 | | | 42 | |
| b69ab31 | | | 43 | // make sure that subsequent function isn't called right away |
| b69ab31 | | | 44 | debounced(2, 'a'); |
| b69ab31 | | | 45 | expect(func1.mock.calls.length).toBe(1); |
| b69ab31 | | | 46 | jest.clearAllTimers(); |
| b69ab31 | | | 47 | }); |
| b69ab31 | | | 48 | |
| b69ab31 | | | 49 | it('should only call the last function per batch', () => { |
| b69ab31 | | | 50 | const wait = 200; |
| b69ab31 | | | 51 | const debounced = debounce(func1, wait); |
| b69ab31 | | | 52 | debounced(1, 'a'); |
| b69ab31 | | | 53 | expect(func1).not.toBeCalled(); |
| b69ab31 | | | 54 | jest.advanceTimersByTime(100); |
| b69ab31 | | | 55 | debounced(2, 'a'); |
| b69ab31 | | | 56 | jest.advanceTimersByTime(100); |
| b69ab31 | | | 57 | debounced(3, 'a'); |
| b69ab31 | | | 58 | jest.advanceTimersByTime(100); |
| b69ab31 | | | 59 | debounced(4, 'a'); |
| b69ab31 | | | 60 | jest.advanceTimersByTime(100); |
| b69ab31 | | | 61 | debounced(5, 'a'); |
| b69ab31 | | | 62 | expect(jest.getTimerCount()).toBe(1); |
| b69ab31 | | | 63 | jest.advanceTimersByTime(wait + BUFFER); |
| b69ab31 | | | 64 | assertCalledWith(5, 'a'); |
| b69ab31 | | | 65 | debounced(6, 'a'); |
| b69ab31 | | | 66 | debounced(7, 'a'); |
| b69ab31 | | | 67 | jest.advanceTimersByTime(wait + BUFFER); |
| b69ab31 | | | 68 | assertCalledWith(7, 'a'); |
| b69ab31 | | | 69 | expect(func1.mock.calls.length).toBe(2); |
| b69ab31 | | | 70 | }); |
| b69ab31 | | | 71 | |
| b69ab31 | | | 72 | it('should be reset-able', () => { |
| b69ab31 | | | 73 | const wait = 300; |
| b69ab31 | | | 74 | const debounced = debounce(func1, wait); |
| b69ab31 | | | 75 | debounced(1, 'a'); |
| b69ab31 | | | 76 | debounced.reset(); |
| b69ab31 | | | 77 | expect(jest.getTimerCount()).toBe(0); |
| b69ab31 | | | 78 | jest.runAllTimers(); |
| b69ab31 | | | 79 | expect(func1).not.toBeCalled(); |
| b69ab31 | | | 80 | }); |
| b69ab31 | | | 81 | |
| b69ab31 | | | 82 | it('should correctly show if the timeout is pending', () => { |
| b69ab31 | | | 83 | const wait = 300; |
| b69ab31 | | | 84 | const debounced = debounce(func1, wait); |
| b69ab31 | | | 85 | expect(debounced.isPending()).toBe(false); |
| b69ab31 | | | 86 | debounced(1, 'a'); |
| b69ab31 | | | 87 | debounced(1, 'a'); |
| b69ab31 | | | 88 | expect(debounced.isPending()).toBe(true); |
| b69ab31 | | | 89 | jest.runAllTimers(); |
| b69ab31 | | | 90 | expect(func1.mock.calls.length).toBe(1); |
| b69ab31 | | | 91 | expect(debounced.isPending()).toBe(false); |
| b69ab31 | | | 92 | }); |
| b69ab31 | | | 93 | |
| b69ab31 | | | 94 | describe('leading', () => { |
| b69ab31 | | | 95 | it('should call the function immediately if able', () => { |
| b69ab31 | | | 96 | const wait = 300; |
| b69ab31 | | | 97 | const debounced = debounce(func1, wait, undefined, true); |
| b69ab31 | | | 98 | debounced(1, 'a'); |
| b69ab31 | | | 99 | expect(func1).toBeCalled(); |
| b69ab31 | | | 100 | }); |
| b69ab31 | | | 101 | it('should gate consecutive calls within the wait time', () => { |
| b69ab31 | | | 102 | const wait = 300; |
| b69ab31 | | | 103 | const debounced = debounce(func1, wait, undefined, true); |
| b69ab31 | | | 104 | debounced(1, 'a'); |
| b69ab31 | | | 105 | debounced(1, 'a'); |
| b69ab31 | | | 106 | debounced(1, 'a'); |
| b69ab31 | | | 107 | debounced(1, 'a'); |
| b69ab31 | | | 108 | debounced(1, 'a'); |
| b69ab31 | | | 109 | expect(func1).toBeCalledTimes(1); |
| b69ab31 | | | 110 | }); |
| b69ab31 | | | 111 | it('should call the function immediately after the wait time', () => { |
| b69ab31 | | | 112 | const wait = 300; |
| b69ab31 | | | 113 | const debounced = debounce(func1, wait, undefined, true); |
| b69ab31 | | | 114 | debounced(1, 'a'); |
| b69ab31 | | | 115 | debounced(1, 'a'); |
| b69ab31 | | | 116 | debounced(1, 'a'); |
| b69ab31 | | | 117 | debounced(1, 'a'); |
| b69ab31 | | | 118 | debounced(1, 'a'); |
| b69ab31 | | | 119 | expect(func1).toBeCalledTimes(1); |
| b69ab31 | | | 120 | jest.advanceTimersByTime(wait + BUFFER); |
| b69ab31 | | | 121 | debounced(1, 'a'); |
| b69ab31 | | | 122 | debounced(1, 'a'); |
| b69ab31 | | | 123 | debounced(1, 'a'); |
| b69ab31 | | | 124 | debounced(1, 'a'); |
| b69ab31 | | | 125 | debounced(1, 'a'); |
| b69ab31 | | | 126 | expect(func1).toBeCalledTimes(2); |
| b69ab31 | | | 127 | }); |
| b69ab31 | | | 128 | it('should extend the wait time whenever it is called within the wait time', () => { |
| b69ab31 | | | 129 | const wait = 300; |
| b69ab31 | | | 130 | const debounced = debounce(func1, wait, undefined, true); |
| b69ab31 | | | 131 | debounced(1, 'a'); |
| b69ab31 | | | 132 | expect(func1).toBeCalledTimes(1); |
| b69ab31 | | | 133 | jest.advanceTimersByTime(wait - BUFFER); |
| b69ab31 | | | 134 | debounced(1, 'a'); |
| b69ab31 | | | 135 | expect(func1).toBeCalledTimes(1); |
| b69ab31 | | | 136 | jest.advanceTimersByTime(wait - BUFFER); |
| b69ab31 | | | 137 | debounced(1, 'a'); |
| b69ab31 | | | 138 | expect(func1).toBeCalledTimes(1); |
| b69ab31 | | | 139 | jest.advanceTimersByTime(wait + BUFFER); |
| b69ab31 | | | 140 | debounced(1, 'a'); |
| b69ab31 | | | 141 | expect(func1).toBeCalledTimes(2); |
| b69ab31 | | | 142 | }); |
| b69ab31 | | | 143 | }); |
| b69ab31 | | | 144 | }); |