fix(plugin-workflow): fix trigger bind logic to avoid duplication (#347)

This commit is contained in:
Junyi 2022-04-30 16:41:40 +08:00 committed by GitHub
parent b7ea6b0a5e
commit eb49849803
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 78 additions and 4 deletions

View File

@ -0,0 +1,72 @@
import { Application } from '@nocobase/server';
import Database from '@nocobase/database';
import { getApp } from '.';
import { EXECUTION_STATUS } from '../constants';
describe('workflow > workflow', () => {
let app: Application;
let db: Database;
let PostModel;
let WorkflowModel;
beforeEach(async () => {
app = await getApp();
db = app.db;
WorkflowModel = db.getCollection('workflows').model;
PostModel = db.getCollection('posts').model;
});
afterEach(() => db.close());
describe('toggle', () => {
it('toggle after update', async () => {
const workflow = await WorkflowModel.create({
enabled: true,
type: 'collection',
config: {
mode: 1,
collection: 'posts'
}
});
const { hooks } = PostModel.options;
expect(hooks.afterCreate.length).toBe(1);
await workflow.update({
config: {
mode: 2,
collection: 'posts'
}
});
expect(hooks.afterCreate.length).toBe(0);
expect(hooks.afterUpdate.length).toBe(1);
await workflow.update({
config: {
mode: 7,
collection: 'posts'
}
});
expect(hooks.afterCreate.length).toBe(1);
expect(hooks.afterUpdate.length).toBe(1);
expect(hooks.afterDestroy.length).toBe(1);
await workflow.update({
enabled: false
});
expect(hooks.afterCreate.length).toBe(0);
expect(hooks.afterUpdate.length).toBe(0);
expect(hooks.afterDestroy.length).toBe(0);
await workflow.update({
enabled: true
});
expect(hooks.afterCreate.length).toBe(1);
expect(hooks.afterUpdate.length).toBe(1);
expect(hooks.afterDestroy.length).toBe(1);
});
});
});

View File

@ -47,10 +47,12 @@ export default {
}
// TODO: duplication when mode change should be considered
for (let [key, event] of MODE_BITMAP_EVENTS.entries()) {
if (mode & key
&& !Collection.model.options.hooks[event]?.find(item => item.name && item.name === this.getHookId())
) {
Collection.model.addHook(event, this.getHookId(), bindHandler.call(this, callback));
if (mode & key) {
if (!Collection.model.options.hooks[event]?.find(item => item.name && item.name === this.getHookId())) {
Collection.model.addHook(event, this.getHookId(), bindHandler.call(this, callback));
}
} else {
Collection.model.removeHook(event, this.getHookId());
}
}
},