Skip to content

Commit f502756

Browse files
committed
Fixed not copying middleware array and just using this, also more tests for it
1 parent 832e8fa commit f502756

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

index.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,14 @@ const botCallback = function botCallback() {
7878
* is function, then callback for bot message is added
7979
*/
8080
if (args.length && typeof args[0] === 'function') {
81-
this.middlewares.push(args[0]);
81+
const copy = {
82+
middlewares: this.middlewares.slice()
83+
};
8284

83-
const callback = botCallback.bind(this);
84-
callback.use = use.bind(this);
85+
copy.middlewares.push(args[0]);
86+
87+
const callback = botCallback.bind(copy);
88+
callback.use = use.bind(copy);
8589

8690
return callback;
8791
}

tests.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ describe('basic middleware usage', () => {
8484
expect(v.join('')).to.equal('ABCD');
8585
});
8686

87-
it('should properly throw errors and also pass it to middleware error handler', function *() {
87+
it('should properly throw errors and also pass it to middleware error handler', function* () {
8888
let wasThereError = false;
8989
let middlewareErrorHandlerWorked = false;
9090

@@ -111,4 +111,33 @@ describe('basic middleware usage', () => {
111111
expect(middlewareErrorHandlerWorked).to.equal(true);
112112
}
113113
});
114+
115+
it('should properly pass copied context and not affect previous middlewares', function* () {
116+
const values = [];
117+
118+
const def = use(() => {
119+
values.push('Z');
120+
});
121+
122+
const a = def.use(() => {
123+
values.push('A');
124+
});
125+
126+
const b = def.use(() => {
127+
values.push('B');
128+
});
129+
130+
const c = b.use(() => {
131+
values.push('C');
132+
});
133+
134+
yield a(botArgumentsMock);
135+
yield b(botArgumentsMock);
136+
137+
expect(values.join('')).to.equal('ZAZB');
138+
139+
yield c(botArgumentsMock);
140+
141+
expect(values.join('')).to.equal('ZAZBZBC');
142+
});
114143
});

0 commit comments

Comments
 (0)