-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e36f0f4
commit 7623287
Showing
3 changed files
with
80 additions
and
101 deletions.
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
node_js: | ||
- "0.10" | ||
- "0.11" | ||
sudo: false | ||
language: node_js | ||
install: "npm install" | ||
script: | ||
- ./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha --report lcovonly --recursive -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js | ||
script: "npm run test-cov" | ||
after_script: "npm install coveralls@2 && cat ./coverage/lcov.info | coveralls" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,96 +1,72 @@ | ||
/** | ||
* Module dependencies | ||
*/ | ||
|
||
var should = require('should'); | ||
var wayfarer = require('./index.js'); | ||
|
||
/** | ||
* Test | ||
*/ | ||
|
||
describe('wayfarer()', function () { | ||
it('should initialize with empty properties', function() { | ||
var router = wayfarer(); | ||
router.defaultPath.should.be.empty; | ||
}); | ||
}); | ||
|
||
describe('.on()', function () { | ||
it('should catch type errors', function () { | ||
var router = wayfarer(); | ||
router.on.bind(router, 123) | ||
.should.throw('Path should be a string'); | ||
router.on.bind(router, '/hi', 123) | ||
.should.throw('Callback should be a function'); | ||
router.on.bind(router, '/hi', function(){}) | ||
.should.not.throw(); | ||
}); | ||
it('should set a default path', function() { | ||
var router = wayfarer({ default: '/404' }); | ||
router.defaultPath.should.eql('/404'); | ||
}); | ||
}); | ||
|
||
describe('.match()', function () { | ||
it('should match simple routes', function (done) { | ||
var router = wayfarer(); | ||
router | ||
.on('/hello', function(){done()}) | ||
.on('/hi', function(){}); | ||
|
||
router.match('/hello'); | ||
}); | ||
|
||
it('should match dynamic routes', function (done) { | ||
var router = wayfarer(); | ||
router | ||
.on('/hello', function() {}) | ||
.on('/:user', function() {done()}); | ||
|
||
router.match('/tobi'); | ||
}); | ||
|
||
it('should provide param object on dynamic routes', function (done) { | ||
var router = wayfarer(); | ||
router | ||
.path('/:user', function(uri, param) { | ||
param.should.have.property('user', 'tobi'); | ||
done(); | ||
}); | ||
|
||
router.match('/tobi'); | ||
}); | ||
|
||
it('should match the default path if no other paths match', function (done) { | ||
var router = wayfarer({ default: '/404' }); | ||
router | ||
.on('/hello', function() {}) | ||
.on('/howdy', function() {}) | ||
.on('/404', function() {done()}) | ||
|
||
router.match('/anotherPath'); | ||
}); | ||
|
||
it('should not match queryStrings', function(done) { | ||
var router = wayfarer({ default: '/404' }); | ||
router | ||
.on('/hello', function() {done()}) | ||
.on('/howdy', function() {}) | ||
.on('/404', function() {}); | ||
|
||
router.match('/hello?hello=false'); | ||
}); | ||
}); | ||
|
||
describe('aliases', function() { | ||
it('.route == .on', function() { | ||
var router = wayfarer() | ||
router.route.should.eql(router.on) | ||
}); | ||
|
||
it('.path == .on', function() { | ||
var router = wayfarer() | ||
router.path.should.eql(router.on) | ||
}); | ||
}); | ||
const test = require('tape') | ||
const should = require('should') | ||
const wayfarer = require('./index.js') | ||
|
||
test('wayfarer() should initialize with empty properties', function(t) { | ||
t.plan(1) | ||
const router = wayfarer() | ||
t.equal(router.defaultPath, '') | ||
}) | ||
|
||
test('wayfarer should set accept a default path', function(t) { | ||
t.plan(1) | ||
const router = wayfarer({ default: '/404' }) | ||
t.equal(router.defaultPath, '/404') | ||
}) | ||
|
||
test('.on() should catch type errors', function(t) { | ||
t.plan(2) | ||
const router = wayfarer() | ||
t.throws(router.on.bind(router, 123), /string/) | ||
t.throws(router.on.bind(router, '/hi', 123), /function/) | ||
}) | ||
|
||
test('.match() should match simple routes', function(t) { | ||
t.plan(1) | ||
const router = wayfarer() | ||
router.on('/hello', function() { t.ok(true, 'correct path called') }) | ||
router.on('/hi', function() { t.ok(false, 'incorrect path called') }) | ||
router.match('/hello') | ||
}) | ||
|
||
test('.match() should match dynamic routes', function(t) { | ||
t.plan(1) | ||
const router = wayfarer() | ||
router.on('/hi', function() { t.ok(false, 'incorrect path called') }) | ||
router.on('/:user', function() { t.ok(true, 'correct path called') }) | ||
router.match('/tobi') | ||
}) | ||
|
||
test('.match() should provide param object on dynamic routes', function(t) { | ||
t.plan(1) | ||
const router = wayfarer() | ||
router.on('/hi', function() { t.ok(false, 'incorrect path called') }) | ||
router.on('/:user', function(uri, param) { t.equal(param.user, 'tobi') }) | ||
router.match('/tobi') | ||
}) | ||
|
||
test('.match() should match the default path if no matches found', function(t) { | ||
t.plan(1) | ||
const router = wayfarer({ default: '/404' }) | ||
router.on('/hi', function() { t.ok(false, 'incorrect path called') }) | ||
router.on('/howdy', function() { t.ok(false, 'incorrect path called') }) | ||
router.on('/404', function() { t.ok(true, 'correct path called') }) | ||
router.match('/derpderp') | ||
}) | ||
|
||
test('.match() should not match queryStrings', function(t) { | ||
t.plan(1) | ||
const router = wayfarer() | ||
router.on('/hi', function() { t.ok(false, 'incorrect path called') }) | ||
router.on('/howdy', function() { t.ok(false, 'incorrect path called') }) | ||
router.on('/404', function() { t.ok(true, 'correct path called') }) | ||
router.match('/404?derp=darp') | ||
}) | ||
|
||
test('aliases', function(t) { | ||
t.plan(2) | ||
const router = wayfarer() | ||
t.equal(router.route, router.on, '.route() == .on()') | ||
t.equal(router.path, router.on, '.path() == .on()') | ||
}) |