-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.js
53 lines (49 loc) · 1.73 KB
/
index.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const routeGroup = require('./index');
const express = require('express');
const chai = require('chai');
const expect = chai.expect;
const sinon = require('sinon');
describe(`Route Group`, () => {
it('adds group function to express router', async () => {
let router = express.Router();
expect(router.group).to.exist;
expect(router.group).to.be.instanceOf(Function);
});
it('add routes to router express', async () => {
let router = express.Router();
const group = router.group('/test', router => {
router.get('/first-endpoint', () => {});
router.get('/first-endpoint2', () => {});
});
expect(router.stack[0].regexp.toString()).equals(
/^\/test\/?(?=\/|$)/i.toString()
);
expect(router.stack[0].handle.stack[0].regexp.toString()).contains(
/^\/first-endpoint\/?$/i.toString()
);
expect(router.stack[0].handle.stack[1].regexp.toString()).contains(
/^\/first-endpoint2\/?$/i.toString()
);
});
it('add middleware to router group', async () => {
let router = express.Router();
function testMiddleware(req, res, next) {
console.log('Applying middleware');
next();
}
const group = router.group('/test', testMiddleware, router => {
router.get('/first-endpoint', () => {});
router.get('/first-endpoint2', () => {});
});
expect(group.stack[0].handle.stack[0].name).equals('testMiddleware');
expect(router.stack[0].regexp.toString()).equals(
/^\/test\/?(?=\/|$)/i.toString()
);
expect(router.stack[0].handle.stack[1].regexp.toString()).contains(
/^\/first-endpoint\/?$/i.toString()
);
expect(router.stack[0].handle.stack[2].regexp.toString()).contains(
/^\/first-endpoint2\/?$/i.toString()
);
});
});