mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-08 14:35:45 +01:00
fixed code smells and added some more tests
This commit is contained in:
@@ -28,7 +28,10 @@ export const LOGOUT_FAILURE = `${LOGOUT}_${types.FAILURE_SUFFIX}`;
|
||||
|
||||
const initialState = {};
|
||||
|
||||
export default function reducer(state: Object = initialState, action: Object) {
|
||||
export default function reducer(
|
||||
state: Object = initialState,
|
||||
action: Object = { type: "UNKNOWN" }
|
||||
) {
|
||||
switch (action.type) {
|
||||
case LOGIN_SUCCESS:
|
||||
case FETCH_ME_SUCCESS:
|
||||
|
||||
@@ -23,7 +23,10 @@ function removeFromState(state: Object, identifier: string) {
|
||||
return newState;
|
||||
}
|
||||
|
||||
export default function reducer(state: Object = {}, action: Action): Object {
|
||||
export default function reducer(
|
||||
state: Object = {},
|
||||
action: Action = { type: "UNKNOWN" }
|
||||
): Object {
|
||||
const type = action.type;
|
||||
if (type.endsWith(FAILURE_SUFFIX)) {
|
||||
const identifier = extractIdentifierFromFailure(action);
|
||||
|
||||
@@ -10,6 +10,12 @@ describe("failure reducer", () => {
|
||||
expect(newState["FETCH_ITEMS"]).toBe(err);
|
||||
});
|
||||
|
||||
it("should do nothing for unknown action types", () => {
|
||||
const state = {};
|
||||
const newState = reducer(state, { type: "UNKNOWN" });
|
||||
expect(newState).toBe(state);
|
||||
});
|
||||
|
||||
it("should set the error for FETCH_ITEMS, if payload has multiple values", () => {
|
||||
const newState = reducer(
|
||||
{},
|
||||
@@ -52,6 +58,18 @@ describe("failure reducer", () => {
|
||||
expect(newState["FETCH_ITEMS"]).toBeFalsy();
|
||||
});
|
||||
|
||||
it("should reset FETCH_ITEMS after FETCH_ITEMS_RESET, but should not affect others", () => {
|
||||
const newState = reducer(
|
||||
{
|
||||
FETCH_ITEMS: err,
|
||||
FETCH_USERS: err
|
||||
},
|
||||
{ type: "FETCH_ITEMS_RESET" }
|
||||
);
|
||||
expect(newState["FETCH_ITEMS"]).toBeFalsy();
|
||||
expect(newState["FETCH_USERS"]).toBe(err);
|
||||
});
|
||||
|
||||
it("should set the error for a single item of FETCH_ITEM", () => {
|
||||
const newState = reducer(
|
||||
{},
|
||||
@@ -59,6 +77,14 @@ describe("failure reducer", () => {
|
||||
);
|
||||
expect(newState["FETCH_ITEM/42"]).toBe(err);
|
||||
});
|
||||
|
||||
it("should reset error for a single item of FETCH_ITEM", () => {
|
||||
const newState = reducer(
|
||||
{ "FETCH_ITEM/42": err },
|
||||
{ type: "FETCH_ITEM_SUCCESS", payload: err, itemId: 42 }
|
||||
);
|
||||
expect(newState["FETCH_ITEM/42"]).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("failure selector", () => {
|
||||
|
||||
@@ -28,7 +28,10 @@ function extractIdentifierFromPending(action: Action) {
|
||||
return identifier;
|
||||
}
|
||||
|
||||
export default function reducer(state: Object = {}, action: Action): Object {
|
||||
export default function reducer(
|
||||
state: Object = {},
|
||||
action: Action = { type: "UNKNOWN" }
|
||||
): Object {
|
||||
const type = action.type;
|
||||
if (type.endsWith(PENDING_SUFFIX)) {
|
||||
const identifier = extractIdentifierFromPending(action);
|
||||
|
||||
@@ -6,6 +6,12 @@ describe("pending reducer", () => {
|
||||
expect(newState["FETCH_ITEMS"]).toBe(true);
|
||||
});
|
||||
|
||||
it("should do nothing for unknown action types", () => {
|
||||
const state = {};
|
||||
const newState = reducer(state, { type: "UNKNOWN" });
|
||||
expect(newState).toBe(state);
|
||||
});
|
||||
|
||||
it("should set pending for FETCH_ITEMS to true, but should not affect others", () => {
|
||||
const newState = reducer(
|
||||
{
|
||||
|
||||
@@ -98,12 +98,7 @@ export function fetchUser(name: string) {
|
||||
return apiClient
|
||||
.get(userUrl)
|
||||
.then(response => {
|
||||
return response;
|
||||
})
|
||||
.then(response => {
|
||||
if (response.ok) {
|
||||
return response.json();
|
||||
}
|
||||
return response.json();
|
||||
})
|
||||
.then(data => {
|
||||
dispatch(fetchUserSuccess(data));
|
||||
|
||||
Reference in New Issue
Block a user