mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-12 00:15:44 +01:00
improve module unit tests
This commit is contained in:
@@ -1,49 +1,93 @@
|
||||
// @flow
|
||||
import reducer, {
|
||||
login,
|
||||
LOGIN_REQUEST,
|
||||
LOGIN_FAILED,
|
||||
IS_AUTHENTICATED,
|
||||
IS_NOT_AUTHENTICATED
|
||||
LOGIN_SUCCESSFUL
|
||||
} from "./login";
|
||||
import { LOGIN, LOGIN_SUCCESSFUL } from "./login";
|
||||
|
||||
test("login", () => {
|
||||
var newState = reducer({}, { type: LOGIN });
|
||||
expect(newState.login).toBe(false);
|
||||
expect(newState.error).toBe(null);
|
||||
import { ME_AUTHENTICATED_REQUEST, ME_AUTHENTICATED_SUCCESS } from "./me";
|
||||
|
||||
import configureMockStore from "redux-mock-store";
|
||||
import thunk from "redux-thunk";
|
||||
import fetchMock from "fetch-mock";
|
||||
|
||||
describe("action tests", () => {
|
||||
const mockStore = configureMockStore([thunk]);
|
||||
|
||||
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 }
|
||||
];
|
||||
|
||||
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();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
test("login request", () => {
|
||||
var newState = reducer({}, { type: LOGIN_REQUEST });
|
||||
expect(newState.login).toBe(undefined);
|
||||
});
|
||||
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.login).toBe(true);
|
||||
expect(newState.error).toBe(null);
|
||||
});
|
||||
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", () => {
|
||||
var newState = reducer({}, { type: LOGIN_FAILED, payload: "error!" });
|
||||
expect(newState.login).toBe(false);
|
||||
expect(newState.error).toBe("error!");
|
||||
});
|
||||
|
||||
test("is authenticated", () => {
|
||||
var newState = reducer(
|
||||
{ login: false },
|
||||
{ type: IS_AUTHENTICATED, username: "test" }
|
||||
);
|
||||
expect(newState.login).toBeTruthy();
|
||||
expect(newState.username).toBe("test");
|
||||
});
|
||||
|
||||
test("is not authenticated", () => {
|
||||
var newState = reducer(
|
||||
{ login: true, username: "foo" },
|
||||
{ type: IS_NOT_AUTHENTICATED }
|
||||
);
|
||||
expect(newState.login).toBe(false);
|
||||
expect(newState.username).toBeNull();
|
||||
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);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user