Skip to content

Commit

Permalink
test: on thow error
Browse files Browse the repository at this point in the history
  • Loading branch information
ShohanRahman committed Mar 26, 2024
1 parent 2be163c commit f57c0f4
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
23 changes: 23 additions & 0 deletions test/services/resource-creator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
27 changes: 27 additions & 0 deletions test/services/resources-updater.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});

0 comments on commit f57c0f4

Please sign in to comment.