Skip to content

Commit

Permalink
test: test on record not found
Browse files Browse the repository at this point in the history
  • Loading branch information
ShohanRahman committed Mar 26, 2024
1 parent 99dea55 commit 2be163c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion test/services/resource-creator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('services > resource-creator', () => {

describe('perform', () => {
describe('when the getter does not found the record', () => {
it('should throw a 404 error', async () => {
it('should catch the 404 error and return the record', async () => {
expect.assertions(1);

const { Film } = buildModelMock();
Expand Down
22 changes: 21 additions & 1 deletion test/services/resources-updater.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { scopeManager } from 'forest-express';
import Sequelize from 'sequelize';
import createError from 'http-errors';
import ResourceUpdater from '../../src/services/resource-updater';
import ResourceGetter from '../../src/services/resource-getter';
import QueryOptions from '../../src/services/query-options';
Expand Down Expand Up @@ -42,6 +43,7 @@ describe('services > resources-updater', () => {

jest.spyOn(record, 'validate');
jest.spyOn(record, 'save');

const error = new Error('Record not found');
error.statusCode = 404;
jest
Expand All @@ -51,11 +53,29 @@ describe('services > resources-updater', () => {
jest.spyOn(QueryOptions.prototype, 'filterByConditionTree').mockResolvedValue();

const resourceUpdater = new ResourceUpdater(Film, params, { name: 'new name' }, user);

jest.spyOn(resourceUpdater._model, 'findOne').mockReturnValue(record);

const result = await resourceUpdater.perform();

expect(result).toStrictEqual(record);
});
});

describe('when it update with a scope but the record does not exist', () => {
it('should throw 404', async () => {
expect.assertions(1);

const { Film } = buildModelMock();
jest.spyOn(scopeManager, 'getScopeForUser').mockReturnValue({ aggregator: 'and', conditions: [{ field: 'name', operator: 'contains', value: 'Scope value' }] });

const record = null;

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(createError(404, 'The film #2 does not exist.'));
});
});
});

0 comments on commit 2be163c

Please sign in to comment.