forked from openmrs/openmrs-esm-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmock.tsx
229 lines (173 loc) · 5.57 KB
/
mock.tsx
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import React from "react";
import type {} from "@openmrs/esm-globals";
import createStore, { Store } from "unistore";
import { never, of } from "rxjs";
interface StoreEntity {
value: Store<any>;
active: boolean;
}
export type MockedStore<T> = Store<T> & { resetMock: () => void };
const initialStates: Record<string, any> = {};
const availableStores: Record<string, StoreEntity> = {};
export function isVersionSatisfied() {
return true;
}
export const mockStores = availableStores;
export function createGlobalStore<TState>(
name: string,
initialState: TState
): Store<TState> {
const available = availableStores[name];
if (available) {
if (available.active) {
console.error(
"Cannot override an existing store. Make sure that stores are only created once."
);
} else {
available.value.setState(initialState, true);
}
available.active = true;
return available.value;
} else {
const store = createStore(initialState);
initialStates[name] = initialState;
availableStores[name] = {
value: store,
active: true,
};
return instrumentedStore(name, store);
}
}
export function getGlobalStore<TState = any>(
name: string,
fallbackState?: TState
): Store<TState> {
const available = availableStores[name];
if (!available) {
const store = createStore(fallbackState);
initialStates[name] = fallbackState;
availableStores[name] = {
value: store,
active: false,
};
return instrumentedStore(name, store);
}
return instrumentedStore(name, available.value);
}
function instrumentedStore<T>(name: string, store: Store<T>) {
return {
action: jest.spyOn(store, "action"),
getState: jest.spyOn(store, "getState"),
setState: jest.spyOn(store, "setState"),
subscribe: jest.spyOn(store, "subscribe"),
unsubscribe: jest.spyOn(store, "unsubscribe"),
resetMock: () => store.setState(initialStates[name]),
} as any as MockedStore<T>;
}
export const configInternalStore = createGlobalStore("config-internal", {
devDefaultsAreOn: false,
});
export const implementerToolsConfigStore = createGlobalStore(
"implementer-tools-config",
{}
);
export const temporaryConfigStore = createGlobalStore("temporary-config", {});
export enum Type {
Array = "Array",
Boolean = "Boolean",
ConceptUuid = "ConceptUuid",
Number = "Number",
Object = "Object",
String = "String",
UUID = "UUID",
}
export const validators = {
isBoolean: jest.fn(),
isString: jest.fn(),
isUuid: jest.fn(),
isObject: jest.fn(),
};
export const getConfig = jest.fn();
export const useConfig = jest.fn();
export function defineConfigSchema() {}
export const createErrorHandler = () => jest.fn().mockReturnValue(never());
export const reportError = jest.fn().mockImplementation((error) => {
throw error;
});
export const switchTo = jest.fn();
export const UserHasAccessReact = (props: any) => props.children;
export const openmrsFetch = jest.fn(() => new Promise(() => {}));
export const openmrsObservableFetch = jest.fn(() =>
of({ data: { entry: [] } })
);
export const setIsUIEditorEnabled = (boolean): void => {};
export const useCurrentPatient = jest.fn(() => [null, null, null, null]);
export const useSessionUser = jest.fn(() => null);
export const useLayoutType = jest.fn(() => "desktop");
export const useExtensionSlot = jest.fn(() => ({
extensionSlotModuleName: "",
attachedExtensionSlotName: "",
extensionIdsToRender: [],
}));
export const useExtension = jest.fn(() => [React.createRef(), undefined]);
export const getCurrentPatient = jest.fn(() =>
jest.fn().mockReturnValue(never())
);
export function getCurrentUser() {
return of({ authenticated: false });
}
export const navigate = jest.fn();
export const interpolateString = jest.fn();
export const getCurrentPatientUuid = jest.fn();
export const newWorkspaceItem = jest.fn();
export const fhirBaseUrl = "/ws/fhir2/R4";
export const ExtensionSlot = ({ children }) => <>{children}</>;
export const Extension = jest.fn().mockImplementation((props: any) => <slot />);
export const ConfigurableLink = jest
.fn()
.mockImplementation((config: { to: string; children: React.ReactNode }) => (
<a href={interpolateString(config.to)}>{config.children}</a>
));
let state = { slots: {}, extensions: {} };
export const extensionStore = {
getState: () => state,
setState: (val) => {
state = { ...state, ...val };
},
subscribe: (updateFcn) => {
updateFcn(state);
return () => {};
},
unsubscribe: () => {},
};
export const ComponentContext = React.createContext(null);
export const openmrsComponentDecorator = jest
.fn()
.mockImplementation(() => (component) => component);
export const UserHasAccess = jest.fn().mockImplementation((props: any) => {
return props.children;
});
export const createUseStore = (store: Store<any>) => (actions) => {
const state = store.getState();
return { ...state, ...actions };
};
export const useExtensionStore = (actions) => {
const state = extensionStore.getState();
return { ...state, ...actions };
};
export const useStore = (store: Store<any>, actions) => {
const state = store.getState();
return { ...state, ...actions };
};
export const showNotification = jest.fn();
export const showToast = jest.fn();
export function setupPaths(config: any) {
window.openmrsBase = config.apiUrl;
window.spaBase = config.spaPath;
window.spaEnv = config.env || "production";
window.spaVersion = process.env.BUILD_VERSION;
window.getOpenmrsSpaBase = () => `${window.spaBase}/`;
}
export const attach = jest.fn();
export const detach = jest.fn();
export const detachAll = jest.fn();