Skip to content

Commit 2979ece

Browse files
committed
Refactor GenericCollection type to OnyxMergeCollectionInput
1 parent cb08498 commit 2979ece

File tree

6 files changed

+33
-31
lines changed

6 files changed

+33
-31
lines changed

tests/unit/DevToolsTest.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ const initialKeyStates = {
2525
[ONYX_KEYS.OBJECT_KEY]: {id: 42},
2626
};
2727

28-
const exampleCollection: GenericCollection = {
28+
const exampleCollection = {
2929
[`${ONYX_KEYS.COLLECTION.NUM_KEY}1`]: 1,
3030
[`${ONYX_KEYS.COLLECTION.NUM_KEY}2`]: 2,
31-
};
31+
} as GenericCollection;
3232

3333
const exampleObject = {name: 'Pedro'};
3434

tests/unit/OnyxConnectionManagerTest.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,10 @@ describe('OnyxConnectionManager', () => {
117117
it('should connect two times to the same key but with different options, and fire the callbacks differently', async () => {
118118
const obj1 = {id: 'entry1_id', name: 'entry1_name'};
119119
const obj2 = {id: 'entry2_id', name: 'entry2_name'};
120-
const collection: GenericCollection = {
120+
const collection = {
121121
[`${ONYXKEYS.COLLECTION.TEST_KEY}entry1`]: obj1,
122122
[`${ONYXKEYS.COLLECTION.TEST_KEY}entry2`]: obj2,
123-
};
123+
} as GenericCollection;
124124
await StorageMock.multiSet([
125125
[`${ONYXKEYS.COLLECTION.TEST_KEY}entry1`, obj1],
126126
[`${ONYXKEYS.COLLECTION.TEST_KEY}entry2`, obj2],

tests/unit/onyxMultiMergeWebStorageTest.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import waitForPromisesToResolve from '../utils/waitForPromisesToResolve';
33
import Storage from '../../lib/storage';
44
import type MockedStorage from '../../lib/storage/__mocks__';
55
import type OnyxInstance from '../../lib/Onyx';
6-
import type GenericCollection from '../utils/GenericCollection';
6+
import type {OnyxKey, OnyxMergeCollectionInput} from '../../lib';
77

88
const StorageMock = Storage as unknown as typeof MockedStorage;
99

@@ -55,15 +55,15 @@ describe('Onyx.mergeCollection() and WebStorage', () => {
5555
test_1: additionalDataOne,
5656
test_2: additionalDataOne,
5757
test_3: additionalDataOne,
58-
} as GenericCollection);
58+
} as unknown as OnyxMergeCollectionInput<OnyxKey>);
5959

6060
// And call again consecutively with different data
6161
const additionalDataTwo = {d: 'd', e: [2]};
6262
Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_KEY, {
6363
test_1: additionalDataTwo,
6464
test_2: additionalDataTwo,
6565
test_3: additionalDataTwo,
66-
} as GenericCollection);
66+
} as unknown as OnyxMergeCollectionInput<OnyxKey>);
6767

6868
return waitForPromisesToResolve().then(() => {
6969
const finalObject = {
@@ -103,7 +103,7 @@ describe('Onyx.mergeCollection() and WebStorage', () => {
103103
test_1: data,
104104
test_2: data,
105105
test_3: data,
106-
} as GenericCollection);
106+
} as unknown as OnyxMergeCollectionInput<OnyxKey>);
107107

108108
return waitForPromisesToResolve()
109109
.then(() => {
@@ -125,7 +125,7 @@ describe('Onyx.mergeCollection() and WebStorage', () => {
125125
test_1: additionalData,
126126
test_2: additionalData,
127127
test_3: additionalData,
128-
} as GenericCollection);
128+
} as unknown as OnyxMergeCollectionInput<OnyxKey>);
129129

130130
return waitForPromisesToResolve();
131131
})
@@ -164,7 +164,7 @@ describe('Onyx.mergeCollection() and WebStorage', () => {
164164
// 2nd call
165165
Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_KEY, {
166166
test_1: {d: 'd', e: 'e'},
167-
} as GenericCollection);
167+
} as unknown as OnyxMergeCollectionInput<OnyxKey>);
168168

169169
// Last call
170170
Onyx.merge('test_1', {f: 'f'});

tests/unit/onyxTest.ts

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import waitForPromisesToResolve from '../utils/waitForPromisesToResolve';
55
import OnyxUtils from '../../lib/OnyxUtils';
66
import type OnyxCache from '../../lib/OnyxCache';
77
import StorageMock from '../../lib/storage';
8-
import type {OnyxCollection, OnyxUpdate} from '../../lib/types';
8+
import type {OnyxCollection, OnyxKey, OnyxMergeCollectionInput, OnyxUpdate} from '../../lib/types';
99
import type {GenericDeepRecord} from '../types';
1010
import type GenericCollection from '../utils/GenericCollection';
1111
import type {Connection} from '../../lib/OnyxConnectionManager';
@@ -624,7 +624,7 @@ describe('Onyx', () => {
624624
ID: 345,
625625
value: 'three',
626626
},
627-
} as GenericCollection)
627+
} as unknown as OnyxMergeCollectionInput<OnyxKey>)
628628
.then(() =>
629629
// 2 key values to update and 2 new keys to add.
630630
// MergeCollection will perform a mix of multiSet and multiMerge
@@ -646,7 +646,7 @@ describe('Onyx', () => {
646646
ID: 567,
647647
value: 'one',
648648
},
649-
} as GenericCollection),
649+
} as unknown as OnyxMergeCollectionInput<OnyxKey>),
650650
)
651651
.then(() => {
652652
// 3 items on the first mergeCollection + 4 items the next mergeCollection
@@ -674,7 +674,7 @@ describe('Onyx', () => {
674674
callback: (data, key) => (valuesReceived[key] = data),
675675
});
676676

677-
return Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_KEY, {test_1: {ID: 123}, notMyTest: {beep: 'boop'}} as GenericCollection).then(() => {
677+
return Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_KEY, {test_1: {ID: 123}, notMyTest: {beep: 'boop'}} as unknown as OnyxMergeCollectionInput<OnyxKey>).then(() => {
678678
expect(valuesReceived).toEqual({});
679679
});
680680
});
@@ -705,7 +705,7 @@ describe('Onyx', () => {
705705
ID: 234,
706706
value: 'two',
707707
},
708-
} as GenericCollection),
708+
} as unknown as OnyxMergeCollectionInput<OnyxKey>),
709709
)
710710
.then(() => {
711711
expect(valuesReceived).toEqual({
@@ -899,7 +899,7 @@ describe('Onyx', () => {
899899
ID: 345,
900900
value: 'three',
901901
},
902-
},
902+
} as unknown as OnyxMergeCollectionInput<OnyxKey>,
903903
},
904904
]);
905905
})
@@ -1058,7 +1058,7 @@ describe('Onyx', () => {
10581058
},
10591059
};
10601060

1061-
return Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_CONNECT_COLLECTION, initialCollectionData as GenericCollection)
1061+
return Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_CONNECT_COLLECTION, initialCollectionData as unknown as OnyxMergeCollectionInput<OnyxKey>)
10621062
.then(() => {
10631063
// When we connect to that collection with waitForCollectionCallback = true
10641064
connection = Onyx.connect({
@@ -1091,7 +1091,7 @@ describe('Onyx', () => {
10911091
return (
10921092
waitForPromisesToResolve()
10931093
// When mergeCollection is called with an updated collection
1094-
.then(() => Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_POLICY, collectionUpdate as GenericCollection))
1094+
.then(() => Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_POLICY, collectionUpdate as unknown as OnyxMergeCollectionInput<OnyxKey>))
10951095
.then(() => {
10961096
// Then we expect the callback to have called twice, once for the initial connect call + once for the collection update
10971097
expect(mockCallback).toHaveBeenCalledTimes(2);
@@ -1120,7 +1120,7 @@ describe('Onyx', () => {
11201120
return (
11211121
waitForPromisesToResolve()
11221122
// When mergeCollection is called with an updated collection
1123-
.then(() => Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_POLICY, collectionUpdate as GenericCollection))
1123+
.then(() => Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_POLICY, collectionUpdate as unknown as OnyxMergeCollectionInput<OnyxKey>))
11241124
.then(() => {
11251125
// Then we expect the callback to have called twice, once for the initial connect call + once for the collection update
11261126
expect(mockCallback).toHaveBeenCalledTimes(2);
@@ -1268,7 +1268,7 @@ describe('Onyx', () => {
12681268
Onyx.update([
12691269
{onyxMethod: Onyx.METHOD.SET, key: ONYX_KEYS.TEST_KEY, value: 'taco'},
12701270
{onyxMethod: Onyx.METHOD.MERGE, key: ONYX_KEYS.OTHER_TEST, value: 'pizza'},
1271-
{onyxMethod: Onyx.METHOD.MERGE_COLLECTION, key: ONYX_KEYS.COLLECTION.TEST_UPDATE, value: {[itemKey]: {a: 'a'}}},
1271+
{onyxMethod: Onyx.METHOD.MERGE_COLLECTION, key: ONYX_KEYS.COLLECTION.TEST_UPDATE, value: {[itemKey]: {a: 'a'}} as OnyxMergeCollectionInput<OnyxKey>},
12721272
]).then(() => {
12731273
expect(collectionCallback).toHaveBeenCalledTimes(2);
12741274
expect(collectionCallback).toHaveBeenNthCalledWith(1, undefined, undefined, undefined);
@@ -1525,10 +1525,10 @@ describe('Onyx', () => {
15251525

15261526
const initialValue = {name: 'Fluffy'};
15271527

1528-
const collectionDiff: GenericCollection = {
1528+
const collectionDiff = {
15291529
[cat]: initialValue,
15301530
[dog]: {name: 'Rex'},
1531-
};
1531+
} as OnyxMergeCollectionInput<OnyxKey>;
15321532

15331533
return Onyx.set(cat, initialValue)
15341534
.then(() => {
@@ -1630,7 +1630,7 @@ describe('Onyx', () => {
16301630
0: 'Bed',
16311631
},
16321632
},
1633-
},
1633+
} as OnyxMergeCollectionInput<OnyxKey>,
16341634
},
16351635
{
16361636
onyxMethod: Onyx.METHOD.MERGE,
@@ -1729,7 +1729,7 @@ describe('Onyx', () => {
17291729
value: {
17301730
[cat]: {age: 5, size: 'S'},
17311731
[dog]: {size: 'M'},
1732-
},
1732+
} as OnyxMergeCollectionInput<OnyxKey>,
17331733
},
17341734
{onyxMethod: Onyx.METHOD.SET, key: cat, value: {age: 3}},
17351735
{onyxMethod: Onyx.METHOD.MERGE, key: cat, value: {sound: 'meow'}},
@@ -2214,7 +2214,7 @@ describe('Onyx', () => {
22142214
value: {
22152215
[routeA]: {name: 'New Route A'},
22162216
[routeB]: {name: 'New Route B'},
2217-
},
2217+
} as OnyxMergeCollectionInput<OnyxKey>,
22182218
},
22192219
]);
22202220
})
@@ -2265,7 +2265,7 @@ describe('Onyx', () => {
22652265
key: ONYX_KEYS.COLLECTION.ROUTES,
22662266
value: {
22672267
[routeA]: {name: 'Final Route A'},
2268-
},
2268+
} as OnyxMergeCollectionInput<OnyxKey>,
22692269
},
22702270
{
22712271
onyxMethod: Onyx.METHOD.MERGE,
@@ -2319,7 +2319,7 @@ describe('Onyx', () => {
23192319
value: {
23202320
[key1]: {id: '1', name: 'Updated Item 1'},
23212321
[key2]: {id: '2', name: 'Updated Item 2'},
2322-
},
2322+
} as OnyxMergeCollectionInput<OnyxKey>,
23232323
},
23242324
]);
23252325

@@ -2663,8 +2663,9 @@ describe('Onyx', () => {
26632663
} as GenericCollection);
26642664

26652665
await Onyx.setCollection(ONYX_KEYS.COLLECTION.ROUTES, {
2666+
// @ts-expect-error invalidRoute is not a valid key
26662667
[invalidRoute]: {name: 'Invalid Route'},
2667-
} as GenericCollection);
2668+
});
26682669

26692670
expect(result).toEqual({
26702671
[routeA]: {name: 'Route A'},

tests/unit/onyxUtilsTest.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,9 @@ describe('OnyxUtils', () => {
210210
} as GenericCollection);
211211

212212
await OnyxUtils.partialSetCollection(ONYXKEYS.COLLECTION.ROUTES, {
213+
// @ts-expect-error invalidRoute is not a valid key
213214
[invalidRoute]: {name: 'Invalid Route'},
214-
} as GenericCollection);
215+
});
215216

216217
expect(result).toEqual({
217218
[routeA]: {name: 'Route A'},

tests/utils/GenericCollection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type {Collection} from '../../lib/types';
1+
import type {OnyxKey, OnyxMergeCollectionInput} from '../../lib/types';
22

3-
type GenericCollection = Collection<string, unknown>;
3+
type GenericCollection = OnyxMergeCollectionInput<OnyxKey>;
44

55
export default GenericCollection;

0 commit comments

Comments
 (0)