2018-07-09 11:38:13 +02:00
|
|
|
// @flow
|
|
|
|
|
import reducer, {
|
2018-07-11 22:01:36 +02:00
|
|
|
login,
|
2018-07-09 11:38:13 +02:00
|
|
|
LOGIN_REQUEST,
|
|
|
|
|
LOGIN_FAILED,
|
2018-07-11 22:01:36 +02:00
|
|
|
LOGIN_SUCCESSFUL
|
2018-07-09 11:38:13 +02:00
|
|
|
} from "./login";
|
|
|
|
|
|
2018-07-11 22:01:36 +02:00
|
|
|
import { ME_AUTHENTICATED_REQUEST, ME_AUTHENTICATED_SUCCESS } from "./me";
|
2018-07-09 11:38:13 +02:00
|
|
|
|
2018-07-11 22:01:36 +02:00
|
|
|
import configureMockStore from "redux-mock-store";
|
|
|
|
|
import thunk from "redux-thunk";
|
|
|
|
|
import fetchMock from "fetch-mock";
|
2018-07-09 11:38:13 +02:00
|
|
|
|
2018-07-11 22:01:36 +02:00
|
|
|
describe("action tests", () => {
|
|
|
|
|
const mockStore = configureMockStore([thunk]);
|
2018-07-09 11:38:13 +02:00
|
|
|
|
2018-07-11 22:01:36 +02:00
|
|
|
afterEach(() => {
|
|
|
|
|
fetchMock.reset();
|
|
|
|
|
fetchMock.restore();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test("login success", () => {
|
|
|
|
|
fetchMock.postOnce("/scm/api/rest/v2/auth/access_token", {
|
|
|
|
|
body: {
|
|
|
|
|
cookie: true,
|
|
|
|
|
grant_type: "password",
|
|
|
|
|
username: "tricia",
|
|
|
|
|
password: "secret123"
|
|
|
|
|
},
|
|
|
|
|
headers: { "content-type": "application/json" }
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
fetchMock.getOnce("/scm/api/rest/v2/me", {
|
|
|
|
|
body: {
|
|
|
|
|
username: "tricia"
|
|
|
|
|
},
|
|
|
|
|
headers: { "content-type": "application/json" }
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const expectedActions = [
|
|
|
|
|
{ type: LOGIN_REQUEST },
|
|
|
|
|
{ type: ME_AUTHENTICATED_REQUEST },
|
|
|
|
|
{ type: LOGIN_SUCCESSFUL }
|
|
|
|
|
];
|
2018-07-09 11:38:13 +02:00
|
|
|
|
2018-07-11 22:01:36 +02:00
|
|
|
const store = mockStore({});
|
|
|
|
|
|
|
|
|
|
return store.dispatch(login("tricia", "secret123")).then(() => {
|
|
|
|
|
expect(store.getActions()).toEqual(expectedActions);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test("login failed", () => {
|
|
|
|
|
fetchMock.postOnce("/scm/api/rest/v2/auth/access_token", {
|
|
|
|
|
status: 400
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const expectedActions = [{ type: LOGIN_REQUEST }, { type: LOGIN_FAILED }];
|
|
|
|
|
|
|
|
|
|
const store = mockStore({});
|
|
|
|
|
return store.dispatch(login("tricia", "secret123")).then(() => {
|
|
|
|
|
const actions = store.getActions();
|
|
|
|
|
expect(actions[0].type).toEqual(LOGIN_REQUEST);
|
|
|
|
|
expect(actions[1].type).toEqual(LOGIN_FAILED);
|
|
|
|
|
expect(actions[1].payload).toBeDefined();
|
|
|
|
|
});
|
|
|
|
|
});
|
2018-07-09 11:38:13 +02:00
|
|
|
});
|
|
|
|
|
|
2018-07-11 22:01:36 +02:00
|
|
|
describe("reducer tests", () => {
|
|
|
|
|
test("login request", () => {
|
|
|
|
|
var newState = reducer({}, { type: LOGIN_REQUEST });
|
|
|
|
|
expect(newState.loading).toBeTruthy();
|
|
|
|
|
expect(newState.login).toBeFalsy();
|
|
|
|
|
expect(newState.error).toBeNull();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test("login successful", () => {
|
|
|
|
|
var newState = reducer({ login: false }, { type: LOGIN_SUCCESSFUL });
|
|
|
|
|
expect(newState.loading).toBeFalsy();
|
|
|
|
|
expect(newState.login).toBeTruthy();
|
|
|
|
|
expect(newState.error).toBe(null);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test("login failed", () => {
|
|
|
|
|
const err = new Error("error!");
|
|
|
|
|
var newState = reducer({}, { type: LOGIN_FAILED, payload: err });
|
|
|
|
|
expect(newState.loading).toBeFalsy();
|
|
|
|
|
expect(newState.login).toBeFalsy();
|
|
|
|
|
expect(newState.error).toBe(err);
|
|
|
|
|
});
|
2018-07-09 11:38:13 +02:00
|
|
|
});
|