-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmadie-util.tsx
More file actions
194 lines (177 loc) · 5.9 KB
/
Copy pathmadie-util.tsx
File metadata and controls
194 lines (177 loc) · 5.9 KB
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
import React, { useState, useEffect } from "react";
// These functions are listed so that they can be referenced in jest files since jest cannot see browser.
// some functions are copy pasted from the source since their side effects are required for tests.
export function useKeyPress(targetKey) {
// State for keeping track of whether key is pressed
const [keyPressed, setKeyPressed] = useState(false);
// If pressed key is our target key then set to true
function downHandler({ key }) {
if (key === targetKey) {
setKeyPressed(true);
}
}
// If released key is our target key then set to false
const upHandler = ({ key }) => {
if (key === targetKey) {
setKeyPressed(false);
}
};
// Add event listeners
useEffect(() => {
window.addEventListener("keydown", downHandler);
window.addEventListener("keyup", upHandler);
// Remove event listeners on cleanup
return () => {
window.removeEventListener("keydown", downHandler);
window.removeEventListener("keyup", upHandler);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []); // Empty array ensures that effect is only run on mount and unmount
return keyPressed;
}
export const useOktaTokens = (storageKey = "okta-token-storage") => {
return {
getAccessToken: () => "test-token",
getAccessTokenObj: () => {},
getUserName: () => "test-fake-user@email.com", //#nosec
getIdToken: () => "test-id-token",
getIdTokenObj: () => {},
};
};
export function useOnClickOutside(ref, handler) {
useEffect(() => {
const listener = (event) => {
// Do nothing if clicking ref's element or descendent elements
if (!ref.current || ref.current.contains(event.target)) {
return;
}
handler(event);
};
document.addEventListener("mousedown", listener);
document.addEventListener("touchstart", listener);
return () => {
document.removeEventListener("mousedown", listener);
document.removeEventListener("touchstart", listener);
};
}, [ref, handler]);
}
export const routeHandlerStore = {
initialState: { canTravel: true, pendingRoute: "" },
state: { canTravel: true, pendingRoute: "" },
subscribe: () => null, // needs to return an object with key subscribe
unsubscribe: () => null,
};
// Check if user can edit a measure
export const checkUserCanEdit = (
owner: string,
sharedWith: string[] = []
): boolean => {
const username = "test-fake-user@email.com"; //#nosec
return owner === username || sharedWith.includes(username);
};
// Check if user can delete a measure
export const checkUserCanDelete = (owner: string): boolean => {
const username = "test-fake-user@email.com"; //#nosec
return owner === username;
};
// Mock userRolesStore
export const userRolesStore = {
getState: () => ({ roles: [], isAdmin: false }),
updateUserRoles: () => {},
subscribe: () => ({ unsubscribe: () => {} }),
};
// Mock useUserRoles hook
export const useUserRoles = () => ({ roles: [], isAdmin: false });
export const getOidFromString = (
oidString: string,
dataModel: string
): string => {
if (dataModel === "QDM") {
return oidString?.split("urn:oid:")[1];
}
return oidString?.split("ValueSet/")[1];
};
export const exportMeasure = jest.fn();
export const downloadZipFile = jest.fn();
export const parseErrorMessageFromBlob = jest.fn();
export const generateTimestampedFileName = jest.fn(
(baseName: string, extension: string) => `${baseName}_timestamp.${extension}`
);
export const EXPORT_FAILURE_MESSAGE =
"Unable to Export measure. Package could not be generated. Please try again and contact the Help Desk if the problem persists.";
export const getNewestMeasureInstance = jest.fn(
(measures: any[]) => measures?.[0]
);
export const ExportAction = ({ onClick }: any) => (
<div
role="button"
tabIndex={0}
data-testid="export-action"
onClick={() => onClick && onClick("Export")}
>
Export Action
</div>
);
export const ViewHRAction = ({ onClick }: any) => (
<button data-testid="view-hr-action-btn" onClick={onClick}>
View HR Action
</button>
);
export const HistoryAction = ({ onClick }: any) => (
<button data-testid="history-action-btn" onClick={onClick}>
History Action
</button>
);
export const CompareVersionsAction = ({ onClick }: any) => (
<button data-testid="compare-versions-action-btn" onClick={onClick}>
Compare Versions Action
</button>
);
export const ExportDialog = ({ open }: any) =>
open ? <div data-testid="export-dialog">Export Dialog</div> : null;
export const ExportIcon = () => <span data-testid="export-icon" />;
export const ViewHRModal = ({ open, onClose }: any) =>
open ? (
<div data-testid="view-human-readable-modal">
View HR Modal
<button data-testid="view-hr-close-btn" onClick={onClose}>
Close
</button>
</div>
) : null;
export const ViewMeasureHistoryDialog = ({ open, onClose }: any) =>
open ? (
<div data-testid="view-measure-history-dialog">
Measure History
<button data-testid="view-history-close-btn" onClick={onClose}>
Close
</button>
</div>
) : null;
export const CompareVersionsDialog = ({ open }: any) =>
open ? (
<div data-testid="compare-versions-dialog">Compare Versions</div>
) : null;
export const ShareAction = ({ measures, activeTab, onClick }: any) => {
const options = activeTab === 1 ? ["Unshare"] : ["Share With", "Unshare"];
return (
<div>
<button data-testid="share-action-btn" disabled={!measures?.length}>
Share
</button>
{options.map((option: string) => (
<div
key={option}
role="menuitem"
tabIndex={0}
onClick={() => onClick && onClick(option)}
>
{option}
</div>
))}
</div>
);
};
export const ShareDialog = ({ open }: any) =>
open ? <div data-testid="share-dialog">Share Dialog</div> : null;
export { formatCmsId, padCmsId } from "../cmsIdFormatterStubs";