Test coverage for refresh(). NEXT: Add `REFRESH_*` to reducer

pull/438/head
Rick Carlino 2017-08-31 10:15:15 -05:00
parent d16217b603
commit 24fcdd2796
4 changed files with 118 additions and 3 deletions

View File

@ -0,0 +1,46 @@
jest.mock("axios", () => ({
default: { get: () => Promise.resolve({ data: {} }) }
}));
jest.mock("../../resources/tagged_resources", () => ({
isTaggedResource: () => false
}));
import { refresh } from "../crud";
import { TaggedDevice } from "../../resources/tagged_resources";
import { API } from "../index";
import { get } from "lodash";
import { Actions } from "../../constants";
describe("refresh()", () => {
API.setBaseUrl("http://localhost:3000");
// 1. Enters the `catch` block.
it("rejects malformed API data", (done) => {
const device1: TaggedDevice = {
"uuid": "device.6.1",
"kind": "device",
"specialStatus": undefined,
"body": {
"id": 6,
"name": "summer-pond-726",
"timezone": "America/Chicago",
"last_seen": "2017-08-30T20:42:35.854Z"
},
};
const thunk = refresh(device1);
const dispatch = jest.fn();
const { mock } = dispatch;
thunk(dispatch);
setImmediate(() => {
expect(mock.calls.length).toEqual(2);
const secondCall = mock.calls[1][0];
expect(get(secondCall, "type", "NO TYPE FOUND"))
.toEqual(Actions.REFRESH_RESOURCE_NO);
expect(get(secondCall, "payload.err.message", "NO ERR MSG FOUND"))
.toEqual("Just saved a malformed TR.");
done();
});
});
});

View File

@ -0,0 +1,63 @@
jest.mock("axios", () => ({
default: {
get: () => Promise.resolve({
data: {
"id": 6,
"name": "New Device From Server",
"timezone": "America/Chicago",
"last_seen": "2017-08-30T20:42:35.854Z"
}
})
}
}));
import { refresh } from "../crud";
import { TaggedDevice } from "../../resources/tagged_resources";
import { API } from "../index";
import { Actions } from "../../constants";
import { get } from "lodash";
describe("successful refresh()", () => {
API.setBaseUrl("http://localhost:3000");
// 1. Correct URL
// 2. call to refreshOK
// 3. Actually replaces resource.
it("re-downloads an existing resource", (done) => {
const device1: TaggedDevice = {
"uuid": "device.6.1",
"kind": "device",
"specialStatus": undefined,
"body": {
"id": 6,
"name": "summer-pond-726",
"timezone": "America/Chicago",
"last_seen": "2017-08-30T20:42:35.854Z"
},
};
const thunk = refresh(device1);
const dispatch = jest.fn();
thunk(dispatch);
setImmediate(() => {
const { calls } = dispatch.mock;
expect(calls.length).toBe(2);
const first = calls[0][0];
expect(first).toBeInstanceOf(Object);
expect(get(first, "type", "TYPE WAS UNDEFINED"))
.toEqual(Actions.REFRESH_RESOURCE_START);
expect(get(first, "payload", "NO PAYLOAD!"))
.toEqual(device1.uuid);
const second = calls[1][0];
expect(second).toBeInstanceOf(Object);
expect(get(second, "type", "TYPE WAS UNDEFINED"))
.toEqual(Actions.REFRESH_RESOURCE_OK);
expect(get(second, "payload.body.name", "DID NOT FIND ANYTHING"))
.toEqual("New Device From Server");
done();
});
});
});

View File

@ -91,8 +91,8 @@ export function save(uuid: string) {
}
export function refresh(resource: TaggedResource) {
return function (dispatch: Function, getState: GetState) {
dispatch({ type: Actions.REFRESH_RESOURCE_START, payload: resource });
return function (dispatch: Function) {
dispatch(refreshStart(resource.uuid));
axios
.get(urlFor(resource.kind) + resource.body.id || "")
.then((resp: HttpData<typeof resource.body>) => {
@ -106,12 +106,17 @@ export function refresh(resource: TaggedResource) {
}
})
.catch(function (err: UnsafeError) {
dispatch(refreshNO({ err, uuid: resource.uuid }));
const action = refreshNO({ err, uuid: resource.uuid });
dispatch(action);
return Promise.reject(err);
});
};
}
function refreshStart(uuid: string): ReduxAction<string> {
return { type: Actions.REFRESH_RESOURCE_START, payload: uuid };
}
function refreshOK(payload: TaggedResource): ReduxAction<TaggedResource> {
return { type: Actions.REFRESH_RESOURCE_OK, payload };
}

View File

@ -1,4 +1,5 @@
export enum BooleanSetting {
/** TODO: Convert these guys to a consistentCase before submitting PR. -RC */
X_AXIS_INVERTED = "x_axis_inverted",
Y_AXIS_INVERTED = "y_axis_inverted",
Z_AXIS_INVERTED = "z_axis_inverted",