import { describe, test, expect } from 'vitest'; import { ActionHandlerBuilder, buildHandler } from '../src/action-handler-builder'; import { actionCreator } from '../src/action'; import { ActionHandler } from '../src/action-handler'; import { ActionMap } from '../src/action-map'; import { b } from 'vitest/dist/chunks/suite.d.FvehnV49'; describe('ActionHandlerBuilder', () => { const InputAC1 = actionCreator('INPUT1'); const InputAC2 = actionCreator('INPUT2'); const OutputAC1 = actionCreator('OUTPUT1'); const OutputAC2 = actionCreator('OUTPUT2'); const runCallback = (actionMap: ActionMap) => {}; const injectDep = { service: 'testService' }; test('should build an ActionHandler with all options specified', () => { const handler = buildHandler('TestHandler') .inputs(InputAC1) .in(InputAC2) // Test alias .outputs(OutputAC1) .out(OutputAC2) // Test alias .run(runCallback) .inject(injectDep) .create(); expect(handler).toBeInstanceOf(ActionHandler); expect(handler.getName()).toBe('TestHandler'); expect(handler.getInputs()).toEqual([InputAC1, InputAC2]); expect(handler.getOutputs()).toEqual([OutputAC1, OutputAC2]); expect(handler.getInjected()).toBe(injectDep); // @ts-expect-error - accessing protected property for test expect(handler._callback).toBe(runCallback); }); test('should build an ActionHandler using aliases ins/outs', () => { const handler = buildHandler().name('AliasHandler').ins(InputAC1).outs(OutputAC1).run(runCallback).create(); expect(handler).toBeInstanceOf(ActionHandler); expect(handler.getName()).toBe('AliasHandler'); expect(handler.getInputs()).toEqual([InputAC1]); expect(handler.getOutputs()).toEqual([OutputAC1]); // @ts-expect-error - accessing protected property for test expect(handler._callback).toBe(runCallback); }); test('should build an ActionHandler without a name', () => { const handler = buildHandler().inputs(InputAC1).run(runCallback).create(); expect(handler).toBeInstanceOf(ActionHandler); expect(handler.getName()).toBe('ActionHandler(Unnamed)'); // Default name expect(handler.getInputs()).toEqual([InputAC1]); expect(handler.getOutputs()).toEqual(undefined); expect(handler.getInjected()).toBeUndefined(); }); test('should build an ActionHandler without outputs', () => { const handler = buildHandler().name('NoOutputHandler').inputs(InputAC1).run(runCallback).create(); expect(handler).toBeInstanceOf(ActionHandler); expect(handler.getName()).toBe('NoOutputHandler'); expect(handler.getInputs()).toEqual([InputAC1]); expect(handler.getOutputs()).toEqual(undefined); expect(handler.getInjected()).toBeUndefined(); }); test('should build an ActionHandler without inject', () => { const handler = buildHandler('NoInjectHandler').inputs(InputAC1).outputs(OutputAC1).run(runCallback).create(); expect(handler).toBeInstanceOf(ActionHandler); expect(handler.getName()).toBe('NoInjectHandler'); expect(handler.getInputs()).toEqual([InputAC1]); expect(handler.getOutputs()).toEqual([OutputAC1]); expect(handler.getInjected()).toBeUndefined(); }); test('should throw error if inputs are not specified', () => { const builder = buildHandler().name('ErrorNoInput').run(runCallback); expect(() => builder.create()).toThrow(/Inputs must be specified for ActionHandler/); }); test('should throw error if inputs array is empty', () => { const builder = buildHandler('ErrorEmptyInput').run(runCallback); expect(() => builder.inputs()).toThrow(/At least one input action creator must be specified/); expect(() => builder.ins()).toThrow(/At least one input action creator must be specified/); expect(() => builder.in()).toThrow(/At least one input action creator must be specified/); expect(() => builder.create()).toThrow(/Inputs must be specified for ActionHandler/); }); test('should throw error if outputs array is empty', () => { const builder = buildHandler('ErrorEmptyInput').run(runCallback); expect(() => builder.outputs()).toThrow(/At least one output action creator must be specified/); expect(() => builder.outs()).toThrow(/At least one output action creator must be specified/); expect(() => builder.out()).toThrow(/At least one output action creator must be specified/); }); test('should throw error if run callback is not specified', () => { const builder = buildHandler('ErrorNoCallback').inputs(InputAC1); expect(() => builder.create()).toThrow(/Run callback must be specified for ActionHandler/); }); test('buildHandler factory function should create a builder', () => { const builder = buildHandler('FactoryHandler'); expect(builder).toBeInstanceOf(ActionHandlerBuilder); // @ts-expect-error - accessing private property for test expect(builder._name).toBe('FactoryHandler'); const handler = builder.inputs(InputAC1).run(runCallback).create(); expect(handler).toBeInstanceOf(ActionHandler); expect(handler.getName()).toBe('FactoryHandler'); }); test('buildHandler factory function without name', () => { const builder = buildHandler(); expect(builder).toBeInstanceOf(ActionHandlerBuilder); // @ts-expect-error - accessing private property for test expect(builder._name).toBeUndefined(); const handler = builder.inputs(InputAC1).run(runCallback).create(); expect(handler).toBeInstanceOf(ActionHandler); expect(handler.getName()).toBe('ActionHandler(Unnamed)'); }); });