-
Notifications
You must be signed in to change notification settings - Fork 324
/
crud_data_tracking_test.ts
80 lines (72 loc) · 2.9 KB
/
crud_data_tracking_test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
jest.mock("../maybe_start_tracking", () => {
return { maybeStartTracking: jest.fn() };
});
jest.mock("../../read_only_mode/app_is_read_only",
() => ({ appIsReadonly: jest.fn() }));
const mockBody: Partial<TaggedUser["body"]> = { id: 23 };
jest.mock("axios", () => {
return {
delete: () => Promise.resolve({}),
post: () => Promise.resolve({ data: mockBody }),
put: () => Promise.resolve({ data: mockBody })
};
});
import { destroy, saveAll, initSave, initSaveGetId } from "../crud";
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";
import { uniq } from "lodash";
describe("AJAX data tracking", () => {
API.setBaseUrl("http://blah.whatever.party");
const initialState = { resources: buildResourceIndex() };
const wrappedReducer =
(state: typeof initialState, action: ReduxAction<void>) => {
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];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
store.dispatch(destroy(uuid) as any);
expect(maybeStartTracking).toHaveBeenCalled();
});
it("sets consistency when calling saveAll()", () => {
const r = resources().map(x => {
x.specialStatus = SpecialStatus.DIRTY;
return x;
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
store.dispatch(saveAll(r) as any);
expect(maybeStartTracking).toHaveBeenCalled();
const list = (maybeStartTracking as jest.Mock).mock.calls;
const uuids: string[] =
uniq(list.map((x: string[]) => x[0]));
expect(uuids.length).toEqual(r.length);
});
it("sets consistency when calling initSave()", () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const action: any = initSave("User", {
name: "tester123",
email: "test@test.com"
});
store.dispatch(action);
expect(maybeStartTracking).toHaveBeenCalled();
});
it("sets consistency when calling initSaveGetId()", () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const action: any = initSaveGetId("User", {
name: "tester123",
email: "test@test.com"
});
store.dispatch(action);
expect(maybeStartTracking).toHaveBeenCalled();
});
});