wrapito 13.1.0 → 13.2.0
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 +4 -4
- package/dist/index.d.ts +4 -4
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -34,9 +34,9 @@ type DefaultUserLib = unknown;
|
|
|
34
34
|
type DefaultUserInstance = unknown;
|
|
35
35
|
type DefaultUserSetupOptions = unknown;
|
|
36
36
|
interface InteractionDescriptor<UserLib = DefaultUserLib, UserInstance = DefaultUserInstance, UserSetupOptions = DefaultUserSetupOptions> {
|
|
37
|
-
UserLib
|
|
38
|
-
UserInstance
|
|
39
|
-
UserSetupOptions
|
|
37
|
+
UserLib: UserLib;
|
|
38
|
+
UserInstance: UserInstance;
|
|
39
|
+
UserSetupOptions: UserSetupOptions;
|
|
40
40
|
}
|
|
41
41
|
interface Wrap<UserInteraction extends InteractionDescriptor = InteractionDescriptor> {
|
|
42
42
|
withNetwork: (responses?: NetworkResponses) => Wrap<UserInteraction>;
|
|
@@ -45,7 +45,7 @@ interface Wrap<UserInteraction extends InteractionDescriptor = InteractionDescri
|
|
|
45
45
|
withInteraction: (config: UserInteraction['UserSetupOptions']) => Wrap<UserInteraction>;
|
|
46
46
|
debugRequests: () => Wrap<UserInteraction>;
|
|
47
47
|
mount: () => RenderResult & {
|
|
48
|
-
user
|
|
48
|
+
user: UserInteraction['UserInstance'];
|
|
49
49
|
};
|
|
50
50
|
}
|
|
51
51
|
interface WrapOptions<SetupOptions = DefaultUserSetupOptions> {
|
package/dist/index.d.ts
CHANGED
|
@@ -34,9 +34,9 @@ type DefaultUserLib = unknown;
|
|
|
34
34
|
type DefaultUserInstance = unknown;
|
|
35
35
|
type DefaultUserSetupOptions = unknown;
|
|
36
36
|
interface InteractionDescriptor<UserLib = DefaultUserLib, UserInstance = DefaultUserInstance, UserSetupOptions = DefaultUserSetupOptions> {
|
|
37
|
-
UserLib
|
|
38
|
-
UserInstance
|
|
39
|
-
UserSetupOptions
|
|
37
|
+
UserLib: UserLib;
|
|
38
|
+
UserInstance: UserInstance;
|
|
39
|
+
UserSetupOptions: UserSetupOptions;
|
|
40
40
|
}
|
|
41
41
|
interface Wrap<UserInteraction extends InteractionDescriptor = InteractionDescriptor> {
|
|
42
42
|
withNetwork: (responses?: NetworkResponses) => Wrap<UserInteraction>;
|
|
@@ -45,7 +45,7 @@ interface Wrap<UserInteraction extends InteractionDescriptor = InteractionDescri
|
|
|
45
45
|
withInteraction: (config: UserInteraction['UserSetupOptions']) => Wrap<UserInteraction>;
|
|
46
46
|
debugRequests: () => Wrap<UserInteraction>;
|
|
47
47
|
mount: () => RenderResult & {
|
|
48
|
-
user
|
|
48
|
+
user: UserInteraction['UserInstance'];
|
|
49
49
|
};
|
|
50
50
|
}
|
|
51
51
|
interface WrapOptions<SetupOptions = DefaultUserSetupOptions> {
|
package/dist/index.js
CHANGED
|
@@ -3,7 +3,7 @@ ${b.default.white.bold.bgRed("wrapito")} ${b.default.redBright.bold("cannot find
|
|
|
3
3
|
${b.default.greenBright(`URL: ${e.url}`)}
|
|
4
4
|
${b.default.greenBright(`METHOD: ${e.method?.toLowerCase()}`)}
|
|
5
5
|
${b.default.greenBright(`REQUEST BODY: ${e._bodyInit}`)}
|
|
6
|
-
`),V=async(e,t,n)=>{let s=e.find($(t));if(!s)return n&&qe(t),Ue();let{multipleResponses:o}=s;if(!o)return G(s);let r=o.find(a=>!a.hasBeenReturned);if(!r){n&&We(s);return}return r.hasBeenReturned=!0,G(r)},Q=(e=[],t=!1)=>{global.window.fetch.mockImplementation((s,o)=>{if(typeof s=="string"){let a=new Request(s,o);return V(e,a,t)}return V(e,s,t)})},We=e=>{let t=`\u{1F32F} Wrapito: Missing response in the multipleResponses array for path ${e.path} and method ${e.method}.`,n=b.default.greenBright(t);console.warn(n)};var S,y=e=>{S={...S,...e}},R=()=>({...S});beforeEach(()=>{global.fetch=U()});afterEach(()=>{global.fetch.mockReset()});var ve=e=>(y({Component:e,responses:[],props:{},path:"",hasPath:!1,interactionConfig:void 0,debug:process.env.npm_config_debugRequests==="true"}),I()),I=()=>{let e=Se();return{withProps:He,withNetwork:je,withInteraction:Be,atPath:Ne,debugRequests:$e,mount:Le,...e}},Ae=e=>{let t=R(),n=[...t.responses,...e];y({...t,responses:n})},Ce=(e,t)=>(t({addResponses:Ae},e),I()),De=(e,t)=>{let{extend:n}=d(),s=n[t];return{...e,[t]:(...o)=>Ce(o,s)}},Se=()=>{let{extend:e}=d();return Object.keys(e).reduce(De,{})},He=e=>{let t=R();return y({...t,props:e}),I()},Be=e=>{let t=R();return y({...t,interactionConfig:e}),I()},je=(e=[])=>{let t=R(),n=Array.isArray(e)?e:[e];return y({...t,responses:[...t.responses,...n]}),I()},Ne=(e,t)=>{let n=R();return y({...n,historyState:t,path:e,hasPath:!0}),I()},$e=()=>{let e=R();return y({...e,debug:!0}),I()},Le=()=>{let{portal:e,portals:t,changeRoute:n,history:s,mount:o,interaction:r}=d(),{Component:a,props:l,responses:i,path:u,hasPath:p,debug:m,historyState:c,interactionConfig:g}=R(),h=a;e&&Y(e),t&&Fe(t),p&&s&&(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"),s.push(u,c)),p&&!s&&n(u),Q(i,m);let f=o(J.createElement(h,{...l}));if(r){let k=r.setup?r.setup(r.userLib,g):r.userLib;return{...f,user:k}}return
|
|
6
|
+
`),V=async(e,t,n)=>{let s=e.find($(t));if(!s)return n&&qe(t),Ue();let{multipleResponses:o}=s;if(!o)return G(s);let r=o.find(a=>!a.hasBeenReturned);if(!r){n&&We(s);return}return r.hasBeenReturned=!0,G(r)},Q=(e=[],t=!1)=>{global.window.fetch.mockImplementation((s,o)=>{if(typeof s=="string"){let a=new Request(s,o);return V(e,a,t)}return V(e,s,t)})},We=e=>{let t=`\u{1F32F} Wrapito: Missing response in the multipleResponses array for path ${e.path} and method ${e.method}.`,n=b.default.greenBright(t);console.warn(n)};var S,y=e=>{S={...S,...e}},R=()=>({...S});beforeEach(()=>{global.fetch=U()});afterEach(()=>{global.fetch.mockReset()});var ve=e=>(y({Component:e,responses:[],props:{},path:"",hasPath:!1,interactionConfig:void 0,debug:process.env.npm_config_debugRequests==="true"}),I()),I=()=>{let e=Se();return{withProps:He,withNetwork:je,withInteraction:Be,atPath:Ne,debugRequests:$e,mount:Le,...e}},Ae=e=>{let t=R(),n=[...t.responses,...e];y({...t,responses:n})},Ce=(e,t)=>(t({addResponses:Ae},e),I()),De=(e,t)=>{let{extend:n}=d(),s=n[t];return{...e,[t]:(...o)=>Ce(o,s)}},Se=()=>{let{extend:e}=d();return Object.keys(e).reduce(De,{})},He=e=>{let t=R();return y({...t,props:e}),I()},Be=e=>{let t=R();return y({...t,interactionConfig:e}),I()},je=(e=[])=>{let t=R(),n=Array.isArray(e)?e:[e];return y({...t,responses:[...t.responses,...n]}),I()},Ne=(e,t)=>{let n=R();return y({...n,historyState:t,path:e,hasPath:!0}),I()},$e=()=>{let e=R();return y({...e,debug:!0}),I()},Le=()=>{let{portal:e,portals:t,changeRoute:n,history:s,mount:o,interaction:r}=d(),{Component:a,props:l,responses:i,path:u,hasPath:p,debug:m,historyState:c,interactionConfig:g}=R(),h=a;e&&Y(e),t&&Fe(t),p&&s&&(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"),s.push(u,c)),p&&!s&&n(u),Q(i,m);let f=o(J.createElement(h,{...l}));if(r){let k=r.setup?r.setup(r.userLib,g):r.userLib;return{...f,user:k}}return{...f,user:void 0}},Y=e=>{if(document.getElementById(e))return;let t=document.createElement("div");t.setAttribute("id",e),t.setAttribute("data-testid",e),document.body.appendChild(t)},Fe=e=>{e.forEach(t=>{Y(t)})};var z=require("jest-diff"),H=(e,t)=>{let n=t?.host?`\u{1F32F} Wrapito: ${t?.host}${e} ain't got called`:`\u{1F32F} Wrapito: ${e} ain't got called`;return{pass:!1,message:()=>n}},K=(e,t,n)=>({pass:!1,message:()=>`\u{1F32F} Wrapito: ${e} is called ${n} times, you expected ${t} times`}),X=(e,t)=>({pass:!1,message:()=>`\u{1F32F} Wrapito: Fetch method does not match, expected ${e} received ${t??"none"}`}),Z=(e,t)=>{let n=t.map(s=>(0,z.diff)(e,s)).join(`
|
|
7
7
|
|
|
8
8
|
`);return{pass:!1,message:()=>`\u{1F32F} Wrapito: Fetch body does not match.
|
|
9
9
|
${n}`}},ee=(e,t)=>{let n=t.find(s=>e!==s);return{pass:!1,message:()=>`\u{1F32F} Wrapito: Host request does not match, expected ${e} received ${n}`}},te=()=>({pass:!1,message:()=>"\u{1F32F} Wrapito: Unable to find body."}),B=()=>({pass:!0,message:()=>"Test passing"}),ne=(e,t)=>{let n=t?.host?`\u{1F32F} Wrapito: ${t.host}${e} is called`:`\u{1F32F} Wrapito: ${e} is called`;return{pass:!0,message:()=>n}};var se=T(require("deep-equal"),1);var oe=()=>{let e=d().defaultHost;return e?.includes("http")?e:"https://default.com"},_e=(e="",t,n)=>t.includes(n)?t:e+t,q=e=>e instanceof Request,Ge=e=>q(e)?e.url:e,W=(e,t={method:"GET"})=>fetch.mock.calls.filter(([s])=>{let o=Ge(s),r=oe(),a=new URL(o,r),l=_e(t?.host,e,r),i=t?.host||r,u=new URL(l,i),p=a.pathname===u.pathname,m=a.search===u.search,c=a.host===u.host;return u.search?p&&m:t?.host?p&&c:p}),re=e=>e.flat(1).filter(q).map(t=>t.method),ae=e=>e.flat(1).filter(q).map(t=>t._bodyInit?JSON.parse(t._bodyInit):{}),ie=e=>e.flat(1).filter(q).map(t=>new URL(t.url,oe()).hostname),ce=(e,t)=>e&&!t.includes(e),pe=(e,t)=>t.map(s=>(0,se.default)(e,s)).every(s=>s===!1),ue=(e,t)=>t.every(s=>s!==e),le=e=>e.length===0;var Ve=(e,t)=>{let n=W(e);if(le(n))return H(e);let s=re(n),o=t?.method;if(ce(o,s))return X(o,s);let r=ae(n),a=t?.body;if(!a)return te();if(pe(a,r))return Z(a,r);let l=ie(n),i=t?.host;return i&&ue(i,l)?ee(i,l):B()},Qe=(e,t={method:"GET"})=>W(e,t).length?ne(e,t):H(e,t),Je=(e,t,n={method:"GET"})=>{let s=W(e,n);return s.length!==t?K(e,t,s.length):B()},Ye={toHaveBeenFetched:Qe,toHaveBeenFetchedWith:Ve,toHaveBeenFetchedTimes:Je};0&&(module.exports={assertions,configure,getConfig,matchers,wrap});
|
package/dist/index.mjs
CHANGED
|
@@ -3,7 +3,7 @@ ${x.white.bold.bgRed("wrapito")} ${x.redBright.bold("cannot find any mock matchi
|
|
|
3
3
|
${x.greenBright(`URL: ${e.url}`)}
|
|
4
4
|
${x.greenBright(`METHOD: ${e.method?.toLowerCase()}`)}
|
|
5
5
|
${x.greenBright(`REQUEST BODY: ${e._bodyInit}`)}
|
|
6
|
-
`),L=async(e,t,n)=>{let s=e.find(H(t));if(!s)return n&&ye(t),ge();let{multipleResponses:o}=s;if(!o)return $(s);let r=o.find(a=>!a.hasBeenReturned);if(!r){n&&Re(s);return}return r.hasBeenReturned=!0,$(r)},F=(e=[],t=!1)=>{global.window.fetch.mockImplementation((s,o)=>{if(typeof s=="string"){let a=new Request(s,o);return L(e,a,t)}return L(e,s,t)})},Re=e=>{let t=`\u{1F32F} Wrapito: Missing response in the multipleResponses array for path ${e.path} and method ${e.method}.`,n=x.greenBright(t);console.warn(n)};var A,y=e=>{A={...A,...e}},R=()=>({...A});beforeEach(()=>{global.fetch=E()});afterEach(()=>{global.fetch.mockReset()});var Ze=e=>(y({Component:e,responses:[],props:{},path:"",hasPath:!1,interactionConfig:void 0,debug:process.env.npm_config_debugRequests==="true"}),b()),b=()=>{let e=xe();return{withProps:ke,withNetwork:Oe,withInteraction:Me,atPath:Te,debugRequests:Ee,mount:Pe,...e}},be=e=>{let t=R(),n=[...t.responses,...e];y({...t,responses:n})},Ie=(e,t)=>(t({addResponses:be},e),b()),we=(e,t)=>{let{extend:n}=f(),s=n[t];return{...e,[t]:(...o)=>Ie(o,s)}},xe=()=>{let{extend:e}=f();return Object.keys(e).reduce(we,{})},ke=e=>{let t=R();return y({...t,props:e}),b()},Me=e=>{let t=R();return y({...t,interactionConfig:e}),b()},Oe=(e=[])=>{let t=R(),n=Array.isArray(e)?e:[e];return y({...t,responses:[...t.responses,...n]}),b()},Te=(e,t)=>{let n=R();return y({...n,historyState:t,path:e,hasPath:!0}),b()},Ee=()=>{let e=R();return y({...e,debug:!0}),b()},Pe=()=>{let{portal:e,portals:t,changeRoute:n,history:s,mount:o,interaction:r}=f(),{Component:a,props:l,responses:i,path:u,hasPath:p,debug:m,historyState:c,interactionConfig:g}=R(),d=a;e&&G(e),t&&Ue(t),p&&s&&(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"),s.push(u,c)),p&&!s&&n(u),F(i,m);let h=o(_.createElement(d,{...l}));if(r){let k=r.setup?r.setup(r.userLib,g):r.userLib;return{...h,user:k}}return
|
|
6
|
+
`),L=async(e,t,n)=>{let s=e.find(H(t));if(!s)return n&&ye(t),ge();let{multipleResponses:o}=s;if(!o)return $(s);let r=o.find(a=>!a.hasBeenReturned);if(!r){n&&Re(s);return}return r.hasBeenReturned=!0,$(r)},F=(e=[],t=!1)=>{global.window.fetch.mockImplementation((s,o)=>{if(typeof s=="string"){let a=new Request(s,o);return L(e,a,t)}return L(e,s,t)})},Re=e=>{let t=`\u{1F32F} Wrapito: Missing response in the multipleResponses array for path ${e.path} and method ${e.method}.`,n=x.greenBright(t);console.warn(n)};var A,y=e=>{A={...A,...e}},R=()=>({...A});beforeEach(()=>{global.fetch=E()});afterEach(()=>{global.fetch.mockReset()});var Ze=e=>(y({Component:e,responses:[],props:{},path:"",hasPath:!1,interactionConfig:void 0,debug:process.env.npm_config_debugRequests==="true"}),b()),b=()=>{let e=xe();return{withProps:ke,withNetwork:Oe,withInteraction:Me,atPath:Te,debugRequests:Ee,mount:Pe,...e}},be=e=>{let t=R(),n=[...t.responses,...e];y({...t,responses:n})},Ie=(e,t)=>(t({addResponses:be},e),b()),we=(e,t)=>{let{extend:n}=f(),s=n[t];return{...e,[t]:(...o)=>Ie(o,s)}},xe=()=>{let{extend:e}=f();return Object.keys(e).reduce(we,{})},ke=e=>{let t=R();return y({...t,props:e}),b()},Me=e=>{let t=R();return y({...t,interactionConfig:e}),b()},Oe=(e=[])=>{let t=R(),n=Array.isArray(e)?e:[e];return y({...t,responses:[...t.responses,...n]}),b()},Te=(e,t)=>{let n=R();return y({...n,historyState:t,path:e,hasPath:!0}),b()},Ee=()=>{let e=R();return y({...e,debug:!0}),b()},Pe=()=>{let{portal:e,portals:t,changeRoute:n,history:s,mount:o,interaction:r}=f(),{Component:a,props:l,responses:i,path:u,hasPath:p,debug:m,historyState:c,interactionConfig:g}=R(),d=a;e&&G(e),t&&Ue(t),p&&s&&(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"),s.push(u,c)),p&&!s&&n(u),F(i,m);let h=o(_.createElement(d,{...l}));if(r){let k=r.setup?r.setup(r.userLib,g):r.userLib;return{...h,user:k}}return{...h,user:void 0}},G=e=>{if(document.getElementById(e))return;let t=document.createElement("div");t.setAttribute("id",e),t.setAttribute("data-testid",e),document.body.appendChild(t)},Ue=e=>{e.forEach(t=>{G(t)})};import{diff as qe}from"jest-diff";var C=(e,t)=>{let n=t?.host?`\u{1F32F} Wrapito: ${t?.host}${e} ain't got called`:`\u{1F32F} Wrapito: ${e} ain't got called`;return{pass:!1,message:()=>n}},V=(e,t,n)=>({pass:!1,message:()=>`\u{1F32F} Wrapito: ${e} is called ${n} times, you expected ${t} times`}),Q=(e,t)=>({pass:!1,message:()=>`\u{1F32F} Wrapito: Fetch method does not match, expected ${e} received ${t??"none"}`}),J=(e,t)=>{let n=t.map(s=>qe(e,s)).join(`
|
|
7
7
|
|
|
8
8
|
`);return{pass:!1,message:()=>`\u{1F32F} Wrapito: Fetch body does not match.
|
|
9
9
|
${n}`}},Y=(e,t)=>{let n=t.find(s=>e!==s);return{pass:!1,message:()=>`\u{1F32F} Wrapito: Host request does not match, expected ${e} received ${n}`}},z=()=>({pass:!1,message:()=>"\u{1F32F} Wrapito: Unable to find body."}),D=()=>({pass:!0,message:()=>"Test passing"}),K=(e,t)=>{let n=t?.host?`\u{1F32F} Wrapito: ${t.host}${e} is called`:`\u{1F32F} Wrapito: ${e} is called`;return{pass:!0,message:()=>n}};import We from"deep-equal";var X=()=>{let e=f().defaultHost;return e?.includes("http")?e:"https://default.com"},ve=(e="",t,n)=>t.includes(n)?t:e+t,P=e=>e instanceof Request,Ae=e=>P(e)?e.url:e,U=(e,t={method:"GET"})=>fetch.mock.calls.filter(([s])=>{let o=Ae(s),r=X(),a=new URL(o,r),l=ve(t?.host,e,r),i=t?.host||r,u=new URL(l,i),p=a.pathname===u.pathname,m=a.search===u.search,c=a.host===u.host;return u.search?p&&m:t?.host?p&&c:p}),Z=e=>e.flat(1).filter(P).map(t=>t.method),ee=e=>e.flat(1).filter(P).map(t=>t._bodyInit?JSON.parse(t._bodyInit):{}),te=e=>e.flat(1).filter(P).map(t=>new URL(t.url,X()).hostname),ne=(e,t)=>e&&!t.includes(e),se=(e,t)=>t.map(s=>We(e,s)).every(s=>s===!1),oe=(e,t)=>t.every(s=>s!==e),re=e=>e.length===0;var Ce=(e,t)=>{let n=U(e);if(re(n))return C(e);let s=Z(n),o=t?.method;if(ne(o,s))return Q(o,s);let r=ee(n),a=t?.body;if(!a)return z();if(se(a,r))return J(a,r);let l=te(n),i=t?.host;return i&&oe(i,l)?Y(i,l):D()},De=(e,t={method:"GET"})=>U(e,t).length?K(e,t):C(e,t),Se=(e,t,n={method:"GET"})=>{let s=U(e,n);return s.length!==t?V(e,t,s.length):D()},ct={toHaveBeenFetched:De,toHaveBeenFetchedWith:Ce,toHaveBeenFetchedTimes:Se};export{ct as assertions,Be as configure,f as getConfig,ct as matchers,Ze as wrap};
|