Skip to content

Commit

Permalink
test(#285): Add Tests for z.number().int()
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianWendelborn committed Sep 24, 2021
1 parent 9a7a632 commit ab95b50
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/kotti-ui/source/kotti-navbar/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { z } from 'zod'

export namespace KottiNavbar {
export const notificationSchema = z.object({
count: z.number(),
count: z.number().int(),
link: z.string(),
title: z.string(),
})
Expand Down
72 changes: 72 additions & 0 deletions packages/kotti-ui/source/make-props.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,78 @@ describe('number', () => {
})
})

describe('number.int', () => {
beforeAll(silenceConsole)

const NUMBER_INT_SUCCESS = [
-1,
0,
1,
42,
Number.MAX_SAFE_INTEGER,
Number.MIN_SAFE_INTEGER,
]
const NUMBER_INT_FAILURE = [
'string',
[],
{},
1.5,
false,
Number.EPSILON,
Number.NaN,
Number.NEGATIVE_INFINITY,
Number.POSITIVE_INFINITY,
]

it('generates vue prop for schema “z.number().int()”', () => {
const schema = z.object({
prop: z.number().int(),
})
const { prop } = makeProps(schema)

expect(prop).toBeRequired()
expect(prop).toBeType(Number)
expect(prop).toValidate(...NUMBER_INT_SUCCESS)
expect(prop).not.toValidate(...NUMBER_INT_FAILURE, null, undefined)
})

it('generates vue prop for schema “z.number().int().nullable()”', () => {
const schema = z.object({
prop: z.number().int().nullable(),
})
const { prop } = makeProps(schema)

expect(prop).toBeRequired()
expect(prop).toBeType(Number)
expect(prop).toValidate(...NUMBER_INT_SUCCESS, null)
expect(prop).not.toValidate(...NUMBER_INT_FAILURE, undefined)
})

it('generates vue prop for schema “z.number().int().default()”', () => {
const schema = z.object({
prop: z.number().int().default(1),
})
const { prop } = makeProps(schema)

expect(prop).toDefaultTo(1)
expect(prop).toBeType(Number)
expect(prop).toValidate(...NUMBER_INT_SUCCESS, undefined)
expect(prop).not.toValidate(...NUMBER_INT_FAILURE, null)
})

it('generates vue prop for schema “z.number().int().nullable().default()”', () => {
const schema = z.object({
prop: z.number().int().nullable().default(null),
})
const { prop } = makeProps(schema)

expect(prop).toDefaultTo(null)
expect(prop).toBeType(Number)
expect(prop).toValidate(...NUMBER_INT_SUCCESS, null, undefined)
expect(prop).not.toValidate(...NUMBER_INT_FAILURE)
})
})

describe('object', () => {
beforeAll(silenceConsole)

Expand Down
2 changes: 1 addition & 1 deletion packages/kotti-ui/source/make-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ const zodToVueType = new Map<z.ZodFirstPartyTypeKind, VuePropConstructor>([
*
* // types.ts
* export namespace KottiUserMenu {
* const propsSchema = z.object({
* export const propsSchema = z.object({
* // ...
* })
* type Props = z.input<typeof propsSchema>
Expand Down

0 comments on commit ab95b50

Please sign in to comment.