Skip to content

Commit 7ee0a21

Browse files
test: cover typedResponse.text (#64)
1 parent 4e61b53 commit 7ee0a21

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

src/api.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,4 +463,41 @@ describe('typedResponse', () => {
463463
])
464464
}
465465
})
466+
467+
it('should return string by default when turning into text', async () => {
468+
const result = await subject.typedResponse(new Response('foo')).text()
469+
type _R = Expect<Equal<typeof result, string>>
470+
expect(result).toBe('foo')
471+
})
472+
473+
it('should accept a type for the text method', async () => {
474+
const result = await subject
475+
.typedResponse(new Response('john@doe.com'))
476+
.text<`${string}@${string}.${string}`>()
477+
type _R = Expect<Equal<typeof result, `${string}@${string}.${string}`>>
478+
expect(result).toBe('john@doe.com')
479+
})
480+
481+
it('should accept a parser for the text method', async () => {
482+
const result = await subject
483+
.typedResponse(new Response('john@doe.com'))
484+
.text(z.string().email())
485+
type _R = Expect<Equal<typeof result, string>>
486+
expect(result).toBe('john@doe.com')
487+
})
488+
489+
it('should throw a ParseResponseError when the text does not match the parser', async () => {
490+
const response = new Response('not an email')
491+
try {
492+
await subject.typedResponse(response).text(z.string().email())
493+
} catch (error) {
494+
if (!(error instanceof ParseResponseError)) throw error
495+
496+
expect(error).toBeInstanceOf(ParseResponseError)
497+
expect(error.message).toContain(
498+
`"message": "Failed to parse response.text"`
499+
)
500+
expect(error.issues.length).toBeGreaterThan(0)
501+
}
502+
})
466503
})

0 commit comments

Comments
 (0)