uikit/tests/query-param-provider-cjs.test.mjs
Mohamadzadeh 96a434129a
All checks were successful
Build and Deploy Next.js + Nginx Docker Image / build-and-deploy (push) Successful in 1m1s
Build and Deploy Next.js + Nginx Docker Image / deploy (push) Successful in 7s
uikit-v1.3.0-vite-swr-biimage
2026-10-27 13:55:26 +03:30

54 lines
4.0 KiB
JavaScript

import assert from 'node:assert/strict';
import fs from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import {pathToFileURL} from 'node:url';
const root=process.cwd();
const {resolveQueryParamProvider}=await import(pathToFileURL(path.join(root,'src/internal/resolve-query-param-provider.js')));
const C=()=>{};
for(const item of [{QueryParamProvider:C},{default:{QueryParamProvider:C}},{default:{default:{QueryParamProvider:C}}},{QueryParamProvider:{$$typeof:Symbol.for('react.forward_ref')}},]) assert.ok(resolveQueryParamProvider(item));
assert.throws(()=>resolveQueryParamProvider({default:{}}),/Unable to resolve/);
async function scenario(mode){
const temp=await fs.mkdtemp(path.join(os.tmpdir(),`uikit-query-param-${mode}-`));
const write=async(p,data)=>{await fs.mkdir(path.dirname(p),{recursive:true});await fs.writeFile(p,data)};
try {
await write(path.join(temp,'package.json'),'{"type":"module"}');
for(const name of ['next-pages-router.js','provider-next-pages-router.js','next-app-router.js','provider-next-app-router.js','chunks/_shared-DHh79jO9.js']) await fs.copyFile(path.join(root,'dist',name),path.join(temp,name)).catch(async(e)=>{if(e.code==='ENOENT') {await fs.mkdir(path.dirname(path.join(temp,name)),{recursive:true});await fs.copyFile(path.join(root,'dist',name),path.join(temp,name))}else throw e});
const pkg=async(name,files,config)=>{
const dir=path.join(temp,'node_modules',name);
await write(path.join(dir,'package.json'),JSON.stringify({...config,name}));
for (const [f,content] of Object.entries(files)) await write(path.join(dir,f),content);
};
await pkg('react',{
'index.js':`const React={isValidElement:()=>false};export default React;`,
'jsx-runtime.js':`export function jsx(type, props){return {type,props}};export {jsx as jsxs};`
},{type:'module',exports:{'.':'./index.js','./jsx-runtime':'./jsx-runtime.js'}});
await pkg('next-query-params',{
'pages.js':`export default {default: function PagesAdapter(){}};`,
'app.js':`export const NextAdapterApp = function AppAdapter(){};`
},{type:'module',exports:{'./pages':'./pages.js','./app':'./app.js'}});
if (mode==='cjs') await pkg('use-query-params',{'index.cjs':`const x={}; x['Query'+'ParamProvider'] = function QueryParamProvider(){}; module.exports=x;`},{type:'commonjs',exports:{'.':'./index.cjs'}});
else if(mode==='nested') await pkg('use-query-params',{'index.js':`const QueryParamProvider = function QueryParamProvider(){}; export default { default: { QueryParamProvider } };`},{type:'module',exports:{'.':'./index.js'}});
else await pkg('use-query-params',{'index.js':`export function QueryParamProvider() {}`},{type:'module',exports:{'.':'./index.js'}});
const symbol=Symbol.for('uikit.provider.feature.v1');
for (const file of ['next-pages-router.js','provider-next-pages-router.js','next-app-router.js','provider-next-app-router.js']) {
const mod=await import(pathToFileURL(path.join(temp,file)));
const pages=file.includes('pages');
const comp=mod[pages?'NextPagesRouter':'NextAppRouter'];
const layer=comp[symbol].Provider;
const result=layer({children:'content'});
assert.equal(result.type.name,'QueryParamProvider');
assert.equal(typeof result.props.adapter,'function');
assert.equal(result.props.options.enableBatching,true,'default batching works for both Next routers');
assert.equal(layer({children:'x',options:{enableBatching:false,skipNull:true}}).props.options.enableBatching,false);
if(pages){
assert.equal(result.props.options.enableBatching,true);
assert.equal(layer({children:'x',options:{enableBatching:false,skipNull:true}}).props.options.enableBatching,false);
assert.equal(layer({children:'x',shallow:false}).props.adapter({}).props.shallow,false);
}
}
console.log('PASS',mode,'all four dist paths, proper QueryParamProvider, custom options');
}finally {await fs.rm(temp,{recursive:true,force:true})}
}
for(const mode of ['cjs','esm','nested']) await scenario(mode);