"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const redux_saga_test_plan_1 = require("redux-saga-test-plan");
const actions_1 = require("../src/store/cas/actions");
const sagas_1 = require("../src/store/cas/sagas");
describe('performEnableCas', () => {
    describe('in the successful case', () => {
        it(`calls setCasStatus with 'allowing', performs the countdown and calls setCasStatus with 'allowed'`, async () => {
            const setCasStatus = jest.fn((casStatus) => Promise.resolve(casStatus));
            // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
            await (0, redux_saga_test_plan_1.expectSaga)(sagas_1.performEnableCas, { setCasStatus }, (0, actions_1.allowCas)(2))
                .put((0, actions_1.allowCasCountdown)(2))
                .put((0, actions_1.updateCasRemaining)(1))
                .put((0, actions_1.allowCasSucceeded)())
                .run(false);
            expect(setCasStatus).toHaveBeenCalledTimes(2);
            expect(setCasStatus).toHaveBeenNthCalledWith(1, 'allowing');
            expect(setCasStatus).toHaveBeenNthCalledWith(2, 'allowed');
        });
    });
    describe('when CAS software is already enabled', () => {
        it(`skips the countdown and calls setCasStatus with 'allowed'`, async () => {
            const setCasStatus = jest.fn(() => Promise.resolve('allowed'));
            // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
            await (0, redux_saga_test_plan_1.expectSaga)(sagas_1.performEnableCas, { setCasStatus }, (0, actions_1.allowCas)(2))
                .put((0, actions_1.allowCasSucceeded)())
                .run(false);
            expect(setCasStatus).toHaveBeenCalledTimes(1);
            expect(setCasStatus).toHaveBeenNthCalledWith(1, 'allowing');
        });
    });
    describe('when the user cancels the countdown', () => {
        it(`calls setCasStatus with 'forbidden' if the user cancels the countdown`, async () => {
            const setCasStatus = jest.fn((casStatus) => Promise.resolve(casStatus));
            // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
            await (0, redux_saga_test_plan_1.expectSaga)(sagas_1.performEnableCas, { setCasStatus }, (0, actions_1.allowCas)(2))
                .put((0, actions_1.allowCasCountdown)(2))
                .put((0, actions_1.updateCasRemaining)(1))
                .delay(1500)
                .dispatch((0, actions_1.allowCasCancelled)())
                .run(false);
            expect(setCasStatus).toHaveBeenCalledTimes(2);
            expect(setCasStatus).toHaveBeenNthCalledWith(1, 'allowing');
            expect(setCasStatus).toHaveBeenNthCalledWith(2, 'forbidden');
        });
        it(`doesn't cancel the countdown if CAS software has been allowed during the countdown`, async () => {
            const setCasStatus = jest
                .fn((casStatus) => Promise.resolve(casStatus))
                .mockImplementationOnce(() => Promise.resolve('allowing'))
                .mockImplementationOnce(() => Promise.resolve('allowed'));
            // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
            await (0, redux_saga_test_plan_1.expectSaga)(sagas_1.performEnableCas, { setCasStatus }, (0, actions_1.allowCas)(2))
                .put((0, actions_1.allowCasCountdown)(2))
                .put((0, actions_1.updateCasRemaining)(1))
                .put((0, actions_1.allowCasSucceeded)())
                .delay(1500)
                .dispatch((0, actions_1.allowCasCancelled)())
                .run(false);
            expect(setCasStatus).toHaveBeenCalledTimes(2);
            expect(setCasStatus).toHaveBeenNthCalledWith(1, 'allowing');
            expect(setCasStatus).toHaveBeenNthCalledWith(2, 'forbidden');
        });
        it(`cancels the process if the post-countdown setCasStatus returns 'forbidden'`, async () => {
            const setCasStatus = jest
                .fn((casStatus) => Promise.resolve(casStatus))
                .mockImplementationOnce(() => Promise.resolve('allowing'))
                .mockImplementationOnce(() => Promise.resolve('forbidden'));
            // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
            await (0, redux_saga_test_plan_1.expectSaga)(sagas_1.performEnableCas, { setCasStatus }, (0, actions_1.allowCas)(2))
                .put((0, actions_1.allowCasCountdown)(2))
                .put((0, actions_1.updateCasRemaining)(1))
                .put((0, actions_1.allowCasCancelled)())
                .run(false);
            expect(setCasStatus).toHaveBeenCalledTimes(2);
            expect(setCasStatus).toHaveBeenNthCalledWith(1, 'allowing');
            expect(setCasStatus).toHaveBeenNthCalledWith(2, 'allowed');
        });
    });
});
//# sourceMappingURL=casSaga.test.js.map