Skip to content

Commit 1f7029e

Browse files
authored
Merge pull request #52822 from huult/48427-fix-cached-data-persists-after-account-switch
fix clear cached data after account switch
2 parents af647b8 + d4194d4 commit 1f7029e

File tree

3 files changed

+198
-1
lines changed

3 files changed

+198
-1
lines changed

src/CONFIG.ts

+1
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,5 @@ export default {
104104
// to read more about StrictMode see: contributingGuides/STRICT_MODE.md
105105
USE_REACT_STRICT_MODE_IN_DEV: false,
106106
ELECTRON_DISABLE_SECURITY_WARNINGS: 'true',
107+
IS_TEST_ENV: process.env.NODE_ENV === 'test',
107108
} as const;

src/libs/actions/QueuedOnyxUpdates.ts

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,44 @@
1-
import type {OnyxUpdate} from 'react-native-onyx';
1+
import type {OnyxKey, OnyxUpdate} from 'react-native-onyx';
22
import Onyx from 'react-native-onyx';
3+
import CONFIG from '@src/CONFIG';
4+
import ONYXKEYS from '@src/ONYXKEYS';
35

46
// In this file we manage a queue of Onyx updates while the SequentialQueue is processing. There are functions to get the updates and clear the queue after saving the updates in Onyx.
57

68
let queuedOnyxUpdates: OnyxUpdate[] = [];
9+
let currentAccountID: number | undefined;
10+
11+
Onyx.connect({
12+
key: ONYXKEYS.SESSION,
13+
callback: (session) => {
14+
currentAccountID = session?.accountID;
15+
},
16+
});
717

818
/**
919
* @param updates Onyx updates to queue for later
1020
*/
1121
function queueOnyxUpdates(updates: OnyxUpdate[]): Promise<void> {
1222
queuedOnyxUpdates = queuedOnyxUpdates.concat(updates);
23+
1324
return Promise.resolve();
1425
}
1526

1627
function flushQueue(): Promise<void> {
28+
if (!currentAccountID && !CONFIG.IS_TEST_ENV) {
29+
const preservedKeys: OnyxKey[] = [
30+
ONYXKEYS.NVP_TRY_FOCUS_MODE,
31+
ONYXKEYS.PREFERRED_THEME,
32+
ONYXKEYS.NVP_PREFERRED_LOCALE,
33+
ONYXKEYS.SESSION,
34+
ONYXKEYS.IS_LOADING_APP,
35+
ONYXKEYS.CREDENTIALS,
36+
ONYXKEYS.IS_SIDEBAR_LOADED,
37+
];
38+
39+
queuedOnyxUpdates = queuedOnyxUpdates.filter((update) => preservedKeys.includes(update.key as OnyxKey));
40+
}
41+
1742
return Onyx.update(queuedOnyxUpdates).then(() => {
1843
queuedOnyxUpdates = [];
1944
});
+171
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
import type {OnyxUpdate} from 'react-native-onyx';
2+
import Onyx from 'react-native-onyx';
3+
import {flushQueue, queueOnyxUpdates} from '@libs/actions/QueuedOnyxUpdates';
4+
import type {OnyxKey} from '@src/ONYXKEYS';
5+
import ONYXKEYS from '@src/ONYXKEYS';
6+
import waitForBatchedUpdates from '../utils/waitForBatchedUpdates';
7+
8+
const queuedOnyxUpdates: OnyxUpdate[] = [
9+
{key: ONYXKEYS.NVP_TRY_FOCUS_MODE, value: true, onyxMethod: 'merge'},
10+
{key: ONYXKEYS.PREFERRED_THEME, value: 'system', onyxMethod: 'merge'},
11+
{key: ONYXKEYS.NVP_PREFERRED_LOCALE, value: 'en', onyxMethod: 'merge'},
12+
{
13+
key: ONYXKEYS.SESSION,
14+
value: {
15+
accountID: 18748326,
16+
authToken: 'testToken',
17+
email: 'abcd+2342424224@gmail.com',
18+
encryptedAuthToken: 'testEncryptedAuthToken',
19+
loading: false,
20+
},
21+
onyxMethod: 'merge',
22+
},
23+
{key: ONYXKEYS.IS_LOADING_APP, value: false, onyxMethod: 'merge'},
24+
{
25+
key: ONYXKEYS.CREDENTIALS,
26+
value: {
27+
autoGeneratedLogin: '',
28+
autoGeneratedPassword: '',
29+
login: 'abcd+2342424224@gmail.com',
30+
},
31+
onyxMethod: 'merge',
32+
},
33+
{key: ONYXKEYS.IS_SIDEBAR_LOADED, value: true, onyxMethod: 'merge'},
34+
{key: `${ONYXKEYS.COLLECTION.REPORT}2175919089355165`, value: {reportID: 'reportID'}, onyxMethod: 'merge'},
35+
{
36+
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}2175919089355165`,
37+
value: {
38+
// eslint-disable-next-line @typescript-eslint/naming-convention
39+
'4135522899867010163': {
40+
reportActionID: '4135522899867010163',
41+
},
42+
},
43+
onyxMethod: 'merge',
44+
},
45+
];
46+
47+
jest.mock('@src/CONFIG', () => ({
48+
IS_TEST_ENV: false,
49+
}));
50+
51+
function getOnyxUpdateValue<T>(key: string): T | undefined {
52+
return queuedOnyxUpdates.find((item) => item.key === key)?.value as T | undefined;
53+
}
54+
55+
async function testOnyxKeyValue<T>(key: OnyxKey): Promise<void> {
56+
const expectedValue = getOnyxUpdateValue<T>(key);
57+
return new Promise<void>((resolve) => {
58+
const connection = Onyx.connect({
59+
key,
60+
waitForCollectionCallback: false,
61+
callback: (value) => {
62+
Onyx.disconnect(connection);
63+
64+
expect(value).toEqual(expectedValue);
65+
resolve();
66+
},
67+
});
68+
});
69+
}
70+
71+
describe('actions/QueuedOnyxUpdates', () => {
72+
beforeAll(() => {
73+
Onyx.init({
74+
keys: ONYXKEYS,
75+
});
76+
});
77+
78+
beforeEach(() => {
79+
return Onyx.clear().then(waitForBatchedUpdates);
80+
});
81+
82+
describe('flushQueue', () => {
83+
it('should filter queued updates when currentAccountID is undefined', async () => {
84+
await queueOnyxUpdates(queuedOnyxUpdates);
85+
await Onyx.multiSet({
86+
[ONYXKEYS.SESSION]: null,
87+
});
88+
89+
await flushQueue();
90+
91+
await testOnyxKeyValue(ONYXKEYS.NVP_TRY_FOCUS_MODE);
92+
await testOnyxKeyValue(ONYXKEYS.PREFERRED_THEME);
93+
await testOnyxKeyValue(ONYXKEYS.NVP_PREFERRED_LOCALE);
94+
await testOnyxKeyValue(ONYXKEYS.SESSION);
95+
await testOnyxKeyValue(ONYXKEYS.IS_LOADING_APP);
96+
await testOnyxKeyValue(ONYXKEYS.CREDENTIALS);
97+
await testOnyxKeyValue(ONYXKEYS.IS_SIDEBAR_LOADED);
98+
99+
await new Promise<void>((resolve) => {
100+
const connection = Onyx.connect({
101+
key: `${ONYXKEYS.COLLECTION.REPORT}2175919089355165`,
102+
waitForCollectionCallback: false,
103+
callback: (report) => {
104+
Onyx.disconnect(connection);
105+
expect(report).toBeUndefined();
106+
107+
resolve();
108+
},
109+
});
110+
});
111+
112+
await new Promise<void>((resolve) => {
113+
const connection = Onyx.connect({
114+
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}2175919089355165`,
115+
waitForCollectionCallback: false,
116+
callback: (report) => {
117+
Onyx.disconnect(connection);
118+
expect(report).toBeUndefined();
119+
120+
resolve();
121+
},
122+
});
123+
});
124+
});
125+
126+
it('should not filter updates if currentAccountID is defined', async () => {
127+
await queueOnyxUpdates(queuedOnyxUpdates);
128+
await Onyx.multiSet({
129+
[ONYXKEYS.SESSION]: {
130+
accountID: 1,
131+
},
132+
});
133+
134+
await flushQueue();
135+
136+
await testOnyxKeyValue(ONYXKEYS.NVP_TRY_FOCUS_MODE);
137+
await testOnyxKeyValue(ONYXKEYS.PREFERRED_THEME);
138+
await testOnyxKeyValue(ONYXKEYS.NVP_PREFERRED_LOCALE);
139+
await testOnyxKeyValue(ONYXKEYS.SESSION);
140+
await testOnyxKeyValue(ONYXKEYS.IS_LOADING_APP);
141+
await testOnyxKeyValue(ONYXKEYS.CREDENTIALS);
142+
await testOnyxKeyValue(ONYXKEYS.IS_SIDEBAR_LOADED);
143+
144+
await new Promise<void>((resolve) => {
145+
const connection = Onyx.connect({
146+
key: `${ONYXKEYS.COLLECTION.REPORT}2175919089355165`,
147+
waitForCollectionCallback: false,
148+
callback: (report) => {
149+
Onyx.disconnect(connection);
150+
expect(report).toEqual(getOnyxUpdateValue(`${ONYXKEYS.COLLECTION.REPORT}2175919089355165`));
151+
152+
resolve();
153+
},
154+
});
155+
});
156+
157+
await new Promise<void>((resolve) => {
158+
const connection = Onyx.connect({
159+
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}2175919089355165`,
160+
waitForCollectionCallback: false,
161+
callback: (reportActions) => {
162+
Onyx.disconnect(connection);
163+
expect(reportActions).toEqual(getOnyxUpdateValue(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}2175919089355165`));
164+
165+
resolve();
166+
},
167+
});
168+
});
169+
});
170+
});
171+
});

0 commit comments

Comments
 (0)