-
Notifications
You must be signed in to change notification settings - Fork 4k
Expand file tree
/
Copy pathDeleteReportTest.ts
More file actions
193 lines (160 loc) · 8.75 KB
/
Copy pathDeleteReportTest.ts
File metadata and controls
193 lines (160 loc) · 8.75 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
import deleteReport from '@libs/actions/Report/DeleteReport';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {ReportAction} from '@src/types/onyx';
import Onyx from 'react-native-onyx';
import createRandomReportAction from '../utils/collections/reportActions';
import {createRandomReport} from '../utils/collections/reports';
import createRandomTransaction from '../utils/collections/transaction';
import getOnyxValue from '../utils/getOnyxValue';
import waitForBatchedUpdates from '../utils/waitForBatchedUpdates';
// Mock Log to avoid noise in tests
jest.mock('@libs/Log');
describe('actions/Report/DeleteReport', () => {
beforeAll(() => {
Onyx.init({
keys: ONYXKEYS,
});
});
beforeEach(() => {
return Onyx.clear();
});
it('should delete a report, its actions, and linked transactions', async () => {
// Given a report with an IOU action and a linked transaction
const reportID = '1';
const transactionID = '123';
const report = createRandomReport(1, undefined);
const transaction = createRandomTransaction(Number(transactionID));
const reportAction: ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.IOU> = {
...createRandomReportAction(100),
actionName: CONST.REPORT.ACTIONS.TYPE.IOU,
previousMessage: [],
message: [{text: '', html: 'iou', type: 'COMMENT'}],
originalMessage: {
type: CONST.IOU.REPORT_ACTION_TYPE.CREATE,
IOUTransactionID: transaction.transactionID,
IOUDetails: {
amount: transaction.amount,
currency: transaction.currency,
comment: '',
},
},
};
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, report);
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, {
[reportAction.reportActionID]: reportAction,
});
await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, transaction);
// When we delete the report
deleteReport(reportID);
await waitForBatchedUpdates();
// Then the report, its actions, and the linked transaction should be deleted
expect(await getOnyxValue(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`)).toBeUndefined();
expect(await getOnyxValue(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`)).toBeUndefined();
expect(await getOnyxValue(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`)).toBeUndefined();
});
it('should delete child reports recursively when shouldDeleteChildReports is true', async () => {
// Given a parent report with a child report
const parentReportID = '1';
const childReportID = '2';
const parentReport = createRandomReport(Number(parentReportID), undefined);
const childReport = createRandomReport(Number(childReportID), undefined);
const parentAction = {
...createRandomReportAction(100),
childReportID,
};
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${parentReportID}`, parentReport);
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${childReportID}`, childReport);
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${parentReportID}`, {
[parentAction.reportActionID]: parentAction,
});
// When we delete the parent report with shouldDeleteChildReports set to true
deleteReport(parentReportID, true);
await waitForBatchedUpdates();
// Then both the parent and child reports should be deleted
expect(await getOnyxValue(`${ONYXKEYS.COLLECTION.REPORT}${parentReportID}`)).toBeUndefined();
expect(await getOnyxValue(`${ONYXKEYS.COLLECTION.REPORT}${childReportID}`)).toBeUndefined();
});
it('should delete linked IOU reports recursively', async () => {
// Given a report with a linked IOU report
const reportID = '1';
const iouReportID = '2';
const report = {
...createRandomReport(Number(reportID), undefined),
iouReportID,
};
const iouReport = createRandomReport(Number(iouReportID), undefined);
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, report);
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${iouReportID}`, iouReport);
// When we delete the report
deleteReport(reportID);
await waitForBatchedUpdates();
// Then both reports should be deleted
expect(await getOnyxValue(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`)).toBeUndefined();
expect(await getOnyxValue(`${ONYXKEYS.COLLECTION.REPORT}${iouReportID}`)).toBeUndefined();
});
it('should clear the report metadata when a report is deleted', async () => {
// Given a report that has metadata (e.g. an optimistic report)
const reportID = '1';
const report = createRandomReport(Number(reportID), undefined);
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, report);
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_METADATA}${reportID}`, {isOptimisticReport: true});
// When we delete the report
deleteReport(reportID);
await waitForBatchedUpdates();
// Then the report metadata should be cleared so nothing (e.g. isOptimisticReport) is left orphaned behind
expect(await getOnyxValue(`${ONYXKEYS.COLLECTION.REPORT_METADATA}${reportID}`)).toBeUndefined();
});
it('should cascade from a chat report to its linked IOU report, transaction thread, and transaction', async () => {
// Given a brand-new chat report linked to an IOU report (via iouReportID and a report preview child action),
// an IOU report whose money-request action opens a transaction thread, and that transaction. This mirrors the
// failed new-chat expense rollback: dismissing deletes the parent chat, which must cascade to everything below.
const chatReportID = '1';
const iouReportID = '2';
const transactionThreadReportID = '3';
const transactionID = '123';
const chatReport = {
...createRandomReport(Number(chatReportID), undefined),
iouReportID,
};
const iouReport = createRandomReport(Number(iouReportID), undefined);
const transactionThreadReport = createRandomReport(Number(transactionThreadReportID), undefined);
const transaction = createRandomTransaction(Number(transactionID));
// Report preview action in the chat linking to the IOU report
const reportPreviewAction = {
...createRandomReportAction(100),
childReportID: iouReportID,
};
// Money-request action in the IOU report linking to the transaction thread and transaction
const iouAction: ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.IOU> = {
...createRandomReportAction(200),
actionName: CONST.REPORT.ACTIONS.TYPE.IOU,
childReportID: transactionThreadReportID,
previousMessage: [],
message: [{text: '', html: 'iou', type: 'COMMENT'}],
originalMessage: {
type: CONST.IOU.REPORT_ACTION_TYPE.CREATE,
IOUTransactionID: transactionID,
IOUDetails: {
amount: transaction.amount,
currency: transaction.currency,
comment: '',
},
},
};
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${chatReportID}`, chatReport);
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${iouReportID}`, iouReport);
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${transactionThreadReportID}`, transactionThreadReport);
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReportID}`, {[reportPreviewAction.reportActionID]: reportPreviewAction});
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReportID}`, {[iouAction.reportActionID]: iouAction});
await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, transaction);
// When we delete the parent chat report with cascade
deleteReport(chatReportID, true);
await waitForBatchedUpdates();
// Then the chat, the linked IOU report, the transaction thread, and the transaction are all removed
expect(await getOnyxValue(`${ONYXKEYS.COLLECTION.REPORT}${chatReportID}`)).toBeUndefined();
expect(await getOnyxValue(`${ONYXKEYS.COLLECTION.REPORT}${iouReportID}`)).toBeUndefined();
expect(await getOnyxValue(`${ONYXKEYS.COLLECTION.REPORT}${transactionThreadReportID}`)).toBeUndefined();
expect(await getOnyxValue(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`)).toBeUndefined();
});
});