Skip to content

Commit 9fca689

Browse files
authored
test: advanced type check for ThemeName (#465)
* test: advanced type check for ThemeName * chore: import ComponentProps
1 parent c06b616 commit 9fca689

8 files changed

Lines changed: 74 additions & 5 deletions

File tree

bun.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/uniwind/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"circular:check": "dpdm --no-warning --no-tree -T --exit-code circular:1 'src/**/*.ts' 'src/**/*.tsx'",
2020
"test:native": "jest --config jest.config.native.js",
2121
"test:web": "jest --config jest.config.web.js",
22+
"test:types": "tsc --project tests/type-test/tsconfig.json",
2223
"test:e2e": "playwright test",
2324
"release": "release-it"
2425
},

packages/uniwind/src/hooks/useUniwind.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { UniwindListener } from '../core/listener'
55
import { ThemeName } from '../core/types'
66
import { StyleDependency } from '../types'
77

8-
export const useUniwind = () => {
8+
export const useUniwind = (): { theme: ThemeName; hasAdaptiveThemes: boolean } => {
99
const uniwindContext = useUniwindContext()
1010
const [theme, setTheme] = useState(Uniwind.currentTheme)
1111
const [hasAdaptiveThemes, setHasAdaptiveThemes] = useState(Uniwind.hasAdaptiveThemes)
@@ -26,7 +26,7 @@ export const useUniwind = () => {
2626
}, [uniwindContext])
2727

2828
return {
29-
theme: uniwindContext.scopedTheme ?? theme as ThemeName,
29+
theme: uniwindContext.scopedTheme ?? theme,
3030
hasAdaptiveThemes: uniwindContext.scopedTheme !== null ? false : hasAdaptiveThemes,
3131
}
3232
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export type Equal<A, B> = (<T>() => T extends A ? 1 : 2) extends (<T>() => T extends B ? 1 : 2) ? true
2+
: false
3+
4+
export type Expect<T extends true> = Equal<T, true>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import 'uniwind'
2+
3+
declare module 'uniwind' {
4+
export interface UniwindConfig {
5+
themes: readonly ['light', 'dark', 'premium', 'custom']
6+
}
7+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import type { ComponentProps } from 'react'
2+
import { ScopedTheme, type ThemeName, Uniwind, useUniwind } from 'uniwind'
3+
import { type Equal, type Expect } from './checks'
4+
5+
type ExpectedThemeName = 'light' | 'dark' | 'premium' | 'custom'
6+
7+
// ThemeName exported from uniwind
8+
type ThemeNameTest = Expect<Equal<ThemeName, ExpectedThemeName>>
9+
10+
// useUniwind.theme
11+
type UseUniwindThemeResult = ReturnType<typeof useUniwind>['theme']
12+
type UseUniwindTest = Expect<Equal<UseUniwindThemeResult, ExpectedThemeName>>
13+
14+
// Uniwind.currentTheme
15+
type UniwindCurrentThemeTest = Expect<Equal<typeof Uniwind.currentTheme, ExpectedThemeName>>
16+
17+
// Uniwind.themes
18+
type UniwindThemesTest = Expect<Equal<typeof Uniwind.themes, Array<ExpectedThemeName>>>
19+
20+
// Uniwind.setTheme
21+
type UniwindSetThemeParameter = Parameters<typeof Uniwind.setTheme>[0]
22+
type UniwindSetThemeTest = Expect<Equal<UniwindSetThemeParameter, ExpectedThemeName | 'system'>>
23+
24+
// Uniwind.updateCSSVariables
25+
type UniwindUpdateCSSVariablesThemeParameter = Parameters<typeof Uniwind.updateCSSVariables>[0]
26+
type UniwindUpdateCSSVariablesThemeTest = Expect<Equal<UniwindUpdateCSSVariablesThemeParameter, ExpectedThemeName>>
27+
28+
// ScopedTheme theme prop
29+
type ScopedThemeThemeProp = ComponentProps<typeof ScopedTheme>['theme']
30+
type ScopedThemeThemePropTest = Expect<Equal<ScopedThemeThemeProp, ExpectedThemeName>>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"noEmit": true,
5+
"rootDir": ".",
6+
"baseUrl": ".",
7+
"paths": {
8+
"uniwind": ["../../dist/module/index.d.ts"]
9+
}
10+
},
11+
"include": [
12+
"setup.d.ts",
13+
"*.ts"
14+
]
15+
}

turbo.json

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,29 @@
1414
"//#check:format": {},
1515
"circular:check": {},
1616
"precommit": {
17-
"dependsOn": ["lint", "check:typescript", "circular:check", "test:native", "test:web", "//#check:format"]
17+
"dependsOn": [
18+
"lint",
19+
"check:typescript",
20+
"circular:check",
21+
"test:native",
22+
"test:web",
23+
"test:types",
24+
"//#check:format"
25+
]
1826
},
1927
"test": {
20-
"dependsOn": ["test:native", "test:web", "test:e2e"]
28+
"dependsOn": ["test:native", "test:web", "test:types", "test:e2e"]
2129
},
2230
"test:native": {
2331
"cache": false
2432
},
2533
"test:web": {
2634
"cache": false
2735
},
36+
"test:types": {
37+
"dependsOn": ["build"],
38+
"cache": false
39+
},
2840
"test:e2e": {
2941
"cache": false
3042
}

0 commit comments

Comments
 (0)