UNPKG

2.12 kBJavaScriptView Raw
1var wayfarer = require('../')
2var walk = require('../walk')
3var noop = require('noop2')
4var tape = require('tape')
5
6tape('walk', function (t) {
7 t.test('should assert input types', function (t) {
8 t.plan(3)
9 t.throws(walk.bind(null), /function/, 'assert first arg is function')
10 t.throws(walk.bind(null, noop), /function/, 'assert second arg is a function')
11 t.throws(walk.bind(null, noop, noop), /object/, 'assert trie exists')
12 })
13
14 t.test('should walk a trie', function (t) {
15 t.plan(2)
16 var router = wayfarer()
17 router.on('/foo', function (x, y) { return x * y })
18 router.on('/bar', function (x, y) { return x / y })
19
20 walk(router, function (route, cb) {
21 var y = 2
22 return function (params, x) {
23 return cb(x, y)
24 }
25 })
26
27 t.equal(router('/foo', 4), 8, 'multiply')
28 t.equal(router('/bar', 8), 4, 'divide')
29 })
30
31 t.test('should walk a nested trie', function (t) {
32 t.plan(3)
33 var router = wayfarer()
34 router.on('/foo/baz', function (x, y) { return x * y })
35 router.on('/bar/bin/barb', function (x, y) { return x / y })
36 router.on('/bar/bin/bla', function (x, y) { return x / y })
37
38 walk(router, function (route, cb) {
39 var y = 2
40 return function (params, x) {
41 return cb(x, y)
42 }
43 })
44
45 t.equal(router('/foo/baz', 4), 8, 'multiply')
46 t.equal(router('/bar/bin/barb', 8), 4, 'divide')
47 t.equal(router('/bar/bin/bla', 8), 4, 'divide')
48 })
49
50 t.test('should walk partials', function (t) {
51 t.plan(4)
52 var router = wayfarer()
53 router.on('/foo', function (route) { return route })
54 router.on('/:foo', function (route) { return route })
55 router.on('/:foo/bar', function (route) { return route })
56 router.on('/:foo/:bar', function (route) { return route })
57
58 walk(router, function (route, cb) { return function () { return cb(route) } })
59
60 t.equal(router('/foo'), '/foo', 'no partials')
61 t.equal(router('/bleep'), '/:foo', 'one partial')
62 t.equal(router('/bleep/bar'), '/:foo/bar', 'partial and normal')
63 t.equal(router('/bleep/bloop'), '/:foo/:bar', 'two partials')
64 })
65})