Farmbot-Web-App/frontend/api/__tests__/crud_data_tracking.ts

81 lines
2.7 KiB
TypeScript
Raw Normal View History

2017-11-21 07:55:51 -07:00
jest.mock("../maybe_start_tracking", () => {
return { maybeStartTracking: jest.fn() };
});
const mockBody: Partial<TaggedUser["body"]> = { id: 23 };
2017-11-21 07:55:51 -07:00
jest.mock("axios", () => {
return {
default: {
delete: () => Promise.resolve({}),
2018-07-20 15:09:35 -06:00
post: () => Promise.resolve({ data: mockBody }),
put: () => Promise.resolve({ data: mockBody })
2017-11-21 07:55:51 -07:00
}
};
});
2018-10-31 17:49:05 -06:00
2018-11-14 17:36:52 -07:00
import { destroy, saveAll, initSave, initSaveGetId } from "../crud";
2017-11-21 07:55:51 -07:00
import { buildResourceIndex } from "../../__test_support__/resource_index_builder";
import { createStore, applyMiddleware } from "redux";
import { resourceReducer } from "../../resources/reducer";
import thunk from "redux-thunk";
import { ReduxAction } from "../../redux/interfaces";
import { maybeStartTracking } from "../maybe_start_tracking";
import { API } from "../api";
import { betterCompact } from "../../util";
import { SpecialStatus, TaggedUser } from "farmbot";
2017-11-21 07:55:51 -07:00
import * as _ from "lodash";
2018-10-31 17:49:05 -06:00
describe("AJAX data tracking", () => {
2017-11-21 07:55:51 -07:00
API.setBaseUrl("http://blah.whatever.party");
const initialState = { resources: buildResourceIndex() };
const wrappedReducer =
(state: typeof initialState, action: ReduxAction<void>) => {
2017-11-21 07:55:51 -07:00
return { resources: resourceReducer(state.resources, action) };
};
const store = createStore(wrappedReducer, initialState, applyMiddleware(thunk));
const resources = () =>
betterCompact(Object.values(store.getState().resources.index.references));
it("sets consistency when calling destroy()", () => {
const uuid = Object.keys(store.getState().resources.index.byKind.Tool)[0];
// tslint:disable-next-line:no-any
2018-07-27 11:48:59 -06:00
store.dispatch(destroy(uuid) as any);
2017-11-21 07:55:51 -07:00
expect(maybeStartTracking).toHaveBeenCalled();
});
it("sets consistency when calling saveAll()", () => {
const r = resources().map(x => {
x.specialStatus = SpecialStatus.DIRTY;
return x;
});
// tslint:disable-next-line:no-any
2018-07-27 11:48:59 -06:00
store.dispatch(saveAll(r) as any);
2017-11-21 07:55:51 -07:00
expect(maybeStartTracking).toHaveBeenCalled();
2018-10-31 12:12:45 -06:00
const list = (maybeStartTracking as jest.Mock).mock.calls;
2017-11-21 07:55:51 -07:00
const uuids: string[] =
2018-10-31 12:12:45 -06:00
_.uniq(list.map((x: string[]) => x[0]));
2017-11-21 07:55:51 -07:00
expect(uuids.length).toEqual(r.length);
});
2018-10-31 17:49:05 -06:00
it("sets consistency when calling initSave()", () => {
// tslint:disable-next-line:no-any
const action: any = initSave("User", {
name: "tester123",
email: "test@test.com"
});
2018-10-30 14:36:52 -06:00
store.dispatch(action);
2017-11-21 07:55:51 -07:00
expect(maybeStartTracking).toHaveBeenCalled();
});
2018-11-14 17:36:52 -07:00
it("sets consistency when calling initSaveGetId()", () => {
// tslint:disable-next-line:no-any
const action: any = initSaveGetId("User", {
name: "tester123",
email: "test@test.com"
});
store.dispatch(action);
expect(maybeStartTracking).toHaveBeenCalled();
});
2017-11-21 07:55:51 -07:00
});