-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
PolicyTest.js
191 lines (170 loc) · 8.27 KB
/
PolicyTest.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import _ from 'lodash';
import Onyx from 'react-native-onyx';
import CONST from '@src/CONST';
import OnyxUpdateManager from '../../src/libs/actions/OnyxUpdateManager';
import * as Policy from '../../src/libs/actions/Policy';
import ONYXKEYS from '../../src/ONYXKEYS';
import * as TestHelper from '../utils/TestHelper';
import waitForBatchedUpdates from '../utils/waitForBatchedUpdates';
const ESH_EMAIL = 'eshgupta1217@gmail.com';
const ESH_ACCOUNT_ID = 1;
const WORKSPACE_NAME = "Esh's Workspace";
OnyxUpdateManager();
describe('actions/Policy', () => {
beforeAll(() => {
Onyx.init({
keys: ONYXKEYS,
});
});
beforeEach(() => {
global.fetch = TestHelper.getGlobalFetchMock();
return Onyx.clear().then(waitForBatchedUpdates);
});
describe('createWorkspace', () => {
it('creates a new workspace', async () => {
fetch.pause();
Onyx.set(ONYXKEYS.SESSION, {email: ESH_EMAIL, accountID: ESH_ACCOUNT_ID});
await waitForBatchedUpdates();
let adminReportID;
let announceReportID;
let expenseReportID;
const policyID = Policy.generatePolicyID();
Policy.createWorkspace(ESH_EMAIL, true, WORKSPACE_NAME, policyID);
await waitForBatchedUpdates();
let policy = await new Promise((resolve) => {
const connectionID = Onyx.connect({
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
waitForCollectionCallback: true,
callback: (workspace) => {
Onyx.disconnect(connectionID);
resolve(workspace);
},
});
});
// check if policy was created with correct values
expect(policy.id).toBe(policyID);
expect(policy.name).toBe(WORKSPACE_NAME);
expect(policy.type).toBe(CONST.POLICY.TYPE.FREE);
expect(policy.role).toBe(CONST.POLICY.ROLE.ADMIN);
expect(policy.owner).toBe(ESH_EMAIL);
expect(policy.isPolicyExpenseChatEnabled).toBe(true);
expect(policy.pendingAction).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD);
const policyMembers = await new Promise((resolve) => {
const connectionID = Onyx.connect({
key: `${ONYXKEYS.COLLECTION.POLICY_MEMBERS}${policyID}`,
waitForCollectionCallback: true,
callback: (members) => {
Onyx.disconnect(connectionID);
resolve(members);
},
});
});
// check if the user was added as an admin to the policy
expect(policyMembers[ESH_ACCOUNT_ID].role).toBe(CONST.POLICY.ROLE.ADMIN);
let allReports = await new Promise((resolve) => {
const connectionID = Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT,
waitForCollectionCallback: true,
callback: (reports) => {
Onyx.disconnect(connectionID);
resolve(reports);
},
});
});
// Three reports should be created: #announce, #admins and expense report
const workspaceReports = _.filter(allReports, (report) => report.policyID === policyID);
expect(_.size(workspaceReports)).toBe(3);
_.forEach(workspaceReports, (report) => {
expect(report.pendingFields.addWorkspaceRoom).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD);
expect(report.participantAccountIDs).toEqual([ESH_ACCOUNT_ID]);
switch (report.chatType) {
case CONST.REPORT.CHAT_TYPE.POLICY_ADMINS: {
adminReportID = report.reportID;
break;
}
case CONST.REPORT.CHAT_TYPE.POLICY_ANNOUNCE: {
announceReportID = report.reportID;
break;
}
case CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT: {
expenseReportID = report.reportID;
break;
}
default:
break;
}
});
let reportActions = await new Promise((resolve) => {
const connectionID = Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT_ACTIONS,
waitForCollectionCallback: true,
callback: (actions) => {
Onyx.disconnect(connectionID);
resolve(actions);
},
});
});
// Each of the three reports should have a a `CREATED` action.
let adminReportActions = _.values(reportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${adminReportID}`]);
let announceReportActions = _.values(reportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${announceReportID}`]);
let expenseReportActions = _.values(reportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${expenseReportID}`]);
let workspaceReportActions = _.concat(adminReportActions, announceReportActions, expenseReportActions);
_.forEach([adminReportActions, announceReportActions, expenseReportActions], (actions) => {
expect(_.size(actions)).toBe(1);
});
_.forEach([...adminReportActions, ...announceReportActions, ...expenseReportActions], (reportAction) => {
expect(reportAction.actionName).toBe(CONST.REPORT.ACTIONS.TYPE.CREATED);
expect(reportAction.pendingAction).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD);
expect(reportAction.actorAccountID).toBe(ESH_ACCOUNT_ID);
});
// Check for success data
fetch.resume();
await waitForBatchedUpdates();
policy = await new Promise((resolve) => {
const connectionID = Onyx.connect({
key: ONYXKEYS.COLLECTION.POLICY,
waitForCollectionCallback: true,
callback: (workspace) => {
Onyx.disconnect(connectionID);
resolve(workspace);
},
});
});
// Check if the policy pending action was cleared
expect(policy.pendingAction).toBeFalsy();
allReports = await new Promise((resolve) => {
const connectionID = Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT,
waitForCollectionCallback: true,
callback: (reports) => {
Onyx.disconnect(connectionID);
resolve(reports);
},
});
});
// Check if the report pending action and fields were cleared
_.forEach(allReports, (report) => {
expect(report.pendingAction).toBeFalsy();
expect(report.pendingFields.addWorkspaceRoom).toBeFalsy();
});
reportActions = await new Promise((resolve) => {
const connectionID = Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT_ACTIONS,
waitForCollectionCallback: true,
callback: (actions) => {
Onyx.disconnect(connectionID);
resolve(actions);
},
});
});
// Check if the report action pending action was cleared
adminReportActions = _.values(reportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${adminReportID}`]);
announceReportActions = _.values(reportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${announceReportID}`]);
expenseReportActions = _.values(reportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${expenseReportID}`]);
workspaceReportActions = _.concat(adminReportActions, announceReportActions, expenseReportActions);
_.forEach(workspaceReportActions, (reportAction) => {
expect(reportAction.pendingAction).toBeFalsy();
});
});
});
});