Skip to content

Commit

Permalink
add test for scope schema (#765)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasoppermann authored Oct 10, 2023
1 parent f0dd7a7 commit dba330b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
29 changes: 29 additions & 0 deletions src/schemas/scopeTokenSchema.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type {ValidScope} from './scopes'
import {scopes} from './scopes'

const validScopes: ValidScope[] = ['all', 'bgColor', 'fgColor', 'borderColor', 'size', 'gap', 'radius']
const scopeSchema = scopes(validScopes)

describe('Schema: scopes', () => {
it('parses valid scopes', () => {
expect(scopeSchema.safeParse(['all']).success).toStrictEqual(true)
expect(scopeSchema.safeParse(['bgColor']).success).toStrictEqual(true)
expect(scopeSchema.safeParse(['fgColor']).success).toStrictEqual(true)
expect(scopeSchema.safeParse(['borderColor']).success).toStrictEqual(true)
expect(scopeSchema.safeParse(['size']).success).toStrictEqual(true)
expect(scopeSchema.safeParse(['gap']).success).toStrictEqual(true)
expect(scopeSchema.safeParse(['radius']).success).toStrictEqual(true)
expect(
scopeSchema.safeParse(['all', 'bgColor', 'fgColor', 'borderColor', 'size', 'gap', 'radius']).success,
).toStrictEqual(true)
expect(scopeSchema.safeParse([]).success).toStrictEqual(true)
})

it('fails on invalid scope', () => {
expect(scopeSchema.safeParse(['invalid']).success).toStrictEqual(false)
expect(scopeSchema.safeParse(['']).success).toStrictEqual(false)
expect(scopeSchema.safeParse([1]).success).toStrictEqual(false)
expect(scopeSchema.safeParse([false]).success).toStrictEqual(false)
expect(scopeSchema.safeParse([undefined]).success).toStrictEqual(false)
})
})
4 changes: 2 additions & 2 deletions src/schemas/scopes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import {z} from 'zod'
import {joinFriendly} from '../utilities/joinFriendly'
import {schemaErrorMessage} from '../utilities/schemaErrorMessage'

type ValidScope = 'all' | 'bgColor' | 'fgColor' | 'borderColor' | 'size' | 'gap' | 'radius' | 'borderColor'
const validScopes: ValidScope[] = ['all', 'bgColor', 'fgColor', 'borderColor', 'size', 'gap', 'radius', 'borderColor']
export type ValidScope = 'all' | 'bgColor' | 'fgColor' | 'borderColor' | 'size' | 'gap' | 'radius'
const validScopes: ValidScope[] = ['all', 'bgColor', 'fgColor', 'borderColor', 'size', 'gap', 'radius']

export const scopes = (scopeSubset?: ValidScope[]) => {
const scopeArray = scopeSubset ?? validScopes
Expand Down

0 comments on commit dba330b

Please sign in to comment.