Skip to content

Commit d2c9379

Browse files
authored
test: fix dom-utils mock (@fehmer) (#7319)
- parital mock dom-utils module, prevents errors like [no "onWindowLoad" export is defined](https://github.com/monkeytypegame/monkeytype/actions/runs/20730640347/job/59517489709#step:11:616) - return empty `ElementsWithUtils` on `qsa` to prevent errors like `.hide is not a function`
1 parent 2dc99e9 commit d2c9379

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

frontend/__tests__/setup-tests.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { vi } from "vitest";
22
import $ from "jquery";
3-
import { ElementWithUtils } from "../src/ts/utils/dom";
3+
import { ElementsWithUtils, ElementWithUtils } from "../src/ts/utils/dom";
44

55
//@ts-expect-error add to global
66
global["$"] = $;
@@ -20,7 +20,7 @@ vi.mock("../src/ts/firebase", () => ({
2020
isAuthenticated: () => false,
2121
}));
2222

23-
vi.mock("../src/ts/utils/dom", () => {
23+
vi.mock("../src/ts/utils/dom", async (importOriginal) => {
2424
const createMockElement = (): ElementWithUtils => {
2525
return {
2626
disable: vi.fn().mockReturnThis(),
@@ -45,9 +45,9 @@ vi.mock("../src/ts/utils/dom", () => {
4545
setStyle: vi.fn().mockReturnThis(),
4646
getStyle: vi.fn().mockReturnValue({}),
4747
isFocused: vi.fn().mockReturnValue(false),
48-
qs: vi.fn().mockReturnValue(null),
48+
qs: vi.fn().mockImplementation(() => createMockElement()),
4949
qsr: vi.fn().mockImplementation(() => createMockElement()),
50-
qsa: vi.fn().mockReturnValue([]),
50+
qsa: vi.fn().mockImplementation(() => new ElementsWithUtils()),
5151
empty: vi.fn().mockReturnThis(),
5252
appendHtml: vi.fn().mockReturnThis(),
5353
append: vi.fn().mockReturnThis(),
@@ -71,10 +71,14 @@ vi.mock("../src/ts/utils/dom", () => {
7171
};
7272
};
7373

74+
const actual = await importOriginal();
75+
7476
return {
77+
//@ts-expect-error - mocking private method
78+
...actual,
7579
qsr: vi.fn().mockImplementation(() => createMockElement()),
7680
qs: vi.fn().mockImplementation(() => createMockElement()),
77-
qsa: vi.fn().mockReturnValue([]),
81+
qsa: vi.fn().mockImplementation(() => new ElementsWithUtils()),
7882
};
7983
});
8084

0 commit comments

Comments
 (0)