Skip to content

Commit dfaa39b

Browse files
committed
Cover setWithRetry and mergeCollection for empty array coercion behavior
1 parent 08939c9 commit dfaa39b

1 file changed

Lines changed: 46 additions & 18 deletions

File tree

tests/unit/onyxTest.ts

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ describe('Onyx', () => {
182182
});
183183
});
184184

185-
it('should merge an object into an empty array (treating [] as {})', () => {
185+
it('should merge an object into an empty array (treating [] as {})', async () => {
186186
let testKeyValue: unknown;
187187

188188
connection = Onyx.connect({
@@ -193,17 +193,13 @@ describe('Onyx', () => {
193193
},
194194
});
195195

196-
return Onyx.set(ONYX_KEYS.TEST_KEY, [])
197-
.then(() => {
198-
expect(testKeyValue).toStrictEqual([]);
199-
return Onyx.merge(ONYX_KEYS.TEST_KEY, {test: 'value'});
200-
})
201-
.then(() => {
202-
expect(testKeyValue).toStrictEqual({test: 'value'});
203-
});
196+
await Onyx.set(ONYX_KEYS.TEST_KEY, []);
197+
expect(testKeyValue).toStrictEqual([]);
198+
await Onyx.merge(ONYX_KEYS.TEST_KEY, {test: 'value'});
199+
expect(testKeyValue).toStrictEqual({test: 'value'});
204200
});
205201

206-
it('should still reject merging an object into a non-empty array', () => {
202+
it('should still reject merging an object into a non-empty array', async () => {
207203
let testKeyValue: unknown;
208204

209205
connection = Onyx.connect({
@@ -214,14 +210,46 @@ describe('Onyx', () => {
214210
},
215211
});
216212

217-
return Onyx.merge(ONYX_KEYS.TEST_KEY, ['existing'])
218-
.then(() => {
219-
expect(testKeyValue).toStrictEqual(['existing']);
220-
return Onyx.merge(ONYX_KEYS.TEST_KEY, {test: 'value'});
221-
})
222-
.then(() => {
223-
expect(testKeyValue).toStrictEqual(['existing']);
224-
});
213+
await Onyx.merge(ONYX_KEYS.TEST_KEY, ['existing']);
214+
expect(testKeyValue).toStrictEqual(['existing']);
215+
await Onyx.merge(ONYX_KEYS.TEST_KEY, {test: 'value'});
216+
expect(testKeyValue).toStrictEqual(['existing']);
217+
});
218+
219+
it('should mergeCollection an object into an empty array (treating [] as {})', async () => {
220+
const collectionKey = `${ONYX_KEYS.COLLECTION.TEST_KEY}1`;
221+
let testKeyValue: unknown;
222+
223+
connection = Onyx.connect({
224+
key: ONYX_KEYS.COLLECTION.TEST_KEY,
225+
initWithStoredValues: false,
226+
waitForCollectionCallback: true,
227+
callback: (value) => {
228+
testKeyValue = value;
229+
},
230+
});
231+
232+
await Onyx.set(collectionKey, []);
233+
expect((testKeyValue as Record<string, unknown>)?.[collectionKey]).toStrictEqual([]);
234+
await Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_KEY, {[collectionKey]: {test: 'value'}});
235+
expect((testKeyValue as Record<string, unknown>)?.[collectionKey]).toStrictEqual({test: 'value'});
236+
});
237+
238+
it('should set an object over an empty array (treating [] as {})', async () => {
239+
let testKeyValue: unknown;
240+
241+
connection = Onyx.connect({
242+
key: ONYX_KEYS.TEST_KEY,
243+
initWithStoredValues: false,
244+
callback: (value) => {
245+
testKeyValue = value;
246+
},
247+
});
248+
249+
await Onyx.set(ONYX_KEYS.TEST_KEY, []);
250+
expect(testKeyValue).toStrictEqual([]);
251+
await Onyx.set(ONYX_KEYS.TEST_KEY, {test: 'value'});
252+
expect(testKeyValue).toStrictEqual({test: 'value'});
225253
});
226254

227255
it('should notify subscribers when data has been cleared', () => {

0 commit comments

Comments
 (0)