Skip to content

Commit

Permalink
test: update with scope
Browse files Browse the repository at this point in the history
  • Loading branch information
ShohanRahman committed Mar 25, 2024
1 parent 806ada6 commit 808148f
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions test/services/resources-updater.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { scopeManager } from 'forest-express';
import Sequelize from 'sequelize';
import ResourceUpdater from '../../src/services/resource-updater';
import ResourceGetter from '../../src/services/resource-getter';
import QueryOptions from '../../src/services/query-options';

describe('services > resources-updater', () => {
const user = { renderingId: 1 };
const params = { timezone: 'Europe/Paris' };

const buildModelMock = () => {
// Sequelize is created here without connection to a database
const sequelize = new Sequelize({ dialect: 'postgres' });

const Actor = sequelize.define('actor', {});
const Film = sequelize.define('film', {});
const ActorFilm = sequelize.define('ActorFilem', {
actorId: {
type: Sequelize.DataTypes.INTEGER,
primaryKey: true,
},
filmId: {
type: Sequelize.DataTypes.INTEGER,
primaryKey: true,
},
});

ActorFilm.belongsTo(Actor);
ActorFilm.belongsTo(Film);

return { Actor, Film, ActorFilm };
};

describe('when it update with a scope and it is not in scope anymore', () => {
it('should still return the record', 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('Record not found');
error.statusCode = 404;
jest
.spyOn(ResourceGetter.prototype, 'perform')
.mockRejectedValue(error);

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);
});
});
});

0 comments on commit 808148f

Please sign in to comment.