Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix clear cached data after account switch #52822

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/CONFIG.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,5 @@ export default {
// to read more about StrictMode see: contributingGuides/STRICT_MODE.md
USE_REACT_STRICT_MODE_IN_DEV: false,
ELECTRON_DISABLE_SECURITY_WARNINGS: 'true',
IS_TEST_ENV: process.env.NODE_ENV === 'test',
} as const;
27 changes: 26 additions & 1 deletion src/libs/actions/QueuedOnyxUpdates.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,44 @@
import type {OnyxUpdate} from 'react-native-onyx';
import type {OnyxKey, OnyxUpdate} from 'react-native-onyx';
import Onyx from 'react-native-onyx';
import CONFIG from '@src/CONFIG';
import ONYXKEYS from '@src/ONYXKEYS';

// 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.

let queuedOnyxUpdates: OnyxUpdate[] = [];
let currentAccountID: number | undefined;

Onyx.connect({
key: ONYXKEYS.SESSION,
callback: (session) => {
currentAccountID = session?.accountID;
},
});

/**
* @param updates Onyx updates to queue for later
*/
function queueOnyxUpdates(updates: OnyxUpdate[]): Promise<void> {
queuedOnyxUpdates = queuedOnyxUpdates.concat(updates);

return Promise.resolve();
}

function flushQueue(): Promise<void> {
if (!currentAccountID && !CONFIG.IS_TEST_ENV) {
const preservedKeys: OnyxKey[] = [
ONYXKEYS.NVP_TRY_FOCUS_MODE,
ONYXKEYS.PREFERRED_THEME,
ONYXKEYS.NVP_PREFERRED_LOCALE,
ONYXKEYS.SESSION,
ONYXKEYS.IS_LOADING_APP,
ONYXKEYS.CREDENTIALS,
ONYXKEYS.IS_SIDEBAR_LOADED,
];

queuedOnyxUpdates = queuedOnyxUpdates.filter((update) => preservedKeys.includes(update.key as OnyxKey));
}

return Onyx.update(queuedOnyxUpdates).then(() => {
queuedOnyxUpdates = [];
});
Expand Down
171 changes: 171 additions & 0 deletions tests/actions/QueuedOnyxUpdatesTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
import type {OnyxUpdate} from 'react-native-onyx';
import Onyx from 'react-native-onyx';
import {flushQueue, queueOnyxUpdates} from '@libs/actions/QueuedOnyxUpdates';
import type {OnyxKey} from '@src/ONYXKEYS';
import ONYXKEYS from '@src/ONYXKEYS';
import waitForBatchedUpdates from '../utils/waitForBatchedUpdates';

const queuedOnyxUpdates: OnyxUpdate[] = [
{key: ONYXKEYS.NVP_TRY_FOCUS_MODE, value: true, onyxMethod: 'merge'},
{key: ONYXKEYS.PREFERRED_THEME, value: 'system', onyxMethod: 'merge'},
{key: ONYXKEYS.NVP_PREFERRED_LOCALE, value: 'en', onyxMethod: 'merge'},
{
key: ONYXKEYS.SESSION,
value: {
accountID: 18748326,
authToken: 'testToken',
email: 'abcd+2342424224@gmail.com',
encryptedAuthToken: 'testEncryptedAuthToken',
loading: false,
},
onyxMethod: 'merge',
},
{key: ONYXKEYS.IS_LOADING_APP, value: false, onyxMethod: 'merge'},
{
key: ONYXKEYS.CREDENTIALS,
value: {
autoGeneratedLogin: '',
autoGeneratedPassword: '',
login: 'abcd+2342424224@gmail.com',
},
onyxMethod: 'merge',
},
{key: ONYXKEYS.IS_SIDEBAR_LOADED, value: true, onyxMethod: 'merge'},
{key: `${ONYXKEYS.COLLECTION.REPORT}2175919089355165`, value: {reportID: 'reportID'}, onyxMethod: 'merge'},
{
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}2175919089355165`,
value: {
// eslint-disable-next-line @typescript-eslint/naming-convention
'4135522899867010163': {
reportActionID: '4135522899867010163',
},
},
onyxMethod: 'merge',
},
];

jest.mock('@src/CONFIG', () => ({
IS_TEST_ENV: false,
}));

function getOnyxUpdateValue<T>(key: string): T | undefined {
return queuedOnyxUpdates.find((item) => item.key === key)?.value as T | undefined;
}

async function testOnyxKeyValue<T>(key: OnyxKey): Promise<void> {
const expectedValue = getOnyxUpdateValue<T>(key);
return new Promise<void>((resolve) => {
const connection = Onyx.connect({
key,
waitForCollectionCallback: false,
callback: (value) => {
Onyx.disconnect(connection);

expect(value).toEqual(expectedValue);
resolve();
},
});
});
}

describe('actions/QueuedOnyxUpdates', () => {
beforeAll(() => {
Onyx.init({
keys: ONYXKEYS,
});
});

beforeEach(() => {
return Onyx.clear().then(waitForBatchedUpdates);
});

describe('flushQueue', () => {
it('should filter queued updates when currentAccountID is undefined', async () => {
await queueOnyxUpdates(queuedOnyxUpdates);
await Onyx.multiSet({
[ONYXKEYS.SESSION]: null,
});

await flushQueue();

await testOnyxKeyValue(ONYXKEYS.NVP_TRY_FOCUS_MODE);
await testOnyxKeyValue(ONYXKEYS.PREFERRED_THEME);
await testOnyxKeyValue(ONYXKEYS.NVP_PREFERRED_LOCALE);
await testOnyxKeyValue(ONYXKEYS.SESSION);
await testOnyxKeyValue(ONYXKEYS.IS_LOADING_APP);
await testOnyxKeyValue(ONYXKEYS.CREDENTIALS);
await testOnyxKeyValue(ONYXKEYS.IS_SIDEBAR_LOADED);

await new Promise<void>((resolve) => {
const connection = Onyx.connect({
key: `${ONYXKEYS.COLLECTION.REPORT}2175919089355165`,
waitForCollectionCallback: false,
callback: (report) => {
Onyx.disconnect(connection);
expect(report).toBeUndefined();

resolve();
},
});
});

await new Promise<void>((resolve) => {
const connection = Onyx.connect({
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}2175919089355165`,
waitForCollectionCallback: false,
callback: (report) => {
Onyx.disconnect(connection);
expect(report).toBeUndefined();

resolve();
},
});
});
});

it('should not filter updates if currentAccountID is defined', async () => {
await queueOnyxUpdates(queuedOnyxUpdates);
await Onyx.multiSet({
[ONYXKEYS.SESSION]: {
accountID: 1,
},
});

await flushQueue();

await testOnyxKeyValue(ONYXKEYS.NVP_TRY_FOCUS_MODE);
await testOnyxKeyValue(ONYXKEYS.PREFERRED_THEME);
await testOnyxKeyValue(ONYXKEYS.NVP_PREFERRED_LOCALE);
await testOnyxKeyValue(ONYXKEYS.SESSION);
await testOnyxKeyValue(ONYXKEYS.IS_LOADING_APP);
await testOnyxKeyValue(ONYXKEYS.CREDENTIALS);
await testOnyxKeyValue(ONYXKEYS.IS_SIDEBAR_LOADED);

await new Promise<void>((resolve) => {
const connection = Onyx.connect({
key: `${ONYXKEYS.COLLECTION.REPORT}2175919089355165`,
waitForCollectionCallback: false,
callback: (report) => {
Onyx.disconnect(connection);
expect(report).toEqual(getOnyxUpdateValue(`${ONYXKEYS.COLLECTION.REPORT}2175919089355165`));

resolve();
},
});
});

await new Promise<void>((resolve) => {
const connection = Onyx.connect({
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}2175919089355165`,
waitForCollectionCallback: false,
callback: (reportActions) => {
Onyx.disconnect(connection);
expect(reportActions).toEqual(getOnyxUpdateValue(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}2175919089355165`));

resolve();
},
});
});
});
});
});
Loading