import { GetWarehousesApiParameter } from "@insite/client-framework/Services/WarehouseService";
import { getDataViewKey } from "@insite/client-framework/Store/Data/DataState";
import { ExpectedState } from "@insite/client-framework/Store/Data/ReducerTestHelper";
import WarehousesReducer from "@insite/client-framework/Store/Data/Warehouses/WarehousesReducer";
import { WarehousesState } from "@insite/client-framework/Store/Data/Warehouses/WarehousesState";
import { WarehouseCollectionModel } from "@insite/client-framework/Types/ApiModels";

describe("WarehousesReducer", () => {
    const parameter: GetWarehousesApiParameter = {
        search: "test",
        latitude: 1,
        longitude: 2,
    };
    const collection: WarehouseCollectionModel = {
        uri: "test",
        properties: {},
        defaultLatitude: 3,
        defaultLongitude: 3,
        defaultRadius: 3,
        distanceUnitOfMeasure: "testMeasure",
        pagination: null,
        warehouses: [],
    };
    const extraData = {
        defaultLatitude: collection.defaultLatitude,
        defaultLongitude: collection.defaultLongitude,
        defaultRadius: collection.defaultRadius,
        distanceUnitOfMeasure: collection.distanceUnitOfMeasure,
    };
    const key = getDataViewKey(parameter);
    let initialState: WarehousesState;
    beforeEach(() => {
        initialState = {
            isLoading: {},
            byId: {},
            dataViews: {},
        };
    });

    test("State object should contain isLoading whose value is true", () => {
        const type = "Data/Warehouses/BeginLoadWarehouses";
        const action = {
            type,
            parameter,
        } as any;
        const expectedState = new ExpectedState(initialState).setStateDataViewLoading(parameter).getState();
        delete expectedState.dataViews[key].fetchedDate;

        const actualState = JSON.parse(JSON.stringify(WarehousesReducer(undefined, action) as any));
        delete actualState.dataViews[key].fetchedDate;

        expect(actualState).toEqual(expectedState);
        expect(actualState.dataViews[key].isLoading).toEqual(true);
    });

    test("State object should contain a loaded dataViews", () => {
        const type = "Data/Warehouses/CompleteLoadWarehouses";
        const action = {
            type,
            parameter,
            collection,
        } as any;
        const reducerName = "warehouses";
        const expectedState = new ExpectedState(initialState)
            .setStateDataViewLoaded(parameter, collection, reducerName, undefined, extraData)
            .getState();
        delete expectedState.dataViews[key].fetchedDate;

        const actualState = JSON.parse(JSON.stringify(WarehousesReducer(undefined, action) as any));
        // fetchedDate won't equal because of the timestamp differece generated by the system
        delete actualState.dataViews[key].fetchedDate;

        expect(actualState).toEqual(expectedState);
    });

    test("Reset should return initialState", () => {
        const type = "Data/Warehouses/Reset";
        const action = {
            type,
        } as any;

        const actualState = WarehousesReducer(undefined, action);

        expect(actualState).toEqual(initialState);
    });
});
