Skip to content

Commit

Permalink
Wait for the switch knob to render
Browse files Browse the repository at this point in the history
  • Loading branch information
obulat committed Oct 8, 2024
1 parent a8b1e9a commit 81dc5f2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
34 changes: 28 additions & 6 deletions frontend/test/playwright/e2e/preferences.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type Page, test } from "@playwright/test"

import { preparePageForTests } from "~~/test/playwright/utils/navigation"
import { preparePageForTests, sleep } from "~~/test/playwright/utils/navigation"

import featureData from "~~/feat/feature-flags.json"

Expand Down Expand Up @@ -75,6 +75,30 @@ const getSwitchableInput = async (
return page.getByRole("checkbox", { name, checked }).first()
}

const toggleChecked = async (
page: Page,
name: string,
originalChecked: boolean | undefined
) => {
const featureFlag = await getSwitchableInput(page, name, originalChecked)
await featureFlag.setChecked(!originalChecked)

// If the switch knob wasn't rendered yet, wait for it to be rendered.
await page.evaluate(
async ([name, className, sleep]) => {
const knobClasses = document
.querySelector(`${name}`)
?.parentElement?.querySelector("span")?.className

if (!knobClasses?.includes(className)) {
await sleep(200)
}
},
// The knob is `bg-default` when the feature is off and `bg-tertiary` when the feature is on.
[name, !originalChecked ? "bg-tertiary" : "bg-default", sleep]
)
}

test.describe("switchable features", () => {
test.beforeEach(async ({ page }) => {
await preparePageForTests(page, "xl")
Expand All @@ -86,8 +110,8 @@ test.describe("switchable features", () => {

test(`can switch ${name} from ${defaultState}`, async ({ page }) => {
await page.goto(`/preferences`)
const featureFlag = await getSwitchableInput(page, name, checked)
await featureFlag.setChecked(!checked)

await toggleChecked(page, name, checked)

await expectCheckboxState(page, name, !checked)
})
Expand All @@ -96,10 +120,8 @@ test.describe("switchable features", () => {
page,
}) => {
await page.goto(`/preferences`)
const featureFlag = await getSwitchableInput(page, name, checked)

await featureFlag.setChecked(!checked)
// Ensure the feature flag is updated
await toggleChecked(page, name, checked)
await expectCheckboxState(page, name, !checked)

// Cookies are not visible to the user, so we are checking that the feature flag
Expand Down
1 change: 0 additions & 1 deletion frontend/test/playwright/utils/assertions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export const expectCheckboxState = async (
if (checked) {
await expect(checkbox).toBeChecked()
} else {
console.log(`Expecting checkbox ${name} checked state to be ${checked}`)
await expect(checkbox).not.toBeChecked()
}
}

0 comments on commit 81dc5f2

Please sign in to comment.