Skip to content

Commit

Permalink
test(compose): a move comprehensive test cases (honojs#198)
Browse files Browse the repository at this point in the history
  • Loading branch information
metrue authored May 5, 2022
1 parent 1a8d172 commit ccffb6f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
43 changes: 41 additions & 2 deletions src/compose.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,30 +130,69 @@ describe('Compose', function () {
return x && typeof x.then === 'function'
}

it('should work', async () => {
it('should get executed order one by one', async () => {
const arr: number[] = []
const stack = []
const called: boolean[] = []

stack.push(async (context: C, next: Function) => {
called.push(true)

arr.push(1)
await next()
arr.push(6)
})

stack.push(async (context: C, next: Function) => {
called.push(true)

arr.push(2)
await next()
arr.push(5)
})

stack.push(async (context: C, next: Function) => {
called.push(true)

arr.push(3)
await next()
arr.push(4)
})

await compose(stack)({})
expect(called).toEqual([true, true, true])
expect(arr).toEqual([1, 2, 3, 4, 5, 6])
})

it('should not get executed if previous next() not triggered', async () => {
const arr: number[] = []
const stack = []
const called: boolean[] = []

stack.push(async (context: C, next: Function) => {
called.push(true)

arr.push(1)
await next()
arr.push(6)
})

stack.push(async (context: C, next: Function) => {
called.push(true)
arr.push(2)
})

stack.push(async (context: C, next: Function) => {
called.push(true)

arr.push(3)
await next()
arr.push(4)
})

await compose(stack)({})
expect(arr).toEqual(expect.arrayContaining([1, 2, 3, 4, 5, 6]))
expect(called).toEqual([true, true])
expect(arr).toEqual([1, 2, 6])
})

it('should be able to be called twice', () => {
Expand Down
5 changes: 3 additions & 2 deletions tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"src/**/*"
],
"exclude": [
"src/**/*.test.ts"
"src/**/*.test.ts",
"src/router/reg-exp-router/*"
]
}
}

0 comments on commit ccffb6f

Please sign in to comment.