2020-03-23 15:35:58 +01:00
|
|
|
/*
|
|
|
|
|
* MIT License
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
2019-10-20 18:02:52 +02:00
|
|
|
import reducer, { isPending } from "./pending";
|
2019-10-19 16:38:07 +02:00
|
|
|
|
2019-10-20 18:02:52 +02:00
|
|
|
describe("pending reducer", () => {
|
|
|
|
|
it("should set pending for FETCH_ITEMS to true", () => {
|
2019-10-19 16:38:07 +02:00
|
|
|
const newState = reducer(
|
|
|
|
|
{},
|
|
|
|
|
{
|
2019-10-20 18:02:52 +02:00
|
|
|
type: "FETCH_ITEMS_PENDING"
|
|
|
|
|
}
|
2019-10-19 16:38:07 +02:00
|
|
|
);
|
2019-10-20 18:02:52 +02:00
|
|
|
expect(newState["FETCH_ITEMS"]).toBe(true);
|
2019-10-19 16:38:07 +02:00
|
|
|
});
|
|
|
|
|
|
2019-10-20 18:02:52 +02:00
|
|
|
it("should do nothing for unknown action types", () => {
|
2019-10-19 16:38:07 +02:00
|
|
|
const state = {};
|
|
|
|
|
const newState = reducer(state, {
|
2019-10-20 18:02:52 +02:00
|
|
|
type: "UNKNOWN"
|
2019-10-19 16:38:07 +02:00
|
|
|
});
|
|
|
|
|
expect(newState).toBe(state);
|
|
|
|
|
});
|
|
|
|
|
|
2019-10-20 18:02:52 +02:00
|
|
|
it("should set pending for FETCH_ITEMS to true, but should not affect others", () => {
|
2019-10-19 16:38:07 +02:00
|
|
|
const newState = reducer(
|
|
|
|
|
{
|
2019-10-20 18:02:52 +02:00
|
|
|
FETCH_USERS: true
|
2019-10-19 16:38:07 +02:00
|
|
|
},
|
|
|
|
|
{
|
2019-10-20 18:02:52 +02:00
|
|
|
type: "FETCH_ITEMS_PENDING"
|
|
|
|
|
}
|
2019-10-19 16:38:07 +02:00
|
|
|
);
|
2019-10-20 18:02:52 +02:00
|
|
|
expect(newState["FETCH_ITEMS"]).toBe(true);
|
|
|
|
|
expect(newState["FETCH_USERS"]).toBe(true);
|
2019-10-19 16:38:07 +02:00
|
|
|
});
|
|
|
|
|
|
2019-10-20 18:02:52 +02:00
|
|
|
it("should reset pending state for FETCH_ITEMS after FETCH_ITEMS_SUCCESS", () => {
|
2019-10-19 16:38:07 +02:00
|
|
|
const newState = reducer(
|
|
|
|
|
{
|
2019-10-20 18:02:52 +02:00
|
|
|
FETCH_ITEMS: true
|
2019-10-19 16:38:07 +02:00
|
|
|
},
|
|
|
|
|
{
|
2019-10-20 18:02:52 +02:00
|
|
|
type: "FETCH_ITEMS_SUCCESS"
|
|
|
|
|
}
|
2019-10-19 16:38:07 +02:00
|
|
|
);
|
2019-10-20 18:02:52 +02:00
|
|
|
expect(newState["FETCH_ITEMS"]).toBeFalsy();
|
2019-10-19 16:38:07 +02:00
|
|
|
});
|
|
|
|
|
|
2019-10-20 18:02:52 +02:00
|
|
|
it("should reset pending state for FETCH_ITEMS after FETCH_ITEMS_FAILURE", () => {
|
2019-10-19 16:38:07 +02:00
|
|
|
const newState = reducer(
|
|
|
|
|
{
|
2019-10-20 18:02:52 +02:00
|
|
|
FETCH_ITEMS: true
|
2019-10-19 16:38:07 +02:00
|
|
|
},
|
|
|
|
|
{
|
2019-10-20 18:02:52 +02:00
|
|
|
type: "FETCH_ITEMS_FAILURE"
|
|
|
|
|
}
|
2019-10-19 16:38:07 +02:00
|
|
|
);
|
2019-10-20 18:02:52 +02:00
|
|
|
expect(newState["FETCH_ITEMS"]).toBeFalsy();
|
2019-10-19 16:38:07 +02:00
|
|
|
});
|
|
|
|
|
|
2019-10-20 18:02:52 +02:00
|
|
|
it("should reset pending state for FETCH_ITEMS after FETCH_ITEMS_RESET", () => {
|
2019-10-19 16:38:07 +02:00
|
|
|
const newState = reducer(
|
|
|
|
|
{
|
2019-10-20 18:02:52 +02:00
|
|
|
FETCH_ITEMS: true
|
2019-10-19 16:38:07 +02:00
|
|
|
},
|
|
|
|
|
{
|
2019-10-20 18:02:52 +02:00
|
|
|
type: "FETCH_ITEMS_RESET"
|
|
|
|
|
}
|
2019-10-19 16:38:07 +02:00
|
|
|
);
|
2019-10-20 18:02:52 +02:00
|
|
|
expect(newState["FETCH_ITEMS"]).toBeFalsy();
|
2019-10-19 16:38:07 +02:00
|
|
|
});
|
|
|
|
|
|
2019-10-20 18:02:52 +02:00
|
|
|
it("should reset pending state for FETCH_ITEMS, if resetPending prop is available", () => {
|
2019-10-19 16:38:07 +02:00
|
|
|
const newState = reducer(
|
|
|
|
|
{
|
2019-10-20 18:02:52 +02:00
|
|
|
FETCH_ITEMS: true
|
2019-10-19 16:38:07 +02:00
|
|
|
},
|
|
|
|
|
{
|
2019-10-20 18:02:52 +02:00
|
|
|
type: "FETCH_ITEMS_SOMETHING",
|
|
|
|
|
resetPending: true
|
|
|
|
|
}
|
2019-10-19 16:38:07 +02:00
|
|
|
);
|
2019-10-20 18:02:52 +02:00
|
|
|
expect(newState["FETCH_ITEMS"]).toBeFalsy();
|
2019-10-19 16:38:07 +02:00
|
|
|
});
|
|
|
|
|
|
2019-10-20 18:02:52 +02:00
|
|
|
it("should reset pending state for FETCH_ITEMS after FETCH_ITEMS_SUCCESS, but should not affect others", () => {
|
2019-10-19 16:38:07 +02:00
|
|
|
const newState = reducer(
|
|
|
|
|
{
|
|
|
|
|
FETCH_USERS: true,
|
2019-10-20 18:02:52 +02:00
|
|
|
FETCH_ITEMS: true
|
2019-10-19 16:38:07 +02:00
|
|
|
},
|
|
|
|
|
{
|
2019-10-20 18:02:52 +02:00
|
|
|
type: "FETCH_ITEMS_SUCCESS"
|
|
|
|
|
}
|
2019-10-19 16:38:07 +02:00
|
|
|
);
|
2019-10-20 18:02:52 +02:00
|
|
|
expect(newState["FETCH_ITEMS"]).toBeFalsy();
|
|
|
|
|
expect(newState["FETCH_USERS"]).toBe(true);
|
2019-10-19 16:38:07 +02:00
|
|
|
});
|
|
|
|
|
|
2019-10-20 18:02:52 +02:00
|
|
|
it("should set pending for a single item", () => {
|
2019-10-19 16:38:07 +02:00
|
|
|
const newState = reducer(
|
|
|
|
|
{
|
2019-10-20 18:02:52 +02:00
|
|
|
"FETCH_USER/42": false
|
2019-10-19 16:38:07 +02:00
|
|
|
},
|
|
|
|
|
{
|
2019-10-20 18:02:52 +02:00
|
|
|
type: "FETCH_USER_PENDING",
|
|
|
|
|
itemId: 21
|
|
|
|
|
}
|
2019-10-19 16:38:07 +02:00
|
|
|
);
|
2019-10-20 18:02:52 +02:00
|
|
|
expect(newState["FETCH_USER/21"]).toBe(true);
|
|
|
|
|
expect(newState["FETCH_USER/42"]).toBe(false);
|
2019-10-19 16:38:07 +02:00
|
|
|
});
|
|
|
|
|
|
2019-10-20 18:02:52 +02:00
|
|
|
it("should reset pending for a single item", () => {
|
2019-10-19 16:38:07 +02:00
|
|
|
const newState = reducer(
|
|
|
|
|
{
|
2019-10-20 18:02:52 +02:00
|
|
|
"FETCH_USER/42": true
|
2019-10-19 16:38:07 +02:00
|
|
|
},
|
|
|
|
|
{
|
2019-10-20 18:02:52 +02:00
|
|
|
type: "FETCH_USER_SUCCESS",
|
|
|
|
|
itemId: 42
|
|
|
|
|
}
|
2019-10-19 16:38:07 +02:00
|
|
|
);
|
2019-10-20 18:02:52 +02:00
|
|
|
expect(newState["FETCH_USER/42"]).toBeFalsy();
|
2019-10-19 16:38:07 +02:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2019-10-20 18:02:52 +02:00
|
|
|
describe("pending selectors", () => {
|
|
|
|
|
it("should return true, while FETCH_ITEMS is pending", () => {
|
2019-10-19 16:38:07 +02:00
|
|
|
const result = isPending(
|
|
|
|
|
{
|
|
|
|
|
pending: {
|
2019-10-20 18:02:52 +02:00
|
|
|
FETCH_ITEMS: true
|
|
|
|
|
}
|
2019-10-19 16:38:07 +02:00
|
|
|
},
|
2019-10-20 18:02:52 +02:00
|
|
|
"FETCH_ITEMS"
|
2019-10-19 16:38:07 +02:00
|
|
|
);
|
|
|
|
|
expect(result).toBe(true);
|
|
|
|
|
});
|
|
|
|
|
|
2019-10-20 18:02:52 +02:00
|
|
|
it("should return false, if pending is not defined", () => {
|
|
|
|
|
const result = isPending({}, "FETCH_ITEMS");
|
2019-10-19 16:38:07 +02:00
|
|
|
expect(result).toBe(false);
|
|
|
|
|
});
|
|
|
|
|
|
2019-10-20 18:02:52 +02:00
|
|
|
it("should return true, while FETCH_ITEM 42 is pending", () => {
|
2019-10-19 16:38:07 +02:00
|
|
|
const result = isPending(
|
|
|
|
|
{
|
|
|
|
|
pending: {
|
2019-10-20 18:02:52 +02:00
|
|
|
"FETCH_ITEM/42": true
|
|
|
|
|
}
|
2019-10-19 16:38:07 +02:00
|
|
|
},
|
2019-10-20 18:02:52 +02:00
|
|
|
"FETCH_ITEM",
|
|
|
|
|
42
|
2019-10-19 16:38:07 +02:00
|
|
|
);
|
|
|
|
|
expect(result).toBe(true);
|
|
|
|
|
});
|
|
|
|
|
|
2019-10-20 18:02:52 +02:00
|
|
|
it("should return true, while FETCH_ITEM 42 is undefined", () => {
|
2019-10-19 16:38:07 +02:00
|
|
|
const result = isPending(
|
|
|
|
|
{
|
|
|
|
|
pending: {
|
2019-10-20 18:02:52 +02:00
|
|
|
"FETCH_ITEM/21": true
|
|
|
|
|
}
|
2019-10-19 16:38:07 +02:00
|
|
|
},
|
2019-10-20 18:02:52 +02:00
|
|
|
"FETCH_ITEM",
|
|
|
|
|
42
|
2019-10-19 16:38:07 +02:00
|
|
|
);
|
|
|
|
|
expect(result).toBe(false);
|
|
|
|
|
});
|
|
|
|
|
});
|