|
| 1 | +/*eslint-disable react/prop-types*/ |
| 2 | + |
| 3 | +import * as rtl from '@testing-library/react' |
| 4 | +import { Provider } from 'react-redux' |
| 5 | +import { createStore } from 'redux' |
| 6 | + |
| 7 | +// In a React Server Components environment, React does not expose the effect |
| 8 | +// hooks that `<Provider>` relies on, so `useIsomorphicLayoutEffect` (which |
| 9 | +// resolves to `useLayoutEffect`/`useEffect`) ends up `undefined`. Simulate that |
| 10 | +// here to assert `<Provider>` surfaces a clear, actionable error rather than a |
| 11 | +// cryptic "X is not a function". |
| 12 | +vi.mock('../../src/utils/useIsomorphicLayoutEffect', () => ({ |
| 13 | + useIsomorphicLayoutEffect: undefined, |
| 14 | +})) |
| 15 | + |
| 16 | +// This mock replaces an internal source module, which only exists as a separate |
| 17 | +// module in the `src` build. When the suite runs against the bundled build |
| 18 | +// artifact (`TEST_DIST`), `useIsomorphicLayoutEffect` is inlined into a single |
| 19 | +// file with no module boundary left to mock, so the RSC condition cannot be |
| 20 | +// simulated there. The guard itself is unchanged between builds, so exercising |
| 21 | +// it against `src` is sufficient. |
| 22 | +const describeSource = process.env.TEST_DIST ? describe.skip : describe |
| 23 | + |
| 24 | +describeSource('React', () => { |
| 25 | + describe('Provider in a React Server Component environment', () => { |
| 26 | + it('throws an actionable error when React effect hooks are unavailable', () => { |
| 27 | + const store = createStore(() => ({})) |
| 28 | + |
| 29 | + const spy = vi.spyOn(console, 'error').mockImplementation(() => {}) |
| 30 | + |
| 31 | + expect(() => |
| 32 | + rtl.render( |
| 33 | + <Provider store={store}> |
| 34 | + <div /> |
| 35 | + </Provider>, |
| 36 | + ), |
| 37 | + ).toThrow(/React Server Component/) |
| 38 | + |
| 39 | + spy.mockRestore() |
| 40 | + }) |
| 41 | + }) |
| 42 | +}) |
0 commit comments