uikit/tests/vite-swr-modules.test.mjs
Mohamadzadeh 1099691b9a
All checks were successful
Build and Deploy Next.js + Nginx Docker Image / build-and-deploy (push) Successful in 50s
Build and Deploy Next.js + Nginx Docker Image / deploy (push) Successful in 7s
uikit-v1.4.0-watermark-complete
2026-11-01 12:52:28 +03:30

75 lines
5.6 KiB
JavaScript

import assert from 'node:assert/strict';
import { readFile, writeFile, mkdir, cp, rm, mkdtemp } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import path from 'node:path';
import { pathToFileURL } from 'node:url';
const p = JSON.parse(await readFile('package.json','utf8'));
assert.ok(Number(p.version.split('.')[0]) > 1 || Number(p.version.split('.')[1]) >= 3, 'SWR/Vite introduced in 1.3.0');
assert.deepEqual(p.exports['./vite'],p.exports['./provider/vite']);
assert.deepEqual(p.exports['./swr'],p.exports['./provider/swr']);
const tmp = await mkdtemp(path.join(tmpdir(),'uikit-vite-swr-'));
try {
await writeFile(path.join(tmp,'package.json'),'{"type":"module"}');
await mkdir(path.join(tmp,'chunks'));
await cp('dist/chunks/_shared-DHh79jO9.js',path.join(tmp,'chunks/_shared-DHh79jO9.js'));
for (const name of ['provider','vite','provider-vite','swr']) await cp(`dist/${name}.js`,path.join(tmp,`${name}.js`));
async function pkg(name, exports, files) {
const dir=path.join(tmp,'node_modules',name);
await mkdir(dir,{recursive:true});
await writeFile(path.join(dir,'package.json'),JSON.stringify({name,type:'module',exports}));
for (const [f,body] of Object.entries(files)) {const full=path.join(dir,f);await mkdir(path.dirname(full),{recursive:true});await writeFile(full,body);}
}
await pkg('react',{'.':'./index.js','./jsx-runtime':'./jsx-runtime.js'}, {
'index.js': `export const Fragment=Symbol.for('mock.fragment');
export function createElement(type,props,...children){return {type,props:{...props,...(children.length?{children:children.length===1?children[0]:children}:{})}};}
export function isValidElement(v){return !!v && typeof v==='object' && 'type' in v && 'props' in v;}
export const Children={forEach(v,fn){const walk=n=>{if(Array.isArray(n))n.forEach(walk);else if(n!=null)fn(n)};walk(v)}};
export function useState(x){return [x,()=>{}]};export function useEffect(){};
export default {Fragment,createElement,isValidElement,Children,useState,useEffect};`,
'jsx-runtime.js':`import {createElement,Fragment} from './index.js';export {Fragment};export const jsx=createElement;export const jsxs=createElement;`,
});
await pkg('react-router-dom',{'.':'./index.js'}, {'index.js':'export function BrowserRouter(props){return props.children}'});
await pkg('use-query-params',{'.':'./index.js','./adapters/react-router-6':'./react-router.js'}, {
'index.js':'export function QueryParamProvider(props){return props.children}',
'react-router.js':'export function ReactRouter6Adapter(props){return props.children}'
});
await pkg('swr',{'.':'./index.js'}, {'index.js':'export function SWRConfig(props){return props.children}'});
const uri=n=>pathToFileURL(path.join(tmp,n+'.js')).href;
const [{ Vite },{ Vite:AliasVite },{ SWR },{ SWR:AliasSWR },{ BiProvider },react] = await Promise.all([
import(uri('vite')),import(uri('provider-vite')),import(uri('swr')),import(uri('swr')),import(uri('provider')),import(pathToFileURL(path.join(tmp,'node_modules/react/index.js')))
]);
const marker=Symbol.for('uikit.provider.feature.v1');
assert.equal(AliasVite,Vite);
assert.equal(AliasSWR,SWR);
const browser = (await import(pathToFileURL(path.join(tmp,'node_modules/react-router-dom/index.js')))).BrowserRouter;
const query = (await import(pathToFileURL(path.join(tmp,'node_modules/use-query-params/index.js')))).QueryParamProvider;
const adapter = (await import(pathToFileURL(path.join(tmp,'node_modules/use-query-params/react-router.js')))).ReactRouter6Adapter;
const swrcfg = (await import(pathToFileURL(path.join(tmp,'node_modules/swr/index.js')))).SWRConfig;
const child=react.createElement('div',{id:'app'});
const wrapped=Vite[marker].Provider({children:child});
assert.equal(wrapped.type,browser,'BrowserRouter is outermost');
assert.equal(wrapped.props.children.type,query,'QueryParamProvider nested within BrowserRouter');
assert.equal(wrapped.props.children.props.adapter,adapter);
assert.deepEqual(wrapped.props.children.props.options,{enableBatching:true},'batching enabled by default');
assert.equal(wrapped.props.children.props.children,child);
assert.equal(Vite[marker].Provider({children:child,queryParams:false}).props.children,child);
const existing = Vite[marker].Provider({children:child,router:false});
assert.equal(existing.type,query,'can reuse external BrowserRouter');
const configured=Vite[marker].Provider({children:child,basename:'/panel',options:{enableBatching:false,skipNull:true}});
assert.equal(configured.props.basename,'/panel');
assert.deepEqual(configured.props.children.props.options,{enableBatching:false,skipNull:true});
const fetcher=()=>{};
const swr=SWR[marker].Provider({children:child,value:{fetcher}});
assert.equal(swr.type,swrcfg);
assert.equal(swr.props.value.fetcher,fetcher);
const both=BiProvider({children:[react.createElement(Vite,{}),react.createElement(SWR,{value:{fetcher}}),child]});
assert.equal(both.type,SWR[marker].Provider,'SWR sorted outer to Vite');
assert.equal(both.props.children.type,Vite[marker].Provider);
assert.throws(()=>BiProvider({children:[react.createElement(Vite,{}),react.createElement(Vite,{})]}),/duplicate/);
// Fake Next adapter: use a marker just like the real ones without importing Next.js
function Next(){};Object.defineProperty(Next,marker,{value:{name:'NextAppRouter',priority:30,Provider:({children})=>children}});
assert.throws(()=>BiProvider({children:[react.createElement(Vite,{}),react.createElement(Next,{})]}),/cannot be combined/);
console.log('PASS: Vite router and query nesting; SWR config; alias identity; feature ordering; override and duplicate protection.');
} finally {await rm(tmp,{recursive:true,force:true});}