Files
overleaf-cep/libraries/validation-tools/test/unit/src/zodHelpers.test.ts
T
Andrew Rumble d467eb1a7e Add build trigger for validation-tools
GitOrigin-RevId: 04299d9ab23c65aa791acecd1c0e63b70df9a8d1
2025-09-19 08:05:18 +00:00

45 lines
1.6 KiB
TypeScript

import { zz } from '../../../zodHelpers'
import { describe, expect, it } from 'vitest'
import mongodb from 'mongodb'
const { ObjectId } = mongodb
describe('zodHelpers', () => {
describe('objectId', () => {
it('fails to parse when provided with an invalid ObjectId', () => {
const parsed = zz.objectId().safeParse('aa')
expect(parsed.success).toBe(false)
expect(parsed.error?.issues).toHaveLength(1)
expect(parsed.error?.issues).toMatchObject([
expect.objectContaining({
message: 'invalid Mongo ObjectId',
}),
])
})
it('parses successfully when provided with a valid ObjectId', () => {
const parsed = zz.objectId().safeParse('507f1f77bcf86cd799439011')
expect(parsed.success).toBe(true)
expect(parsed.data).toBe('507f1f77bcf86cd799439011')
})
})
describe('coercedObjectId', () => {
it('fails to parse when provided with an invalid ObjectId', () => {
const parsed = zz.coercedObjectId().safeParse('aa')
expect(parsed.success).toBe(false)
expect(parsed.error?.issues).toHaveLength(1)
expect(parsed.error?.issues).toMatchObject([
expect.objectContaining({
message: 'invalid Mongo ObjectId',
}),
])
})
it('parses to an ObjectId when provided with a valid ObjectId string', () => {
const parsed = zz.coercedObjectId().safeParse('507f1f77bcf86cd799439011')
expect(parsed.success).toBe(true)
expect(parsed.data).toBeInstanceOf(ObjectId)
expect(parsed.data?.toString()).toBe('507f1f77bcf86cd799439011')
})
})
})