Skip to content

Commit

Permalink
feat(功能完善): 更新 rollup 打包方式;新增 subscribe.test.ts 单元测试;
Browse files Browse the repository at this point in the history
  • Loading branch information
bocheng.zbc committed Nov 13, 2018
1 parent 51f38d1 commit d38400d
Show file tree
Hide file tree
Showing 6 changed files with 238 additions and 47 deletions.
24 changes: 15 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,21 @@ npm test

## Usage

test path: http://forbeslindesay.github.io/express-route-tester/
### param

example: https://repl.it/@boycgit/ette-router-param

test your router rule: http://forbeslindesay.github.io/express-route-tester/

## document

```bash
npm run doc
```

then open the generated `out/index.html` file in your browser.



注:你写路由匹配规则时的 `/` 是很重要的,但你在用 `request` 方法中的 path 里的 `/` 则是无关紧要的;

Expand Down Expand Up @@ -72,14 +86,6 @@ router.get('/', function(ctx, next) {
> 注意:只有当匹配路由规则的时候才会运行相关的中间件,匹配中间件上的 path 并不会运行中间件!!

## document

```bash
npm run doc
```

then open the generated `out/index.html` file in your browser.

## License

[MIT](LICENSE).
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"url-parse": "^1.4.3"
},
"devDependencies": {
"ette": "^0.2.1",
"ette": "^0.3.1",
"@types/node": "^10.11.6",
"@types/url-parse": "^1.4.1",
"@types/jest": "^23.3.1",
Expand Down
14 changes: 9 additions & 5 deletions rollup.config.production.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var uglify = require('rollup-plugin-uglify').uglify;
var terser = require('rollup-plugin-terser').terser;
var path = require('path');
var pkg = require('./package.json');
var deps = Object.keys(pkg.dependencies || {});

const targetName = 'index';
const capitalize = ([first, ...rest], lowerRest = false) =>
Expand Down Expand Up @@ -31,9 +32,12 @@ const compileConfig = function({
if (shouldMinified) {
outputFileArr.splice(1, 0, 'min');
}
return Object.assign(external ? {external: external} : {}, {
return Object.assign(external ? { external: external } : {}, {
input: path.resolve(fromDir, `${targetName}.js`),
output: Object.assign(
{
exports: 'named' // 这个很关键,统一 cmd 的引用方式
},
format === 'umd'
? {
name: capitalize(targetName),
Expand Down Expand Up @@ -73,15 +77,15 @@ module.exports = [
// `file` and `format` for each target)
compileConfig({
fromDir: '.build.cjs',
external: ['big.js'],
external: deps,
outputFileName: path.parse(pkg.main).name,
shouldMinified: false,
format: 'cjs'
}),
// minified
compileConfig({
fromDir: '.build.cjs',
external: ['big.js'],
external: deps,
outputFileName: path.parse(pkg.main).name,
shouldMinified: true,
format: 'cjs'
Expand All @@ -90,15 +94,15 @@ module.exports = [
// es
compileConfig({
fromDir: '.build.es',
external: ['big.js'],
external: deps,
outputFileName: path.parse(pkg.module).name,
shouldMinified: false,
format: 'es'
}),
// es, minified
compileConfig({
fromDir: '.build.es',
external: ['big.js'],
external: deps,
outputFileName: path.parse(pkg.module).name,
shouldMinified: true,
format: 'es'
Expand Down
69 changes: 38 additions & 31 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import Router from '../src/index';
import { HTTP_METHOD } from '../src/lib';
import Ette from 'ette';
const METHODS_LOWERCASE: string[] = Object.keys(HTTP_METHOD).map(k =>
HTTP_METHOD[k as any].toLowerCase()
);
const Ette = require('ette');

describe('[Router] 构造函数 - 创建实例', function() {
let app, router, client;
Expand Down Expand Up @@ -1590,7 +1590,7 @@ function testPrefix(prefix) {
router = new Router();
client = app.client;
middlewareCount = 0;
// 这个算中间件
// 这个算中间件
router.use(function(ctx, next) {
middlewareCount++;
ctx.thing = 'worked';
Expand Down Expand Up @@ -1641,39 +1641,46 @@ function testPrefix(prefix) {
};
}

describe('[Router] 静态方法 - url', function() {
it('生成路由 URL', function() {
var url = Router.url('/:category/:title', {
category: 'programming',
title: 'how-to-node'
});
expect(url).toEqual('/programming/how-to-node');
});

describe('[Router] 静态方法 - url', function () {
it('生成路由 URL', function () {
var url = Router.url('/:category/:title', { category: 'programming', title: 'how-to-node' });
expect(url).toEqual('/programming/how-to-node');
it('使用 encodeURIComponent() 转义', function() {
var url = Router.url('/:category/:title', {
category: 'programming',
title: 'how to node'
});
expect(url).toEqual('/programming/how%20to%20node');
});

it('使用 encodeURIComponent() 转义', function () {
var url = Router.url('/:category/:title', { category: 'programming', title: 'how to node' });
expect(url).toEqual('/programming/how%20to%20node');
it('根据 params 和 query 生成 URL', function() {
var url = Router.url('/books/:category/:id', 'programming', 4, {
query: { page: 3, limit: 10 }
});
expect(url).toEqual('/books/programming/4?page=3&limit=10');
var url = Router.url(
'/books/:category/:id',
{ category: 'programming', id: 4 },
{ query: { page: 3, limit: 10 } }
);
expect(url).toEqual('/books/programming/4?page=3&limit=10');
var url = Router.url(
'/books/:category/:id',
{ category: 'programming', id: 4 },
{ query: 'page=3&limit=10' }
);
expect(url).toEqual('/books/programming/4?page=3&limit=10');
});

it('根据 params 和 query 生成 URL', function () {
var url = Router.url('/books/:category/:id', 'programming', 4, {
query: { page: 3, limit: 10 }
});
expect(url).toEqual('/books/programming/4?page=3&limit=10');
var url = Router.url('/books/:category/:id',
{ category: 'programming', id: 4 },
{ query: { page: 3, limit: 10 } }
);
expect(url).toEqual('/books/programming/4?page=3&limit=10');
var url = Router.url('/books/:category/:id',
{ category: 'programming', id: 4 },
{ query: 'page=3&limit=10' }
);
expect(url).toEqual('/books/programming/4?page=3&limit=10');
});

it('在无 params 而有 query 的情况下也可以生成 url', function () {
var url = Router.url('/category', {
query: { page: 3, limit: 10 }
});
expect(url).toEqual('/category?page=3&limit=10');
it('在无 params 而有 query 的情况下也可以生成 url', function() {
var url = Router.url('/category', {
query: { page: 3, limit: 10 }
});
expect(url).toEqual('/category?page=3&limit=10');
});
});
2 changes: 1 addition & 1 deletion test/layer.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Router from '../src/index';
const Ette = require('ette');
import Ette from 'ette';
import Layer from '../src/layer';

describe('[Layer] 中间件 - compose 多个中间件', function() {
Expand Down
174 changes: 174 additions & 0 deletions test/subscribe.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
import Router from '../src/index';
import Ette from 'ette';

describe('[Router] subscribe 路由 - 路由规则', () => {
let app, router, client;

beforeEach(() => {
app = new Ette();
router = new Router();
client = app.client;
});

test('支持根据 ctx.routerPath 自定义路由规则 [ref - custom routerPath]', done => {
let count = 0;
app.use(function(ctx, next) {
// bind /users => example/users
ctx.routerPath = `/example${ctx.request.path}`;
count++;
return next();
});
router.subscribe('/example/users', function(ctx, next) {
ctx.response.body = ctx.request.method + ' ' + ctx.request.path;
ctx.response.status = 202;
count++;
return next();
});

app.use(router.routes());

app.subscribe('/users', {
onMessage: () => {
count++;
}
});

const sender = (client as any).subscribe('/users');

sender.send('good job');

setTimeout(() => {
expect(count).toBe(3);
done();
}, 0);
});

test('当匹配到路由规则时,才会运行其对应的中间件', done => {
let count = 0;

var otherRouter = new Router();

// 不会匹配到该路由
router.use(function(ctx, next) {
ctx.response.body = { bar: 'baz' };
count++;
return next();
});

// 将会匹配到该路由
(otherRouter as any).subscribe('/bar', function(ctx, next) {
ctx.response.status = 202;
ctx.response.body = { foo: 'bar' };
count++;
return next();
});

app.use(router.routes()).use(otherRouter.routes());

app.subscribe('/bar', {
onMessage: () => {
count++;
}
});

const sender = (client as any).subscribe('/bar');

sender
.send('good job')
.then(res => {
expect(res.status).toBe(202);
expect(res.body).toEqual({
foo: 'bar'
});

expect(count).toBe(2);
done();
})
.catch(err => {
console.log(err);
done();
});
});
test('当匹配不到路由规则时,不会执行对应的中间件逻辑', done => {
let count = 0;

var otherRouter = new Router();

// 不会匹配该路由规则
router.use(function(ctx, next) {
ctx.response.body = { bar: 'baz' };
count++;
return next();
});

// 这里 `get` 规则,匹配不到
(otherRouter as any).get('/bar', function(ctx, next) {
ctx.response.status = 202;
ctx.response.body = { foo: 'bar' };
count++;
return next();
});

app.use(router.routes()).use(otherRouter.routes());

app.subscribe('/bar', {
onMessage: () => {
count++;
}
});

const sender = (client as any).subscribe('/bar');

sender
.send('good job')
.then(res => {
expect(res.status).toBe(404);
expect(res.body).toEqual({});
expect(count).toBe(1);
done();
})
.catch(err => {
console.log(err);
done();
});
});

test('如果不调用 next 方法,将不会调用后续路由', done => {
let count = 0;

router.subscribe(
'user_page',
'/bar',
function(ctx, next) {
// return next();
// no next()
},
function(ctx) {
ctx.response.body = { order: 1 };
}
);

app.use(router.routes());

// 不会被调用,因为之前的路由没有调用 next 方法
app.subscribe('/bar', {
onMessage: () => {
count++;
}
});

const sender = (client as any).subscribe('/bar');
sender
.send('good job')
.then(res => {
expect(res.status).toBe(404);
expect(res.body).toEqual({});
expect(count).toBe(0);
done();
})
.catch(err => {
console.log(err);
done();
});
});
});

0 comments on commit d38400d

Please sign in to comment.