2022-01-05 18:22:53 +00:00
|
|
|
import makeServiceWorkerEnv from 'service-worker-mock'
|
|
|
|
import { Context } from '../src/context'
|
|
|
|
|
2022-01-08 20:30:50 +00:00
|
|
|
// eslint-disable-next-line
|
2022-01-06 22:03:54 +00:00
|
|
|
declare let global: any
|
2022-01-05 18:22:53 +00:00
|
|
|
Object.assign(global, makeServiceWorkerEnv())
|
|
|
|
|
|
|
|
describe('Context', () => {
|
|
|
|
const req = new Request('/')
|
|
|
|
const c = new Context(req, new Response())
|
|
|
|
|
|
|
|
it('c.text', async () => {
|
2022-01-06 22:03:54 +00:00
|
|
|
const res = c.text('text in c')
|
2022-01-05 18:22:53 +00:00
|
|
|
expect(res.status).toBe(200)
|
2022-01-08 04:46:49 +00:00
|
|
|
expect(res.headers.get('Content-Type')).toBe('text/plain')
|
2022-01-05 18:22:53 +00:00
|
|
|
expect(await res.text()).toBe('text in c')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('c.json', async () => {
|
2022-01-06 22:03:54 +00:00
|
|
|
const res = c.json({ message: 'Hello' })
|
2022-01-05 18:22:53 +00:00
|
|
|
expect(res.status).toBe(200)
|
2022-01-08 04:46:49 +00:00
|
|
|
expect(res.headers.get('Content-Type')).toMatch('application/json')
|
2022-01-05 18:22:53 +00:00
|
|
|
const text = await res.text()
|
|
|
|
const object = JSON.parse(text)
|
|
|
|
expect(object.message).toBe('Hello')
|
|
|
|
})
|
2022-01-08 04:46:49 +00:00
|
|
|
|
|
|
|
it('c.html', async () => {
|
|
|
|
const res = c.html('<h1>Hello! Hono!</h1>')
|
|
|
|
expect(res.status).toBe(200)
|
|
|
|
expect(res.headers.get('Content-Type')).toMatch('text/html')
|
|
|
|
expect(await res.text()).toBe('<h1>Hello! Hono!</h1>')
|
|
|
|
})
|
2022-01-05 18:22:53 +00:00
|
|
|
})
|