-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpopup.spec.ts
More file actions
76 lines (63 loc) · 2.52 KB
/
popup.spec.ts
File metadata and controls
76 lines (63 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import pkg from "../../package.json" assert { type: "json" };
import { expect, test } from "../fixtures";
function toUnknownRecord(value: unknown): Record<string, unknown> {
if (typeof value !== "object" || value === null) return {};
const result: Record<string, unknown> = {};
for (const [key, fieldValue] of Object.entries(value)) {
result[key] = fieldValue;
}
return result;
}
function toBooleanRecord(value: unknown): Record<string, boolean> {
const unknownRecord = toUnknownRecord(value);
const result: Record<string, boolean> = {};
for (const [key, fieldValue] of Object.entries(unknownRecord)) {
if (typeof fieldValue === "boolean") {
result[key] = fieldValue;
}
}
return result;
}
test.describe("Popup", () => {
test("popup shows correct UI and updates storage when toggled", async ({
page,
extensionId,
}) => {
await page.goto(`chrome-extension://${extensionId}/popup.html`);
await expect(page.getByRole("heading", { name: "Amgif" })).toBeVisible();
const offIcon = page.getByRole("img", { name: "amgiflol off icon" });
await expect(offIcon).toBeVisible();
await expect(offIcon).toHaveAttribute("src", /icon-off/i);
await expect(page.getByText(`v${pkg.version}`)).toBeVisible();
const storageBefore = await page.evaluate(async () => {
return browser.storage.local.get(null);
});
const toggle = page.getByLabel("Active:");
await expect(toggle).not.toBeChecked();
await page.getByText("Active:").click();
await expect(toggle).toBeChecked();
const logoAfter = page.getByRole("img", { name: "amgiflol on icon" });
await expect(logoAfter).toBeVisible();
const storageAfter = await page.evaluate(async () => {
return browser.storage.local.get(null);
});
expect(Object.hasOwn(storageBefore, "amg-state")).toBeTruthy();
expect(Object.hasOwn(storageAfter, "amg-state")).toBeTruthy();
const domain = await page.evaluate(async () => {
const [tab] = await browser.tabs.query({
active: true,
currentWindow: true,
});
if (!tab?.url) return "";
return new URL(tab.url).host;
});
const beforeAmgState = toUnknownRecord(storageBefore["amg-state"]);
const afterAmgState = toUnknownRecord(storageAfter["amg-state"]);
const beforeDomains = toBooleanRecord(beforeAmgState.domains);
const afterDomains = toBooleanRecord(afterAmgState.domains);
const beforeDomainValue = beforeDomains[domain];
const afterDomainValue = afterDomains[domain];
expect(beforeDomainValue === undefined || beforeDomainValue === false).toBeTruthy();
expect(afterDomainValue).toBeTruthy();
});
});