|
| 1 | +/* eslint-disable no-underscore-dangle */ |
| 2 | +/* eslint-disable import/first */ |
| 3 | +/** |
| 4 | + * @jest-environment jsdom |
| 5 | + */ |
| 6 | + |
| 7 | +// Mock webpack require system for RSC |
| 8 | +window.__webpack_require__ = jest.fn(); |
| 9 | +window.__webpack_chunk_load__ = jest.fn(); |
| 10 | + |
| 11 | +import * as React from 'react'; |
| 12 | +import { enableFetchMocks } from 'jest-fetch-mock'; |
| 13 | +import { render, waitFor, screen } from '@testing-library/react'; |
| 14 | +import '@testing-library/jest-dom'; |
| 15 | +import path from 'path'; |
| 16 | +import fs from 'fs'; |
| 17 | +import { createNodeReadableStream } from './testUtils'; |
| 18 | + |
| 19 | +// const __filename = fileURLToPath(import.meta.url); |
| 20 | +// const __dirname = path.dirname(__filename); |
| 21 | + |
| 22 | +import RSCClientRoot, {resetRenderCache } from '../src/RSCClientRoot'; |
| 23 | + |
| 24 | +enableFetchMocks(); |
| 25 | + |
| 26 | +describe('RSCClientRoot', () => { |
| 27 | + beforeEach(() => { |
| 28 | + jest.clearAllMocks(); |
| 29 | + |
| 30 | + jest.resetModules(); |
| 31 | + resetRenderCache(); |
| 32 | + }); |
| 33 | + |
| 34 | + it('throws error when React.use is not defined', () => { |
| 35 | + jest.mock('react', () => ({ |
| 36 | + ...jest.requireActual('react'), |
| 37 | + use: undefined |
| 38 | + })); |
| 39 | + |
| 40 | + expect(() => { |
| 41 | + // Re-import to trigger the check |
| 42 | + jest.requireActual('../src/RSCClientRoot'); |
| 43 | + }).toThrow('React.use is not defined'); |
| 44 | + }); |
| 45 | + |
| 46 | + const mockRSCRequest = (rscRenderingUrlPath = 'rsc-render') => { |
| 47 | + const chunksDirectory = path.join(__dirname, 'fixtures', 'rsc-payloads', 'simple-shell-with-async-component'); |
| 48 | + const chunk1 = JSON.parse(fs.readFileSync(path.join(chunksDirectory, 'chunk1.json'), 'utf8')); |
| 49 | + const chunk2 = JSON.parse(fs.readFileSync(path.join(chunksDirectory, 'chunk2.json'), 'utf8')); |
| 50 | + |
| 51 | + const { stream, push } = createNodeReadableStream(); |
| 52 | + window.fetchMock.mockResolvedValue(new Response(stream)); |
| 53 | + |
| 54 | + const props = { |
| 55 | + componentName: 'TestComponent', |
| 56 | + rscRenderingUrlPath |
| 57 | + }; |
| 58 | + |
| 59 | + const { rerender } = render(<RSCClientRoot {...props} />); |
| 60 | + |
| 61 | + return { |
| 62 | + rerender: () => rerender(<RSCClientRoot {...props} />), |
| 63 | + pushFirstChunk: () => push(JSON.stringify(chunk1)), |
| 64 | + pushSecondChunk: () => push(JSON.stringify(chunk2)), |
| 65 | + pushCustomChunk: (chunk) => push(chunk), |
| 66 | + endStream: () => push(null), |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + it('fetches and caches component data', async () => { |
| 71 | + const { rerender, pushFirstChunk, pushSecondChunk, endStream } = mockRSCRequest(); |
| 72 | + |
| 73 | + expect(window.fetch).toHaveBeenCalledWith('/rsc-render/TestComponent'); |
| 74 | + expect(window.fetch).toHaveBeenCalledTimes(1); |
| 75 | + expect(screen.queryByText('StaticServerComponent')).not.toBeInTheDocument(); |
| 76 | + |
| 77 | + pushFirstChunk(); |
| 78 | + await waitFor(() => expect(screen.getByText('StaticServerComponent')).toBeInTheDocument()); |
| 79 | + expect(screen.getByText('Loading AsyncComponent...')).toBeInTheDocument(); |
| 80 | + expect(screen.queryByText('AsyncComponent')).not.toBeInTheDocument(); |
| 81 | + |
| 82 | + pushSecondChunk(); |
| 83 | + endStream(); |
| 84 | + await waitFor(() => expect(screen.getByText('AsyncComponent')).toBeInTheDocument()); |
| 85 | + expect(screen.queryByText('Loading AsyncComponent...')).not.toBeInTheDocument(); |
| 86 | + |
| 87 | + // Second render - should use cache |
| 88 | + rerender(); |
| 89 | + |
| 90 | + expect(screen.getByText('AsyncComponent')).toBeInTheDocument(); |
| 91 | + expect(window.fetch).toHaveBeenCalledTimes(1); |
| 92 | + }); |
| 93 | + |
| 94 | + it('replays console logs', async () => { |
| 95 | + const consoleSpy = jest.spyOn(console, 'log'); |
| 96 | + const { rerender, pushFirstChunk, pushSecondChunk, endStream } = mockRSCRequest(); |
| 97 | + |
| 98 | + pushFirstChunk(); |
| 99 | + await waitFor(() => expect(consoleSpy).toHaveBeenCalledWith('[SERVER] Console log at first chunk')); |
| 100 | + expect(consoleSpy).toHaveBeenCalledTimes(1); |
| 101 | + |
| 102 | + pushSecondChunk(); |
| 103 | + await waitFor(() => expect(consoleSpy).toHaveBeenCalledWith('[SERVER] Console log at second chunk')); |
| 104 | + endStream(); |
| 105 | + expect(consoleSpy).toHaveBeenCalledTimes(2); |
| 106 | + |
| 107 | + // On rerender, console logs should not be replayed again |
| 108 | + rerender(); |
| 109 | + expect(consoleSpy).toHaveBeenCalledTimes(2); |
| 110 | + }); |
| 111 | + |
| 112 | + it('strips leading and trailing slashes from rscRenderingUrlPath', async () => { |
| 113 | + const { pushFirstChunk, pushSecondChunk, endStream } = mockRSCRequest('/rsc-render/'); |
| 114 | + |
| 115 | + pushFirstChunk(); |
| 116 | + pushSecondChunk(); |
| 117 | + endStream(); |
| 118 | + |
| 119 | + await waitFor(() => expect(window.fetch).toHaveBeenCalledWith('/rsc-render/TestComponent')); |
| 120 | + expect(window.fetch).toHaveBeenCalledTimes(1); |
| 121 | + |
| 122 | + await waitFor(() => expect(screen.getByText('StaticServerComponent')).toBeInTheDocument()); |
| 123 | + }); |
| 124 | +}); |
0 commit comments