hezarfen_frontend
Clone
https://git.tahinli.com/Hezarfen/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import { fireEvent, render, screen } from "@solidjs/testing-library"; |
| 2 | import { |
| 3 | createTablePreferences, |
| 4 | defaultTablePreferences, |
| 5 | readTablePreferences, |
| 6 | } from "@/lib/table-preferences"; |
| 7 | |
| 8 | const key = "users"; |
| 9 | const storageKey = `hezarfen.table.${key}`; |
| 10 | |
| 11 | beforeEach(() => localStorage.clear()); |
| 12 | |
| 13 | test("reads saved visibility", () => { |
| 14 | localStorage.setItem( |
| 15 | storageKey, |
| 16 | JSON.stringify({ visibility: { email: false } }), |
| 17 | ); |
| 18 | |
| 19 | expect(readTablePreferences(key)).toEqual({ visibility: { email: false } }); |
| 20 | }); |
| 21 | |
| 22 | test("falls back to defaults for broken storage", () => { |
| 23 | localStorage.setItem(storageKey, "{broken"); |
| 24 | expect(readTablePreferences(key)).toEqual(defaultTablePreferences); |
| 25 | }); |
| 26 | |
| 27 | function PreferencesProbe() { |
| 28 | const controller = createTablePreferences(key); |
| 29 | return ( |
| 30 | <> |
| 31 | <output data-testid="preferences">{JSON.stringify(controller.preferences())}</output> |
| 32 | <button type="button" onClick={() => controller.setVisibility({ email: false })}>visibility</button> |
| 33 | </> |
| 34 | ); |
| 35 | } |
| 36 | |
| 37 | test("persists visibility changes", () => { |
| 38 | render(() => <PreferencesProbe />); |
| 39 | |
| 40 | fireEvent.click(screen.getByRole("button", { name: "visibility" })); |
| 41 | |
| 42 | expect(JSON.parse(localStorage.getItem(storageKey) ?? "{}")).toEqual({ |
| 43 | visibility: { email: false }, |
| 44 | }); |
| 45 | expect(screen.getByTestId("preferences").textContent).toContain('"email":false'); |
| 46 | }); |