fix(plugin-workflow): fix trigger bind logic to avoid duplication (#347)
This commit is contained in:
		
							parent
							
								
									b7ea6b0a5e
								
							
						
					
					
						commit
						eb49849803
					
				
							
								
								
									
										72
									
								
								packages/plugins/workflow/src/__tests__/workflow.test.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										72
									
								
								packages/plugins/workflow/src/__tests__/workflow.test.ts
									
									
									
									
									
										Normal 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);
 | 
			
		||||
    });
 | 
			
		||||
  });
 | 
			
		||||
});
 | 
			
		||||
@ -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());
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  },
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user