@zendesk/react-measure-timing-hooks
Version:
react hooks for measuring time to interactive and time to render of components
115 lines • 5.83 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const vitest_1 = require("vitest");
const debounce_1 = require("./debounce");
(0, vitest_1.describe)('debounce', () => {
let mockFn;
let optionsRef;
let debouncedFn;
(0, vitest_1.beforeEach)(() => {
vitest_1.vitest.useFakeTimers();
mockFn = vitest_1.vitest.fn();
optionsRef = {
fn: mockFn,
debounceMs: 100,
};
debouncedFn = (0, debounce_1.debounce)(optionsRef);
});
(0, vitest_1.afterEach)(() => {
vitest_1.vitest.useRealTimers();
});
(0, vitest_1.it)('should call the debounced function once after the debounce period', () => {
debouncedFn('test');
(0, vitest_1.expect)(mockFn).not.toHaveBeenCalled();
vitest_1.vitest.advanceTimersByTime(100);
(0, vitest_1.expect)(mockFn).toHaveBeenCalledWith('test', debounce_1.DebounceReason);
});
(0, vitest_1.it)('should not call the debounced function if it is cancelled before the debounce period', () => {
debouncedFn('test');
(0, vitest_1.expect)(mockFn).not.toHaveBeenCalled();
debouncedFn.cancel();
vitest_1.vitest.advanceTimersByTime(100);
(0, vitest_1.expect)(mockFn).not.toHaveBeenCalled();
});
(0, vitest_1.it)('should call the debounced function with the TimeoutReason if a timeout is set', () => {
debouncedFn = (0, debounce_1.debounce)({ ...optionsRef, timeoutMs: 200 });
debouncedFn('test');
(0, vitest_1.expect)(mockFn).not.toHaveBeenCalled();
// keep timeout after regular debounce:
mockFn.mockReturnValue(true);
vitest_1.vitest.advanceTimersByTime(200);
(0, vitest_1.expect)(mockFn).toHaveBeenNthCalledWith(1, 'test', debounce_1.DebounceReason);
(0, vitest_1.expect)(mockFn).toHaveBeenNthCalledWith(2, 'test', debounce_1.TimeoutReason);
});
(0, vitest_1.it)('should reset the debounce function state', () => {
debouncedFn('test');
debouncedFn.reset();
(0, vitest_1.expect)(debouncedFn.getIsScheduled()).toBe(false);
(0, vitest_1.expect)(debouncedFn.reset()).toBeUndefined();
vitest_1.vitest.advanceTimersByTime(100);
(0, vitest_1.expect)(mockFn).not.toHaveBeenCalled();
});
(0, vitest_1.it)('should manually flush the debounced function', () => {
debouncedFn('test');
const wasFlushed = debouncedFn.flush();
(0, vitest_1.expect)(wasFlushed).toBe(true);
(0, vitest_1.expect)(mockFn).toHaveBeenCalledWith('test', 'manual');
});
(0, vitest_1.it)('should not flush if there is nothing scheduled', () => {
const wasFlushed = debouncedFn.flush();
(0, vitest_1.expect)(wasFlushed).toBe(false);
(0, vitest_1.expect)(mockFn).not.toHaveBeenCalled();
});
(0, vitest_1.it)('should keep the timeout if the debounced function returns true', () => {
mockFn.mockReturnValue(true);
debouncedFn = (0, debounce_1.debounce)({ ...optionsRef, timeoutMs: 200 });
debouncedFn('test');
vitest_1.vitest.advanceTimersByTime(100);
(0, vitest_1.expect)(mockFn).toHaveBeenCalledWith('test', debounce_1.DebounceReason);
(0, vitest_1.expect)(debouncedFn.getIsScheduled()).toBe(true);
});
(0, vitest_1.it)('should clear the timeout if the debounced function returns false', () => {
mockFn.mockReturnValue(false);
debouncedFn = (0, debounce_1.debounce)({ ...optionsRef, timeoutMs: 200 });
debouncedFn('test');
vitest_1.vitest.advanceTimersByTime(100);
(0, vitest_1.expect)(mockFn).toHaveBeenCalledWith('test', debounce_1.DebounceReason);
(0, vitest_1.expect)(debouncedFn.getIsScheduled()).toBe(false);
});
(0, vitest_1.it)('should pass FlushReason as the last argument to the debounced function', () => {
debouncedFn('test');
vitest_1.vitest.advanceTimersByTime(100);
(0, vitest_1.expect)(mockFn).toHaveBeenCalledWith('test', debounce_1.DebounceReason);
});
(0, vitest_1.it)('should correctly handle optionsRef being mutated after the debounced function was created', () => {
debouncedFn('test');
const otherMockFn = vitest_1.vitest.fn();
optionsRef.debounceMs = 200;
optionsRef.timeoutMs = 300;
optionsRef.fn = otherMockFn;
debouncedFn('test2');
vitest_1.vitest.advanceTimersByTime(100);
(0, vitest_1.expect)(mockFn).not.toHaveBeenCalled();
(0, vitest_1.expect)(otherMockFn).not.toHaveBeenCalled();
vitest_1.vitest.advanceTimersByTime(100);
(0, vitest_1.expect)(mockFn).not.toHaveBeenCalled();
(0, vitest_1.expect)(otherMockFn).toHaveBeenLastCalledWith('test2', debounce_1.DebounceReason);
otherMockFn.mockReturnValue(true);
debouncedFn('test3');
vitest_1.vitest.advanceTimersByTime(200);
(0, vitest_1.expect)(mockFn).not.toHaveBeenCalled();
(0, vitest_1.expect)(otherMockFn).toHaveBeenLastCalledWith('test3', debounce_1.DebounceReason);
vitest_1.vitest.advanceTimersByTime(100);
(0, vitest_1.expect)(otherMockFn).toHaveBeenLastCalledWith('test3', debounce_1.TimeoutReason);
});
(0, vitest_1.it)('should correctly call the underlying function once, even when run multiple times in a row with different arguments', () => {
debouncedFn('test1');
debouncedFn('test2');
debouncedFn('test3');
(0, vitest_1.expect)(mockFn).not.toHaveBeenCalled();
vitest_1.vitest.advanceTimersByTime(100);
(0, vitest_1.expect)(mockFn).toHaveBeenCalledTimes(1);
(0, vitest_1.expect)(mockFn).toHaveBeenCalledWith('test3', debounce_1.DebounceReason);
});
});
//# sourceMappingURL=debounce.test.js.map
;