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