Skip to content

Commit b988fad

Browse files
committed
tests: fix missing window.matchMedia in frontend tests
1 parent eb29941 commit b988fad

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

frontend/test/decrypt.spec.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, it, expect, beforeAll, afterEach, afterAll } from "vitest"
1+
import { describe, it, expect, beforeAll, afterEach, afterAll, vi } from "vitest"
22
import { cleanup, render, screen } from "@testing-library/react"
33
import { DecryptPaste } from "../components/DecryptPaste.js"
44

@@ -7,6 +7,7 @@ import { userEvent } from "@testing-library/user-event"
77
import { setupServer } from "msw/node"
88
import { http, HttpResponse } from "msw"
99
import { encodeKey, encrypt, genKey } from "../utils/encryption.js"
10+
import { stubBrowerFunctions, unStubBrowerFunctions } from "./testUtils.js"
1011

1112
describe("decrypt page", async () => {
1213
const scheme = "AES-GCM"
@@ -21,6 +22,7 @@ describe("decrypt page", async () => {
2122
)
2223

2324
beforeAll(() => {
25+
stubBrowerFunctions()
2426
server.listen()
2527
})
2628

@@ -30,15 +32,12 @@ describe("decrypt page", async () => {
3032
})
3133

3234
afterAll(() => {
35+
unStubBrowerFunctions()
3336
server.close()
3437
})
3538

3639
it("decrypt correctly", async () => {
37-
Object.defineProperty(window, "location", {
38-
configurable: true,
39-
enumerable: true,
40-
value: new URL(`https://example.com/e/abcd#${await encodeKey(key)}`),
41-
})
40+
vi.stubGlobal("location", new URL(`https://example.com/e/abcd#${await encodeKey(key)}`))
4241
global.URL.createObjectURL = () => ""
4342
render(<DecryptPaste />)
4443

frontend/test/index.spec.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, it, expect, beforeAll, afterEach, afterAll } from "vitest"
1+
import { describe, it, vi, expect, beforeAll, afterEach, afterAll } from "vitest"
22
import { cleanup, render, screen } from "@testing-library/react"
33
import { PasteBin } from "../components/PasteBin.js"
44

@@ -21,6 +21,7 @@ export const server = setupServer(
2121
)
2222

2323
beforeAll(() => {
24+
stubBrowerFunctions()
2425
server.listen()
2526
})
2627

@@ -30,6 +31,7 @@ afterEach(() => {
3031
})
3132

3233
afterAll(() => {
34+
unStubBrowerFunctions()
3335
server.close()
3436
})
3537

@@ -38,6 +40,7 @@ import { userEvent } from "@testing-library/user-event"
3840
import { PasteResponse } from "../../shared/interfaces.js"
3941
import { setupServer } from "msw/node"
4042
import { http, HttpResponse } from "msw"
43+
import { stubBrowerFunctions, unStubBrowerFunctions } from "./testUtils.js"
4144

4245
describe("Pastebin", () => {
4346
it("can upload", async () => {
@@ -74,11 +77,7 @@ describe("Pastebin", () => {
7477

7578
describe("Pastebin admin page", () => {
7679
it("renders admin page", async () => {
77-
Object.defineProperty(window, "location", {
78-
configurable: true,
79-
enumerable: true,
80-
value: new URL("https://example.com/abcd:xxxxxxxxx"),
81-
})
80+
vi.stubGlobal("location", new URL("https://example.com/abcd:xxxxxxxxx"))
8281
render(<PasteBin />)
8382

8483
const editor = screen.getByRole("textbox", { name: "Paste editor" })

frontend/test/testUtils.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { vi } from "vitest"
2+
3+
export function stubBrowerFunctions() {
4+
vi.spyOn(window, "matchMedia").mockImplementation((_query: string): MediaQueryList => {
5+
return {
6+
matches: false,
7+
addListener(_callback: ((this: MediaQueryList, ev: MediaQueryListEvent) => unknown) | null) {},
8+
addEventListener(_name: string, _listener: EventListenerOrEventListenerObject) {},
9+
removeEventListener(_name: string, _listener: EventListenerOrEventListenerObject) {},
10+
} as MediaQueryList
11+
})
12+
}
13+
14+
export function unStubBrowerFunctions() {
15+
vi.resetAllMocks()
16+
}

0 commit comments

Comments
 (0)