mirror of
https://github.com/strapi/strapi.git
synced 2025-11-11 15:49:50 +00:00
Add lifecyle manager tests
This commit is contained in:
parent
9020fc4e91
commit
c1da5a5685
@ -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();
|
||||||
|
});
|
||||||
|
});
|
||||||
@ -24,7 +24,7 @@ class DatabaseManager {
|
|||||||
this.models = new Map();
|
this.models = new Map();
|
||||||
|
|
||||||
this.migrations = createMigrationManager(this);
|
this.migrations = createMigrationManager(this);
|
||||||
this.lifecycles = createLifecycleManager(this);
|
this.lifecycles = createLifecycleManager();
|
||||||
}
|
}
|
||||||
|
|
||||||
async initialize() {
|
async initialize() {
|
||||||
|
|||||||
@ -3,9 +3,8 @@ const debug = require('debug')('strapi-database:lifecycle');
|
|||||||
const { isFunction, isNil } = require('lodash/fp');
|
const { isFunction, isNil } = require('lodash/fp');
|
||||||
|
|
||||||
class LifecycleManager {
|
class LifecycleManager {
|
||||||
constructor(db) {
|
constructor() {
|
||||||
debug('Initialize lifecycle manager');
|
debug('Initialize lifecycle manager');
|
||||||
this.db = db;
|
|
||||||
this.lifecycles = [];
|
this.lifecycles = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -13,6 +12,7 @@ class LifecycleManager {
|
|||||||
debug('Register lifecycle');
|
debug('Register lifecycle');
|
||||||
|
|
||||||
this.lifecycles.push(lifecycle);
|
this.lifecycles.push(lifecycle);
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
async run(action, model, ...args) {
|
async run(action, model, ...args) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user