Skip to content

Commit ecd8610

Browse files
committed
Improve tests
1 parent c520dd4 commit ecd8610

8 files changed

Lines changed: 73 additions & 122 deletions

tests/perf-test/Onyx.perf-test.ts

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import {measureAsyncFunction} from 'reassure';
22
import type {OnyxUpdate} from '../../lib';
33
import Onyx from '../../lib';
4-
import {createCollection} from '../utils/collections/createCollection';
5-
import createRandomReportAction from '../utils/collections/reportActions';
4+
import createRandomReportAction, {getRandomReportActions} from '../utils/collections/reportActions';
65
import type GenericCollection from '../utils/GenericCollection';
76
import OnyxUtils from '../../lib/OnyxUtils';
87
import createDeferredTask from '../../lib/createDeferredTask';
8+
import alternateLists from '../utils/alternateLists';
99

1010
const ONYXKEYS = {
1111
TEST_KEY: 'test',
@@ -20,20 +20,8 @@ const ONYXKEYS = {
2020
},
2121
};
2222

23-
const alternateLists = (list1: unknown[], list2: unknown[]) => {
24-
return list1.length > list2.length
25-
? list1.flatMap((item, i) => [item, list2[i]]).filter((x) => x !== undefined)
26-
: list2.flatMap((item, i) => [list1[i], item]).filter((x) => x !== undefined);
27-
};
28-
29-
const getMockedReportActions = (collection = ONYXKEYS.COLLECTION.TEST_KEY, length = 10000) =>
30-
createCollection<Record<string, unknown>>(
31-
(item) => `${collection}${item.reportActionID}`,
32-
(index) => createRandomReportAction(index),
33-
length,
34-
);
35-
36-
const mockedReportActionsMap = getMockedReportActions();
23+
const collectionKey = ONYXKEYS.COLLECTION.TEST_KEY;
24+
const mockedReportActionsMap = getRandomReportActions(collectionKey);
3725

3826
const clearOnyxAfterEachMeasure = async () => {
3927
await Onyx.clear();
@@ -59,7 +47,7 @@ describe('Onyx', () => {
5947
() =>
6048
Promise.all(
6149
Object.values(mockedReportActionsMap).map((reportAction) => {
62-
return Onyx.set(`${ONYXKEYS.COLLECTION.TEST_KEY}${reportAction.reportActionID}`, reportAction);
50+
return Onyx.set(`${collectionKey}${reportAction.reportActionID}`, reportAction);
6351
}),
6452
),
6553
{afterEach: clearOnyxAfterEachMeasure},
@@ -80,7 +68,7 @@ describe('Onyx', () => {
8068
() =>
8169
Promise.all(
8270
Object.values(changedReportActions).map((changedReportAction) => {
83-
return Onyx.merge(`${ONYXKEYS.COLLECTION.TEST_KEY}${changedReportAction.reportActionID}`, changedReportAction);
71+
return Onyx.merge(`${collectionKey}${changedReportAction.reportActionID}`, changedReportAction);
8472
}),
8573
),
8674
{
@@ -98,7 +86,7 @@ describe('Onyx', () => {
9886
const changedReportActions = Object.fromEntries(
9987
Object.entries(mockedReportActionsMap).map(([k, v]) => [k, createRandomReportAction(Number(v.reportActionID))] as const),
10088
) as GenericCollection;
101-
await measureAsyncFunction(() => Onyx.mergeCollection(ONYXKEYS.COLLECTION.TEST_KEY, changedReportActions), {
89+
await measureAsyncFunction(() => Onyx.mergeCollection(collectionKey, changedReportActions), {
10290
beforeEach: async () => {
10391
await Onyx.multiSet(mockedReportActionsMap);
10492
},
@@ -112,7 +100,7 @@ describe('Onyx', () => {
112100
const changedReportActions = Object.fromEntries(
113101
Object.entries(mockedReportActionsMap).map(([k, v]) => [k, createRandomReportAction(Number(v.reportActionID))] as const),
114102
) as GenericCollection;
115-
await measureAsyncFunction(() => Onyx.setCollection(ONYXKEYS.COLLECTION.TEST_KEY, changedReportActions), {
103+
await measureAsyncFunction(() => Onyx.setCollection(collectionKey, changedReportActions), {
116104
beforeEach: async () => {
117105
await Onyx.multiSet(mockedReportActionsMap);
118106
},

tests/perf-test/OnyxCache.perf-test.ts

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import {measureAsyncFunction, measureFunction} from 'reassure';
22
import type OnyxCache from '../../lib/OnyxCache';
3-
import {createCollection} from '../utils/collections/createCollection';
4-
import createRandomReportAction from '../utils/collections/reportActions';
3+
import createRandomReportAction, {getRandomReportActions} from '../utils/collections/reportActions';
54
import type GenericCollection from '../utils/GenericCollection';
65
import {TASK} from '../../lib/OnyxCache';
76

@@ -22,15 +21,7 @@ const ONYXKEYS = {
2221
};
2322

2423
const collectionKey = ONYXKEYS.COLLECTION.TEST_KEY;
25-
26-
const getMockedReportActions = (collection = collectionKey, length = 10000) =>
27-
createCollection<Record<string, unknown>>(
28-
(item) => `${collection}${item.reportActionID}`,
29-
(index) => createRandomReportAction(index),
30-
length,
31-
);
32-
33-
const mockedReportActionsMap = getMockedReportActions();
24+
const mockedReportActionsMap = getRandomReportActions(collectionKey);
3425
const mockedReportActionsKeys = Object.keys(mockedReportActionsMap);
3526

3627
let cache: typeof OnyxCache;
@@ -57,29 +48,23 @@ describe('OnyxCache', () => {
5748
describe('setAllKeys', () => {
5849
test('one call setting 10k keys', async () => {
5950
await measureFunction(() => cache.setAllKeys(mockedReportActionsKeys), {
60-
beforeEach: async () => {
61-
resetCacheBeforeEachMeasure();
62-
},
51+
beforeEach: resetCacheBeforeEachMeasure,
6352
});
6453
});
6554
});
6655

6756
describe('addKey', () => {
6857
test('one call adding one key', async () => {
6958
await measureFunction(() => cache.addKey(mockedReportActionsKeys[0]), {
70-
beforeEach: async () => {
71-
resetCacheBeforeEachMeasure();
72-
},
59+
beforeEach: resetCacheBeforeEachMeasure,
7360
});
7461
});
7562
});
7663

7764
describe('addNullishStorageKey', () => {
7865
test('one call adding one key', async () => {
7966
await measureFunction(() => cache.addNullishStorageKey(mockedReportActionsKeys[0]), {
80-
beforeEach: async () => {
81-
resetCacheBeforeEachMeasure();
82-
},
67+
beforeEach: resetCacheBeforeEachMeasure,
8368
});
8469
});
8570
});
@@ -132,9 +117,7 @@ describe('OnyxCache', () => {
132117
test('one call setting one key', async () => {
133118
const value = mockedReportActionsMap[mockedReportActionsKeys[0]];
134119
await measureFunction(() => cache.set(mockedReportActionsKeys[0], value), {
135-
beforeEach: async () => {
136-
resetCacheBeforeEachMeasure();
137-
},
120+
beforeEach: resetCacheBeforeEachMeasure,
138121
});
139122
});
140123
});
@@ -190,19 +173,15 @@ describe('OnyxCache', () => {
190173
describe('captureTask', () => {
191174
test('one call capturing one task', async () => {
192175
await measureAsyncFunction(() => cache.captureTask(`${TASK.GET}:${mockedReportActionsKeys[0]}`, Promise.resolve()), {
193-
beforeEach: async () => {
194-
resetCacheBeforeEachMeasure();
195-
},
176+
beforeEach: resetCacheBeforeEachMeasure,
196177
});
197178
});
198179
});
199180

200181
describe('addToAccessedKeys', () => {
201182
test('one call adding one key', async () => {
202183
await measureFunction(() => cache.addToAccessedKeys(mockedReportActionsKeys[0]), {
203-
beforeEach: async () => {
204-
resetCacheBeforeEachMeasure();
205-
},
184+
beforeEach: resetCacheBeforeEachMeasure,
206185
});
207186
});
208187
});
@@ -222,9 +201,7 @@ describe('OnyxCache', () => {
222201
describe('setRecentKeysLimit', () => {
223202
test('one call', async () => {
224203
await measureFunction(() => cache.setRecentKeysLimit(10000), {
225-
beforeEach: async () => {
226-
resetCacheBeforeEachMeasure();
227-
},
204+
beforeEach: resetCacheBeforeEachMeasure,
228205
});
229206
});
230207
});

tests/perf-test/OnyxConnectionManager.perf-test.ts

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import Onyx from '../../lib';
33
import type {Connection} from '../../lib/OnyxConnectionManager';
44
import connectionManager from '../../lib/OnyxConnectionManager';
55
import createDeferredTask from '../../lib/createDeferredTask';
6-
import {createCollection} from '../utils/collections/createCollection';
7-
import createRandomReportAction from '../utils/collections/reportActions';
6+
import {getRandomReportActions} from '../utils/collections/reportActions';
87

98
const ONYXKEYS = {
109
TEST_KEY: 'test',
@@ -23,15 +22,7 @@ const ONYXKEYS = {
2322
};
2423

2524
const collectionKey = ONYXKEYS.COLLECTION.TEST_KEY;
26-
27-
const getMockedReportActions = (collection = collectionKey, length = 10000) =>
28-
createCollection<Record<string, unknown>>(
29-
(item) => `${collection}${item.reportActionID}`,
30-
(index) => createRandomReportAction(index),
31-
length,
32-
);
33-
34-
const mockedReportActionsMap = getMockedReportActions();
25+
const mockedReportActionsMap = getRandomReportActions(collectionKey);
3526
const mockedReportActionsKeys = Object.keys(mockedReportActionsMap);
3627

3728
// We need access to some internal properties of `connectionManager` during the tests but they are private,
@@ -62,9 +53,7 @@ describe('OnyxConnectionManager', () => {
6253
describe('generateConnectionID', () => {
6354
test('one call', async () => {
6455
await measureFunction(() => generateConnectionID({key: mockedReportActionsKeys[0]}), {
65-
afterEach: async () => {
66-
resetConectionManagerAfterEachMeasure();
67-
},
56+
afterEach: resetConectionManagerAfterEachMeasure,
6857
});
6958
});
7059
});
@@ -151,9 +140,7 @@ describe('OnyxConnectionManager', () => {
151140
describe('refreshSessionID', () => {
152141
test('one call', async () => {
153142
await measureFunction(() => connectionManager.refreshSessionID(), {
154-
afterEach: async () => {
155-
resetConectionManagerAfterEachMeasure();
156-
},
143+
afterEach: resetConectionManagerAfterEachMeasure,
157144
});
158145
});
159146
});

tests/perf-test/OnyxUtils.perf-test.ts

Lines changed: 22 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import {measureAsyncFunction, measureFunction} from 'reassure';
2-
import {createCollection} from '../utils/collections/createCollection';
3-
import createRandomReportAction from '../utils/collections/reportActions';
2+
import createRandomReportAction, {getRandomReportActions} from '../utils/collections/reportActions';
43
import type {OnyxKey, Selector} from '../../lib';
54
import Onyx from '../../lib';
65
import StorageMock from '../../lib/storage';
76
import OnyxUtils from '../../lib/OnyxUtils';
87
import type GenericCollection from '../utils/GenericCollection';
98
import type {Mapping, OnyxUpdate} from '../../lib/Onyx';
10-
import type {WithOnyxInstance} from '../../lib/withOnyx/types';
119
import createDeferredTask from '../../lib/createDeferredTask';
1210
import type {OnyxInputKeyValueMapping} from '../../lib/types';
11+
import generateEmptyWithOnyxInstance from '../utils/generateEmptyWithOnyxInstance';
1312

1413
const ONYXKEYS = {
1514
TEST_KEY: 'test',
@@ -31,18 +30,6 @@ const safeEvictionKeys = [ONYXKEYS.COLLECTION.EVICTABLE_TEST_KEY];
3130

3231
const initialKeyStates = {};
3332

34-
const collectionKey = ONYXKEYS.COLLECTION.TEST_KEY;
35-
36-
const generateEmptyWithOnyxInstance = () => {
37-
return new (class {
38-
// eslint-disable-next-line @typescript-eslint/no-empty-function
39-
setStateProxy() {}
40-
41-
// eslint-disable-next-line @typescript-eslint/no-empty-function
42-
setWithOnyxState() {}
43-
})() as unknown as WithOnyxInstance;
44-
};
45-
4633
// @ts-expect-error bypass
4734
const generateTestSelector = (): Selector<string, unknown, unknown> => (value: Record<string, unknown>) => {
4835
return {
@@ -51,14 +38,8 @@ const generateTestSelector = (): Selector<string, unknown, unknown> => (value: R
5138
};
5239
};
5340

54-
const getMockedReportActions = (collection = collectionKey, length = 10000) =>
55-
createCollection<Record<string, unknown>>(
56-
(item) => `${collection}${item.reportActionID}`,
57-
(index) => createRandomReportAction(index),
58-
length,
59-
);
60-
61-
const mockedReportActionsMap = getMockedReportActions();
41+
const collectionKey = ONYXKEYS.COLLECTION.TEST_KEY;
42+
const mockedReportActionsMap = getRandomReportActions(collectionKey);
6243
const mockedReportActionsKeys = Object.keys(mockedReportActionsMap);
6344

6445
const clearOnyxAfterEachMeasure = async () => {
@@ -107,11 +88,11 @@ describe('OnyxUtils', () => {
10788
describe('initStoreValues', () => {
10889
test('one call with 50k heavy objects', async () => {
10990
const data = {
110-
...getMockedReportActions(ONYXKEYS.COLLECTION.TEST_KEY),
111-
...getMockedReportActions(ONYXKEYS.COLLECTION.TEST_KEY_2),
112-
...getMockedReportActions(ONYXKEYS.COLLECTION.TEST_KEY_3),
113-
...getMockedReportActions(ONYXKEYS.COLLECTION.TEST_KEY_4),
114-
...getMockedReportActions(ONYXKEYS.COLLECTION.TEST_KEY_5),
91+
...getRandomReportActions(collectionKey),
92+
...getRandomReportActions(ONYXKEYS.COLLECTION.TEST_KEY_2),
93+
...getRandomReportActions(ONYXKEYS.COLLECTION.TEST_KEY_3),
94+
...getRandomReportActions(ONYXKEYS.COLLECTION.TEST_KEY_4),
95+
...getRandomReportActions(ONYXKEYS.COLLECTION.TEST_KEY_5),
11596
};
11697

11798
await measureFunction(() => OnyxUtils.initStoreValues(ONYXKEYS, data, safeEvictionKeys), {
@@ -165,12 +146,12 @@ describe('OnyxUtils', () => {
165146

166147
describe('isCollectionKey', () => {
167148
test('one call', async () => {
168-
await measureFunction(() => OnyxUtils.isCollectionKey(ONYXKEYS.COLLECTION.TEST_KEY));
149+
await measureFunction(() => OnyxUtils.isCollectionKey(collectionKey));
169150
});
170151
});
171152

172153
describe('isCollectionMemberKey', () => {
173-
const collectionKeyLength = ONYXKEYS.COLLECTION.TEST_KEY.length;
154+
const collectionKeyLength = collectionKey.length;
174155

175156
test('one call with correct key', async () => {
176157
await measureFunction(() => OnyxUtils.isCollectionMemberKey(collectionKey, `${collectionKey}entry1`, collectionKeyLength));
@@ -211,8 +192,8 @@ describe('OnyxUtils', () => {
211192
const key = `${collectionKey}0`;
212193
const reportAction = mockedReportActionsMap[`${collectionKey}0`];
213194
const collections = {
214-
...getMockedReportActions(ONYXKEYS.COLLECTION.TEST_KEY_2),
215-
...getMockedReportActions(collectionKey),
195+
...getRandomReportActions(ONYXKEYS.COLLECTION.TEST_KEY_2),
196+
...getRandomReportActions(collectionKey),
216197
};
217198

218199
test('one call passing normal key without selector', async () => {
@@ -266,7 +247,7 @@ describe('OnyxUtils', () => {
266247

267248
describe('removeLastAccessedKey', () => {
268249
test('one call removing one key', async () => {
269-
await measureFunction(() => OnyxUtils.removeLastAccessedKey(`${ONYXKEYS.COLLECTION.TEST_KEY}5000`), {
250+
await measureFunction(() => OnyxUtils.removeLastAccessedKey(`${collectionKey}5000`), {
270251
beforeEach: async () => {
271252
mockedReportActionsKeys.forEach((key) => OnyxUtils.addLastAccessedKey(key));
272253
},
@@ -279,7 +260,7 @@ describe('OnyxUtils', () => {
279260

280261
describe('addLastAccessedKey', () => {
281262
test('one call adding one key', async () => {
282-
await measureFunction(() => OnyxUtils.addLastAccessedKey(`${ONYXKEYS.COLLECTION.TEST_KEY}5000`), {
263+
await measureFunction(() => OnyxUtils.addLastAccessedKey(`${collectionKey}5000`), {
283264
beforeEach: async () => {
284265
mockedReportActionsKeys.forEach((key) => OnyxUtils.addLastAccessedKey(key));
285266
},
@@ -292,8 +273,8 @@ describe('OnyxUtils', () => {
292273

293274
describe('addAllSafeEvictionKeysToRecentlyAccessedList', () => {
294275
const data = {
295-
...getMockedReportActions(ONYXKEYS.COLLECTION.TEST_KEY, 1000),
296-
...getMockedReportActions(ONYXKEYS.COLLECTION.EVICTABLE_TEST_KEY, 1000),
276+
...getRandomReportActions(collectionKey, 1000),
277+
...getRandomReportActions(ONYXKEYS.COLLECTION.EVICTABLE_TEST_KEY, 1000),
297278
};
298279

299280
test('one call adding 1k keys', async () => {
@@ -309,11 +290,11 @@ describe('OnyxUtils', () => {
309290
describe('getCachedCollection', () => {
310291
test('one call retrieving a collection with 5k heavy objects', async () => {
311292
const data = {
312-
...getMockedReportActions(ONYXKEYS.COLLECTION.TEST_KEY, 5000),
313-
...getMockedReportActions(ONYXKEYS.COLLECTION.TEST_KEY_2, 5000),
293+
...getRandomReportActions(collectionKey, 5000),
294+
...getRandomReportActions(ONYXKEYS.COLLECTION.TEST_KEY_2, 5000),
314295
};
315296

316-
await measureFunction(() => OnyxUtils.getCachedCollection(ONYXKEYS.COLLECTION.TEST_KEY), {
297+
await measureFunction(() => OnyxUtils.getCachedCollection(collectionKey), {
317298
beforeEach: async () => {
318299
await Onyx.multiSet(data);
319300
},
@@ -807,7 +788,7 @@ describe('OnyxUtils', () => {
807788
const skippableCollectionMemberIDs = OnyxUtils.getSkippableCollectionMemberIDs();
808789
await measureFunction(() => OnyxUtils.getSkippableCollectionMemberIDs(), {
809790
beforeEach: async () => {
810-
OnyxUtils.setSkippableCollectionMemberIDs(new Set([ONYXKEYS.COLLECTION.TEST_KEY, ONYXKEYS.COLLECTION.TEST_KEY_2]));
791+
OnyxUtils.setSkippableCollectionMemberIDs(new Set([collectionKey, ONYXKEYS.COLLECTION.TEST_KEY_2]));
811792
},
812793
afterEach: async () => {
813794
OnyxUtils.setSkippableCollectionMemberIDs(skippableCollectionMemberIDs);
@@ -819,7 +800,7 @@ describe('OnyxUtils', () => {
819800
describe('setSkippableCollectionMemberIDs', () => {
820801
test('one call', async () => {
821802
const skippableCollectionMemberIDs = OnyxUtils.getSkippableCollectionMemberIDs();
822-
await measureFunction(() => OnyxUtils.setSkippableCollectionMemberIDs(new Set([ONYXKEYS.COLLECTION.TEST_KEY, ONYXKEYS.COLLECTION.TEST_KEY_2])), {
803+
await measureFunction(() => OnyxUtils.setSkippableCollectionMemberIDs(new Set([collectionKey, ONYXKEYS.COLLECTION.TEST_KEY_2])), {
823804
afterEach: async () => {
824805
OnyxUtils.setSkippableCollectionMemberIDs(skippableCollectionMemberIDs);
825806
},

0 commit comments

Comments
 (0)