Skip to content

Instantly share code, notes, and snippets.

@schickling
Created May 29, 2023 15:03
Show Gist options
  • Save schickling/62a5ce056cd0b50f83f89b021bfff688 to your computer and use it in GitHub Desktop.
Save schickling/62a5ce056cd0b50f83f89b021bfff688 to your computer and use it in GitHub Desktop.
import { describe, expect, it } from 'vitest'
import { Equal } from './index.js'
import * as Schema from './Schema.js'
import { Wrapper, wrapper } from './SchemaWrapper.js'
describe('wrapper', () => {
it('wrapper', () => {
const wrapped = wrapper(Schema.number)
const decoded = Schema.parse(wrapped)(10)
expect(decoded).toEqual(new Wrapper(10))
})
it('wrapper array', () => {
const wrapped = Schema.array(wrapper(Schema.number))
const decoded = Schema.parse(wrapped)([10, 20])
expect(decoded).toEqual([new Wrapper(10), new Wrapper(20)])
})
it('wrapper schema-class', () => {
type ProfilePicture = { url: string; description: string }
const makeProfilePicture = () => ({ url: 'https://example.com', description: 'example' })
class User extends Schema.SchemaClass({
id: Schema.string,
profilePicture: Schema.jsonUnsafe<ProfilePicture>(),
}) {}
const parse = Schema.parse(User.schema())
const userA = parse({ id: 'u1', profilePicture: makeProfilePicture() })
const userB = parse({ id: 'u1', profilePicture: makeProfilePicture() })
expect(Equal.equals(userA, userB)).toBe(true)
})
it('wrapper schema-class array', () => {
type ProfilePicture = { url: string; description: string }
const makeProfilePicture = () => ({ url: 'https://example.com', description: 'example' })
class User extends Schema.SchemaClass({
id: Schema.string,
profilePictures: Schema.array(Schema.jsonUnsafe<ProfilePicture>()),
}) {}
const parse = Schema.parse(User.schema())
const userA = parse({ id: 'u1', profilePictures: [makeProfilePicture()] })
const userB = parse({ id: 'u1', profilePictures: [makeProfilePicture()] })
expect(Equal.equals(userA, userB)).toBe(true)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment