@ecopages/react 0.2.0-rc.6 → 0.2.0-rc.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,199 +0,0 @@
1
- import { afterEach, describe, expect, it } from 'vitest';
2
- import { createHydrationScript } from './hydration-scripts.js';
3
- const routerAdapter = {
4
- name: 'eco-router',
5
- bundle: {
6
- importPath: '/assets/router.js',
7
- outputName: 'router',
8
- externals: [],
9
- },
10
- components: {
11
- router: 'EcoRouter',
12
- pageContent: 'PageContent',
13
- },
14
- getRouterProps: (page, props) => `{ page: ${page}, pageProps: ${props} }`,
15
- };
16
- function createModuleUrl(source) {
17
- return `data:text/javascript;base64,${btoa(source)}`;
18
- }
19
- async function importModule(moduleUrl, scriptId) {
20
- let marker;
21
- if (scriptId) {
22
- marker = document.createElement('script');
23
- marker.setAttribute('data-eco-script-id', scriptId);
24
- document.head.appendChild(marker);
25
- }
26
- await import(/* @vite-ignore */ moduleUrl);
27
- marker?.remove();
28
- }
29
- function createRuntimeModules() {
30
- const reactImportPath = createModuleUrl('export const createElement = (...args) => ({ args });');
31
- const reactDomClientImportPath = createModuleUrl(`
32
- export const hydrateRoot = (container, tree, options) => {
33
- const runtime = window.__ECO_REACT_HYDRATION_TEST__;
34
- runtime.hydrateCalls.push({
35
- containerTag: container.tagName,
36
- hasRecoverableErrorHandler: typeof options?.onRecoverableError === "function",
37
- tree,
38
- });
39
-
40
- return {
41
- render() {},
42
- unmount() {
43
- runtime.unmountCount += 1;
44
- },
45
- };
46
- };
47
- `);
48
- const importPath = createModuleUrl('export default function Page() { return null; }');
49
- const routerImportPath = createModuleUrl(`
50
- export function EcoRouter(props) {
51
- return props;
52
- }
53
-
54
- export function PageContent() {
55
- return null;
56
- }
57
- `);
58
- return {
59
- importPath,
60
- reactImportPath,
61
- reactDomClientImportPath,
62
- routerImportPath,
63
- layoutComposeImportPath: createModuleUrl('export function composeLayoutPageTree() { return null; }'),
64
- pageLayoutNormalizationImportPath: createModuleUrl('export function ensurePageConfigLayouts(config) { return config; }'),
65
- };
66
- }
67
- describe('createHydrationScript browser execution', () => {
68
- afterEach(() => {
69
- document.body.innerHTML = '';
70
- const testWindow = window;
71
- delete testWindow.__ECO_PAGES__;
72
- delete testWindow.__ECO_REACT_HYDRATION_TEST__;
73
- });
74
- it('registers router ownership and cleanup when the browser hydration bootstrap runs', async () => {
75
- const runtimeModules = createRuntimeModules();
76
- const testWindow = window;
77
- testWindow.__ECO_REACT_HYDRATION_TEST__ = {
78
- hydrateCalls: [],
79
- renderCalls: [],
80
- claimedOwners: [],
81
- releasedOwners: [],
82
- registrations: [],
83
- unmountCount: 0,
84
- };
85
- testWindow.__ECO_PAGES__ = {
86
- navigation: {
87
- getOwnerState: () => ({
88
- owner: 'html',
89
- canHandleSpaNavigation: false,
90
- }),
91
- register: (registration) => {
92
- testWindow.__ECO_REACT_HYDRATION_TEST__?.registrations.push(registration);
93
- },
94
- claimOwnership: (owner) => {
95
- testWindow.__ECO_REACT_HYDRATION_TEST__?.claimedOwners.push(owner);
96
- },
97
- releaseOwnership: (owner) => {
98
- testWindow.__ECO_REACT_HYDRATION_TEST__?.releasedOwners.push(owner);
99
- },
100
- },
101
- };
102
- document.body.innerHTML = `<script id="__ECO_PAGE_DATA__" type="application/json">${JSON.stringify({
103
- title: 'Hello React',
104
- locals: { theme: 'dark' },
105
- })}</script>`;
106
- const script = createHydrationScript({
107
- ...runtimeModules,
108
- scriptId: 'ecopages-react-page',
109
- hmrEnabled: true,
110
- isMdx: false,
111
- router: routerAdapter,
112
- pageDataReaderImportPath: new URL('./page-data-reader.js', import.meta.url).href,
113
- });
114
- const moduleUrl = createModuleUrl(script);
115
- await importModule(moduleUrl, 'ecopages-react-page');
116
- expect(testWindow.__ECO_REACT_HYDRATION_TEST__?.hydrateCalls).toHaveLength(1);
117
- expect(testWindow.__ECO_REACT_HYDRATION_TEST__?.hydrateCalls[0]?.containerTag).toBe('BODY');
118
- expect(testWindow.__ECO_REACT_HYDRATION_TEST__?.hydrateCalls[0]?.hasRecoverableErrorHandler).toBe(true);
119
- expect(testWindow.__ECO_REACT_HYDRATION_TEST__?.claimedOwners).toEqual(['react-router']);
120
- expect(testWindow.__ECO_REACT_HYDRATION_TEST__?.registrations).toHaveLength(1);
121
- expect(typeof testWindow.__ECO_PAGES__?.react?.cleanupPageRoot).toBe('function');
122
- expect(testWindow.__ECO_PAGES__?.page).toEqual({
123
- module: moduleUrl,
124
- props: {
125
- title: 'Hello React',
126
- locals: { theme: 'dark' },
127
- },
128
- });
129
- await testWindow.__ECO_PAGES__?.react?.cleanupPageRoot?.();
130
- expect(testWindow.__ECO_REACT_HYDRATION_TEST__?.unmountCount).toBe(1);
131
- expect(testWindow.__ECO_REACT_HYDRATION_TEST__?.releasedOwners).toEqual(['react-router']);
132
- expect(testWindow.__ECO_PAGES__?.page).toBeUndefined();
133
- expect(testWindow.__ECO_PAGES__?.react?.pageRoot).toBeNull();
134
- });
135
- it('reuses an existing router-owned page root during rerun bootstrap execution', async () => {
136
- const runtimeModules = createRuntimeModules();
137
- const testWindow = window;
138
- testWindow.__ECO_REACT_HYDRATION_TEST__ = {
139
- hydrateCalls: [],
140
- renderCalls: [],
141
- claimedOwners: [],
142
- releasedOwners: [],
143
- registrations: [],
144
- unmountCount: 0,
145
- };
146
- const existingRoot = {
147
- render: (tree) => {
148
- testWindow.__ECO_REACT_HYDRATION_TEST__?.renderCalls.push(tree);
149
- },
150
- unmount: () => {
151
- testWindow.__ECO_REACT_HYDRATION_TEST__.unmountCount += 1;
152
- },
153
- };
154
- testWindow.__ECO_PAGES__ = {
155
- navigation: {
156
- getOwnerState: () => ({
157
- owner: 'react-router',
158
- canHandleSpaNavigation: true,
159
- }),
160
- register: (registration) => {
161
- testWindow.__ECO_REACT_HYDRATION_TEST__?.registrations.push(registration);
162
- },
163
- claimOwnership: (owner) => {
164
- testWindow.__ECO_REACT_HYDRATION_TEST__?.claimedOwners.push(owner);
165
- },
166
- releaseOwnership: (owner) => {
167
- testWindow.__ECO_REACT_HYDRATION_TEST__?.releasedOwners.push(owner);
168
- },
169
- },
170
- react: {
171
- pageRoot: existingRoot,
172
- },
173
- };
174
- document.body.innerHTML = `<script id="__ECO_PAGE_DATA__" type="application/json">${JSON.stringify({
175
- title: 'Rerun',
176
- })}</script>`;
177
- const script = createHydrationScript({
178
- ...runtimeModules,
179
- scriptId: 'ecopages-react-page-rerun',
180
- hmrEnabled: true,
181
- isMdx: false,
182
- router: routerAdapter,
183
- pageDataReaderImportPath: new URL('./page-data-reader.js', import.meta.url).href,
184
- });
185
- const moduleUrl = createModuleUrl(script);
186
- await importModule(moduleUrl, 'ecopages-react-page-rerun');
187
- expect(testWindow.__ECO_REACT_HYDRATION_TEST__?.hydrateCalls).toHaveLength(0);
188
- expect(testWindow.__ECO_REACT_HYDRATION_TEST__?.renderCalls).toHaveLength(0);
189
- expect(testWindow.__ECO_REACT_HYDRATION_TEST__?.claimedOwners).toHaveLength(0);
190
- expect(testWindow.__ECO_REACT_HYDRATION_TEST__?.registrations).toHaveLength(0);
191
- expect(testWindow.__ECO_PAGES__?.react?.pageRoot).toBe(existingRoot);
192
- expect(testWindow.__ECO_PAGES__?.page).toEqual({
193
- module: moduleUrl,
194
- props: {
195
- title: 'Rerun',
196
- },
197
- });
198
- });
199
- });