"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * @jest-environment jsdom
 */
const featureFlags_1 = require("../../featureFlags");
const reducer_1 = require("../reducer");
const usePostOnboardingContext_1 = require("./usePostOnboardingContext");
const mock_1 = require("../mock");
const react_1 = require("@testing-library/react");
const types_devices_1 = require("@ledgerhq/types-devices");
const types_live_1 = require("@ledgerhq/types-live");
const usePostOnboardingHubState_1 = require("./usePostOnboardingHubState");
jest.mock("react-redux", () => ({
    useSelector: val => val(),
}));
jest.mock("../../featureFlags");
jest.mock("./usePostOnboardingContext");
jest.mock("../reducer");
const mockedUseFeatureFlags = jest.mocked(featureFlags_1.useFeatureFlags);
const mockedGetFeatureWithMockFeatureEnabled = enabled => ({
    isFeature: () => true,
    getFeature: id => {
        if (id === mock_1.mockedFeatureIdToTest)
            return { enabled };
        return { enabled: true };
    },
    overrideFeature: () => { },
    resetFeature: () => { },
    resetFeatures: () => { },
});
const mockedUsePostOnboardingContext = jest.mocked(usePostOnboardingContext_1.usePostOnboardingContext);
const mockedHubStateSelector = jest.mocked(reducer_1.hubStateSelector);
const defaultHubState = {
    deviceModelId: types_devices_1.DeviceModelId.nanoX,
    actionsToComplete: [],
    actionsCompleted: {},
    lastActionCompleted: null,
    postOnboardingInProgress: false,
};
const stateAllCompleted = {
    deviceModelId: types_devices_1.DeviceModelId.nanoX,
    actionsToComplete: [
        types_live_1.PostOnboardingActionId.claimMock,
        types_live_1.PostOnboardingActionId.personalizeMock,
        types_live_1.PostOnboardingActionId.migrateAssetsMock,
    ],
    actionsCompleted: {
        [types_live_1.PostOnboardingActionId.claimMock]: true,
        [types_live_1.PostOnboardingActionId.personalizeMock]: true,
        [types_live_1.PostOnboardingActionId.migrateAssetsMock]: true,
    },
    lastActionCompleted: types_live_1.PostOnboardingActionId.personalizeMock,
    postOnboardingInProgress: true,
};
const stateAllNotCompleted = {
    deviceModelId: types_devices_1.DeviceModelId.nanoX,
    actionsToComplete: [
        types_live_1.PostOnboardingActionId.claimMock,
        types_live_1.PostOnboardingActionId.personalizeMock,
        types_live_1.PostOnboardingActionId.migrateAssetsMock,
    ],
    actionsCompleted: {
        [types_live_1.PostOnboardingActionId.claimMock]: false,
        [types_live_1.PostOnboardingActionId.personalizeMock]: false,
        [types_live_1.PostOnboardingActionId.migrateAssetsMock]: false,
    },
    lastActionCompleted: null,
    postOnboardingInProgress: true,
};
describe("usePostOnboardingHubState", () => {
    beforeEach(() => {
        mockedUseFeatureFlags.mockReturnValue(mockedGetFeatureWithMockFeatureEnabled(true));
        mockedUsePostOnboardingContext.mockReturnValue({
            getPostOnboardingActionsForDevice: () => [],
            navigateToPostOnboardingHub: () => { },
            getPostOnboardingAction: mock_1.getPostOnboardingAction,
        });
    });
    it("should return an empty state if the context isn't properly set (`getPostOnboardingAction` missing)", () => {
        const state = stateAllCompleted;
        mockedHubStateSelector.mockReturnValue(state);
        mockedUsePostOnboardingContext.mockReturnValue({
            getPostOnboardingActionsForDevice: () => [],
            navigateToPostOnboardingHub: () => { },
            getPostOnboardingAction: undefined,
        });
        const { result } = (0, react_1.renderHook)(() => (0, usePostOnboardingHubState_1.usePostOnboardingHubState)());
        expect(result.current).toEqual({
            deviceModelId: state.deviceModelId,
            lastActionCompleted: null,
            actionsState: [],
            postOnboardingInProgress: true,
        });
    });
    it("should return an empty state if the store is in an empty state", () => {
        const state = defaultHubState;
        mockedHubStateSelector.mockReturnValue(state);
        const { result } = (0, react_1.renderHook)(() => (0, usePostOnboardingHubState_1.usePostOnboardingHubState)());
        expect(result.current).toEqual({
            deviceModelId: state.deviceModelId,
            lastActionCompleted: null,
            actionsState: [],
            postOnboardingInProgress: false,
        });
    });
    it("should not return actions that have a disabled feature flag ", () => {
        const state = stateAllCompleted;
        mockedHubStateSelector.mockReturnValue(state);
        mockedUseFeatureFlags.mockReturnValue(mockedGetFeatureWithMockFeatureEnabled(false));
        const { result: { current: { actionsState, lastActionCompleted }, }, } = (0, react_1.renderHook)(() => (0, usePostOnboardingHubState_1.usePostOnboardingHubState)());
        expect(actionsState.find(action => action.featureFlagId === mock_1.mockedFeatureIdToTest)).toBe(undefined);
        expect(lastActionCompleted).toBe(null);
    });
    it("should return actions that have a feature flag enabled", () => {
        const state = stateAllCompleted;
        mockedHubStateSelector.mockReturnValue(state);
        mockedUseFeatureFlags.mockReturnValue(mockedGetFeatureWithMockFeatureEnabled(true));
        const { result: { current: { actionsState, lastActionCompleted }, }, } = (0, react_1.renderHook)(() => (0, usePostOnboardingHubState_1.usePostOnboardingHubState)());
        expect(actionsState.find(action => action.featureFlagId === mock_1.mockedFeatureIdToTest)).toBeTruthy();
        expect(lastActionCompleted).toBeTruthy();
    });
    it("should return actions in their correct state (all actions completed)", () => {
        const state = stateAllCompleted;
        mockedHubStateSelector.mockReturnValue(state);
        mockedUseFeatureFlags.mockReturnValue(mockedGetFeatureWithMockFeatureEnabled(true));
        const { result: { current: { actionsState }, }, } = (0, react_1.renderHook)(() => (0, usePostOnboardingHubState_1.usePostOnboardingHubState)());
        expect(actionsState.every(action => action.completed)).toBe(true);
    });
    it("should return actions in their correct state (no actions completed)", () => {
        const state = stateAllNotCompleted;
        mockedHubStateSelector.mockReturnValue(state);
        mockedUseFeatureFlags.mockReturnValue(mockedGetFeatureWithMockFeatureEnabled(true));
        const { result: { current: { actionsState }, }, } = (0, react_1.renderHook)(() => (0, usePostOnboardingHubState_1.usePostOnboardingHubState)());
        expect(actionsState.every(action => action.completed)).toBe(false);
    });
});
//# sourceMappingURL=usePostOnboardingHubState.test.js.map