From f57c0f4024252d9bd396eb8f9043de5d2988dc2a Mon Sep 17 00:00:00 2001 From: ShohanRahman Date: Tue, 26 Mar 2024 09:53:26 +0100 Subject: [PATCH] test: on thow error --- test/services/resource-creator.test.js | 23 +++++++++++++++++++++ test/services/resources-updater.test.js | 27 +++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/test/services/resource-creator.test.js b/test/services/resource-creator.test.js index 16de590f..987a2e4f 100644 --- a/test/services/resource-creator.test.js +++ b/test/services/resource-creator.test.js @@ -65,6 +65,29 @@ describe('services > resource-creator', () => { expect(result).toStrictEqual(record); }); }); + + describe('when there is a scope and ResourcesGetter throw an error', () => { + it('should throw the error', async () => { + expect.assertions(1); + + const { Film } = buildModelMock(); + const record = { dataValues: { id: 1, title: 'The Godfather' } }; + + const error = new Error('Unauthorized'); + error.statusCode = 401; + jest + .spyOn(ResourceGetter.prototype, 'perform') + .mockRejectedValue(error); + + jest.spyOn(Film.prototype, 'save').mockReturnValue(record); + jest.spyOn(scopeManager, 'getScopeForUser').mockReturnValue({}); + + const body = { actor: 2 }; + + const resourceCreator = new ResourceCreator(Film, params, body, user); + await expect(resourceCreator.perform()).rejects.toThrow(error); + }); + }); }); describe('_getTargetKey', () => { diff --git a/test/services/resources-updater.test.js b/test/services/resources-updater.test.js index 2b0ce953..06bcc9dd 100644 --- a/test/services/resources-updater.test.js +++ b/test/services/resources-updater.test.js @@ -78,4 +78,31 @@ describe('services > resources-updater', () => { await expect(resourceUpdater.perform()).rejects.toThrow(createError(404, 'The film #2 does not exist.')); }); }); + + describe('when there is a scope and ResourcesGetter throw an error', () => { + it('should throw the error', async () => { + expect.assertions(1); + + const { Film } = buildModelMock(); + jest.spyOn(scopeManager, 'getScopeForUser').mockReturnValue({ aggregator: 'and', conditions: [{ field: 'name', operator: 'contains', value: 'Scope value' }] }); + + const record = { dataValues: { id: 1, title: 'The Godfather' }, validate: () => {}, save: () => {} }; + + jest.spyOn(record, 'validate'); + jest.spyOn(record, 'save'); + + const error = new Error('Unauthorized'); + error.statusCode = 401; + jest + .spyOn(ResourceGetter.prototype, 'perform') + .mockRejectedValue(error); + + jest.spyOn(QueryOptions.prototype, 'filterByConditionTree').mockResolvedValue(); + + const resourceUpdater = new ResourceUpdater(Film, { ...params, recordId: 2 }, { name: 'new name' }, user); + jest.spyOn(resourceUpdater._model, 'findOne').mockReturnValue(record); + + await expect(resourceUpdater.perform()).rejects.toThrow(error); + }); + }); });