Skip to content

Commit eb72079

Browse files
committed
added test cases
1 parent 9ac951d commit eb72079

File tree

5 files changed

+96
-25
lines changed

5 files changed

+96
-25
lines changed

test/global/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
'use strict';
22
import './Chai';
3-
import './Fetch';

test/middlewares/PersonMiddleware.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
3+
export const PersonMiddleware1 = {
4+
walk: target => next => step => {
5+
step += 1;
6+
return next(step);
7+
},
8+
speak: target => next => word => {
9+
word = 'from middleware: ' + word;
10+
return next(word);
11+
}
12+
};
13+
14+
export class PersonMiddleware2 {
15+
constructor() {
16+
// Define function names for middleware target.
17+
this.middlewareMethods = ['walk', 'speak'];
18+
}
19+
20+
walk(target) {
21+
return next => step => {
22+
step += 1;
23+
return next(step);
24+
}
25+
}
26+
27+
speak(target) {
28+
return next => word => {
29+
word = 'from middleware: ' + word;
30+
return next(word);
31+
}
32+
}
33+
}

test/middlewares/WalkMiddleware.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
// a middleware function for walk function
3+
const WalkMiddleware = target => next => step => {
4+
step += 1;
5+
return next(step);
6+
}
7+
export default WalkMiddleware;

test/person/Person.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
3+
export default class Person {
4+
5+
constructor() {
6+
this.step = 0;
7+
this.word = '';
8+
}
9+
10+
walk(step) {
11+
this.step = step;
12+
}
13+
14+
speak(word) {
15+
this.word = word;
16+
}
17+
}

test/specs/Middleware.spec.js

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,54 @@
11
'use strict';
2-
import FAST from '../../lib/FAST';
3-
import {DopplerServer} from '../../lib/servers/Doppler';
2+
import {MiddlewareManager} from '../../lib/Middleware';
3+
import Person from '../person/Person';
4+
import WalkMiddleware from '../middlewares/WalkMiddleware';
5+
import {PersonMiddleware1, PersonMiddleware2} from '../middlewares/PersonMiddleware';
46

5-
describe('FAST APIs: ', () => {
6-
let fast;
7-
let context = {'app':'chromecast', 'app_version':'111111', 'user_id':'2000000671'};
7+
describe('Middleware: ', () => {
8+
let person;
9+
let middlewareManager;
810

9-
before(() => {
10-
fast = new FAST({
11-
production: false,
12-
source: 'playback'
13-
});
11+
beforeEach(() => {
12+
person = new Person();
13+
middlewareManager = new MiddlewareManager(person);
1414
});
1515

16-
after(() => {
17-
fast = null;
16+
afterEach(() => {
17+
person = null;
18+
middlewareManager = null;
1819
});
1920

20-
describe('.setConfigs(): ', () => {
21-
it('should contains context', () => {
22-
fast.setConfigs({context: context});
23-
return assert.deepEqual(fast.configs.context, context);
21+
describe('middleware function: ', () => {
22+
it('should apply the middlweare function', () => {
23+
middlewareManager.use('walk', WalkMiddleware);
24+
const step = person.step;
25+
const newStep = 3;
26+
person.walk(newStep);
27+
return assert.equal(person.step, newStep + 1);
2428
});
29+
});
2530

26-
it('should has a true production but not change source', () => {
27-
let source = fast.configs.source;
28-
fast.setConfigs({production: true});
29-
assert.isTrue(fast.configs.production);
30-
assert.equal(fast.configs.source, source);
31+
describe('middleware object: ', () => {
32+
it('should apply the middlweare object', () => {
33+
middlewareManager.use(PersonMiddleware1);
34+
const step = person.step;
35+
const newStep = 3;
36+
person.walk(newStep);
37+
person.speak('hello');
38+
assert.equal(person.step, newStep + 1);
39+
assert.isTrue(/from middleware/g.test(person.word), newStep + 1);
3140
});
3241
});
3342

34-
describe('.doppler: ', () => {
35-
it('should be a DopplerServer instance', () => {
36-
return assert.instanceOf(fast.doppler, DopplerServer);
43+
describe('middlewareMethods: ', () => {
44+
it('should apply the middlweare object', () => {
45+
middlewareManager.use(new PersonMiddleware2());
46+
const step = person.step;
47+
const newStep = 3;
48+
person.walk(newStep);
49+
person.speak('hello');
50+
assert.equal(person.step, newStep + 1);
51+
assert.isTrue(/from middleware/g.test(person.word), newStep + 1);
3752
});
3853
});
3954
});

0 commit comments

Comments
 (0)