Add lifecyle manager tests

This commit is contained in:
Alexandre Bodin 2021-02-17 15:51:47 +01:00
parent 9020fc4e91
commit c1da5a5685
3 changed files with 57 additions and 3 deletions

View File

@ -0,0 +1,54 @@
'use strict';
const createLifecycleManager = require('../lifecycle-manager');
describe('Lifecycle Manager', () => {
test('Allows registering lifecycles', () => {
const manager = createLifecycleManager();
const lifecycle = {};
manager.register(lifecycle);
expect(manager.lifecycles).toEqual([lifecycle]);
});
test('Will run all the lifecycles if no model specified', async () => {
const lifecycleA = {
find: jest.fn(),
};
const lifecycleB = {
find: jest.fn(),
};
const manager = createLifecycleManager();
manager.register(lifecycleA).register(lifecycleB);
await manager.run('find', { uid: 'test-uid' });
expect(lifecycleA.find).toHaveBeenCalled();
expect(lifecycleB.find).toHaveBeenCalled();
});
test('Will match on model if specified', async () => {
const lifecycleA = {
model: 'test-uid',
find: jest.fn(),
};
const lifecycleB = {
model: 'other-uid',
find: jest.fn(),
};
const manager = createLifecycleManager();
manager.register(lifecycleA).register(lifecycleB);
await manager.run('find', { uid: 'test-uid' });
expect(lifecycleA.find).toHaveBeenCalled();
expect(lifecycleB.find).not.toHaveBeenCalled();
});
});

View File

@ -24,7 +24,7 @@ class DatabaseManager {
this.models = new Map();
this.migrations = createMigrationManager(this);
this.lifecycles = createLifecycleManager(this);
this.lifecycles = createLifecycleManager();
}
async initialize() {

View File

@ -3,9 +3,8 @@ const debug = require('debug')('strapi-database:lifecycle');
const { isFunction, isNil } = require('lodash/fp');
class LifecycleManager {
constructor(db) {
constructor() {
debug('Initialize lifecycle manager');
this.db = db;
this.lifecycles = [];
}
@ -13,6 +12,7 @@ class LifecycleManager {
debug('Register lifecycle');
this.lifecycles.push(lifecycle);
return this;
}
async run(action, model, ...args) {