From 4106a49379460b9d37c6ce17d0503052b527c406 Mon Sep 17 00:00:00 2001 From: Pascal Precht Date: Mon, 17 Feb 2020 12:26:17 +0100 Subject: [PATCH] feat(utils/testing): make mock apiCall() async This is needed because API handlers are potentially async as well. --- packages/stack/pipeline/test/api.spec.js | 36 ++++++++++++------------ packages/utils/testing/src/plugin.js | 3 +- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/packages/stack/pipeline/test/api.spec.js b/packages/stack/pipeline/test/api.spec.js index 93578f55a2..05f52f4afb 100644 --- a/packages/stack/pipeline/test/api.spec.js +++ b/packages/stack/pipeline/test/api.spec.js @@ -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 })); }); @@ -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 })); }); @@ -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 })); }); @@ -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); }); @@ -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, diff --git a/packages/utils/testing/src/plugin.js b/packages/utils/testing/src/plugin.js index 971e329b10..5890b053e4 100644 --- a/packages/utils/testing/src/plugin.js +++ b/packages/utils/testing/src/plugin.js @@ -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`); @@ -163,6 +163,7 @@ class PluginsMock { }; resp.status = sinon.fake.returns(resp); + await apiFn(req, resp); return resp; } }