Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More Unit Tests for Refs in Hidden Subtrees #31404

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 125 additions & 0 deletions packages/react-refresh/src/__tests__/ReactFreshIntegration-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,131 @@ describe('ReactFreshIntegration', () => {
await patch(code);
});

// @gate __DEV__ && enableActivity
it('ignores ref for functional component in hidden subtree', async () => {
const code = `
import {unstable_Activity as Activity} from 'react';

// Avoid creating a new component on Fast Refresh.
global.A = global.A ?? function A() {
return <div />;
}
const A = global.A;

function hiddenRef() {
throw new Error('Unexpected hiddenRef() invocation.');
}

export default function App() {
return (
<Activity mode="hidden">
<A ref={hiddenRef} />
</Activity>
);
};
`;

await render(code);
await patch(code);
});

// @gate __DEV__ && enableActivity
it('ignores ref for ref forwarding component in hidden subtree', async () => {
const code = `
import {
forwardRef,
unstable_Activity as Activity,
} from 'react';

// Avoid creating a new component on Fast Refresh.
global.A = global.A ?? forwardRef(function A(props, ref) {
return <div ref={ref} />;
});
const A = global.A;

function hiddenRef() {
throw new Error('Unexpected hiddenRef() invocation.');
}

export default function App() {
return (
<Activity mode="hidden">
<A ref={hiddenRef} />
</Activity>
);
};
`;

await render(code);
await patch(code);
});

// @gate __DEV__ && enableActivity
it('ignores ref for simple memo component in hidden subtree', async () => {
const code = `
import {
memo,
unstable_Activity as Activity,
} from 'react';

// Avoid creating a new component on Fast Refresh.
global.A = global.A ?? memo(function A() {
return <div />;
});
const A = global.A;

function hiddenRef() {
throw new Error('Unexpected hiddenRef() invocation.');
}

export default function App() {
return (
<Activity mode="hidden">
<A ref={hiddenRef} />
</Activity>
);
};
`;

await render(code);
await patch(code);
});

// @gate __DEV__ && enableActivity
it('ignores ref for memo component in hidden subtree', async () => {
// A custom compare function means this won't use SimpleMemoComponent.
const code = `
import {
memo,
unstable_Activity as Activity,
} from 'react';

// Avoid creating a new component on Fast Refresh.
global.A = global.A ?? memo(
function A() {
return <div />;
},
() => false,
);
const A = global.A;

function hiddenRef() {
throw new Error('Unexpected hiddenRef() invocation.');
}

export default function App() {
return (
<Activity mode="hidden">
<A ref={hiddenRef} />
</Activity>
);
};
`;

await render(code);
await patch(code);
});

it('reloads HOCs if they return functions', async () => {
if (__DEV__) {
await render(`
Expand Down
Loading