-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: allow the compose function to be configurable
- Loading branch information
1 parent
30242ee
commit 5e37230
Showing
3 changed files
with
79 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
|
||
'use strict' | ||
|
||
const request = require('supertest') | ||
const assert = require('assert') | ||
const Koa = require('../..') | ||
|
||
describe('app.compose', () => { | ||
it('should work with default compose ', async () => { | ||
const app = new Koa() | ||
const calls = [] | ||
|
||
app.use((ctx, next) => { | ||
calls.push(1) | ||
return next().then(() => { | ||
calls.push(4) | ||
}) | ||
}) | ||
|
||
app.use((ctx, next) => { | ||
calls.push(2) | ||
return next().then(() => { | ||
calls.push(3) | ||
}) | ||
}) | ||
|
||
const server = app.listen() | ||
|
||
await request(server) | ||
.get('/') | ||
.expect(404) | ||
|
||
assert.deepStrictEqual(calls, [1, 2, 3, 4]) | ||
}) | ||
|
||
it('should work with configurable compose', async () => { | ||
const calls = [] | ||
let count = 0 | ||
const app = new Koa({ | ||
compose (fns){ | ||
return async (ctx) => { | ||
const dispatch = async () => { | ||
count++ | ||
const fn = fns.shift() | ||
fn && fn(ctx, dispatch) | ||
} | ||
dispatch() | ||
} | ||
} | ||
}) | ||
|
||
app.use((ctx, next) => { | ||
calls.push(1) | ||
next() | ||
calls.push(4) | ||
}) | ||
app.use((ctx, next) => { | ||
calls.push(2) | ||
next() | ||
calls.push(3) | ||
}) | ||
|
||
const server = app.listen() | ||
|
||
await request(server) | ||
.get('/') | ||
|
||
assert.deepStrictEqual(calls, [1, 2, 3, 4]) | ||
assert.equal(count, 3) | ||
}) | ||
}) |
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