Skip to content

Commit

Permalink
Add yarn tsd test step + small theme type tweaks (#1387)
Browse files Browse the repository at this point in the history
* Intentionally break type in index.d.ts

* Add tests for Theme.components.<Component>.baseStyle type

* Revert intentional breakage of type, also baseStyle should probably be a Partial to match appearances/sizes types

* Widen Color type to allow string arrays
  • Loading branch information
brandongregoryscott authored Feb 3, 2022
1 parent dc7492d commit d75c9ad
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ on:
branches:
- master
push:
branches:
- master
branches: ['*']

jobs:
test:
Expand Down
6 changes: 3 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ export type ComponentStyles<T extends Components = Components> = {
}

export interface Theme<TComponents extends Components = Components> {
colors: { [color: string]: Color<string | { [group: string]: Color }> }
colors: { [color: string]: Color<string | string[] | { [group: string]: Color }> }
fills: { [fill: string]: Fill }
intents: { [intent: string]: Intent }
fontFamilies: FontFamilies
Expand All @@ -323,7 +323,7 @@ export interface Theme<TComponents extends Components = Components> {
components: Partial<ComponentStyles<TComponents>>
}

export type Color<T extends string | { [group: string]: Color } = string> = T
export type Color<T extends string | string[] | { [group: string]: Color } = string> = T
export interface Fill {
backgroundColor: string
color: string
Expand Down Expand Up @@ -380,7 +380,7 @@ export interface DefaultTheme extends Theme {
intents: { [intent in DefaultThemeIntent]: Intent }
components: {
[Component in Components]: {
baseStyle: StyleProps<Component>
baseStyle: Partial<StyleProps<Component>>
appearances: Record<string & ComponentAppearances<Component>, Partial<StyleProps<Component>>>
sizes: Record<Size & ComponentSizes<Component>, Partial<StyleProps<Component>>>
}
Expand Down
32 changes: 30 additions & 2 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { expectAssignable, expectType, expectError } from 'tsd'
import { defaultTheme, mergeTheme, StyleProps, Intent, Theme, Fill, Partial, Pick } from '.'
import { defaultTheme, mergeTheme, StyleProps, Intent, Theme, Fill, Partial, Pick, Color } from '.'

interface ThemeOverrides extends Partial<Pick<Theme, 'fills' | 'components'>> {
interface ThemeOverrides extends Partial<Pick<Theme, 'colors' | 'fills' | 'components'>> {
colors: {
speakers: Color<string[]>
}
fills: {
awesomeBlue: Fill
}
Expand All @@ -16,6 +19,9 @@ interface ThemeOverrides extends Partial<Pick<Theme, 'fills' | 'components'>> {
}

const themeOverridesOrAdditions: ThemeOverrides = {
colors: {
speakers: ['#0f4880', '#1d781d', '#db0a5b', '#8d6708', '#d43900']
},
fills: {
awesomeBlue: {
color: '#3492eb',
Expand Down Expand Up @@ -50,6 +56,13 @@ expectType<Fill>(defaultTheme.fills.neutral)
expectType<string>(defaultTheme.fills.neutral.color)
expectType<string>(defaultTheme.fills.neutral.backgroundColor)
expectType<Intent>(defaultTheme.intents.info)
expectType<Intent>(defaultTheme.intents.danger)
expectType<Intent>(defaultTheme.intents.warning)
expectType<Intent>(defaultTheme.intents.success)
expectType<Partial<StyleProps<'Button'>>>(defaultTheme.components.Button.baseStyle)
expectType<Partial<StyleProps<'Button'>>>(defaultTheme.components.Button.sizes.small)
expectType<Partial<StyleProps<'Button'>>>(defaultTheme.components.Button.sizes.medium)
expectType<Partial<StyleProps<'Button'>>>(defaultTheme.components.Button.sizes.large)
expectType<Partial<StyleProps<'Button'>>>(defaultTheme.components.Button.appearances.minimal)
expectType<Partial<StyleProps<'Button'>>>(defaultTheme.components.Button.appearances.default)
expectType<Partial<StyleProps<'Button'>>>(defaultTheme.components.Button.appearances.destructive)
Expand All @@ -76,13 +89,22 @@ expectType<Fill>(customTheme.fills.neutral)
expectType<string>(customTheme.fills.neutral.color)
expectType<string>(customTheme.fills.neutral.backgroundColor)
expectType<Intent>(customTheme.intents.info)
expectType<Intent>(customTheme.intents.danger)
expectType<Intent>(customTheme.intents.warning)
expectType<Intent>(customTheme.intents.success)
expectType<Partial<StyleProps<'Button'>>>(customTheme.components.Button.baseStyle)
expectType<Partial<StyleProps<'Button'>>>(customTheme.components.Button.sizes.small)
expectType<Partial<StyleProps<'Button'>>>(customTheme.components.Button.sizes.medium)
expectType<Partial<StyleProps<'Button'>>>(customTheme.components.Button.sizes.large)
expectType<Partial<StyleProps<'Button'>>>(customTheme.components.Button.appearances.minimal)
expectType<Partial<StyleProps<'Button'>>>(customTheme.components.Button.appearances.default)
expectType<Partial<StyleProps<'Button'>>>(customTheme.components.Button.appearances.destructive)
expectType<Partial<StyleProps<'Button'>>>(customTheme.components.Button.appearances.primary)
expectType<Partial<StyleProps<'Text'>>>(customTheme.components.Text.sizes[300])

// Ensure new values are strongly typed
expectAssignable<Fill>(customTheme.fills.awesomeBlue)
expectType<string[]>(customTheme.colors.speakers)

// Negative case - attempting to reference pseudoselector not defined in index.d.ts
const themeWithNonExistentPseudoSelector = {
Expand All @@ -97,3 +119,9 @@ const themeWithNonExistentPseudoSelector = {
}
}
expectError(mergeTheme(defaultTheme, themeWithNonExistentPseudoSelector))

// Negative case - attempting to assign colors property to string[] (should at least have a key wrapping it)
const themeWithTopLevelColorsArray = {
colors: ['#0f4880', '#1d781d', '#db0a5b', '#8d6708', '#d43900']
}
expectError(mergeTheme(defaultTheme, themeWithTopLevelColorsArray))
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
],
"sideEffects": false,
"scripts": {
"test": "yarn lint && yarn jest",
"test": "yarn lint && yarn tsd && yarn jest",
"test:watch": "yarn run test -- --watch",
"lint": "eslint \"src/**/*.js\" --cache",
"prepublishOnly": "rm -rf esm commonjs umd && yarn build",
Expand Down

0 comments on commit d75c9ad

Please sign in to comment.