-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
LHNTestUtils.js
170 lines (161 loc) · 5.81 KB
/
LHNTestUtils.js
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
import React from 'react';
import {render} from '@testing-library/react-native';
import ComposeProviders from '../../src/components/ComposeProviders';
import OnyxProvider from '../../src/components/OnyxProvider';
import {LocaleContextProvider} from '../../src/components/withLocalize';
import SidebarLinks from '../../src/pages/home/sidebar/SidebarLinks';
import CONST from '../../src/CONST';
import DateUtils from '../../src/libs/DateUtils';
const fakePersonalDetails = {
'[email protected]': {
login: '[email protected]',
displayName: 'Email One',
avatar: 'none',
firstName: 'One',
},
'[email protected]': {
login: '[email protected]',
displayName: 'Email Two',
avatar: 'none',
firstName: 'Two',
},
'[email protected]': {
login: '[email protected]',
displayName: 'Email Three',
avatar: 'none',
firstName: 'Three',
},
'[email protected]': {
login: '[email protected]',
displayName: 'Email Four',
avatar: 'none',
firstName: 'Four',
},
'[email protected]': {
login: '[email protected]',
displayName: 'Email Five',
avatar: 'none',
firstName: 'Five',
},
'[email protected]': {
login: '[email protected]',
displayName: 'Email Six',
avatar: 'none',
firstName: 'Six',
},
'[email protected]': {
login: '[email protected]',
displayName: 'Email Seven',
avatar: 'none',
firstName: 'Seven',
},
'[email protected]': {
login: '[email protected]',
displayName: 'Email Eight',
avatar: 'none',
firstName: 'Eight',
},
'[email protected]': {
login: '[email protected]',
displayName: 'Email Nine',
avatar: 'none',
firstName: 'Nine',
},
};
let lastFakeReportID = 0;
/**
* @param {String[]} participants
* @param {Number} millisecondsInThePast the number of milliseconds in the past for the last message timestamp (to order reports by most recent messages)
* @param {boolean} isUnread
* @returns {Object}
*/
function getFakeReport(participants = ['[email protected]', '[email protected]'], millisecondsInThePast = 0, isUnread = false) {
const lastActionCreated = DateUtils.getDBTime(Date.now() - millisecondsInThePast);
return {
reportID: `${++lastFakeReportID}`,
reportName: 'Report',
lastActionCreated,
lastReadTime: isUnread ? DateUtils.subtractMillisecondsFromDateTime(lastActionCreated, 1) : lastActionCreated,
participants,
};
}
/**
* There is one setting not represented here, which is hasOutstandingIOU. In order to test that setting, there must be
* additional reports in Onyx, so it's being left out for now.
*
* @param {boolean} isArchived
* @param {boolean} isUserCreatedPolicyRoom
* @param {boolean} hasAddWorkspaceError
* @param {boolean} isUnread
* @param {boolean} isPinned
* @param {boolean} hasDraft
* @returns {Object}
*/
function getAdvancedFakeReport(isArchived, isUserCreatedPolicyRoom, hasAddWorkspaceError, isUnread, isPinned, hasDraft) {
return {
...getFakeReport(['[email protected]', '[email protected]'], 0, isUnread),
chatType: isUserCreatedPolicyRoom ? CONST.REPORT.CHAT_TYPE.POLICY_ROOM : CONST.REPORT.CHAT_TYPE.POLICY_ADMINS,
statusNum: isArchived ? CONST.REPORT.STATUS.CLOSED : 0,
stateNum: isArchived ? CONST.REPORT.STATE_NUM.SUBMITTED : 0,
errorFields: hasAddWorkspaceError ? {addWorkspaceRoom: 'blah'} : null,
isPinned,
hasDraft,
};
}
/**
* @param {String} [reportIDFromRoute]
* @returns {RenderAPI}
*/
function getDefaultRenderedSidebarLinks(reportIDFromRoute = '') {
// An ErrorBoundary needs to be added to the rendering so that any errors that happen while the component
// renders are logged to the console. Without an error boundary, Jest only reports the error like "The above error
// occurred in your component", except, there is no "above error". It's just swallowed up by Jest somewhere.
// With the ErrorBoundary, those errors are caught and logged to the console so you can find exactly which error
// might be causing a rendering issue when developing tests.
class ErrorBoundary extends React.Component {
// Error boundaries have to implement this method. It's for providing a fallback UI, but
// we don't need that for unit testing, so this is basically a no-op.
static getDerivedStateFromError(error) {
return {error};
}
componentDidCatch(error, errorInfo) {
console.error(error, errorInfo);
}
render() {
// eslint-disable-next-line react/prop-types
return this.props.children;
}
}
// Wrap the SideBarLinks inside of LocaleContextProvider so that all the locale props
// are passed to the component. If this is not done, then all the locale props are missing
// and there are a lot of render warnings. It needs to be done like this because normally in
// our app (App.js) is when the react application is wrapped in the context providers
return render((
<ComposeProviders
components={[
OnyxProvider,
LocaleContextProvider,
]}
>
<ErrorBoundary>
<SidebarLinks
onLinkClick={() => {}}
insets={{
top: 0,
left: 0,
right: 0,
bottom: 0,
}}
isSmallScreenWidth={false}
reportIDFromRoute={reportIDFromRoute}
/>
</ErrorBoundary>
</ComposeProviders>
));
}
export {
fakePersonalDetails,
getDefaultRenderedSidebarLinks,
getAdvancedFakeReport,
getFakeReport,
};