Skip to content

Commit 4a10e03

Browse files
committed
test: use t.context for shared vars
1 parent 9bdcb38 commit 4a10e03

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

test/createAPI.spec.js

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,15 @@ import * as applyMiddleware from '../src/applyMiddleware';
44
import callAPIMethod from '../src/callAPIMethod';
55
import createAPI from '../src/createAPI';
66

7-
let stubApplyMiddleware;
8-
let stubCallAPIMethod;
97
const boundCallAPIMethod = _ => _;
108
test.beforeEach(t => {
11-
stubCallAPIMethod = sinon.stub(callAPIMethod, 'bind');
12-
stubCallAPIMethod.returns(boundCallAPIMethod);
13-
stubApplyMiddleware = sinon.stub(applyMiddleware, 'default');
9+
t.context.stubCallAPIMethod = sinon.stub(callAPIMethod, 'bind');
10+
t.context.stubCallAPIMethod.returns(boundCallAPIMethod);
11+
t.context.stubApplyMiddleware = sinon.stub(applyMiddleware, 'default');
1412
});
1513
test.afterEach.always(t => {
16-
stubApplyMiddleware.restore();
17-
stubCallAPIMethod.restore();
14+
t.context.stubApplyMiddleware.restore();
15+
t.context.stubCallAPIMethod.restore();
1816
});
1917

2018
test('undefined resources', t => {
@@ -39,7 +37,7 @@ test('supports resource prefix insted of namespace', t => {
3937

4038
API.user.get(1);
4139

42-
t.true(stubCallAPIMethod.calledWithExactly(
40+
t.true(t.context.stubCallAPIMethod.calledWithExactly(
4341
null, 'https://example.com', {}, 'user-endpoint'
4442
), 'correct arguments passed to callAPIMethod');
4543
});
@@ -81,25 +79,25 @@ test('general behaviour', t => {
8179
const methodOptions = { specific: 'option' };
8280
API.user.delete({ id: 1 }, methodOptions);
8381

84-
t.true(stubCallAPIMethod.calledOnce);
85-
t.true(stubApplyMiddleware.calledOnce);
86-
t.true(stubCallAPIMethod.calledBefore(stubApplyMiddleware));
87-
t.true(stubCallAPIMethod.calledWithExactly(
82+
t.true(t.context.stubCallAPIMethod.calledOnce);
83+
t.true(t.context.stubApplyMiddleware.calledOnce);
84+
t.true(t.context.stubCallAPIMethod.calledBefore(t.context.stubApplyMiddleware));
85+
t.true(t.context.stubCallAPIMethod.calledWithExactly(
8886
null, 'https://example.com', {}, 'user'
8987
));
9088

91-
t.is(stubApplyMiddleware.lastCall.args[0], boundCallAPIMethod, 'stubApplyMiddleware called with boundCallAPIMethod');
92-
t.is(stubApplyMiddleware.lastCall.args[1], middleware, 'stubApplyMiddleware called with middleware');
93-
t.is(stubApplyMiddleware.lastCall.args[2], methodOptions, 'stubApplyMiddleware called with methodOptions');
94-
t.deepEqual(stubApplyMiddleware.lastCall.args[3], {
89+
t.is(t.context.stubApplyMiddleware.lastCall.args[0], boundCallAPIMethod, 't.context.stubApplyMiddleware called with boundCallAPIMethod');
90+
t.is(t.context.stubApplyMiddleware.lastCall.args[1], middleware, 't.context.stubApplyMiddleware called with middleware');
91+
t.is(t.context.stubApplyMiddleware.lastCall.args[2], methodOptions, 't.context.stubApplyMiddleware called with methodOptions');
92+
t.deepEqual(t.context.stubApplyMiddleware.lastCall.args[3], {
9593
path: ['delete', 1], options: { method: 'DELETE'}
96-
}, 'stubApplyMiddleware called with correct apiParams');
97-
t.is(stubApplyMiddleware.lastCall.args[4], 'user', 'stubApplyMiddleware called with correct resourceId');
98-
t.is(stubApplyMiddleware.lastCall.args[5], 'delete', 'stubApplyMiddleware called with correct method');
94+
}, 't.context.stubApplyMiddleware called with correct apiParams');
95+
t.is(t.context.stubApplyMiddleware.lastCall.args[4], 'user', 't.context.stubApplyMiddleware called with correct resourceId');
96+
t.is(t.context.stubApplyMiddleware.lastCall.args[5], 'delete', 't.context.stubApplyMiddleware called with correct method');
9997

10098
API.user.delete({ id: 1 });
10199

102-
t.true(stubCallAPIMethod.calledTwice);
103-
t.true(stubApplyMiddleware.calledTwice);
104-
t.true(stubCallAPIMethod.calledBefore(stubApplyMiddleware));
100+
t.true(t.context.stubCallAPIMethod.calledTwice);
101+
t.true(t.context.stubApplyMiddleware.calledTwice);
102+
t.true(t.context.stubCallAPIMethod.calledBefore(t.context.stubApplyMiddleware));
105103
});

0 commit comments

Comments
 (0)