Skip to content

Commit 9e0acc1

Browse files
tyao1facebook-github-bot
authored andcommitted
Improve test coverage for recycleNodesInto on deeply frozen data
Reviewed By: alunyov Differential Revision: D50918113 fbshipit-source-id: 3f51f5b42dbeb9b47a96025562cee87e1bec141e
1 parent ccd4255 commit 9e0acc1

File tree

1 file changed

+69
-2
lines changed

1 file changed

+69
-2
lines changed

packages/relay-runtime/util/__tests__/recycleNodesInto-test.js

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
'use strict';
1212

13+
const deepFreeze = require('../deepFreeze');
1314
const recycleNodesInto = require('../recycleNodesInto');
1415

1516
describe('recycleNodesInto', () => {
@@ -177,10 +178,9 @@ describe('recycleNodesInto', () => {
177178
expect(recycleNodesInto(prevData, nextData)).toBe(prevData);
178179
});
179180

180-
it('does not recycle into frozen `nextData`', () => {
181+
it('does not recycle different `nextData`', () => {
181182
const prevData = [{x: 1}, 2, 3];
182183
const nextData = [{x: 1}, 2, 4];
183-
Object.freeze(nextData);
184184
expect(recycleNodesInto(prevData, nextData)).toBe(nextData);
185185
});
186186

@@ -347,4 +347,71 @@ describe('recycleNodesInto', () => {
347347
expect(recycleNodesInto(a, b)).toBe(b);
348348
});
349349
});
350+
351+
describe('deepFreeze', () => {
352+
it('does not mutate deeply frozen array in `nextData`', () => {
353+
const prevData = [[{x: 1}], 1];
354+
const nextData = [[{x: 1}], 2];
355+
deepFreeze(nextData);
356+
const recycled = recycleNodesInto(prevData, nextData);
357+
expect(recycled).toBe(nextData);
358+
expect(recycled[0]).toBe(nextData[0]);
359+
expect(recycled[0][0]).toBe(nextData[0][0]);
360+
});
361+
362+
it('does not mutate deeply frozen object in `nextData`', () => {
363+
const nextItem = {
364+
c: 1,
365+
};
366+
const nextObject = {
367+
b: nextItem,
368+
};
369+
const nextData = {
370+
a: nextObject,
371+
};
372+
const prevData = {a: {b: {c: 1}}, d: 1};
373+
374+
deepFreeze(nextData);
375+
const recycled = recycleNodesInto(prevData, nextData);
376+
expect(recycled).toBe(nextData);
377+
expect(recycled.a).toBe(nextObject);
378+
expect(recycled.a.b).toBe(nextItem);
379+
});
380+
381+
it('reuse prevData and does not mutate deeply frozen array in `nextData`', () => {
382+
const nextItem = {x: 1};
383+
const nextArray = [nextItem];
384+
const prevData = [[{x: 1}], 1];
385+
const nextData = [nextArray, 1];
386+
deepFreeze(nextData);
387+
const recycled = recycleNodesInto(prevData, nextData);
388+
expect(recycled).toBe(prevData);
389+
expect(nextData[0]).toBe(nextArray);
390+
expect(nextData[0][0]).toBe(nextItem);
391+
});
392+
393+
it('reuse prevData and does not mutate deeply frozen object in `nextData`', () => {
394+
const nextItem = {
395+
c: 1,
396+
};
397+
const nextObject = {
398+
b: nextItem,
399+
};
400+
const nextData = {
401+
a: nextObject,
402+
};
403+
const prevData = {
404+
a: {
405+
b: {
406+
c: 1,
407+
},
408+
},
409+
};
410+
deepFreeze(nextData);
411+
const recycled = recycleNodesInto(prevData, nextData);
412+
expect(recycled).toBe(prevData);
413+
expect(nextData.a).toBe(nextObject);
414+
expect(nextData.a.b).toBe(nextItem);
415+
});
416+
});
350417
});

0 commit comments

Comments
 (0)