By: Kunal Hire

it('should dispatch an action to fetch the leaves', (done) => {
    const request = api.get(`/leaves`).reply(200, [leaveFormStub]);

    store.dispatch(leavesActions.fetchLeaves())
        .then((resp) => {
            request.done();
            let executedActions = store.getActions();
            expect(executedActions[0].type).to.equal(constants.LEAVE_FETCHING);
            expect(executedActions[1].type).to.equal(constants.LEAVES_FETCHED);
            expect(executedActions[1].leaves).to.deep.equal([leaveFormStub]);
            done();
        })
        .catch(done);
});
const leaveFetching = (leaveId) => ({
    type: constants.LEAVE_FETCHING,
    leaveId
});const leavesFetched = (leaves) => ({
    type: constants.LEAVES_FETCHED,
    leaves
});const fetchLeaves = () => {
    return (dispatch) => {
        dispatch(leaveFetching());
        return http.get(dispatch, `${config.serverUrl}/leaves`, null, {
            'Accept': APPLICATION_JSON
        }).then((resp) => {
            dispatch(leavesFetched(resp));
        }).catch(() => {
        })
    }
};
it('should dispatch an action to set error when fetchLeaves returns the error', (done) => {
    const request = api.get(`/leaves`).reply(500, {error: 'error'});

    store.dispatch(leavesActions.fetchLeaves())
        .then((resp) => {
            request.done();
            let executedActions = store.getActions();
            expect(executedActions[0].type).to.equal(constants.LEAVE_FETCHING);
            expect(executedActions[1].type).to.equal(constants.LEAVE_ERROR);
            expect(executedActions[1].error).to.deep.equal('error occurred');
            done();
        })
        .catch(done);
});
const fetchLeaves = () => {
    return (dispatch) => {
        dispatch(leaveFetching());
        return http.get(dispatch, `${config.serverUrl}/leaves`, null, {
            'Accept': APPLICATION_JSON
        }).then((resp) => {
            dispatch(leavesFetched(resp));
        }).catch((err) => {
            dispatch(leaveError('error occurred'))
        })
    }
};
it("should return a new state with modified leaves when LEAVES_FETCHED action is received", () => {
    let leaves = [{dummy: 'dummy'}];
    const action = {type: constants.LEAVES_FETCHED, leaves: leaves};

    const updatedState = reducer(undefined, action);

    expect(updatedState.leaves).deep.equal(leaves);
});
case constants.LEAVES_FETCHED:
    return Object.assign({}, state, {
        leaves: action.leaves,
        status: "SUCCESS",
        error: null
    });
// TEST
it("should return a new state with status ERROR when a LEAVE_ERROR action is received", () => {
    let error = {error: 'dummy'};
    const action = {type: constants.LEAVE_ERROR, error: error};

    const updatedState = reducer(undefined, action);

    expect(updatedState.error).deep.equal(error);
});// CODE
case constants.LEAVE_ERROR:
    return Object.assign({}, state, {
        error: action.error,
        status: "ERROR"
    });
it('should pass fetchLeaves as props to the connected component', () => {
    const store = mockStore({leaves: {}});
    let shallowWrapper = wrapper.dive({context: {store}});
    shallowWrapper.prop('fetchLeaves')();
    expect(store.getActions()[0].type).deep.equal('LEAVE_FETCHING');
});
const mapDispatchToProps = (dispatch) => {
    return {
        fetchLeaves: () => {
            dispatch(leavesActions.fetchLeaves())
        }
    };
};
it('should pass leaves to connected component', () => {
    const leaves = [{dummy: 'dummy'}];
    const store = mockStore({leaves: {leaves: leaves}});
    let shallowWrapper = wrapper.dive({context: {store}});
    expect(shallowWrapper.prop('leaves')).deep.equal(leaves);
});
const mapStateToProps = (state) => {

    return {
        leaves: state.leaves.leaves
    }
};
it('should make an action to fetch leaves when component is loaded', () => {wrapper = shallow(<Dashboard navigateToNewLeave={navigateToNewLeaveStub} fetchLeaves={fetchLeavesStub} leaves={leaves}/>);
    expect(fetchLeavesStub.called).equal(true);
});
componentWillMount() {
    this.props.fetchLeaves();
}
describe('Table', () => {
    it('should render the table headers', () => {
        const tableHeaders = wrapper.find('thead tr');

        expect(tableHeaders.childAt(0).text()).equal('From Date');
        expect(tableHeaders.childAt(1).text()).equal('To Date');
        expect(tableHeaders.childAt(2).text()).equal('Reason');
        expect(tableHeaders.childAt(3).text()).equal('Type');
    });
});
render() {
    return (
        <div className="container">
           <FormGroup>
                <Row>
                    <Table>
                        <thead>
                            <tr>
                                <th>From Date</th>
                                <th>To Date</th>
                                <th>Reason</th>
                                <th>Type</th>
                            </tr>
                        </thead>
                    </Table>
                </Row>
            </FormGroup>
        </div>
    )
}