@heliosgraphics/ui 2.0.1-alpha.10 → 2.0.1-alpha.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { describe, expect, it } from "bun:test"
|
|
2
|
+
import type { HeliosFixedThemeType } from "../../types/themes"
|
|
3
|
+
import { code } from "./Setup.utils"
|
|
4
|
+
|
|
5
|
+
describe("Setup", () => {
|
|
6
|
+
it("should execute when serialized into an inline script", () => {
|
|
7
|
+
const rootDataset: Record<string, string> = {}
|
|
8
|
+
const localStorageValues = new Map<string, string>()
|
|
9
|
+
const listeners: Record<string, (event: { matches: boolean }) => void> = {}
|
|
10
|
+
|
|
11
|
+
globalThis.__theme = "light" as HeliosFixedThemeType
|
|
12
|
+
globalThis.__onThemeChange = undefined
|
|
13
|
+
globalThis.__setPreferredTheme = (): void => {}
|
|
14
|
+
globalThis.document = {
|
|
15
|
+
documentElement: {
|
|
16
|
+
dataset: rootDataset,
|
|
17
|
+
},
|
|
18
|
+
} as Document
|
|
19
|
+
globalThis.localStorage = {
|
|
20
|
+
getItem: (key: string): string | null => localStorageValues.get(key) ?? null,
|
|
21
|
+
setItem: (key: string, value: string): void => {
|
|
22
|
+
localStorageValues.set(key, value)
|
|
23
|
+
},
|
|
24
|
+
} as Storage
|
|
25
|
+
globalThis.matchMedia = ((query: string): MediaQueryList => {
|
|
26
|
+
expect(query).toBe("(prefers-color-scheme: dark)")
|
|
27
|
+
return {
|
|
28
|
+
matches: true,
|
|
29
|
+
addEventListener: (type: string, listener: (event: { matches: boolean }) => void): void => {
|
|
30
|
+
listeners[type] = listener
|
|
31
|
+
},
|
|
32
|
+
} as MediaQueryList
|
|
33
|
+
}) as typeof globalThis.matchMedia
|
|
34
|
+
|
|
35
|
+
new Function(`return (${code.toString()})("system", undefined)`)()
|
|
36
|
+
|
|
37
|
+
expect(globalThis.__theme).toBe("dark")
|
|
38
|
+
expect(rootDataset["theme"]).toBe("dark")
|
|
39
|
+
expect(typeof listeners["change"]).toBe("function")
|
|
40
|
+
|
|
41
|
+
const changeListener = listeners["change"]
|
|
42
|
+
expect(changeListener).toBeDefined()
|
|
43
|
+
if (!changeListener) {
|
|
44
|
+
throw new Error("expected change listener to be registered")
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
changeListener({ matches: false })
|
|
48
|
+
|
|
49
|
+
expect(globalThis.__theme).toBe("light")
|
|
50
|
+
expect(rootDataset["theme"]).toBe("light")
|
|
51
|
+
expect(localStorageValues.get("theme")).toBe("light")
|
|
52
|
+
})
|
|
53
|
+
})
|
|
@@ -1,18 +1,20 @@
|
|
|
1
1
|
import type { HeliosFixedThemeType, HeliosThemeType } from "../../types/themes"
|
|
2
2
|
|
|
3
|
-
const setTheme = (newTheme: HeliosFixedThemeType): void => {
|
|
4
|
-
globalThis.__theme = newTheme
|
|
5
|
-
document.documentElement.dataset["theme"] = newTheme
|
|
6
|
-
globalThis.__onThemeChange?.(newTheme)
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
const handleDarkModeChange = (event: MediaQueryListEvent): void => {
|
|
10
|
-
globalThis.__setPreferredTheme(event.matches ? "dark" : "light")
|
|
11
|
-
}
|
|
12
|
-
|
|
13
3
|
export const code = (theme: HeliosThemeType = "system", fixedTheme?: HeliosFixedThemeType): void => {
|
|
14
4
|
globalThis.__onThemeChange = function (_theme: HeliosFixedThemeType): void {}
|
|
15
5
|
|
|
6
|
+
/* eslint-disable-next-line unicorn/consistent-function-scoping */
|
|
7
|
+
const setTheme = (newTheme: HeliosFixedThemeType): void => {
|
|
8
|
+
globalThis.__theme = newTheme
|
|
9
|
+
document.documentElement.dataset["theme"] = newTheme
|
|
10
|
+
globalThis.__onThemeChange?.(newTheme)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/* eslint-disable-next-line unicorn/consistent-function-scoping */
|
|
14
|
+
const handleDarkModeChange = (event: MediaQueryListEvent): void => {
|
|
15
|
+
globalThis.__setPreferredTheme(event.matches ? "dark" : "light")
|
|
16
|
+
}
|
|
17
|
+
|
|
16
18
|
const isLocked: boolean = fixedTheme !== undefined || theme !== "system"
|
|
17
19
|
|
|
18
20
|
if (fixedTheme !== undefined && theme !== "system") {
|