Skip to content

Commit

Permalink
* update deps (#150)
Browse files Browse the repository at this point in the history
* code clean
* add tests for Layer.params()
  • Loading branch information
etroynov authored Jun 25, 2022
1 parent 1aead99 commit 65414f4
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 80 deletions.
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@
"email": "[email protected]"
},
"dependencies": {
"debug": "^4.1.1",
"debug": "^4.3.4",
"http-errors": "^1.7.3",
"koa-compose": "^4.1.0",
"methods": "^1.1.2",
"path-to-regexp": "^6.1.0"
"path-to-regexp": "^6.2.1"
},
"devDependencies": {
"@ladjs/env": "^1.0.0",
"expect.js": "^0.3.1",
"jsdoc-to-markdown": "^5.0.3",
"koa": "^2.11.0",
"mocha": "^7.0.1",
"nyc": "^15.0.0",
"koa": "^2.13.4",
"mocha": "^10.0.0",
"nyc": "^15.1.0",
"should": "^13.2.3",
"supertest": "^4.0.2",
"wrk": "^1.2.0"
"supertest": "^6.2.3",
"wrk": "^1.2.1"
},
"engines": {
"node": ">= 8.0.0"
Expand All @@ -47,6 +47,7 @@
"scripts": {
"docs": "NODE_ENV=test jsdoc2md -t ./lib/API_tpl.hbs --src ./lib/*.js >| API.md",
"test": "mocha test/**/*.js",
"test:watch": "mocha test/**/*.js --watch",
"coverage": "nyc npm run test",
"bench": "make -C bench"
}
Expand Down
1 change: 0 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* Module tests
*/

const koa = require('koa');
const should = require('should');

describe('module', function() {
Expand Down
70 changes: 49 additions & 21 deletions test/lib/layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
const Koa = require('koa');
const http = require('http');
const request = require('supertest');
const Router = require('../../lib/router');
const should = require('should');
const Router = require('../../lib/router');
const Layer = require('../../lib/layer');

describe('Layer', function() {
Expand Down Expand Up @@ -49,7 +49,7 @@ describe('Layer', function() {
request(http.createServer(app.callback()))
.get('/match/this')
.expect(204)
.end(function(err, res) {
.end(function(err) {
if (err) return done(err);
done();
});
Expand Down Expand Up @@ -105,7 +105,7 @@ describe('Layer', function() {
ctx.captures.should.be.type('object');
ctx.captures.should.have.property(0, '101%');
return next();
}, function (ctx, next) {
}, function (ctx) {
ctx.should.have.property('captures');
ctx.captures.should.be.type('object');
ctx.captures.should.have.property(0, '101%');
Expand Down Expand Up @@ -216,16 +216,53 @@ describe('Layer', function() {
done();
});
});
});

describe('Layer#params()', function () {
let route;

before(function() {
route = new Layer('/:category', ['GET'], [function() {}]);
});

it('should return an empty object if params were not pass', function() {
const params = route.params('', []);

params.should.deepEqual({});
});

it('should return empty object if params is empty string', function() {
const params = route.params('', ['']);

params.should.deepEqual({});
});

it('should return an object with escaped params', function() {
const params = route.params('', ['how%20to%20node'])

params.should.deepEqual({ category: 'how to node' });
});

it('should return an object with the same params if an error occurs', function() {
const params = route.params('', ['%E0%A4%A'])

params.should.deepEqual({ category: '%E0%A4%A' });
});

it('should return an object with data if params were pass', function() {
const params = route.params('', ['programming']);

params.should.deepEqual({ category: 'programming' });
});


it('should return empty object if params were not pass', function() {
route.paramNames = [];
const params = route.params('', ['programming']);

params.should.deepEqual({});
});

it('param with paramNames positive check', function () {
const route = new Layer('/:category/:title', ['get'], [function () {}], {name: 'books'});
route.paramNames = [{
name: 'category',
}]
const paramSet = route.params('/:category/:title', ['programming', 'ydkjs'], {'title': 'how-to-code'})
paramSet.should.have.property('title', 'how-to-code')
paramSet.should.have.property( 'category', 'programming' )
})
});

describe('Layer#url()', function() {
Expand All @@ -237,15 +274,6 @@ describe('Layer', function() {
url.should.equal('/programming/how-to-node');
});

it('escapes using encodeURIComponent()', function() {
const route = new Layer('/:category/:title', ['get'], [function () {}], {name: 'books'});
const url = route.url(
{ category: 'programming', title: 'how to node' },
{ encode: encodeURIComponent }
);
url.should.equal('/programming/how%20to%20node');
});

it('setPrefix method checks Layer for path', function () {
const route = new Layer('/category', ['get'], [function () {}], {name: 'books'});
route.path = '/hunter2'
Expand Down
Loading

0 comments on commit 65414f4

Please sign in to comment.