{"size": 3565, "ext": "js", "max_stars_repo_path": "App/src/tests/thunkActionTests/loginThunkAction.test.js", "max_stars_repo_name": "kylerdaybell/Project_GreenBean", "max_stars_repo_stars_event_min_datetime": "2019-11-24T02:40:42.000Z", "max_stars_repo_stars_event_max_datetime": "2019-11-24T02:40:42.000Z", "max_issues_repo_path": "App/src/tests/thunkActionTests/loginThunkAction.test.js", "max_issues_repo_name": "kylerdaybell/Project_GreenBean", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "App/src/tests/thunkActionTests/loginThunkAction.test.js", "max_forks_repo_name": "kylerdaybell/Project_GreenBean", "max_forks_repo_forks_event_min_datetime": "2019-10-26T18:18:49.000Z", "max_forks_repo_forks_event_max_datetime": "2019-12-14T16:32:23.000Z"}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": 3, "avg_line_length": 28.9837398374, "max_line_length": 65, "alphanum_fraction": 0.5366058906}
import configureMockStore from "redux-mock-store";
import fetchMock from "fetch-mock";
import thunk from "redux-thunk";
import * as ActionTypes from "../../store/constants";
import * as Actions from "../../store/actions/onlineActions";
import {initialState} from "../../store/initialState";

const middleware = [thunk];
const mockStore = configureMockStore(middleware);
//empty offlineAPI mock for actions
jest.mock('../../Services/OfflineAPI.js', ()=>()=>({

}))
describe("Login Tests", () => {
    afterEach(()=> {
        fetchMock.restore();
    });
    test("Successful Login. dispatch Login Success", () => {
        fetchMock.mock("*", {
            body: {Result: "Success", UserId: -1},
            headers: { "content-type": "application/json" }
        })

        const expected = [
            {
                type: ActionTypes.LOGIN_SUCCESS,
                loggedIn: true,
                email: "testEmail@test.com",
                password: "testPassword1%",
                userId: -1
            }
        ]

        const store = mockStore(initialState);
        return store
        .dispatch(
            Actions.Login("testEmail@test.com","testPassword1%")
        )
        .then(()=> expect(store.getActions()).toEqual(expected))
    })
    
    test("Failed Login. dispatch Login Failure", () => {
        fetchMock.mock("*", {
            body: {Result: "Failure", UserId: -1},
            headers: { "content-type": "application/json" }
        })

        const expected = [
            {
                type: ActionTypes.LOGIN_FAILURE
            }
        ]

        const store = mockStore(initialState);
        return store
        .dispatch(
            Actions.Login("testEmail@test.com","testPassword1%")
        )
        .then(()=> expect(store.getActions()).toEqual(expected))
    })

    test("Login API call failed. dispatch Login Failure", () => {
        fetchMock.mock("*", Promise.reject())

        const expected = [
            {
                type: ActionTypes.LOGIN_FAILURE
            }
        ]

        const store = mockStore(initialState);
        return store
        .dispatch(
            Actions.Login("testEmail@test.com","testPassword1%")
        )
        .then(()=> expect(store.getActions()).toEqual(expected))
    });

    test("Login Success. return true", () => {
        fetchMock.mock("*", {
            body: {Result: "Success", UserId: -1},
            headers: { "content-type": "application/json" }
        })

        const expected = true

        const store = mockStore(initialState);
        return store
        .dispatch(
            Actions.Login("testEmail@test.com","testPassword1%")
        )
        .then((actual)=> expect(actual).toEqual(expected))
    });

    test("Login Failure. return false", () => {
        fetchMock.mock("*", {
            body: {Result: "Failure"},
            headers: { "content-type": "application/json" }
        })

        const expected = false

        const store = mockStore(initialState);
        return store
        .dispatch(
            Actions.Login("testEmail@test.com","testPassword1%")
        )
        .then((actual)=> expect(actual).toEqual(expected))
    });

    test("Login API fails. return false", () => {
        fetchMock.mock("*", Promise.reject())

        const expected = false

        const store = mockStore(initialState);
        return store
        .dispatch(
            Actions.Login("testEmail@test.com","testPassword1%")
        )
        .then((actual)=> expect(actual).toEqual(expected))
    });
});