mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-06-10 22:50:46 +02:00
d467eb1a7e
GitOrigin-RevId: 04299d9ab23c65aa791acecd1c0e63b70df9a8d1
45 lines
1.6 KiB
TypeScript
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')
|
|
})
|
|
})
|
|
})
|