Skip to content

Commit

Permalink
Improve test coverage for recycleNodesInto on deeply frozen data
Browse files Browse the repository at this point in the history
Reviewed By: alunyov

Differential Revision: D50918113

fbshipit-source-id: 3f51f5b42dbeb9b47a96025562cee87e1bec141e
  • Loading branch information
tyao1 authored and facebook-github-bot committed Nov 6, 2023
1 parent ccd4255 commit 9e0acc1
Showing 1 changed file with 69 additions and 2 deletions.
71 changes: 69 additions & 2 deletions packages/relay-runtime/util/__tests__/recycleNodesInto-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

'use strict';

const deepFreeze = require('../deepFreeze');
const recycleNodesInto = require('../recycleNodesInto');

describe('recycleNodesInto', () => {
Expand Down Expand Up @@ -177,10 +178,9 @@ describe('recycleNodesInto', () => {
expect(recycleNodesInto(prevData, nextData)).toBe(prevData);
});

it('does not recycle into frozen `nextData`', () => {
it('does not recycle different `nextData`', () => {
const prevData = [{x: 1}, 2, 3];
const nextData = [{x: 1}, 2, 4];
Object.freeze(nextData);
expect(recycleNodesInto(prevData, nextData)).toBe(nextData);
});

Expand Down Expand Up @@ -347,4 +347,71 @@ describe('recycleNodesInto', () => {
expect(recycleNodesInto(a, b)).toBe(b);
});
});

describe('deepFreeze', () => {
it('does not mutate deeply frozen array in `nextData`', () => {
const prevData = [[{x: 1}], 1];
const nextData = [[{x: 1}], 2];
deepFreeze(nextData);
const recycled = recycleNodesInto(prevData, nextData);
expect(recycled).toBe(nextData);
expect(recycled[0]).toBe(nextData[0]);
expect(recycled[0][0]).toBe(nextData[0][0]);
});

it('does not mutate deeply frozen object in `nextData`', () => {
const nextItem = {
c: 1,
};
const nextObject = {
b: nextItem,
};
const nextData = {
a: nextObject,
};
const prevData = {a: {b: {c: 1}}, d: 1};

deepFreeze(nextData);
const recycled = recycleNodesInto(prevData, nextData);
expect(recycled).toBe(nextData);
expect(recycled.a).toBe(nextObject);
expect(recycled.a.b).toBe(nextItem);
});

it('reuse prevData and does not mutate deeply frozen array in `nextData`', () => {
const nextItem = {x: 1};
const nextArray = [nextItem];
const prevData = [[{x: 1}], 1];
const nextData = [nextArray, 1];
deepFreeze(nextData);
const recycled = recycleNodesInto(prevData, nextData);
expect(recycled).toBe(prevData);
expect(nextData[0]).toBe(nextArray);
expect(nextData[0][0]).toBe(nextItem);
});

it('reuse prevData and does not mutate deeply frozen object in `nextData`', () => {
const nextItem = {
c: 1,
};
const nextObject = {
b: nextItem,
};
const nextData = {
a: nextObject,
};
const prevData = {
a: {
b: {
c: 1,
},
},
};
deepFreeze(nextData);
const recycled = recycleNodesInto(prevData, nextData);
expect(recycled).toBe(prevData);
expect(nextData.a).toBe(nextObject);
expect(nextData.a.b).toBe(nextItem);
});
});
});

0 comments on commit 9e0acc1

Please sign in to comment.