-
-
Notifications
You must be signed in to change notification settings - Fork 748
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(start): Fix json() helper merging of headers, and set up unit tes…
…ts for @tanstack/start (#2956) * Simplify json implementation and fix headers handling * set up tests for start package * Fix ssr script test * Add test for json helper * name test script consistently * add name and watch options to test config * add test script * don't override existing Content-Type header
- Loading branch information
1 parent
58fd908
commit 85d9d79
Showing
5 changed files
with
55 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { json } from '../json' | ||
|
||
describe('json', () => { | ||
it('sets the content type to application/json and stringifies the data', async () => { | ||
const data = { foo: 'bar' } | ||
const response = json(data) | ||
|
||
expect(response.headers.get('Content-Type')).toBe('application/json') | ||
|
||
const responseClone = response.clone() | ||
await expect(responseClone.text()).resolves.toEqual(JSON.stringify(data)) | ||
|
||
await expect(response.json()).resolves.toEqual(data) | ||
}) | ||
it("doesn't override the content type if it's already set", () => { | ||
const response = json(null, { headers: { 'Content-Type': 'text/plain' } }) | ||
|
||
expect(response.headers.get('Content-Type')).toBe('text/plain') | ||
}) | ||
it('reflects passed status and statusText', () => { | ||
const response = json(null, { status: 404, statusText: 'Not Found' }) | ||
|
||
expect(response.status).toBe(404) | ||
expect(response.statusText).toBe('Not Found') | ||
}) | ||
it.each<[string, HeadersInit]>([ | ||
['plain object', { 'X-TYPE': 'example' }], | ||
['array', [['X-TYPE', 'example']]], | ||
['Headers', new Headers({ 'X-TYPE': 'example' })], | ||
])('merges headers from %s', (_, headers) => { | ||
const response = json(null, { headers }) | ||
|
||
expect(response.headers.get('X-TYPE')).toBe('example') | ||
expect(response.headers.get('Content-Type')).toBe('application/json') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters