Repository navigation
fix: follow a theme another tab picks #1524
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
Open
AdilAzhariOmsan
wants to merge
1
commit into
dev-null-sh:production
Choose a base branch
from
AdilAzhariOmsan:fix/appearance-across-tabs
base: production
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+132
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| /// <reference types="node" /> | ||
| import assert from 'node:assert/strict'; | ||
| import * as React from 'react'; | ||
| import { beforeEach, test, vi } from 'vite-plus/test'; | ||
|
|
||
| const rendered = vi.fn(); | ||
| vi.doMock('react', () => ({ | ||
| ...React, | ||
| useSyncExternalStore: ( | ||
| subscribe: (listener: () => void) => () => void, | ||
| snapshot: () => unknown, | ||
| ) => { | ||
| subscribe(rendered); | ||
| return snapshot(); | ||
| }, | ||
| })); | ||
|
|
||
| const saved = new Map<string, string>(); | ||
| const storageListeners: ((event: { key: string | null }) => void)[] = []; | ||
| const systemThemeListeners: (() => void)[] = []; | ||
| let systemIsDark = false; | ||
| const classes = new Set<string>(); | ||
|
|
||
| const localStorage = { | ||
| getItem: (key: string) => saved.get(key) ?? null, | ||
| setItem: (key: string, value: string) => saved.set(key, value), | ||
| removeItem: (key: string) => saved.delete(key), | ||
| }; | ||
|
|
||
| Object.assign(globalThis, { | ||
| localStorage, | ||
| window: { | ||
| localStorage, | ||
| matchMedia: () => ({ | ||
| get matches() { | ||
| return systemIsDark; | ||
| }, | ||
| addEventListener: (_type: string, listener: () => void) => | ||
| systemThemeListeners.push(listener), | ||
| }), | ||
| addEventListener: ( | ||
| type: string, | ||
| listener: (event: { key: string | null }) => void, | ||
| ) => { | ||
| if (type === 'storage') { | ||
| storageListeners.push(listener); | ||
| } | ||
| }, | ||
| }, | ||
| document: { | ||
| cookie: '', | ||
| documentElement: { | ||
| classList: { | ||
| toggle: (name: string, on: boolean) => | ||
| on ? classes.add(name) : classes.delete(name), | ||
| }, | ||
| style: {}, | ||
| }, | ||
| querySelectorAll: () => [], | ||
| }, | ||
| getComputedStyle: () => ({ getPropertyValue: () => '' }), | ||
| }); | ||
|
|
||
| const { initializeTheme, useAppearance } = | ||
| await import('../../resources/js/hooks/use-appearance'); | ||
|
|
||
| saved.set('appearance', 'light'); | ||
| initializeTheme(); | ||
|
|
||
| beforeEach(() => { | ||
| rendered.mockClear(); | ||
| }); | ||
|
|
||
| test('follows a theme another tab saves', () => { | ||
| useAppearance(); | ||
| saved.set('appearance', 'dark'); | ||
| storageListeners.forEach((listener) => listener({ key: 'appearance' })); | ||
|
|
||
| assert.ok(classes.has('dark')); | ||
| assert.equal(useAppearance().appearance, 'dark'); | ||
| assert.ok(rendered.mock.calls.length > 0); | ||
| }); | ||
|
|
||
| test('falls back to the system theme when another tab wipes local storage', () => { | ||
| systemIsDark = false; | ||
| saved.clear(); | ||
| storageListeners.forEach((listener) => listener({ key: null })); | ||
|
|
||
| assert.ok(!classes.has('dark')); | ||
| assert.equal(useAppearance().appearance, 'system'); | ||
| }); | ||
|
|
||
| test('ignores what other tabs save under other keys', () => { | ||
| saved.set('appearance', 'dark'); | ||
| storageListeners.forEach((listener) => listener({ key: 'chat-dock' })); | ||
|
|
||
| assert.equal(useAppearance().appearance, 'system'); | ||
| assert.equal(rendered.mock.calls.length, 0); | ||
| }); | ||
|
|
||
| test('re-renders the resolved theme when the system one changes', () => { | ||
| saved.set('appearance', 'system'); | ||
| storageListeners.forEach((listener) => listener({ key: 'appearance' })); | ||
| useAppearance(); | ||
| rendered.mockClear(); | ||
|
|
||
| systemIsDark = true; | ||
| systemThemeListeners.forEach((listener) => listener()); | ||
|
|
||
| assert.ok(classes.has('dark')); | ||
| assert.ok(rendered.mock.calls.length > 0); | ||
| assert.equal(useAppearance().resolvedAppearance, 'dark'); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Script executed:
Repository: dev-null-sh/dev-null.sh
Length of output: 13435
Script executed:
Repository: dev-null-sh/dev-null.sh
Length of output: 6513
Web query:
vite-plus 1.5.0 vite-plus/test vitest import directly node test runner internal stateResult:
Script executed:
Repository: dev-null-sh/dev-null.sh
Length of output: 3915
Web query:
Vitest 5.1.0 "Vitest failed to access its internal state" "imported directly"Result:
Script executed:
Repository: dev-null-sh/dev-null.sh
Length of output: 13437
Script executed:
Repository: dev-null-sh/dev-null.sh
Length of output: 5046
Run the appearance test with Vitest.
test:unitselects this.tsfile with Node, which does not initialize the Vitest mocker. This can fail the frontend CI job.Rename the file to
Appearance.test.tsxand run it separately with Vitest:Update the test script