'use strict';

import {
    actionTypes,
    clearDesignAreaSelection,
    selectDesignArea,
    setPreviousEditedDesignAreaIdentier,
} from '../Actions';
import designAreaReducer from '../Reducer';
import configureStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import { selectDesignView } from '../../DesignView/Actions';

const middlewares = [thunk]; // add your middlewares like `redux-thunk`
const mockStore = configureStore(middlewares);

describe('Reducers/DesignArea', () => {
    test('selectDesignArea dispatches the correct actions', async () => {
        const designViewIdentifier = 'front';
        const store = mockStore({
            designArea: {
                ...designAreaReducer(),
                selectedDesignArea: {
                    designView: {
                        identifier: designViewIdentifier,
                    },
                },
            },
        });
        const designAreaIdentifier = 'test_identifier';
        await store.dispatch(selectDesignArea(designAreaIdentifier));
        const actions = store.getActions();
        expect(actions[0]).toEqual({
            type: actionTypes.SELECT_DESIGN_AREA,
            designAreaIdentifier,
        });
        expect(actions[1]).toEqual(selectDesignView(designViewIdentifier));
    });

    test('clearDesignAreaSelection creates the correct action', () => {
        const dispatch = jest.fn(v => v);
        const getState = () => ({ designArea: designAreaReducer() });
        clearDesignAreaSelection(true)(dispatch, getState);
        expect(dispatch).toBeCalledWith({
            type: actionTypes.CLEAR_DESIGN_AREA_SELECTION,
            force: true,
        });
    });

    test('setPreviousEditedDesignAreaIdentier creates the correct action', () => {
        const designAreaIdentifier = 'test_identifier';
        const result = setPreviousEditedDesignAreaIdentier(designAreaIdentifier);
        const expected = {
            designAreaIdentifier: designAreaIdentifier,
            type: actionTypes.SET_PREVIOUS_EDITED_DESIGN_AREA,
        };
        expect(result).toEqual(expected);
    });
});
