Moar tests
parent
2c4ebec7a4
commit
3637cba6cd
|
@ -1,4 +1,4 @@
|
|||
import { isSafeError } from "../interceptor_support";
|
||||
import { isSafeError, inferUpdateId } from "../interceptor_support";
|
||||
|
||||
describe("isSafeError", () => {
|
||||
it("infers if it is safe to proceed", () => {
|
||||
|
@ -8,3 +8,10 @@ describe("isSafeError", () => {
|
|||
expect(isSafeError(safe)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("inferUpdateId", () => {
|
||||
it("it handles failure by returning `*`", () => {
|
||||
expect(inferUpdateId("foo/123/456")).toBe("*");
|
||||
expect(inferUpdateId((true as any))).toBe("*");
|
||||
});
|
||||
});
|
||||
|
|
|
@ -10,11 +10,12 @@ jest.mock("../../resources/tagged_resources", () => ({
|
|||
isTaggedResource: () => false
|
||||
}));
|
||||
|
||||
import { refresh } from "../crud";
|
||||
import { refresh, destroyCatch } from "../crud";
|
||||
import { TaggedDevice, SpecialStatus } from "../../resources/tagged_resources";
|
||||
import { API } from "../index";
|
||||
import { get } from "lodash";
|
||||
import { Actions } from "../../constants";
|
||||
import { destroyNO } from "../../resources/actions";
|
||||
|
||||
describe("refresh()", () => {
|
||||
API.setBaseUrl("http://localhost:3000");
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
import { SpecialStatus } from "../../resources/tagged_resources";
|
||||
import { destroyNO } from "../../resources/actions";
|
||||
import { destroyCatch } from "../crud";
|
||||
|
||||
describe("destroyCatch", () => {
|
||||
it("Calls appropriate handlers", () => {
|
||||
const dispatch = jest.fn();
|
||||
const uuid = "foo.12.34";
|
||||
const statusBeforeError = SpecialStatus.DIRTY;
|
||||
const err = {};
|
||||
const handler = destroyCatch({
|
||||
dispatch,
|
||||
uuid,
|
||||
statusBeforeError
|
||||
});
|
||||
handler(err);
|
||||
const expected = destroyNO({ err, uuid, statusBeforeError });
|
||||
expect(dispatch).toHaveBeenCalledWith(expected);
|
||||
});
|
||||
});
|
|
@ -152,6 +152,21 @@ function update(uuid: string, statusBeforeError: SpecialStatus) {
|
|||
};
|
||||
}
|
||||
|
||||
interface DestroyNoProps {
|
||||
uuid: string;
|
||||
statusBeforeError: SpecialStatus;
|
||||
dispatch: Function;
|
||||
}
|
||||
|
||||
export const destroyCatch = (p: DestroyNoProps) => (err: UnsafeError) => {
|
||||
p.dispatch(destroyNO({
|
||||
err,
|
||||
uuid: p.uuid,
|
||||
statusBeforeError: p.statusBeforeError
|
||||
}));
|
||||
return Promise.reject(err);
|
||||
};
|
||||
|
||||
export function destroy(uuid: string, force = false) {
|
||||
return function (dispatch: Function, getState: GetState) {
|
||||
const resource = findByUuid(getState().resources.index, uuid);
|
||||
|
@ -165,10 +180,7 @@ export function destroy(uuid: string, force = false) {
|
|||
.then(function (resp: HttpData<typeof resource.body>) {
|
||||
dispatch(destroyOK(resource));
|
||||
})
|
||||
.catch(function (err: UnsafeError) {
|
||||
dispatch(destroyNO({ err, uuid, statusBeforeError }));
|
||||
return Promise.reject(err);
|
||||
});
|
||||
.catch(destroyCatch({ dispatch, uuid, statusBeforeError }));
|
||||
} else {
|
||||
dispatch(destroyOK(resource));
|
||||
return Promise.resolve("");
|
||||
|
|
|
@ -70,7 +70,7 @@ export function notifyBotOfChanges(url: string | undefined,
|
|||
|
||||
/** More nasty hacks until we have time to implement proper API push state
|
||||
* notifications. */
|
||||
function inferUpdateId(url: string) {
|
||||
export function inferUpdateId(url: string) {
|
||||
try {
|
||||
const ids = url
|
||||
.split("/")
|
||||
|
|
Loading…
Reference in New Issue