Skip to content

Commit

Permalink
test for typography schema (#761)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasoppermann authored Oct 9, 2023
1 parent c95563b commit d0831d0
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/schemas/typographyToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import {baseToken} from './baseToken'
import {fontWeightValue} from './fontWeightValue'
import {tokenType} from './tokenType'

const typographyValue = z.object({
export const typographyValue = z.object({
fontSize: z.union([dimensionValue, referenceValue]),
lineHeight: z.union([dimensionValue, referenceValue]).optional(),
fontWeight: z.union([fontWeightValue, referenceValue]),
fontFamily: z.union([z.string(), referenceValue]),
fontFamily: z.union([z.string().min(1), referenceValue]),
})

export const typographyToken = baseToken
Expand Down
58 changes: 58 additions & 0 deletions src/schemas/typographyTokenSchema.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import {typographyToken, typographyValue} from './typographyToken'

describe('Schema: typographyToken', () => {
const validValue = {
fontSize: '16px',
lineHeight: '24px',
fontWeight: 600,
fontFamily: 'Helvetica',
}

const validToken = {
$value: validValue,
$type: 'typography',
$description: 'a typography token',
}

it('parses valid tokens', () => {
expect(typographyToken.safeParse(validToken).success).toStrictEqual(true)
})

it('parses valid value', () => {
expect(typographyValue.safeParse(validValue).success).toStrictEqual(true)
// lineHeight is optional
expect(typographyValue.safeParse({...validValue, lineHeight: undefined}).success).toStrictEqual(true)
})

it('it fails on missing property', () => {
expect(typographyValue.safeParse({...validValue, fontSize: undefined}).success).toStrictEqual(false)
expect(typographyValue.safeParse({...validValue, fontWeight: undefined}).success).toStrictEqual(false)
expect(typographyValue.safeParse({...validValue, fontFamily: undefined}).success).toStrictEqual(false)
})

it('it fails on invalid fontSize values', () => {
expect(typographyValue.safeParse({...validValue, fontSize: '100%'}).success).toStrictEqual(false)
expect(typographyValue.safeParse({...validValue, fontSize: '100'}).success).toStrictEqual(false)
expect(typographyValue.safeParse({...validValue, fontSize: ''}).success).toStrictEqual(false)
expect(typographyValue.safeParse({...validValue, fontSize: 10}).success).toStrictEqual(false)
})

it('it fails on invalid lineHeight values', () => {
expect(typographyValue.safeParse({...validValue, lineHeight: '100%'}).success).toStrictEqual(false)
expect(typographyValue.safeParse({...validValue, lineHeight: '100'}).success).toStrictEqual(false)
expect(typographyValue.safeParse({...validValue, lineHeight: ''}).success).toStrictEqual(false)
expect(typographyValue.safeParse({...validValue, lineHeight: 10}).success).toStrictEqual(false)
})

it('it fails on invalid fontWeight values', () => {
expect(typographyValue.safeParse({...validValue, fontWeight: '600'}).success).toStrictEqual(false)
expect(typographyValue.safeParse({...validValue, fontWeight: 'bold'}).success).toStrictEqual(false)
expect(typographyValue.safeParse({...validValue, fontWeight: ''}).success).toStrictEqual(false)
expect(typographyValue.safeParse({...validValue, fontWeight: 10}).success).toStrictEqual(false)
})

it('it fails on invalid fontFamily values', () => {
expect(typographyValue.safeParse({...validValue, fontFamily: ''}).success).toStrictEqual(false)
expect(typographyValue.safeParse({...validValue, fontFamily: 10}).success).toStrictEqual(false)
})
})
File renamed without changes.

0 comments on commit d0831d0

Please sign in to comment.