|
| 1 | +/* |
| 2 | +Copyright 2025 New Vector Ltd. |
| 3 | +
|
| 4 | +SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial |
| 5 | +Please see LICENSE in the repository root for full details. |
| 6 | +*/ |
| 7 | + |
| 8 | +import { afterEach, beforeAll, describe, expect, test, vi } from "vitest"; |
| 9 | + |
| 10 | +import { shouldDisambiguate } from "./displayname"; |
| 11 | +import { alice } from "./test-fixtures"; |
| 12 | +import { mockMatrixRoom } from "./test"; |
| 13 | + |
| 14 | +// Ideally these tests would be in ./displayname.test.ts but I can't figure out how to |
| 15 | +// just spy on the removeHiddenChars() function without impacting the other tests. |
| 16 | +// So, these tests are in this separate test file. |
| 17 | +vi.mock("matrix-js-sdk/src/utils"); |
| 18 | + |
| 19 | +describe("shouldDisambiguate", () => { |
| 20 | + // eslint-disable-next-line @typescript-eslint/consistent-type-imports |
| 21 | + let jsUtils: typeof import("matrix-js-sdk/src/utils"); |
| 22 | + |
| 23 | + beforeAll(async () => { |
| 24 | + jsUtils = await import("matrix-js-sdk/src/utils"); |
| 25 | + vi.spyOn(jsUtils, "removeHiddenChars").mockImplementation((str) => str); |
| 26 | + }); |
| 27 | + afterEach(() => { |
| 28 | + vi.clearAllMocks(); |
| 29 | + }); |
| 30 | + |
| 31 | + test("should only call removeHiddenChars once for a single displayname", () => { |
| 32 | + const room = mockMatrixRoom({}); |
| 33 | + shouldDisambiguate(alice, [], room); |
| 34 | + expect(jsUtils.removeHiddenChars).toHaveBeenCalledTimes(1); |
| 35 | + for (let i = 0; i < 10; i++) { |
| 36 | + shouldDisambiguate(alice, [], room); |
| 37 | + } |
| 38 | + expect(jsUtils.removeHiddenChars).toHaveBeenCalledTimes(1); |
| 39 | + }); |
| 40 | +}); |
0 commit comments