uikit/tests/provider-hydration.test.mjs
Mohamadzadeh 105544ba9e
All checks were successful
Build and Deploy Next.js + Nginx Docker Image / build-and-deploy (push) Successful in 52s
Build and Deploy Next.js + Nginx Docker Image / deploy (push) Successful in 7s
uikit-v1.2.7-hydration-fix
2026-10-26 17:13:33 +03:30

76 lines
4.6 KiB
JavaScript

import assert from 'node:assert/strict';
import { copyFile, mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import { pathToFileURL } from 'node:url';
// Test the distributed ESM entry, not only its JSX source. Use a minimal React
// fake so the check can run without downloading any dependencies.
const temp = await mkdtemp(path.join(os.tmpdir(), 'uikit-hydration-'));
try {
await mkdir(path.join(temp, 'chunks'));
await mkdir(path.join(temp, 'node_modules', 'react'), { recursive: true });
await writeFile(path.join(temp, 'package.json'), '{"type":"module"}');
await copyFile('dist/provider.js', path.join(temp, 'provider.js'));
await copyFile('dist/chunks/_shared-DHh79jO9.js', path.join(temp, 'chunks/_shared-DHh79jO9.js'));
await writeFile(path.join(temp, 'node_modules/react/package.json'),
JSON.stringify({ name: 'react', type: 'module', exports: { '.': './index.js' } }));
await writeFile(path.join(temp, 'node_modules/react/index.js'), `
export const Fragment = Symbol.for('mock.react.fragment');
let isMounted = false;
let effects = [];
export function useState(initial) { return [isMounted, (value) => { isMounted = value }]; }
export function useEffect(callback) { effects.push(callback); }
export function flushEffects() { for (const callback of effects.splice(0)) callback(); }
export const createElement = (type, props, ...children) => ({type,props:{...props,...(children.length?{children:children.length===1?children[0]:children}:{})}});
export const isValidElement = (value) => Boolean(value && value.type && value.props);
export const Children = { forEach: (value, callback) => { const each = x => {if (Array.isArray(x)) x.forEach(each); else if (x != null) callback(x)}; each(value); } };
export default { Fragment, useState, useEffect, createElement, isValidElement, Children };
`);
const { BiProvider } = await import(pathToFileURL(path.join(temp, 'provider.js')).href);
const React = await import(pathToFileURL(path.join(temp, 'node_modules/react/index.js')).href);
const featureMarker = Symbol.for('uikit.provider.feature.v1');
const defineFeature = (name, priority) => {
const Component = function Component() {};
Object.defineProperty(Component, featureMarker, {
value: { name, priority, Provider: function Layer() {} },
});
return Component;
};
const Chakra = defineFeature('Chakra', 10);
const Keycloak = defineFeature('Keycloak', 40);
const Permission = defineFeature('Permission', 60);
const main = React.createElement('main', { id: 'app' });
const tree = BiProvider({ children: [
React.createElement(React.Fragment, null, React.createElement(Chakra, {})),
React.createElement(Keycloak, { clientId: 'k' }),
React.createElement(Permission, { clientId: 'p' }),
main,
] });
assert.equal(typeof tree.type, 'function', 'with Chakra, a React-only ClientBoundary must wrap the feature tree');
assert.equal(tree.type({ children: tree.props.children }), null, 'SSR must emit no Chakra/Emotion subtree');
assert.equal(tree.type({ children: tree.props.children }), null, 'first client hydration pass must match SSR');
React.flushEffects();
const hydrated = tree.type({ children: tree.props.children });
assert.ok(hydrated, 'after hydration/mount render Chakra feature tree');
assert.equal(hydrated.type, Chakra[featureMarker].Provider, 'Chakra remains outermost');
assert.equal(hydrated.props.children.type, Keycloak[featureMarker].Provider, 'Keycloak still wraps Permission');
assert.equal(hydrated.props.children.props.children.type, Permission[featureMarker].Provider);
const fragment = hydrated.props.children.props.children.props.children;
assert.equal(fragment.type, React.Fragment);
assert.equal(fragment.props.children.props.id, 'app');
// Combinations without Chakra should preserve SSR (no client-only boundary).
const pureTree = BiProvider({ children: [React.createElement(Keycloak, { clientId: 'k' }), main] });
assert.equal(pureTree.type, Keycloak[featureMarker].Provider);
assert.equal(BiProvider({ children: main }).type, React.Fragment);
assert.throws(() => BiProvider({ children: [React.createElement(Chakra, {}), React.createElement(Chakra, {})] }), /duplicate/);
const oldSource = await readFile('src/core/BiProvider.jsx', 'utf8');
assert.match(oldSource, /if \(!mounted\)/, 'legacy SSR gating unchanged');
console.log('PASS: dist SSR null + first hydration null + mount Chakra, independent SSR, feature nesting and duplicate protection.');
} finally {
await rm(temp, { recursive: true, force: true });
}