|
1 | 1 | import pkg from "../../package.json" assert { type: "json" }; |
2 | 2 | import { expect, test } from "../fixtures"; |
3 | 3 |
|
| 4 | +function toUnknownRecord(value: unknown): Record<string, unknown> { |
| 5 | + if (typeof value !== "object" || value === null) return {}; |
| 6 | + const result: Record<string, unknown> = {}; |
| 7 | + for (const [key, fieldValue] of Object.entries(value)) { |
| 8 | + result[key] = fieldValue; |
| 9 | + } |
| 10 | + return result; |
| 11 | +} |
| 12 | + |
| 13 | +function toBooleanRecord(value: unknown): Record<string, boolean> { |
| 14 | + const unknownRecord = toUnknownRecord(value); |
| 15 | + const result: Record<string, boolean> = {}; |
| 16 | + for (const [key, fieldValue] of Object.entries(unknownRecord)) { |
| 17 | + if (typeof fieldValue === "boolean") { |
| 18 | + result[key] = fieldValue; |
| 19 | + } |
| 20 | + } |
| 21 | + return result; |
| 22 | +} |
| 23 | + |
4 | 24 | test.describe("Popup", () => { |
5 | 25 | test("popup shows correct UI and updates storage when toggled", async ({ |
6 | 26 | page, |
@@ -31,11 +51,26 @@ test.describe("Popup", () => { |
31 | 51 | const storageAfter = await page.evaluate(async () => { |
32 | 52 | return browser.storage.local.get(null); |
33 | 53 | }); |
34 | | - const changedKeys = Object.keys(storageAfter).filter( |
35 | | - (key) => storageAfter[key] !== storageBefore[key], |
36 | | - ); |
| 54 | + expect(Object.hasOwn(storageBefore, "amg-state")).toBeTruthy(); |
| 55 | + expect(Object.hasOwn(storageAfter, "amg-state")).toBeTruthy(); |
| 56 | + |
| 57 | + const domain = await page.evaluate(async () => { |
| 58 | + const [tab] = await browser.tabs.query({ |
| 59 | + active: true, |
| 60 | + currentWindow: true, |
| 61 | + }); |
| 62 | + if (!tab?.url) return ""; |
| 63 | + return new URL(tab.url).host; |
| 64 | + }); |
| 65 | + |
| 66 | + const beforeAmgState = toUnknownRecord(storageBefore["amg-state"]); |
| 67 | + const afterAmgState = toUnknownRecord(storageAfter["amg-state"]); |
| 68 | + const beforeDomains = toBooleanRecord(beforeAmgState.domains); |
| 69 | + const afterDomains = toBooleanRecord(afterAmgState.domains); |
| 70 | + const beforeDomainValue = beforeDomains[domain]; |
| 71 | + const afterDomainValue = afterDomains[domain]; |
37 | 72 |
|
38 | | - expect(changedKeys.length).toBeGreaterThan(0); |
39 | | - expect(changedKeys.some((key) => storageAfter[key] === true)).toBeTruthy(); |
| 73 | + expect(beforeDomainValue === undefined || beforeDomainValue === false).toBeTruthy(); |
| 74 | + expect(afterDomainValue).toBeTruthy(); |
40 | 75 | }); |
41 | 76 | }); |
0 commit comments