Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 20 additions & 33 deletions e2e-tests/persistence.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,31 @@ import { expect, test, type Page } from "@playwright/test";
const storageKey = "eslint-explorer";

async function getPersistedJavaScriptCode(page: Page): Promise<string> {
return page.evaluate(key => {
const storedValue = window.localStorage.getItem(key);
const storedValue = await page.localStorage.getItem(storageKey);

if (!storedValue) {
return "";
}
if (!storedValue) {
return "";
}

return JSON.parse(storedValue).state.code.javascript;
}, storageKey);
return JSON.parse(storedValue).state.code.javascript;
}

async function getPersistedExplorerState(page: Page): Promise<string> {
return page.evaluate(key => {
const storedValue = window.localStorage.getItem(key);
const storedValue = await page.localStorage.getItem(storageKey);

if (!storedValue) {
throw new Error("No persisted state found in localStorage");
}
if (!storedValue) {
throw new Error("No persisted state found in localStorage");
}

return storedValue;
}, storageKey);
return storedValue;
}

async function getStoredHashValue(page: Page): Promise<string> {
return page.evaluate(key => {
return (
new URLSearchParams(window.location.hash.slice(1)).get(key) ?? ""
);
}, storageKey);
function getStoredHashValue(page: Page): string {
return (
new URLSearchParams(new URL(page.url()).hash.slice(1)).get(
storageKey,
) ?? ""
);
}

//-----------------------------------------------------------------------------
Expand All @@ -67,11 +63,9 @@ test("should persist unicode code safely in the URL hash", async ({ page }) => {
await expect.poll(() => getPersistedJavaScriptCode(page)).toBe(unicodeCode);
await expect.poll(() => getStoredHashValue(page)).toContain("v2.");

const persistedHash = await page.evaluate(() => window.location.hash);
const persistedHash = new URL(page.url()).hash;

await page.evaluate(key => {
window.localStorage.removeItem(key);
}, storageKey);
await page.localStorage.removeItem(storageKey);
await page.goto(`/${persistedHash}`);

await expect(codeEditor).toContainText(unicodeCode);
Expand Down Expand Up @@ -104,9 +98,7 @@ test("should still load state from legacy hash links", async ({ page }) => {
[storageKey, persistedValue],
);

await page.evaluate(key => {
window.localStorage.removeItem(key);
}, storageKey);
await page.localStorage.removeItem(storageKey);
await page.goto(`/#${legacyHash}`);

await expect(codeEditor).toContainText(legacyCode);
Expand Down Expand Up @@ -152,12 +144,7 @@ test("should fall back to localStorage when a v2 hash is malformed", async ({
return searchParams.toString();
}, storageKey);

await page.evaluate(
([key, value]) => {
window.localStorage.setItem(key, value);
},
[storageKey, persistedValue],
);
await page.localStorage.setItem(storageKey, persistedValue);
await page.goto(`/#${malformedHash}`);

await expect(codeEditor).toContainText(fallbackCode);
Expand Down
Loading