Repository navigation
test: add unit and E2E tests for settings toggles #850
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
981c1c1
8f5c9a3
b98d698
ae3d2be
ac37a44
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import { test, expect } from "@playwright/test"; | ||
|
|
||
| test.describe("Settings Toggles", () => { | ||
| test.describe("Debug Mode Toggle", () => { | ||
| test("should toggle debug mode on and off", async ({ page }) => { | ||
| await page.goto("/dashboard/settings"); | ||
| await page.waitForLoadState("networkidle"); | ||
| await page.click("text=Advanced"); | ||
|
|
||
| const debugToggle = page.locator('[aria-label*="debug" i], [data-testid*="debug" i]').first(); | ||
|
|
||
| if (await debugToggle.isVisible({ timeout: 3000 }).catch(() => false)) { | ||
| const initialState = await debugToggle.isChecked(); | ||
| await debugToggle.click(); | ||
| await page.waitForTimeout(500); | ||
| const newState = await debugToggle.isChecked(); | ||
| expect(newState).not.toBe(initialState); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| test.describe("Sidebar Visibility Toggle", () => { | ||
| test("should toggle sidebar items visibility", async ({ page }) => { | ||
| await page.goto("/dashboard/settings"); | ||
| await page.waitForLoadState("networkidle"); | ||
| await page.click("text=General"); | ||
|
|
||
| const sidebarToggle = page | ||
| .locator('[aria-label*="sidebar" i], [data-testid*="sidebar" i]') | ||
| .first(); | ||
|
|
||
| if (await sidebarToggle.isVisible({ timeout: 3000 }).catch(() => false)) { | ||
| const initialState = await sidebarToggle.isChecked(); | ||
| await sidebarToggle.click(); | ||
| await page.waitForTimeout(500); | ||
| const newState = await sidebarToggle.isChecked(); | ||
| expect(newState).not.toBe(initialState); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| test.describe("Settings Persistence", () => { | ||
| test("should persist debug mode after page reload", async ({ page }) => { | ||
| await page.goto("/dashboard/settings"); | ||
| await page.waitForLoadState("networkidle"); | ||
| await page.click("text=Advanced"); | ||
|
|
||
| const debugToggle = page.locator('[aria-label*="debug" i], [data-testid*="debug" i]').first(); | ||
|
|
||
| if (await debugToggle.isVisible({ timeout: 3000 }).catch(() => false)) { | ||
| const wasChecked = await debugToggle.isChecked(); | ||
| await debugToggle.click(); | ||
| await page.waitForTimeout(500); | ||
| await page.reload(); | ||
| await page.waitForLoadState("networkidle"); | ||
| await page.click("text=Advanced"); | ||
| const isChecked = await debugToggle.isChecked(); | ||
| expect(isChecked).not.toBe(wasChecked); | ||
| } | ||
| }); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,67 @@ | ||||||||||||||||||||||
| import { describe, test } from "node:test"; | ||||||||||||||||||||||
| import assert from "node:assert/strict"; | ||||||||||||||||||||||
| import { getSettings, updateSettings } from "../../src/lib/localDb.ts"; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| describe("Settings API - debugMode and hiddenSidebarItems", () => { | ||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WARNING: Tests modify global settings state without resetting between tests - this can cause test interference and non-deterministic results. Consider adding a |
||||||||||||||||||||||
| describe("debugMode", () => { | ||||||||||||||||||||||
| test("PATCH with debugMode=true succeeds", async () => { | ||||||||||||||||||||||
| const result = await updateSettings({ debugMode: true }); | ||||||||||||||||||||||
| assert.strictEqual(result.ok, true, "updateSettings should return ok: true"); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const settings = getSettings(); | ||||||||||||||||||||||
| assert.strictEqual(settings.debugMode, true, "debugMode should be true"); | ||||||||||||||||||||||
|
Comment on lines
+8
to
+12
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test has two correctness issues that will cause it to fail:
This pattern is repeated throughout the file and should be corrected in all test cases.
Suggested change
|
||||||||||||||||||||||
| }); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| test("PATCH with debugMode=false succeeds", async () => { | ||||||||||||||||||||||
| const result = await updateSettings({ debugMode: false }); | ||||||||||||||||||||||
| assert.strictEqual(result.ok, true, "updateSettings should return ok: true"); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const settings = getSettings(); | ||||||||||||||||||||||
| assert.strictEqual(settings.debugMode, false, "debugMode should be false"); | ||||||||||||||||||||||
| }); | ||||||||||||||||||||||
| }); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| describe("hiddenSidebarItems", () => { | ||||||||||||||||||||||
| test("PATCH with hiddenSidebarItems=['translator'] succeeds", async () => { | ||||||||||||||||||||||
| const result = await updateSettings({ hiddenSidebarItems: ["translator"] }); | ||||||||||||||||||||||
| assert.strictEqual(result.ok, true, "updateSettings should return ok: true"); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const settings = getSettings(); | ||||||||||||||||||||||
| assert.deepStrictEqual( | ||||||||||||||||||||||
| settings.hiddenSidebarItems, | ||||||||||||||||||||||
| ["translator"], | ||||||||||||||||||||||
| "hiddenSidebarItems should contain translator" | ||||||||||||||||||||||
| ); | ||||||||||||||||||||||
| }); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| test("PATCH with empty hiddenSidebarItems succeeds", async () => { | ||||||||||||||||||||||
| const result = await updateSettings({ hiddenSidebarItems: [] }); | ||||||||||||||||||||||
| assert.strictEqual(result.ok, true, "updateSettings should return ok: true"); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const settings = getSettings(); | ||||||||||||||||||||||
| assert.deepStrictEqual( | ||||||||||||||||||||||
| settings.hiddenSidebarItems, | ||||||||||||||||||||||
| [], | ||||||||||||||||||||||
| "hiddenSidebarItems should be empty array" | ||||||||||||||||||||||
| ); | ||||||||||||||||||||||
| }); | ||||||||||||||||||||||
| }); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| describe("combined updates", () => { | ||||||||||||||||||||||
| test("PATCH with both debugMode and hiddenSidebarItems succeeds", async () => { | ||||||||||||||||||||||
| const result = await updateSettings({ | ||||||||||||||||||||||
| debugMode: true, | ||||||||||||||||||||||
| hiddenSidebarItems: ["translator"], | ||||||||||||||||||||||
| }); | ||||||||||||||||||||||
| assert.strictEqual(result.ok, true, "updateSettings should return ok: true"); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const settings = getSettings(); | ||||||||||||||||||||||
| assert.strictEqual(settings.debugMode, true, "debugMode should be true"); | ||||||||||||||||||||||
| assert.deepStrictEqual( | ||||||||||||||||||||||
| settings.hiddenSidebarItems, | ||||||||||||||||||||||
| ["translator"], | ||||||||||||||||||||||
| "hiddenSidebarItems should be updated" | ||||||||||||||||||||||
| ); | ||||||||||||||||||||||
| }); | ||||||||||||||||||||||
| }); | ||||||||||||||||||||||
| }); | ||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid using conditional logic (
if) to wrap assertions in E2E tests. If the element is not found, the test should fail to indicate a regression or UI change. Additionally, replacepage.waitForTimeoutwith Playwright's web-first assertions liketoBeChecked(), which automatically wait for the expected state and are more resilient to timing issues.