-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtests.js
143 lines (108 loc) · 3.4 KB
/
tests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
'use strict';
const expect = require('chai').expect;
const middleware = require('./index');
const use = middleware.use;
require('co-mocha');
const botArgumentsMock = {
chat: {
id: 1
}
};
describe('basic middleware usage', () => {
it('should return function', () => {
expect(use(() => {})).to.be.a('function');
});
const integratedValues = [];
it('should execute middlewares in proper order', function* () {
yield use(() => {
integratedValues.push('A');
}).use(() => {
integratedValues.push('B');
}).use(function* gen1() {
integratedValues.push('C');
}).use(function* gen2() {
integratedValues.push('D');
})(botArgumentsMock);
expect(integratedValues.join('')).to.equal('ABCD');
});
it('should have properties from default middleware', function* () {
let thereIsMsg = false;
yield use(function () {
thereIsMsg = typeof this.msg !== 'undefined';
})(botArgumentsMock);
expect(thereIsMsg).to.equal(true);
});
it('should have new middlewares and not have old used, when using use() from scratch', function* () {
yield use(() => {
integratedValues.push('E');
})(botArgumentsMock);
// If test result would be false - value would be ABCDABCDE
expect(integratedValues.join('')).to.equal('ABCDE');
});
it('should stop executing middleware when .stop() is used', function* () {
const values = [];
yield use(function() {
this.stop();
}).use(() => {
values.push('A');
})(botArgumentsMock);
expect(values).to.be.empty;
yield use(function () {
values.push('A');
}).use(function () {
this.stop();
}).use(function () {
values.push('B');
})(botArgumentsMock);
expect(values.join('')).to.equal('A');
});
it('should work properly with alternative syntax', function* () {
const v = [];
yield use(() => v.push('A')).use(() => v.push('B'))(() => v.push('C'))(botArgumentsMock);
expect(v.join('')).to.equal('ABC');
yield use(() => v.push('D'))(function() { this.stop(); })(() => v.push('E'))(botArgumentsMock);
expect(v.join('')).to.equal('ABCD');
});
it('should properly throw errors and also pass it to middleware error handler', function* () {
let wasThereError = false;
let middlewareErrorHandlerWorked = false;
function customErrorHandler(err) {
throw err;
}
function middlewareWithErrorHandler() {
this.undefinedMethod();
}
middlewareWithErrorHandler.onErrorHandler = function() {
middlewareErrorHandlerWorked = true;
};
middleware.setOnErrorHandler(customErrorHandler);
try {
yield use(middlewareWithErrorHandler)(botArgumentsMock);
} catch (err) {
wasThereError = true;
} finally {
expect(wasThereError).to.equal(true);
expect(middlewareErrorHandlerWorked).to.equal(true);
}
});
it('should properly pass copied context and not affect previous middlewares', function* () {
const values = [];
const def = use(() => {
values.push('Z');
});
const a = def.use(() => {
values.push('A');
});
const b = def.use(() => {
values.push('B');
});
const c = b.use(() => {
values.push('C');
});
yield a(botArgumentsMock);
yield b(botArgumentsMock);
expect(values.join('')).to.equal('ZAZB');
yield c(botArgumentsMock);
expect(values.join('')).to.equal('ZAZBZBC');
});
});