Files
SCM-Manager/scm-ui/src/permissions/modules/permissions.test.js

107 lines
2.9 KiB
JavaScript
Raw Normal View History

2018-08-21 16:16:15 +02:00
// @flow
import configureMockStore from "redux-mock-store";
import thunk from "redux-thunk";
import fetchMock from "fetch-mock";
2018-08-23 09:10:23 +02:00
import reducer, {
2018-08-21 16:16:15 +02:00
fetchPermissions,
2018-08-23 09:10:23 +02:00
fetchPermissionsSuccess,
2018-08-21 16:16:15 +02:00
FETCH_PERMISSIONS_PENDING,
2018-08-21 16:21:41 +02:00
FETCH_PERMISSIONS_SUCCESS,
FETCH_PERMISSIONS_FAILURE
2018-08-21 16:16:15 +02:00
} from "./permissions";
import type { Permission, Permissions } from "../types/Permissions";
const s_bPermission_user_eins: Permission = {
name: "user_eins",
type: "READ",
groupPermission: true,
_links: {
self: {
href:
"http://localhost:8081/scm/api/rest/v2/repositories/s/b/permissions/user_eins"
},
delete: {
href:
"http://localhost:8081/scm/api/rest/v2/repositories/s/b/permissions/user_eins"
},
update: {
href:
"http://localhost:8081/scm/api/rest/v2/repositories/s/b/permissions/user_eins"
}
}
};
const s_bPermissions: Permissions = [s_bPermission_user_eins];
describe("permission fetch", () => {
const REPOS_URL = "/scm/api/rest/v2/repositories";
const mockStore = configureMockStore([thunk]);
afterEach(() => {
fetchMock.reset();
fetchMock.restore();
});
it("should successfully fetch permissions to repo s/b", () => {
fetchMock.getOnce(REPOS_URL + "/s/b/permissions", s_bPermissions);
const expectedActions = [
{
type: FETCH_PERMISSIONS_PENDING,
payload: {
namespace: "s",
name: "b"
},
itemId: "s/b"
},
{
type: FETCH_PERMISSIONS_SUCCESS,
payload: s_bPermissions,
itemId: "s/b"
}
];
const store = mockStore({});
return store.dispatch(fetchPermissions("s", "b")).then(() => {
expect(store.getActions()).toEqual(expectedActions);
});
});
2018-08-21 16:21:41 +02:00
it("should dispatch FETCH_PERMISSIONS_FAILURE, it the request fails", () => {
fetchMock.getOnce(REPOS_URL + "/s/b/permissions", {
status: 500
});
const store = mockStore({});
return store.dispatch(fetchPermissions("s", "b")).then(() => {
const actions = store.getActions();
expect(actions[0].type).toEqual(FETCH_PERMISSIONS_PENDING);
expect(actions[1].type).toEqual(FETCH_PERMISSIONS_FAILURE);
expect(actions[1].payload).toBeDefined();
});
});
2018-08-21 16:16:15 +02:00
});
2018-08-23 09:10:23 +02:00
describe("repos reducer", () => {
it("should return empty object, if state and action is undefined", () => {
expect(reducer()).toEqual({});
});
it("should return the same state, if the action is undefined", () => {
const state = { x: true };
expect(reducer(state)).toBe(state);
});
it("should return the same state, if the action is unknown to the reducer", () => {
const state = { x: true };
expect(reducer(state, { type: "EL_SPECIALE" })).toBe(state);
});
it("should store the permissions on FETCH_PERMISSION_SUCCESS", () => {
const newState = reducer(
{},
fetchPermissionsSuccess(s_bPermissions, "s", "b")
);
});
});