UNPKG

node-sagas

Version:

Library for handling distributed transactions in the microservices architecture

57 lines 2.66 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const saga_1 = require("../saga"); const saga_params_1 = require("./saga-params"); const saga_flow_1 = require("../saga-flow"); const exceptions_1 = require("../exceptions"); const expectSagaFlowMethod = (sagaFlowMethod, sagaParams) => { expect(sagaFlowMethod).toHaveBeenCalledTimes(1); expect(sagaFlowMethod).toHaveBeenCalledWith(sagaParams); }; describe('Saga', () => { let sagaFlow; let saga; beforeEach(() => { sagaFlow = new saga_flow_1.SagaFlow(); saga = new saga_1.Saga(sagaFlow); }); test('construct', () => { expect(saga).toBeInstanceOf(saga_1.Saga); expect(saga.getState()).toBe(saga_1.SagaStates.New); }); test('execute with empty steps', () => { expect(async () => await saga.execute({})).not.toThrow(); }); test('execute with positive flow', async () => { sagaFlow.invoke = jest.fn(); sagaFlow.compensate = jest.fn(); const sagaParams = new saga_params_1.SagaParams(); const sagaPromise = saga.execute(sagaParams); expect(saga.getState()).toBe(saga_1.SagaStates.InProgress); await sagaPromise; expect(saga.getState()).toBe(saga_1.SagaStates.Complete); expectSagaFlowMethod(sagaFlow.invoke, sagaParams); expect(sagaFlow.compensate).not.toHaveBeenCalled(); }); test('execute with compensation flow', async () => { const sagaFlow = new saga_flow_1.SagaFlow(); sagaFlow.invoke = jest.fn(() => Promise.reject(new Error())); sagaFlow.compensate = jest.fn(); const saga = new saga_1.Saga(sagaFlow); const sagaParams = new saga_params_1.SagaParams(); await expect(saga.execute(sagaParams)).rejects.toThrow(exceptions_1.SagaExecutionFailed); expect(saga.getState()).toBe(saga_1.SagaStates.CompensationComplete); expectSagaFlowMethod(sagaFlow.invoke, sagaParams); expectSagaFlowMethod(sagaFlow.compensate, sagaParams); }); test('execute with compensation flow error', async () => { sagaFlow.invoke = jest.fn(() => Promise.reject(new Error())); sagaFlow.compensate = jest.fn(() => Promise.reject(new Error())); const sagaParams = new saga_params_1.SagaParams(); await expect(saga.execute(sagaParams)).rejects.toThrow(exceptions_1.SagaCompensationFailed); expect(saga.getState()).toBe(saga_1.SagaStates.CompensationError); expectSagaFlowMethod(sagaFlow.invoke, sagaParams); expectSagaFlowMethod(sagaFlow.compensate, sagaParams); }); }); //# sourceMappingURL=saga.spec.js.map