compose.test.ts
This file
Path
src/compose.test.ts
94 lines · 2.4 KiB · TypeScript
Last changed by
refactor: compose (#116)Yusuke Wadabefore adoptiona66296b
History of this fileTake it
refactor: directory structure (#114)Yusuke Wadabefore adoption0a6afc2
line 1why, one step back1import { compose } from './compose'refactor: compose (#116)Yusuke Wadabefore adoptiona66296b
line 2why, one step back2import { Context } from './context'refactor: directory structure (#114)Yusuke Wadabefore adoption0a6afc2
lines 3–4why, one step back34type C = {refactor: compose (#116)Yusuke Wadabefore adoptiona66296b
lines 5–6why, one step back5 req: Record<string, string>6 res: Record<string, string>refactor: directory structure (#114)Yusuke Wadabefore adoption0a6afc2
lines 7–8why, one step back7}8refactor: compose (#116)Yusuke Wadabefore adoptiona66296b
line 9why, one step back9describe('compose', () => {refactor: directory structure (#114)Yusuke Wadabefore adoption0a6afc2
lines 10–19why, one step back10 const middleware: Function[] = []1112 const a = async (c: C, next: Function) => {13 c.req['log'] = 'log'14 await next()15 }16 middleware.push(a)1718 const b = async (c: C, next: Function) => {19 await next()refactor: compose (#116)Yusuke Wadabefore adoptiona66296b
line 20why, one step back20 c.res['headers'] = 'custom-header'refactor: directory structure (#114)Yusuke Wadabefore adoption0a6afc2
lines 21–31why, one step back21 }22 middleware.push(b)2324 const handler = async (c: C, next: Function) => {25 c.req['log'] = `${c.req.log} message`26 await next()27 c.res = { message: 'new response' }28 }29 middleware.push(handler)3031 it('Request', async () => {refactor: compose (#116)Yusuke Wadabefore adoptiona66296b
line 32why, one step back32 const c: C = { req: {}, res: {} }refactor: directory structure (#114)Yusuke Wadabefore adoption0a6afc2
line 33why, one step back33 const composed = compose<C>(middleware)refactor: compose (#116)Yusuke Wadabefore adoptiona66296b
lines 34–36why, one step back34 const context = await composed(c)35 expect(context.req['log']).not.toBeNull()36 expect(context.req['log']).toBe('log message')refactor: directory structure (#114)Yusuke Wadabefore adoption0a6afc2
lines 37–38why, one step back37 })38 it('Response', async () => {refactor: compose (#116)Yusuke Wadabefore adoptiona66296b
lines 39–92why, one step back39 const c: C = { req: {}, res: {} }40 const composed = compose<C>(middleware)41 const context = await composed(c)42 expect(context.res['headers']).not.toBeNull()43 expect(context.res['headers']).toBe('custom-header')44 expect(context.res['message']).toBe('new response')45 })46})4748describe('compose with Context', () => {49 const middleware: Function[] = []5051 const req = new Request('http://localhost/')52 const c: Context = new Context(req)53 const onError = (error: Error, c: Context) => {54 return c.text('onError', 500)55 }5657 it('Error on handler', async () => {58 const handler = () => {59 throw new Error()60 }6162 const mHandler = async (c: Context, next: Function) => {63 await next()64 }6566 middleware.push(mHandler)67 middleware.push(handler)6869 const composed = compose<Context>(middleware, onError)70 const context = await composed(c)71 expect(context.res).not.toBeNull()72 expect(context.res.status).toBe(500)73 expect(await context.res.text()).toBe('onError')74 })7576 it('Error on middleware', async () => {77 const handler = (c: Context) => {78 return c.text('OK')79 }8081 const mHandler = async () => {82 throw new Error()83 }8485 middleware.push(mHandler)86 middleware.push(handler)8788 const composed = compose<Context>(middleware, onError)89 const context = await composed(c)90 expect(c.res).not.toBeNull()91 expect(c.res.status).toBe(500)92 expect(await context.res.text()).toBe('onError')refactor: directory structure (#114)Yusuke Wadabefore adoption0a6afc2
lines 93–94why, one step back93 })94})