Skip to content

Commit

Permalink
make findElement public again, fix localStorage access in test
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseditson committed Jun 18, 2024
1 parent c7a9972 commit dc63891
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
11 changes: 4 additions & 7 deletions src/upd8.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,7 @@ export class Upd8View<State, Event> {
};
}

private _upd8_findElement(
el: string | HTMLElement,
selector?: string
): HTMLElement {
public findElement(el: string | HTMLElement, selector?: string): HTMLElement {
let htmlEl;
const isSel = typeof el === "string";
if (isSel) {
Expand Down Expand Up @@ -281,7 +278,7 @@ export class Upd8View<State, Event> {
this.setContent(el, value);
}
} else {
const htmlEl = this._upd8_findElement(el);
const htmlEl = this.findElement(el);
if (Array.isArray(value)) {
htmlEl?.replaceChildren(...value);
} else {
Expand All @@ -294,7 +291,7 @@ export class Upd8View<State, Event> {
data: Record<string, string>,
selector?: string
) {
const htmlEl = this._upd8_findElement(el, selector);
const htmlEl = this.findElement(el, selector);
Object.entries(data).forEach((d) => {
const [k, v] = d;
htmlEl.dataset[k] = v;
Expand All @@ -305,7 +302,7 @@ export class Upd8View<State, Event> {
data: Record<string, string>,
selector?: string
) {
const htmlEl = this._upd8_findElement(el, selector);
const htmlEl = this.findElement(el, selector);
Object.entries(data).forEach((d) => {
const [k, v] = d;
if (typeof v === "boolean") {
Expand Down
33 changes: 24 additions & 9 deletions tests/todo-mvc-app.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,17 +441,16 @@ async function createDefaultTodos(page: Page) {
}
}

const getTODOsFromLocalStorage = () => {
try {
return JSON.parse(localStorage.getItem("todos-upd8")!);
} catch (e) {
console.error(`FAILED QUERYING LOCALSTORAGE: ${e}`);
return { todos: [] };
}
};

async function checkNumberOfTodosInLocalStorage(page: Page, expected: number) {
return await page.waitForFunction(async (e) => {
const getTODOsFromLocalStorage = () => {
try {
return JSON.parse(localStorage.getItem("todos-upd8")!);
} catch (e) {
console.error(`FAILED QUERYING LOCALSTORAGE: ${e}`);
return { todos: [] };
}
};
return getTODOsFromLocalStorage().length === e;
}, expected);
}
Expand All @@ -461,6 +460,14 @@ async function checkNumberOfCompletedTodosInLocalStorage(
expected: number
) {
return await page.waitForFunction(async (e) => {
const getTODOsFromLocalStorage = () => {
try {
return JSON.parse(localStorage.getItem("todos-upd8")!);
} catch (e) {
console.error(`FAILED QUERYING LOCALSTORAGE: ${e}`);
return { todos: [] };
}
};
return (
getTODOsFromLocalStorage().todos.filter((todo: any) => todo.completed)
.length === e
Expand All @@ -470,6 +477,14 @@ async function checkNumberOfCompletedTodosInLocalStorage(

async function checkTodosInLocalStorage(page: Page, title: string) {
return await page.waitForFunction(async (t) => {
const getTODOsFromLocalStorage = () => {
try {
return JSON.parse(localStorage.getItem("todos-upd8")!);
} catch (e) {
console.error(`FAILED QUERYING LOCALSTORAGE: ${e}`);
return { todos: [] };
}
};
return getTODOsFromLocalStorage()
.todos.map((todo: any) => todo.title)
.includes(t);
Expand Down

0 comments on commit dc63891

Please sign in to comment.