Skip to content

Commit

Permalink
Regression test for freezing complex resplver values
Browse files Browse the repository at this point in the history
Reviewed By: alunyov

Differential Revision: D50846632

fbshipit-source-id: 5d022b2d3984f1a8141b856268ba7fc4e2a05f56
  • Loading branch information
tyao1 authored and facebook-github-bot committed Nov 1, 2023
1 parent 61c7bff commit 52b2c13
Showing 1 changed file with 38 additions and 9 deletions.
47 changes: 38 additions & 9 deletions packages/react-relay/__tests__/RelayResolverModel-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,11 @@ describe.each([
});
});

const getMutableEntityQuery = graphql`
query RelayResolverModelTestGetMutableEntityQuery {
mutable_entity
}
`;
test('should not mutate complex resolver values', () => {
resetModels();
// Do not deep freeze
Expand All @@ -508,21 +513,14 @@ describe.each([
TestRenderer.act(() => {
setIsHuman(true);
});

function GetMutableEntity() {
const data = useClientQuery(
graphql`
query RelayResolverModelTestGetMutableEntityQuery {
mutable_entity
}
`,
{},
);
const data = useClientQuery(getMutableEntityQuery, {});
if (data.mutable_entity == null) {
return null;
}
return `${data.mutable_entity.type}:${data.mutable_entity.props.battery}`;
}

const renderer = TestRenderer.create(
<EnvironmentWrapper environment={environment}>
<GetMutableEntity />
Expand All @@ -544,6 +542,37 @@ describe.each([
// TODO: Should be 0. Relay should not mutate the value here.
expect(renderer.toJSON()).toEqual('human:100');

TestRenderer.act(() => {
renderer.unmount();
});
jest.unmock('relay-runtime/util/deepFreeze');
});

test('should not freeze complex resolver values', () => {
resetModels();
TestRenderer.act(() => {
setIsHuman(false);
});
function GetMutableEntity() {
const data = useClientQuery(getMutableEntityQuery, {});
if (data.mutable_entity == null) {
return null;
}
return `${data.mutable_entity.type}:${data.mutable_entity.props.battery}`;
}

const renderer = TestRenderer.create(
<EnvironmentWrapper environment={environment}>
<GetMutableEntity />
</EnvironmentWrapper>,
);
expect(renderer.toJSON()).toEqual('robot:0');

// TODO: should not throw on mutating the inner of the resolver value
expect(() => {
chargeBattery();
}).toThrow();

TestRenderer.act(() => {
renderer.unmount();
});
Expand Down

0 comments on commit 52b2c13

Please sign in to comment.