2022-06-15 14:14:05 +00:00
|
|
|
|
/*
|
2024-09-09 13:57:16 +00:00
|
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-06-15 14:14:05 +00:00
|
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
2024-09-09 13:57:16 +00:00
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2022-06-15 14:14:05 +00:00
|
|
|
|
*/
|
|
|
|
|
|
2022-11-17 11:01:31 +00:00
|
|
|
|
import { renderHook } from "@testing-library/react-hooks";
|
2022-06-15 14:14:05 +00:00
|
|
|
|
|
|
|
|
|
import { useDebouncedCallback } from "../../src/hooks/spotlight/useDebouncedCallback";
|
|
|
|
|
|
|
|
|
|
describe("useDebouncedCallback", () => {
|
2022-11-17 11:01:31 +00:00
|
|
|
|
beforeAll(() => jest.useFakeTimers());
|
|
|
|
|
afterAll(() => jest.useRealTimers());
|
|
|
|
|
|
2023-02-13 11:39:16 +00:00
|
|
|
|
function render(enabled: boolean, callback: (...params: any[]) => void, params: any[]) {
|
2022-12-12 11:24:14 +00:00
|
|
|
|
return renderHook(({ enabled, callback, params }) => useDebouncedCallback(enabled, callback, params), {
|
|
|
|
|
initialProps: {
|
2022-11-17 11:01:31 +00:00
|
|
|
|
enabled,
|
|
|
|
|
callback,
|
|
|
|
|
params,
|
2022-12-12 11:24:14 +00:00
|
|
|
|
},
|
|
|
|
|
});
|
2022-11-17 11:01:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-15 14:14:05 +00:00
|
|
|
|
it("should be able to handle empty parameters", async () => {
|
2022-11-17 11:01:31 +00:00
|
|
|
|
// When
|
2023-02-13 11:39:16 +00:00
|
|
|
|
const params: any[] = [];
|
2022-06-15 14:14:05 +00:00
|
|
|
|
const callback = jest.fn();
|
2022-11-17 11:01:31 +00:00
|
|
|
|
render(true, callback, params);
|
|
|
|
|
jest.advanceTimersByTime(1);
|
|
|
|
|
|
|
|
|
|
// Then
|
|
|
|
|
expect(callback).toHaveBeenCalledTimes(0);
|
2022-06-15 14:14:05 +00:00
|
|
|
|
|
2022-11-17 11:01:31 +00:00
|
|
|
|
// When
|
|
|
|
|
jest.advanceTimersByTime(500);
|
2022-06-15 14:14:05 +00:00
|
|
|
|
|
2022-11-17 11:01:31 +00:00
|
|
|
|
// Then
|
2022-06-15 14:14:05 +00:00
|
|
|
|
expect(callback).toHaveBeenCalledTimes(1);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should call the callback with the parameters", async () => {
|
2022-11-17 11:01:31 +00:00
|
|
|
|
// When
|
|
|
|
|
const params = ["USER NAME"];
|
|
|
|
|
const callback = jest.fn();
|
|
|
|
|
render(true, callback, params);
|
|
|
|
|
jest.advanceTimersByTime(500);
|
|
|
|
|
|
|
|
|
|
// Then
|
|
|
|
|
expect(callback).toHaveBeenCalledTimes(1);
|
|
|
|
|
expect(callback).toHaveBeenCalledWith(...params);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should call the callback with the parameters when parameters change during the timeout", async () => {
|
|
|
|
|
// When
|
2022-06-15 14:14:05 +00:00
|
|
|
|
const params = ["USER NAME"];
|
|
|
|
|
const callback = jest.fn();
|
2022-11-17 11:01:31 +00:00
|
|
|
|
const { rerender } = render(true, callback, []);
|
2022-06-15 14:14:05 +00:00
|
|
|
|
|
2022-11-17 11:01:31 +00:00
|
|
|
|
jest.advanceTimersByTime(1);
|
|
|
|
|
rerender({ enabled: true, callback, params });
|
|
|
|
|
jest.advanceTimersByTime(500);
|
2022-06-15 14:14:05 +00:00
|
|
|
|
|
2022-11-17 11:01:31 +00:00
|
|
|
|
// Then
|
2022-06-15 14:14:05 +00:00
|
|
|
|
expect(callback).toHaveBeenCalledTimes(1);
|
|
|
|
|
expect(callback).toHaveBeenCalledWith(...params);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should handle multiple parameters", async () => {
|
2022-11-17 11:01:31 +00:00
|
|
|
|
// When
|
2022-06-15 14:14:05 +00:00
|
|
|
|
const params = [4, 8, 15, 16, 23, 42];
|
|
|
|
|
const callback = jest.fn();
|
2022-11-17 11:01:31 +00:00
|
|
|
|
const { rerender } = render(true, callback, []);
|
2022-06-15 14:14:05 +00:00
|
|
|
|
|
2022-11-17 11:01:31 +00:00
|
|
|
|
jest.advanceTimersByTime(1);
|
|
|
|
|
rerender({ enabled: true, callback, params });
|
|
|
|
|
jest.advanceTimersByTime(500);
|
2022-06-15 14:14:05 +00:00
|
|
|
|
|
2022-11-17 11:01:31 +00:00
|
|
|
|
// Then
|
2022-06-15 14:14:05 +00:00
|
|
|
|
expect(callback).toHaveBeenCalledTimes(1);
|
|
|
|
|
expect(callback).toHaveBeenCalledWith(...params);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should debounce quick changes", async () => {
|
2022-11-17 11:01:31 +00:00
|
|
|
|
// When
|
2022-06-15 14:14:05 +00:00
|
|
|
|
const queries = [
|
|
|
|
|
"U",
|
|
|
|
|
"US",
|
|
|
|
|
"USE",
|
|
|
|
|
"USER",
|
|
|
|
|
"USER ",
|
|
|
|
|
"USER N",
|
|
|
|
|
"USER NM",
|
|
|
|
|
"USER NMA",
|
|
|
|
|
"USER NM",
|
|
|
|
|
"USER N",
|
|
|
|
|
"USER NA",
|
|
|
|
|
"USER NAM",
|
|
|
|
|
"USER NAME",
|
|
|
|
|
];
|
|
|
|
|
const callback = jest.fn();
|
|
|
|
|
|
2022-11-17 11:01:31 +00:00
|
|
|
|
const { rerender } = render(true, callback, []);
|
|
|
|
|
jest.advanceTimersByTime(1);
|
|
|
|
|
|
|
|
|
|
for (const query of queries) {
|
|
|
|
|
rerender({ enabled: true, callback, params: [query] });
|
|
|
|
|
jest.advanceTimersByTime(50);
|
|
|
|
|
}
|
2022-06-15 14:14:05 +00:00
|
|
|
|
|
2022-11-17 11:01:31 +00:00
|
|
|
|
jest.advanceTimersByTime(500);
|
|
|
|
|
|
|
|
|
|
// Then
|
2022-06-15 14:14:05 +00:00
|
|
|
|
const query = queries[queries.length - 1];
|
|
|
|
|
expect(callback).toHaveBeenCalledTimes(1);
|
|
|
|
|
expect(callback).toHaveBeenCalledWith(query);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should not debounce slow changes", async () => {
|
2022-11-17 11:01:31 +00:00
|
|
|
|
// When
|
2022-06-15 14:14:05 +00:00
|
|
|
|
const queries = [
|
|
|
|
|
"U",
|
|
|
|
|
"US",
|
|
|
|
|
"USE",
|
|
|
|
|
"USER",
|
|
|
|
|
"USER ",
|
|
|
|
|
"USER N",
|
|
|
|
|
"USER NM",
|
|
|
|
|
"USER NMA",
|
|
|
|
|
"USER NM",
|
|
|
|
|
"USER N",
|
|
|
|
|
"USER NA",
|
|
|
|
|
"USER NAM",
|
|
|
|
|
"USER NAME",
|
|
|
|
|
];
|
|
|
|
|
const callback = jest.fn();
|
|
|
|
|
|
2022-11-17 11:01:31 +00:00
|
|
|
|
const { rerender } = render(true, callback, []);
|
|
|
|
|
jest.advanceTimersByTime(1);
|
|
|
|
|
for (const query of queries) {
|
|
|
|
|
rerender({ enabled: true, callback, params: [query] });
|
|
|
|
|
jest.advanceTimersByTime(200);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
jest.advanceTimersByTime(500);
|
2022-06-15 14:14:05 +00:00
|
|
|
|
|
2022-11-17 11:01:31 +00:00
|
|
|
|
// Then
|
2022-06-15 14:14:05 +00:00
|
|
|
|
const query = queries[queries.length - 1];
|
|
|
|
|
expect(callback).toHaveBeenCalledTimes(queries.length);
|
|
|
|
|
expect(callback).toHaveBeenCalledWith(query);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should not call the callback if it’s disabled", async () => {
|
2022-11-17 11:01:31 +00:00
|
|
|
|
// When
|
2022-06-15 14:14:05 +00:00
|
|
|
|
const queries = [
|
|
|
|
|
"U",
|
|
|
|
|
"US",
|
|
|
|
|
"USE",
|
|
|
|
|
"USER",
|
|
|
|
|
"USER ",
|
|
|
|
|
"USER N",
|
|
|
|
|
"USER NM",
|
|
|
|
|
"USER NMA",
|
|
|
|
|
"USER NM",
|
|
|
|
|
"USER N",
|
|
|
|
|
"USER NA",
|
|
|
|
|
"USER NAM",
|
|
|
|
|
"USER NAME",
|
|
|
|
|
];
|
|
|
|
|
const callback = jest.fn();
|
|
|
|
|
|
2022-11-17 11:01:31 +00:00
|
|
|
|
const { rerender } = render(false, callback, []);
|
|
|
|
|
jest.advanceTimersByTime(1);
|
|
|
|
|
for (const query of queries) {
|
|
|
|
|
rerender({ enabled: false, callback, params: [query] });
|
|
|
|
|
jest.advanceTimersByTime(200);
|
|
|
|
|
}
|
2022-06-15 14:14:05 +00:00
|
|
|
|
|
2022-11-17 11:01:31 +00:00
|
|
|
|
jest.advanceTimersByTime(500);
|
|
|
|
|
|
|
|
|
|
// Then
|
2022-06-15 14:14:05 +00:00
|
|
|
|
expect(callback).toHaveBeenCalledTimes(0);
|
|
|
|
|
});
|
|
|
|
|
});
|