Skip to content

Commit

Permalink
fix: missing symbol faker (#313)
Browse files Browse the repository at this point in the history
  • Loading branch information
soc221b authored Jan 3, 2025
1 parent 92acf57 commit 80308d2
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
- ✅ z.record
- ✅ z.set
- ✅ z.string[^1]
- ✅ z.symbol
- ✅ z.tuple
- ✅ z.undefined
- ✅ z.union
Expand Down
14 changes: 14 additions & 0 deletions src/zod-symbol-faker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import * as z from 'zod'
import { ZodTypeFaker } from './zod-type-faker'

export class ZodSymbolFaker<T extends z.ZodSymbol> extends ZodTypeFaker<T> {
fake(): z.infer<T> {
return Symbol()
}

static create<T extends z.ZodSymbol>(schema: T): ZodSymbolFaker<T> {
return new ZodSymbolFaker(schema)
}
}

export const zodSymbolFaker: typeof ZodSymbolFaker.create = ZodSymbolFaker.create
2 changes: 2 additions & 0 deletions src/zod-type-kind-to-zod-type-faker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { ZodReadonlyFaker } from './zod-readonly-faker'
import { ZodRecordFaker } from './zod-record-faker'
import { ZodSetFaker } from './zod-set-faker'
import { ZodStringFaker } from './zod-string-faker'
import { ZodSymbolFaker } from './zod-symbol-faker'
import { ZodTupleFaker } from './zod-tuple-faker'
import { ZodTypeFaker } from './zod-type-faker'
import { ZodUndefinedFaker } from './zod-undefined-faker'
Expand Down Expand Up @@ -69,6 +70,7 @@ export function install(): void {
zodTypeKindToZodTypeFaker.set(z.ZodFirstPartyTypeKind.ZodRecord, ZodRecordFaker)
zodTypeKindToZodTypeFaker.set(z.ZodFirstPartyTypeKind.ZodSet, ZodSetFaker)
zodTypeKindToZodTypeFaker.set(z.ZodFirstPartyTypeKind.ZodString, ZodStringFaker)
zodTypeKindToZodTypeFaker.set(z.ZodFirstPartyTypeKind.ZodSymbol, ZodSymbolFaker)
zodTypeKindToZodTypeFaker.set(z.ZodFirstPartyTypeKind.ZodTuple, ZodTupleFaker)
zodTypeKindToZodTypeFaker.set(z.ZodFirstPartyTypeKind.ZodUndefined, ZodUndefinedFaker)
zodTypeKindToZodTypeFaker.set(z.ZodFirstPartyTypeKind.ZodUnion, ZodUnionFaker)
Expand Down
41 changes: 41 additions & 0 deletions tests/zod-symbol-faker.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { expect, test } from 'vitest'
import * as z from 'zod'
import { zodSymbolFaker, ZodSymbolFaker } from '../src/zod-symbol-faker'
import { expectType, TypeEqual } from 'ts-expect'

test('ZodSymbolFaker should assert parameters', () => {
const invalidSchema = void 0 as any
expect(() => zodSymbolFaker(invalidSchema)).toThrow()
})

test('ZodSymbolFaker should accepts a ZodSymbol schema', () => {
const schema = z.symbol()
expect(() => zodSymbolFaker(schema)).not.toThrow()
})

test('ZodSymbolFaker should return a ZodSymbolFaker instance', () => {
expect(typeof zodSymbolFaker).toBe('function')

const schema = z.symbol()
const faker = zodSymbolFaker(schema)
expect(faker instanceof ZodSymbolFaker).toBe(true)
})

test('ZodSymbolFaker.fake should be a function', () => {
const schema = z.symbol()
const faker = zodSymbolFaker(schema)
expect(typeof faker.fake).toBe('function')
})

test('ZodSymbolFaker.fake should return the given type', () => {
const schema = z.symbol()
const faker = zodSymbolFaker(schema)
expectType<TypeEqual<ReturnType<typeof faker.fake>, z.infer<typeof schema>>>(true)
})

test('ZodSymbolFaker.fake should return a valid data', () => {
const schema = z.symbol()
const faker = zodSymbolFaker(schema)
const data = faker.fake()
expect(schema.safeParse(data).success).toBe(true)
})

0 comments on commit 80308d2

Please sign in to comment.