Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion test/treaty2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { treaty } from '../src'
import { describe, expect, it, beforeAll, afterAll, mock } from 'bun:test'


const randomObject = { a: 'a', b: 2, c: true, d: false, e: null, f: new Date(0) }
const randomObject = { a: 'a', b: 2, c: true, d: false, e: null, f: new Date(0), g: "1970-01-01T00:00:00.000Z"}
const randomArray = ['a', 2, true, false, null, new Date(0), { a: 'a', b: 2, c: true, d: false, e: null, f: new Date(0)}]
const websocketPayloads = [
// strings
Expand All @@ -17,6 +17,8 @@ const websocketPayloads = [
null,
// A date
new Date(0),
// A date as a string
"1970-01-01T00:00:00.000Z",
// A random object
randomObject,
// A random array
Expand All @@ -29,6 +31,10 @@ const app = new Elysia()
.get('/number', () => 1)
.get('/true', () => true)
.get('/false', () => false)
.get('/date', () => new Date("2022-01-01"))
.get('/dateString', () => "1970-01-01T00:00:00.000Z")
.get('/objectWithDateString', () => ({ d: "1970-01-01T00:00:00.000Z"}))
.get('/randomObject', () => randomObject)
.post('/array', ({ body }) => body, {
body: t.Array(t.String())
})
Expand Down Expand Up @@ -171,6 +177,29 @@ describe('Treaty2', () => {
expect(data).toEqual(false)
})

it('parse date', async () => {
const { data } = await client.date.get()
expect(data instanceof Date).toBeTrue()
expect(data).toEqual(new Date("2022-01-01"))
})

it('parse date string', async () => {
const { data } = await client.dateString.get()
expect(typeof data).toBe("string")
expect(data).toBe("1970-01-01T00:00:00.000Z")
})

it('parse object with date string', async () => {
const { data } = await client.objectWithDateString.get()
expect(typeof data?.d).toBe("string")
expect(data?.d).toBe("1970-01-01T00:00:00.000Z")
})

it('get random object', async () => {
const { data } = await client.randomObject.get()
expect(data).toEqual(randomObject)
})

it('post array', async () => {
const { data } = await client.array.post(['a', 'b'])

Expand Down