wrapito 13.2.2 → 13.3.0-beta10
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.
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +10 -7
- package/dist/index.mjs +10 -7
- package/package.json +5 -4
package/dist/index.d.cts
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
|
-
"use strict";var
|
|
2
|
-
${
|
|
3
|
-
${
|
|
4
|
-
${
|
|
5
|
-
${
|
|
6
|
-
`),
|
|
1
|
+
"use strict";var re=Object.create;var y=Object.defineProperty;var ae=Object.getOwnPropertyDescriptor;var ie=Object.getOwnPropertyNames;var ce=Object.getPrototypeOf,pe=Object.prototype.hasOwnProperty;var ue=(e,t)=>{for(var s in t)y(e,s,{get:t[s],enumerable:!0})},D=(e,t,s,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of ie(t))!pe.call(e,n)&&n!==s&&y(e,n,{get:()=>t[n],enumerable:!(o=ae(t,n))||o.enumerable});return e};var x=(e,t,s)=>(s=e!=null?re(ce(e)):{},D(t||!e||!e.__esModule?y(s,"default",{value:e,enumerable:!0}):s,e)),de=e=>D(y({},"__esModule",{value:!0}),e);var Be={};ue(Be,{assertions:()=>Ae,configure:()=>he,getConfig:()=>u,matchers:()=>Ae,wrap:()=>le});module.exports=de(Be);var C=x(require("react"),1);var g=x(require("chalk"),1);var q=x(require("object-hash"),1);var O=require("@testing-library/react"),I={defaultHost:"",extend:{},mount:O.render,changeRoute:e=>window.history.replaceState(null,"",e)};function he(e){I={...I,...e}}var u=()=>({...I});var H=e=>t=>{let{method:s="GET",path:o,host:n=u().defaultHost,requestBody:r=void 0,catchParams:a}=t,d=n+o,p=!u().handleQueryParams||a,c=(0,q.default)({url:p?d:d.split("?")[0],method:s.toUpperCase(),requestBody:r}),i;return"_bodyInit"in e&&e._bodyInit!==void 0&&(i=JSON.parse(e._bodyInit)),(0,q.default)({url:p?e.url:e.url.split("?")[0],method:e.method,requestBody:i})===c};var me=async()=>{let e={json:()=>Promise.resolve(),status:200,ok:!0,headers:new Headers({"Content-Type":"application/json"})};return Promise.resolve(e)},P=async e=>{let{responseBody:t,status:s=200,headers:o={},delay:n}=e,r={json:()=>Promise.resolve(t),status:s,ok:s>=200&&s<=299,headers:new Headers({"Content-Type":"application/json",...o})};return n?new Promise(a=>setTimeout(()=>a(r),n)):Promise.resolve(r)},ge=e=>console.warn(`
|
|
2
|
+
${g.default.white.bold.bgRed("wrapito")} ${g.default.redBright.bold("cannot find any mock matching:")}
|
|
3
|
+
${g.default.greenBright(`URL: ${e.url}`)}
|
|
4
|
+
${g.default.greenBright(`METHOD: ${e.method?.toLowerCase()}`)}
|
|
5
|
+
${g.default.greenBright(`REQUEST BODY: ${e._bodyInit}`)}
|
|
6
|
+
`),A=async(e,t,s)=>{let o=e.find(H(t));if(!o)return s&&ge(t),me();let{multipleResponses:n}=o;if(!n)return P(o);let r=n.find(a=>!a.hasBeenReturned);if(!r){s&&fe(o);return}return r.hasBeenReturned=!0,P(r)},B=(e=[],t=!1)=>{global.window.fetch.mockImplementation((o,n)=>{if(typeof o=="string"){let a=new Request(o,n);return A(e,a,t)}return A(e,o,t)})},fe=e=>{let t=`\u{1F32F} Wrapito: Missing response in the multipleResponses array for path ${e.path} and method ${e.method}.`,s=g.default.greenBright(t);console.warn(s)};var E,h=e=>{E={...E,...e}},m=()=>({...E});var N=require("@vitest/spy");beforeEach(()=>{global.fetch=(0,N.createMockInstance)()});afterEach(()=>{global.fetch.mockReset()});var le=e=>(h({Component:e,responses:[],props:{},path:"",hasPath:!1,interactionConfig:void 0,debug:process.env.npm_config_debugRequests==="true"}),f()),f=()=>{let e=be();return{withProps:Me,withNetwork:qe,withInteraction:Ie,atPath:Ee,debugRequests:we,mount:Ue,...e}},Re=e=>{let t=m(),s=[...t.responses,...e];h({...t,responses:s})},ye=(e,t)=>(t({addResponses:Re},e),f()),xe=(e,t)=>{let{extend:s}=u(),o=s[t];return{...e,[t]:(...n)=>ye(n,o)}},be=()=>{let{extend:e}=u();return Object.keys(e).reduce(xe,{})},Me=e=>{let t=m();return h({...t,props:e}),f()},Ie=e=>{let t=m();return h({...t,interactionConfig:e}),f()},qe=(e=[])=>{let t=m(),s=Array.isArray(e)?e:[e];return h({...t,responses:[...t.responses,...s]}),f()},Ee=(e,t)=>{let s=m();return h({...s,historyState:t,path:e,hasPath:!0}),f()},we=()=>{let e=m();return h({...e,debug:!0}),f()},Ue=()=>{let{portal:e,portals:t,changeRoute:s,history:o,mount:n,interaction:r}=u(),{Component:a,props:d,responses:p,path:c,hasPath:i,debug:R,historyState:M,interactionConfig:se}=m(),oe=a;e&&v(e),t&&We(t),i&&o&&(console.warn("wrapito WARNING: history is DEPRECATED. Pass a changeRoute function to the config instead."),console.warn("Read about changeRoute in: https://github.com/mercadona/wrapito#changeRoute"),o.push(c,M)),i&&!o&&s(c),B(p,R);let k=n(C.createElement(oe,{...d}));if(r){let ne=r.setup?r.setup(r.userLib,se):r.userLib;return{...k,user:ne}}return{...k,user:void 0}},v=e=>{if(document.getElementById(e))return;let t=document.createElement("div");t.setAttribute("id",e),t.setAttribute("data-testid",e),document.body.appendChild(t)},We=e=>{e.forEach(t=>{v(t)})};var w=require("jest-diff"),U=(e,t)=>{let s=t?.host?`\u{1F32F} Wrapito: ${t?.host}${e} ain't got called`:`\u{1F32F} Wrapito: ${e} ain't got called`;return{pass:!1,message:()=>s}},T=(e,t,s)=>({pass:!1,message:()=>`\u{1F32F} Wrapito: ${e} is called ${s} times, you expected ${t} times`}),S=(e,t)=>({pass:!1,message:()=>`\u{1F32F} Wrapito: Fetch method does not match, expected ${e} received ${t??"none"}`}),$=(e,t)=>{let s=t.map(o=>(0,w.diff)(e,o)).join(`
|
|
7
7
|
|
|
8
8
|
`);return{pass:!1,message:()=>`\u{1F32F} Wrapito: Fetch body does not match.
|
|
9
|
-
${
|
|
9
|
+
${s}`}},L=(e,t)=>{let s=t.find(o=>e!==o);return{pass:!1,message:()=>`\u{1F32F} Wrapito: Host request does not match, expected ${e} received ${s}`}},j=(e,t)=>{let s=t.map(o=>(0,w.diff)(e,o)).join(`
|
|
10
|
+
|
|
11
|
+
`);return{pass:!1,message:()=>`\u{1F32F} Wrapito: Fetch headers do not match.
|
|
12
|
+
${s}`}},F=()=>({pass:!1,message:()=>"\u{1F32F} Wrapito: Unable to find body."}),W=()=>({pass:!0,message:()=>"Test passing"}),_=(e,t)=>{let s=t?.host?`\u{1F32F} Wrapito: ${t.host}${e} is called`:`\u{1F32F} Wrapito: ${e} is called`;return{pass:!0,message:()=>s}};var G=x(require("deep-equal"),1);var Q=()=>{let e=u().defaultHost;return e?.includes("http")?e:"https://default.com"},ke=(e="",t,s)=>t.includes(s)?t:e+t,l=e=>e instanceof Request,De=e=>l(e)?e.url:e,b=(e,t={method:"GET"})=>fetch.mock.calls.filter(([o])=>{let n=De(o),r=Q(),a=new URL(n,r),d=ke(t?.host,e,r),p=t?.host||r,c=new URL(d,p),i=a.pathname===c.pathname,R=a.search===c.search,M=a.host===c.host;return c.search?i&&R:t?.host?i&&M:i}),J=e=>e.flat(1).filter(l).map(t=>t.method),Y=e=>e.flat(1).filter(l).map(t=>t._bodyInit?JSON.parse(t._bodyInit):{}),X=e=>e.flat(1).filter(l).map(t=>new URL(t.url,Q()).hostname),z=e=>e.flat(1).filter(l).map(t=>{let s={};return t.headers.forEach((o,n)=>{s[n]=o}),s}),K=(e,t)=>e&&!t.includes(e),V=(e,t)=>t.map(o=>(0,G.default)(e,o)).every(o=>o===!1),Z=(e,t)=>t.every(o=>o!==e),ee=(e,t)=>t.every(o=>Object.entries(e).some(([n,r])=>o[n.toLowerCase()]!==r)),te=e=>e.length===0;var Oe=(e,t)=>{let s=b(e);if(te(s))return U(e);let o=J(s),n=t?.method;if(K(n,o))return S(n,o);let r=Y(s),a=t?.body;if(!a)return F();if(V(a,r))return $(a,r);let d=X(s),p=t?.host;if(p&&Z(p,d))return L(p,d);let c=z(s),i=t?.headers;return i&&ee(i,c)?j(i,c):W()},He=(e,t={method:"GET"})=>b(e,t).length?_(e,t):U(e,t),Pe=(e,t,s={method:"GET"})=>{let o=b(e,s);return o.length!==t?T(e,t,o.length):W()},Ae={toHaveBeenFetched:He,toHaveBeenFetchedWith:Oe,toHaveBeenFetchedTimes:Pe};0&&(module.exports={assertions,configure,getConfig,matchers,wrap});
|
package/dist/index.mjs
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
|
-
import*as
|
|
2
|
-
${
|
|
3
|
-
${
|
|
4
|
-
${
|
|
5
|
-
${
|
|
6
|
-
`),
|
|
1
|
+
import*as O from"react";import f from"chalk";import w from"object-hash";import{render as Z}from"@testing-library/react";var b={defaultHost:"",extend:{},mount:Z,changeRoute:e=>window.history.replaceState(null,"",e)};function Ie(e){b={...b,...e}}var d=()=>({...b});var U=e=>t=>{let{method:s="GET",path:o,host:r=d().defaultHost,requestBody:n=void 0,catchParams:a}=t,u=r+o,p=!d().handleQueryParams||a,c=w({url:p?u:u.split("?")[0],method:s.toUpperCase(),requestBody:n}),i;return"_bodyInit"in e&&e._bodyInit!==void 0&&(i=JSON.parse(e._bodyInit)),w({url:p?e.url:e.url.split("?")[0],method:e.method,requestBody:i})===c};var ee=async()=>{let e={json:()=>Promise.resolve(),status:200,ok:!0,headers:new Headers({"Content-Type":"application/json"})};return Promise.resolve(e)},W=async e=>{let{responseBody:t,status:s=200,headers:o={},delay:r}=e,n={json:()=>Promise.resolve(t),status:s,ok:s>=200&&s<=299,headers:new Headers({"Content-Type":"application/json",...o})};return r?new Promise(a=>setTimeout(()=>a(n),r)):Promise.resolve(n)},te=e=>console.warn(`
|
|
2
|
+
${f.white.bold.bgRed("wrapito")} ${f.redBright.bold("cannot find any mock matching:")}
|
|
3
|
+
${f.greenBright(`URL: ${e.url}`)}
|
|
4
|
+
${f.greenBright(`METHOD: ${e.method?.toLowerCase()}`)}
|
|
5
|
+
${f.greenBright(`REQUEST BODY: ${e._bodyInit}`)}
|
|
6
|
+
`),k=async(e,t,s)=>{let o=e.find(U(t));if(!o)return s&&te(t),ee();let{multipleResponses:r}=o;if(!r)return W(o);let n=r.find(a=>!a.hasBeenReturned);if(!n){s&&se(o);return}return n.hasBeenReturned=!0,W(n)},D=(e=[],t=!1)=>{global.window.fetch.mockImplementation((o,r)=>{if(typeof o=="string"){let a=new Request(o,r);return k(e,a,t)}return k(e,o,t)})},se=e=>{let t=`\u{1F32F} Wrapito: Missing response in the multipleResponses array for path ${e.path} and method ${e.method}.`,s=f.greenBright(t);console.warn(s)};var M,h=e=>{M={...M,...e}},m=()=>({...M});import{createMockInstance as oe}from"@vitest/spy";beforeEach(()=>{global.fetch=oe()});afterEach(()=>{global.fetch.mockReset()});var Ce=e=>(h({Component:e,responses:[],props:{},path:"",hasPath:!1,interactionConfig:void 0,debug:process.env.npm_config_debugRequests==="true"}),g()),g=()=>{let e=ie();return{withProps:ce,withNetwork:ue,withInteraction:pe,atPath:de,debugRequests:he,mount:me,...e}},ne=e=>{let t=m(),s=[...t.responses,...e];h({...t,responses:s})},re=(e,t)=>(t({addResponses:ne},e),g()),ae=(e,t)=>{let{extend:s}=d(),o=s[t];return{...e,[t]:(...r)=>re(r,o)}},ie=()=>{let{extend:e}=d();return Object.keys(e).reduce(ae,{})},ce=e=>{let t=m();return h({...t,props:e}),g()},pe=e=>{let t=m();return h({...t,interactionConfig:e}),g()},ue=(e=[])=>{let t=m(),s=Array.isArray(e)?e:[e];return h({...t,responses:[...t.responses,...s]}),g()},de=(e,t)=>{let s=m();return h({...s,historyState:t,path:e,hasPath:!0}),g()},he=()=>{let e=m();return h({...e,debug:!0}),g()},me=()=>{let{portal:e,portals:t,changeRoute:s,history:o,mount:r,interaction:n}=d(),{Component:a,props:u,responses:p,path:c,hasPath:i,debug:R,historyState:x,interactionConfig:z}=m(),K=a;e&&H(e),t&&ge(t),i&&o&&(console.warn("wrapito WARNING: history is DEPRECATED. Pass a changeRoute function to the config instead."),console.warn("Read about changeRoute in: https://github.com/mercadona/wrapito#changeRoute"),o.push(c,x)),i&&!o&&s(c),D(p,R);let E=r(O.createElement(K,{...u}));if(n){let V=n.setup?n.setup(n.userLib,z):n.userLib;return{...E,user:V}}return{...E,user:void 0}},H=e=>{if(document.getElementById(e))return;let t=document.createElement("div");t.setAttribute("id",e),t.setAttribute("data-testid",e),document.body.appendChild(t)},ge=e=>{e.forEach(t=>{H(t)})};import{diff as P}from"jest-diff";var I=(e,t)=>{let s=t?.host?`\u{1F32F} Wrapito: ${t?.host}${e} ain't got called`:`\u{1F32F} Wrapito: ${e} ain't got called`;return{pass:!1,message:()=>s}},A=(e,t,s)=>({pass:!1,message:()=>`\u{1F32F} Wrapito: ${e} is called ${s} times, you expected ${t} times`}),B=(e,t)=>({pass:!1,message:()=>`\u{1F32F} Wrapito: Fetch method does not match, expected ${e} received ${t??"none"}`}),C=(e,t)=>{let s=t.map(o=>P(e,o)).join(`
|
|
7
7
|
|
|
8
8
|
`);return{pass:!1,message:()=>`\u{1F32F} Wrapito: Fetch body does not match.
|
|
9
|
-
${
|
|
9
|
+
${s}`}},N=(e,t)=>{let s=t.find(o=>e!==o);return{pass:!1,message:()=>`\u{1F32F} Wrapito: Host request does not match, expected ${e} received ${s}`}},v=(e,t)=>{let s=t.map(o=>P(e,o)).join(`
|
|
10
|
+
|
|
11
|
+
`);return{pass:!1,message:()=>`\u{1F32F} Wrapito: Fetch headers do not match.
|
|
12
|
+
${s}`}},T=()=>({pass:!1,message:()=>"\u{1F32F} Wrapito: Unable to find body."}),q=()=>({pass:!0,message:()=>"Test passing"}),S=(e,t)=>{let s=t?.host?`\u{1F32F} Wrapito: ${t.host}${e} is called`:`\u{1F32F} Wrapito: ${e} is called`;return{pass:!0,message:()=>s}};import fe from"deep-equal";var $=()=>{let e=d().defaultHost;return e?.includes("http")?e:"https://default.com"},le=(e="",t,s)=>t.includes(s)?t:e+t,l=e=>e instanceof Request,Re=e=>l(e)?e.url:e,y=(e,t={method:"GET"})=>fetch.mock.calls.filter(([o])=>{let r=Re(o),n=$(),a=new URL(r,n),u=le(t?.host,e,n),p=t?.host||n,c=new URL(u,p),i=a.pathname===c.pathname,R=a.search===c.search,x=a.host===c.host;return c.search?i&&R:t?.host?i&&x:i}),L=e=>e.flat(1).filter(l).map(t=>t.method),j=e=>e.flat(1).filter(l).map(t=>t._bodyInit?JSON.parse(t._bodyInit):{}),F=e=>e.flat(1).filter(l).map(t=>new URL(t.url,$()).hostname),_=e=>e.flat(1).filter(l).map(t=>{let s={};return t.headers.forEach((o,r)=>{s[r]=o}),s}),G=(e,t)=>e&&!t.includes(e),Q=(e,t)=>t.map(o=>fe(e,o)).every(o=>o===!1),J=(e,t)=>t.every(o=>o!==e),Y=(e,t)=>t.every(o=>Object.entries(e).some(([r,n])=>o[r.toLowerCase()]!==n)),X=e=>e.length===0;var ye=(e,t)=>{let s=y(e);if(X(s))return I(e);let o=L(s),r=t?.method;if(G(r,o))return B(r,o);let n=j(s),a=t?.body;if(!a)return T();if(Q(a,n))return C(a,n);let u=F(s),p=t?.host;if(p&&J(p,u))return N(p,u);let c=_(s),i=t?.headers;return i&&Y(i,c)?v(i,c):q()},xe=(e,t={method:"GET"})=>y(e,t).length?S(e,t):I(e,t),be=(e,t,s={method:"GET"})=>{let o=y(e,s);return o.length!==t?A(e,t,o.length):q()},_e={toHaveBeenFetched:xe,toHaveBeenFetchedWith:ye,toHaveBeenFetchedTimes:be};export{_e as assertions,Ie as configure,d as getConfig,_e as matchers,Ce as wrap};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "wrapito",
|
|
3
|
-
"version": "13.
|
|
3
|
+
"version": "13.3.0-beta10",
|
|
4
4
|
"description": "🌯 🌯 Wrap you tests so that you can test both behaviour and components with less effort.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -52,6 +52,7 @@
|
|
|
52
52
|
},
|
|
53
53
|
"homepage": "https://github.com/mercadona/wrapito#readme",
|
|
54
54
|
"dependencies": {
|
|
55
|
+
"@vitest/spy": "^4.0.15",
|
|
55
56
|
"chalk": "^5.6.2",
|
|
56
57
|
"deep-equal": "^2.2.3",
|
|
57
58
|
"jest-diff": "^29.7.0",
|
|
@@ -66,9 +67,9 @@
|
|
|
66
67
|
"@types/object-hash": "^3.0.6",
|
|
67
68
|
"@types/react": "^19.1.8",
|
|
68
69
|
"@types/react-router-dom": "^5.3.3",
|
|
69
|
-
"@vitest/coverage-istanbul": "^
|
|
70
|
+
"@vitest/coverage-istanbul": "^3.2.4",
|
|
70
71
|
"eslint": "^8.56.0",
|
|
71
|
-
"eslint-config-prettier": "^
|
|
72
|
+
"eslint-config-prettier": "^10.1.8",
|
|
72
73
|
"eslint-config-react-app": "^7.0.1",
|
|
73
74
|
"eslint-plugin-prettier": "^5.1.1",
|
|
74
75
|
"eslint-plugin-react": "^7.33.2",
|
|
@@ -84,7 +85,7 @@
|
|
|
84
85
|
"tinyspy": "^2.2.1",
|
|
85
86
|
"tsup": "^8.5.1",
|
|
86
87
|
"typescript": "^5.9.3",
|
|
87
|
-
"vitest": "^
|
|
88
|
+
"vitest": "^3.2.4"
|
|
88
89
|
},
|
|
89
90
|
"peerDependencies": {
|
|
90
91
|
"@testing-library/jest-dom": ">=5.16.4",
|