Skip to content

Commit

Permalink
feat(utils/testing): make mock apiCall() async
Browse files Browse the repository at this point in the history
This is needed because API handlers are potentially async as well.
  • Loading branch information
0x-r4bbit authored and iurimatias committed Feb 26, 2020
1 parent 1a56d5f commit 4106a49
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
36 changes: 18 additions & 18 deletions packages/stack/pipeline/test/api.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ describe('stack/pipeline/api', () => {
test(`it should register ${method} ${endpoint}`, () => {
pipelineApi.plugins.assert.apiCallRegistered(method, endpoint);
});
test('it should throw error when guarding bad files', () => {
test('it should throw error when guarding bad files', async () => {
const error = "testing error";
pipelineApi.apiGuardBadFile = sinon.stub().throws(new Error(error));
const resp = pipelineApi.plugins.mock.apiCall(method, endpoint, req);
const resp = await pipelineApi.plugins.mock.apiCall(method, endpoint, req);
assert(resp.send.calledWith({ error }));
});
test('it should return a file', () => {
test('it should return a file', async () => {
pipelineApi.apiGuardBadFile = sinon.stub().returns();
const resp = pipelineApi.plugins.mock.apiCall(method, endpoint, req);
const resp = await pipelineApi.plugins.mock.apiCall(method, endpoint, req);
assert(readFileSync.calledWith(filepath, 'utf8'));
assert(resp.send.calledWith({ name: "file", content: "content", path: filepath }));
});
Expand All @@ -107,15 +107,15 @@ describe('stack/pipeline/api', () => {
test(`it should register ${method} ${endpoint}`, () => {
pipelineApi.plugins.assert.apiCallRegistered(method, endpoint);
});
test('it should throw error when guarding bad files', () => {
test('it should throw error when guarding bad files', async () => {
const error = "testing error";
pipelineApi.apiGuardBadFile = sinon.stub().throws(new Error(error));
const resp = pipelineApi.plugins.mock.apiCall(method, endpoint, req);
const resp = await pipelineApi.plugins.mock.apiCall(method, endpoint, req);
assert(resp.send.calledWith({ error }));
});
test('it should create a folder', () => {
test('it should create a folder', async () => {
pipelineApi.apiGuardBadFile = sinon.stub().returns();
const resp = pipelineApi.plugins.mock.apiCall(method, endpoint, req);
const resp = await pipelineApi.plugins.mock.apiCall(method, endpoint, req);
assert(mkdirpSync.calledWith(filepath));
assert(resp.send.calledWith({ name: "folder", path: filepath }));
});
Expand All @@ -137,15 +137,15 @@ describe('stack/pipeline/api', () => {
test(`it should register ${method} ${endpoint}`, () => {
pipelineApi.plugins.assert.apiCallRegistered(method, endpoint);
});
test('it should throw error when guarding bad files', () => {
test('it should throw error when guarding bad files', async () => {
const error = "testing error";
pipelineApi.apiGuardBadFile = sinon.stub().throws(new Error(error));
const resp = pipelineApi.plugins.mock.apiCall(method, endpoint, req);
const resp = await pipelineApi.plugins.mock.apiCall(method, endpoint, req);
assert(resp.send.calledWith({ error }));
});
test('it should write a file to the filesystem', () => {
test('it should write a file to the filesystem', async () => {
pipelineApi.apiGuardBadFile = sinon.stub().returns();
const resp = pipelineApi.plugins.mock.apiCall(method, endpoint, req);
const resp = await pipelineApi.plugins.mock.apiCall(method, endpoint, req);
assert(writeFileSync.calledWith(req.path, req.content, { encoding: 'utf8' }));
assert(resp.send.calledWith({ name: "file", ...req }));
});
Expand All @@ -167,15 +167,15 @@ describe('stack/pipeline/api', () => {
test(`it should register ${method} ${endpoint}`, () => {
pipelineApi.plugins.assert.apiCallRegistered(method, endpoint);
});
test('it should throw error when guarding bad files', () => {
test('it should throw error when guarding bad files', async () => {
const error = "testing error";
pipelineApi.apiGuardBadFile = sinon.stub().throws(new Error(error));
const resp = pipelineApi.plugins.mock.apiCall(method, endpoint, req);
const resp = await pipelineApi.plugins.mock.apiCall(method, endpoint, req);
assert(resp.send.calledWith({ error }));
});
test('it should delete a file from the filesystem', () => {
test('it should delete a file from the filesystem', async () => {
pipelineApi.apiGuardBadFile = sinon.stub().returns();
const resp = pipelineApi.plugins.mock.apiCall(method, endpoint, req);
const resp = await pipelineApi.plugins.mock.apiCall(method, endpoint, req);
assert(removeSync.calledWith(req.path));
assert(resp.send.called);
});
Expand Down Expand Up @@ -211,8 +211,8 @@ describe('stack/pipeline/api', () => {
test(`it should register ${method} ${endpoint}`, () => {
pipelineApi.plugins.assert.apiCallRegistered(method, endpoint);
});
test('it should return a tree of file objects for the dapp', () => {
const resp = pipelineApi.plugins.mock.apiCall(method, endpoint, req);
test('it should return a tree of file objects for the dapp', async () => {
const resp = await pipelineApi.plugins.mock.apiCall(method, endpoint, req);
const expectedValue = [
{
isRoot: true,
Expand Down
3 changes: 2 additions & 1 deletion packages/utils/testing/src/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class PluginsMock {
this.plugins = plugins;
}

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

resp.status = sinon.fake.returns(resp);
await apiFn(req, resp);
return resp;
}
}
Expand Down

0 comments on commit 4106a49

Please sign in to comment.