"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const types_devices_1 = require("@ledgerhq/types-devices");
const types_live_1 = require("@ledgerhq/types-live");
const reducer_1 = __importStar(require("./reducer"));
const actions_1 = require("./actions");
const initializationParamsA = [
    {
        deviceModelId: types_devices_1.DeviceModelId.nanoX,
        actionsIds: [
            types_live_1.PostOnboardingActionId.claimMock,
            types_live_1.PostOnboardingActionId.migrateAssetsMock,
            types_live_1.PostOnboardingActionId.personalizeMock,
        ],
    },
];
// initialState -> importPostOnboardingState(...initializationParamsA)
const stateA0 = {
    deviceModelId: types_devices_1.DeviceModelId.nanoX,
    walletEntryPointDismissed: false,
    entryPointFirstDisplayedDate: new Date("2020-01-20"),
    actionsToComplete: [
        types_live_1.PostOnboardingActionId.claimMock,
        types_live_1.PostOnboardingActionId.migrateAssetsMock,
        types_live_1.PostOnboardingActionId.personalizeMock,
    ],
    actionsCompleted: {
        [types_live_1.PostOnboardingActionId.claimMock]: false,
        [types_live_1.PostOnboardingActionId.migrateAssetsMock]: false,
        [types_live_1.PostOnboardingActionId.personalizeMock]: false,
    },
    lastActionCompleted: null,
    postOnboardingInProgress: true,
};
// stateA0 -> setPostOnboardingActionCompleted(claimMock)
const stateA1 = {
    deviceModelId: types_devices_1.DeviceModelId.nanoX,
    walletEntryPointDismissed: false,
    entryPointFirstDisplayedDate: new Date("2020-01-20"),
    actionsToComplete: [
        types_live_1.PostOnboardingActionId.claimMock,
        types_live_1.PostOnboardingActionId.migrateAssetsMock,
        types_live_1.PostOnboardingActionId.personalizeMock,
    ],
    actionsCompleted: {
        [types_live_1.PostOnboardingActionId.claimMock]: true,
        [types_live_1.PostOnboardingActionId.migrateAssetsMock]: false,
        [types_live_1.PostOnboardingActionId.personalizeMock]: false,
    },
    lastActionCompleted: types_live_1.PostOnboardingActionId.claimMock,
    postOnboardingInProgress: true,
};
// stateA1 -> clearPostOnboardingLastActionCompleted()
const stateA2 = {
    deviceModelId: types_devices_1.DeviceModelId.nanoX,
    walletEntryPointDismissed: false,
    entryPointFirstDisplayedDate: new Date("2020-01-20"),
    actionsToComplete: [
        types_live_1.PostOnboardingActionId.claimMock,
        types_live_1.PostOnboardingActionId.migrateAssetsMock,
        types_live_1.PostOnboardingActionId.personalizeMock,
    ],
    actionsCompleted: {
        [types_live_1.PostOnboardingActionId.claimMock]: true,
        [types_live_1.PostOnboardingActionId.migrateAssetsMock]: false,
        [types_live_1.PostOnboardingActionId.personalizeMock]: false,
    },
    lastActionCompleted: null,
    postOnboardingInProgress: true,
};
// stateA2 -> setPostOnboardingActionCompleted(personalizeMock)
const stateA3 = {
    deviceModelId: types_devices_1.DeviceModelId.nanoX,
    walletEntryPointDismissed: false,
    entryPointFirstDisplayedDate: new Date("2020-01-20"),
    actionsToComplete: [
        types_live_1.PostOnboardingActionId.claimMock,
        types_live_1.PostOnboardingActionId.migrateAssetsMock,
        types_live_1.PostOnboardingActionId.personalizeMock,
    ],
    actionsCompleted: {
        [types_live_1.PostOnboardingActionId.claimMock]: true,
        [types_live_1.PostOnboardingActionId.migrateAssetsMock]: false,
        [types_live_1.PostOnboardingActionId.personalizeMock]: true, // stateA2 -> setPostOnboardingActionCompleted(personalizeMock)
    },
    lastActionCompleted: types_live_1.PostOnboardingActionId.personalizeMock,
    postOnboardingInProgress: true,
};
// stateA3 -> hidePostOnboardingWalletEntryPoint()
const stateA4 = {
    deviceModelId: types_devices_1.DeviceModelId.nanoX,
    walletEntryPointDismissed: true,
    entryPointFirstDisplayedDate: null,
    actionsToComplete: [
        types_live_1.PostOnboardingActionId.claimMock,
        types_live_1.PostOnboardingActionId.migrateAssetsMock,
        types_live_1.PostOnboardingActionId.personalizeMock,
    ],
    actionsCompleted: {
        [types_live_1.PostOnboardingActionId.claimMock]: true,
        [types_live_1.PostOnboardingActionId.migrateAssetsMock]: false,
        [types_live_1.PostOnboardingActionId.personalizeMock]: true,
    },
    lastActionCompleted: types_live_1.PostOnboardingActionId.personalizeMock,
    postOnboardingInProgress: true,
};
const initializationParamsB = [
    {
        deviceModelId: types_devices_1.DeviceModelId.nanoS,
        actionsIds: [types_live_1.PostOnboardingActionId.claimMock],
    },
];
// initialState -> importPostOnboardingState(...initializationParamsB)
const stateB0 = {
    deviceModelId: types_devices_1.DeviceModelId.nanoS,
    walletEntryPointDismissed: false,
    entryPointFirstDisplayedDate: new Date("2020-01-20"),
    actionsToComplete: [types_live_1.PostOnboardingActionId.claimMock],
    actionsCompleted: { [types_live_1.PostOnboardingActionId.claimMock]: false },
    lastActionCompleted: null,
    postOnboardingInProgress: true,
};
// stateB0 -> setPostOnboardingActionCompleted(claimMock)
const stateB1 = {
    deviceModelId: types_devices_1.DeviceModelId.nanoS,
    walletEntryPointDismissed: false,
    entryPointFirstDisplayedDate: new Date("2020-01-20"),
    actionsToComplete: [types_live_1.PostOnboardingActionId.claimMock],
    actionsCompleted: { [types_live_1.PostOnboardingActionId.claimMock]: true },
    lastActionCompleted: types_live_1.PostOnboardingActionId.claimMock,
    postOnboardingInProgress: true,
};
const initializationParamsC = [
    {
        deviceModelId: types_devices_1.DeviceModelId.nanoSP,
        actionsIds: [],
    },
];
// initialState -> importPostOnboardingState(...initializationParamsC)
const stateC0 = {
    deviceModelId: types_devices_1.DeviceModelId.nanoSP,
    walletEntryPointDismissed: false,
    entryPointFirstDisplayedDate: new Date("2020-01-20"),
    actionsToComplete: [],
    actionsCompleted: {},
    lastActionCompleted: null,
    postOnboardingInProgress: true,
};
describe("postOnboarding reducer (& action creators)", () => {
    beforeAll(() => {
        jest.useFakeTimers().setSystemTime(new Date("2020-01-20"));
    });
    afterAll(() => {
        jest.runOnlyPendingTimers();
        jest.useRealTimers();
    });
    let state;
    beforeEach(() => {
        // reset state to initial state;
        state = (0, reducer_1.default)(undefined, {});
    });
    it("should initialize the state properly", () => {
        expect(state).toEqual(reducer_1.initialState);
    });
    it("should handle importPostOnboardingState", () => {
        state = (0, reducer_1.default)(state, (0, actions_1.importPostOnboardingState)({ newState: stateA0 }));
        expect(state).toEqual(stateA0);
        state = (0, reducer_1.default)(state, (0, actions_1.importPostOnboardingState)({ newState: stateA1 }));
        expect(state).toEqual(stateA1);
    });
    it("it should handle initPostOnboarding", () => {
        state = (0, reducer_1.default)(state, (0, actions_1.initPostOnboarding)(...initializationParamsA));
        expect(state).toEqual(stateA0);
    });
    it("it should handle setPostOnboardingActionCompleted", () => {
        state = stateA0;
        state = (0, reducer_1.default)(state, (0, actions_1.setPostOnboardingActionCompleted)({
            actionId: types_live_1.PostOnboardingActionId.claimMock,
        }));
        expect(state).toEqual(stateA1);
    });
    it("it should handle clearPostOnboardingLastActionCompleted", () => {
        state = stateA1;
        state = (0, reducer_1.default)(state, (0, actions_1.clearPostOnboardingLastActionCompleted)());
        expect(state).toEqual({ ...stateA2 });
    });
    it("it should handle hidePostOnboardingWalletEntryPoint", () => {
        state = stateA3;
        state = (0, reducer_1.default)(state, (0, actions_1.hidePostOnboardingWalletEntryPoint)());
        expect(state).toEqual(stateA4);
    });
    it("it should handle successive actions properly", () => {
        // initializing state with new device & set of actions
        state = (0, reducer_1.default)(state, (0, actions_1.initPostOnboarding)(...initializationParamsA));
        expect(state).toEqual(stateA0);
        // setting completed for claimMock
        state = (0, reducer_1.default)(state, (0, actions_1.setPostOnboardingActionCompleted)({
            actionId: types_live_1.PostOnboardingActionId.claimMock,
        }));
        expect(state).toEqual(stateA1);
        // clearing last completed action
        state = (0, reducer_1.default)(state, (0, actions_1.clearPostOnboardingLastActionCompleted)());
        expect(state).toEqual(stateA2);
        // setting completed for personalizeMock
        state = (0, reducer_1.default)(state, (0, actions_1.setPostOnboardingActionCompleted)({
            actionId: types_live_1.PostOnboardingActionId.personalizeMock,
        }));
        expect(state).toEqual(stateA3);
        // hiding wallet entrypoint
        state = (0, reducer_1.default)(state, (0, actions_1.hidePostOnboardingWalletEntryPoint)());
        expect(state).toEqual(stateA4);
        // initializing state with new device & set of actions
        state = (0, reducer_1.default)(state, (0, actions_1.initPostOnboarding)(...initializationParamsB));
        expect(state).toEqual(stateB0);
        // setting completed for claimMock
        state = (0, reducer_1.default)(state, (0, actions_1.setPostOnboardingActionCompleted)({
            actionId: types_live_1.PostOnboardingActionId.claimMock,
        }));
        expect(state).toEqual(stateB1);
        // initializing state with new device & set of actions
        state = (0, reducer_1.default)(state, (0, actions_1.initPostOnboarding)(...initializationParamsC));
        expect(state).toEqual(stateC0);
    });
});
describe("postOnboarding selectors", () => {
    it("should keep valid device ids", () => {
        const stateValidDeviceId = {
            deviceModelId: types_devices_1.DeviceModelId.nanoX,
            walletEntryPointDismissed: false,
            entryPointFirstDisplayedDate: new Date("2020-01-20"),
            actionsToComplete: [],
            actionsCompleted: {},
            lastActionCompleted: null,
            postOnboardingInProgress: false,
        };
        const storeState = { postOnboarding: stateValidDeviceId };
        const postOnboarding = (0, reducer_1.postOnboardingSelector)(storeState);
        expect(postOnboarding).toEqual(stateValidDeviceId);
        const hubState = (0, reducer_1.hubStateSelector)(storeState);
        expect(hubState).toEqual({
            deviceModelId: stateValidDeviceId.deviceModelId,
            actionsToComplete: stateValidDeviceId.actionsToComplete,
            actionsCompleted: stateValidDeviceId.actionsCompleted,
            lastActionCompleted: stateValidDeviceId.lastActionCompleted,
            postOnboardingInProgress: false,
        });
        const deviceModelId = (0, reducer_1.postOnboardingDeviceModelIdSelector)(storeState);
        expect(deviceModelId).toEqual(stateValidDeviceId.deviceModelId);
    });
    it('should sanitize "nanoFTS" device ids to "stax"', () => {
        const stateValidDeviceId = {
            deviceModelId: "nanoFTS",
            walletEntryPointDismissed: false,
            entryPointFirstDisplayedDate: new Date("2020-01-20"),
            actionsToComplete: [],
            actionsCompleted: {},
            lastActionCompleted: null,
            postOnboardingInProgress: false,
        };
        const storeState = { postOnboarding: stateValidDeviceId };
        const postOnboarding = (0, reducer_1.postOnboardingSelector)(storeState);
        expect(postOnboarding).toEqual({
            ...stateValidDeviceId,
            deviceModelId: types_devices_1.DeviceModelId.stax,
        });
        const hubState = (0, reducer_1.hubStateSelector)(storeState);
        expect(hubState).toEqual({
            deviceModelId: types_devices_1.DeviceModelId.stax,
            actionsToComplete: stateValidDeviceId.actionsToComplete,
            actionsCompleted: stateValidDeviceId.actionsCompleted,
            lastActionCompleted: stateValidDeviceId.lastActionCompleted,
            postOnboardingInProgress: false,
        });
        const deviceModelId = (0, reducer_1.postOnboardingDeviceModelIdSelector)(storeState);
        expect(deviceModelId).toEqual(types_devices_1.DeviceModelId.stax);
    });
});
//# sourceMappingURL=reducer.test.js.map