Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

json.test.ts

37 lines1.3 KB

1import { describe, expect, it } from 'vitest'
2import { json } from '../src/ssr/json'
3​
4describe('json', () => {
5 it('sets the content type to application/json and stringifies the data', async () => {
6 const data = { foo: 'bar' }
7 const response = json(data)
8​
9 expect(response.headers.get('Content-Type')).toBe('application/json')
10​
11 const responseClone = response.clone()
12 await expect(responseClone.text()).resolves.toEqual(JSON.stringify(data))
13​
14 await expect(response.json()).resolves.toEqual(data)
15 })
16 it("doesn't override the content type if it's already set", () => {
17 const response = json(null, { headers: { 'Content-Type': 'text/plain' } })
18​
19 expect(response.headers.get('Content-Type')).toBe('text/plain')
20 })
21 it('reflects passed status and statusText', () => {
22 const response = json(null, { status: 404, statusText: 'Not Found' })
23​
24 expect(response.status).toBe(404)
25 expect(response.statusText).toBe('Not Found')
26 })
27 it.each<[string, HeadersInit]>([
28 ['plain object', { 'X-TYPE': 'example' }],
29 ['array', [['X-TYPE', 'example']]],
30 ['Headers', new Headers({ 'X-TYPE': 'example' })],
31 ])('merges headers from %s', (_, headers) => {
32 const response = json(null, { headers })
33​
34 expect(response.headers.get('X-TYPE')).toBe('example')
35 expect(response.headers.get('Content-Type')).toBe('application/json')
36 })
37})