wzp
2021-05-13 7d694a9113118daec5be7ac224dab46a3b20f106
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
'use strict'
 
const { test } = require('tap')
const FindMyWay = require('../')
 
function initializeRoutes (router, handler, quantity) {
  for (const x of Array(quantity).keys()) {
    router.on('GET', '/test-route-' + x, handler)
  }
  return router
}
 
test('verify routes registered', t => {
  const quantity = 5
  // 1 (check length) + quantity of routes * quantity of tests per route
  t.plan(1 + (quantity * 1))
 
  let findMyWay = FindMyWay()
  const defaultHandler = (req, res, params) => res.end(JSON.stringify({ hello: 'world' }))
 
  findMyWay = initializeRoutes(findMyWay, defaultHandler, quantity)
  t.strictEqual(findMyWay.routes.length, quantity)
  findMyWay.routes.map((route, idx) => {
    t.deepEqual(route, {
      method: 'GET',
      path: '/test-route-' + idx,
      opts: {},
      handler: defaultHandler,
      store: undefined
    })
  })
})
 
test('verify routes registered and deregister', t => {
  // 1 (check length) + quantity of routes * quantity of tests per route
  t.plan(2)
 
  let findMyWay = FindMyWay()
  const quantity = 2
  const defaultHandler = (req, res, params) => res.end(JSON.stringify({ hello: 'world' }))
 
  findMyWay = initializeRoutes(findMyWay, defaultHandler, quantity)
  t.strictEqual(findMyWay.routes.length, quantity)
  findMyWay.off('GET', '/test-route-0')
  t.strictEqual(findMyWay.routes.length, quantity - 1)
})