Skip to content

Commit 4106a49

Browse files
0x-r4bbitiurimatias
authored andcommitted
feat(utils/testing): make mock apiCall() async
This is needed because API handlers are potentially async as well.
1 parent 1a56d5f commit 4106a49

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

packages/stack/pipeline/test/api.spec.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,15 @@ describe('stack/pipeline/api', () => {
7777
test(`it should register ${method} ${endpoint}`, () => {
7878
pipelineApi.plugins.assert.apiCallRegistered(method, endpoint);
7979
});
80-
test('it should throw error when guarding bad files', () => {
80+
test('it should throw error when guarding bad files', async () => {
8181
const error = "testing error";
8282
pipelineApi.apiGuardBadFile = sinon.stub().throws(new Error(error));
83-
const resp = pipelineApi.plugins.mock.apiCall(method, endpoint, req);
83+
const resp = await pipelineApi.plugins.mock.apiCall(method, endpoint, req);
8484
assert(resp.send.calledWith({ error }));
8585
});
86-
test('it should return a file', () => {
86+
test('it should return a file', async () => {
8787
pipelineApi.apiGuardBadFile = sinon.stub().returns();
88-
const resp = pipelineApi.plugins.mock.apiCall(method, endpoint, req);
88+
const resp = await pipelineApi.plugins.mock.apiCall(method, endpoint, req);
8989
assert(readFileSync.calledWith(filepath, 'utf8'));
9090
assert(resp.send.calledWith({ name: "file", content: "content", path: filepath }));
9191
});
@@ -107,15 +107,15 @@ describe('stack/pipeline/api', () => {
107107
test(`it should register ${method} ${endpoint}`, () => {
108108
pipelineApi.plugins.assert.apiCallRegistered(method, endpoint);
109109
});
110-
test('it should throw error when guarding bad files', () => {
110+
test('it should throw error when guarding bad files', async () => {
111111
const error = "testing error";
112112
pipelineApi.apiGuardBadFile = sinon.stub().throws(new Error(error));
113-
const resp = pipelineApi.plugins.mock.apiCall(method, endpoint, req);
113+
const resp = await pipelineApi.plugins.mock.apiCall(method, endpoint, req);
114114
assert(resp.send.calledWith({ error }));
115115
});
116-
test('it should create a folder', () => {
116+
test('it should create a folder', async () => {
117117
pipelineApi.apiGuardBadFile = sinon.stub().returns();
118-
const resp = pipelineApi.plugins.mock.apiCall(method, endpoint, req);
118+
const resp = await pipelineApi.plugins.mock.apiCall(method, endpoint, req);
119119
assert(mkdirpSync.calledWith(filepath));
120120
assert(resp.send.calledWith({ name: "folder", path: filepath }));
121121
});
@@ -137,15 +137,15 @@ describe('stack/pipeline/api', () => {
137137
test(`it should register ${method} ${endpoint}`, () => {
138138
pipelineApi.plugins.assert.apiCallRegistered(method, endpoint);
139139
});
140-
test('it should throw error when guarding bad files', () => {
140+
test('it should throw error when guarding bad files', async () => {
141141
const error = "testing error";
142142
pipelineApi.apiGuardBadFile = sinon.stub().throws(new Error(error));
143-
const resp = pipelineApi.plugins.mock.apiCall(method, endpoint, req);
143+
const resp = await pipelineApi.plugins.mock.apiCall(method, endpoint, req);
144144
assert(resp.send.calledWith({ error }));
145145
});
146-
test('it should write a file to the filesystem', () => {
146+
test('it should write a file to the filesystem', async () => {
147147
pipelineApi.apiGuardBadFile = sinon.stub().returns();
148-
const resp = pipelineApi.plugins.mock.apiCall(method, endpoint, req);
148+
const resp = await pipelineApi.plugins.mock.apiCall(method, endpoint, req);
149149
assert(writeFileSync.calledWith(req.path, req.content, { encoding: 'utf8' }));
150150
assert(resp.send.calledWith({ name: "file", ...req }));
151151
});
@@ -167,15 +167,15 @@ describe('stack/pipeline/api', () => {
167167
test(`it should register ${method} ${endpoint}`, () => {
168168
pipelineApi.plugins.assert.apiCallRegistered(method, endpoint);
169169
});
170-
test('it should throw error when guarding bad files', () => {
170+
test('it should throw error when guarding bad files', async () => {
171171
const error = "testing error";
172172
pipelineApi.apiGuardBadFile = sinon.stub().throws(new Error(error));
173-
const resp = pipelineApi.plugins.mock.apiCall(method, endpoint, req);
173+
const resp = await pipelineApi.plugins.mock.apiCall(method, endpoint, req);
174174
assert(resp.send.calledWith({ error }));
175175
});
176-
test('it should delete a file from the filesystem', () => {
176+
test('it should delete a file from the filesystem', async () => {
177177
pipelineApi.apiGuardBadFile = sinon.stub().returns();
178-
const resp = pipelineApi.plugins.mock.apiCall(method, endpoint, req);
178+
const resp = await pipelineApi.plugins.mock.apiCall(method, endpoint, req);
179179
assert(removeSync.calledWith(req.path));
180180
assert(resp.send.called);
181181
});
@@ -211,8 +211,8 @@ describe('stack/pipeline/api', () => {
211211
test(`it should register ${method} ${endpoint}`, () => {
212212
pipelineApi.plugins.assert.apiCallRegistered(method, endpoint);
213213
});
214-
test('it should return a tree of file objects for the dapp', () => {
215-
const resp = pipelineApi.plugins.mock.apiCall(method, endpoint, req);
214+
test('it should return a tree of file objects for the dapp', async () => {
215+
const resp = await pipelineApi.plugins.mock.apiCall(method, endpoint, req);
216216
const expectedValue = [
217217
{
218218
isRoot: true,

packages/utils/testing/src/plugin.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ class PluginsMock {
142142
this.plugins = plugins;
143143
}
144144

145-
apiCall(method, endpoint, params) {
145+
async apiCall(method, endpoint, params) {
146146
const index = (method + endpoint).toLowerCase();
147147
const apiFn = this.plugins.plugin.apiCalls[index];
148148
assert(apiFn, `API call for '${method} ${endpoint}' wanted, but not registered`);
@@ -163,6 +163,7 @@ class PluginsMock {
163163
};
164164

165165
resp.status = sinon.fake.returns(resp);
166+
await apiFn(req, resp);
166167
return resp;
167168
}
168169
}

0 commit comments

Comments
 (0)