vitest-marbles
Version:
Marble testing helpers library for RxJs and Jest
2 lines • 4.52 kB
JavaScript
(()=>{"use strict";rxjs;const t=rxjs/testing;var e;!function(t){t.Completion="|",t.Error="#",t.TimeFrame="-",t.Subscription="^",t.Unsubscription="!",t.GroupStart="(",t.GroupEnd=")"}(e||(e={}));class i{constructor(t){this.start=t,this.marbles=""}get end(){return this.start+this.marbles.length}}const n={},s={N:n,C:e.Completion,E:e.Error};class r{static marblize(t){const i=r.getNotificationEvents(t);let n="";for(let t=0,s=0;t<i.length;s=i[t].end,t++)n=`${n}${e.TimeFrame.repeat(i[t].start-s)+i[t].marbles}`;return n}static marblizeSubscriptions(t){return t.map((t=>this.marblizeLogEntry(t.subscribedFrame/10,e.Subscription)+this.marblizeLogEntry((t.unsubscribedFrame-t.subscribedFrame)/10-1,e.Unsubscription)))}static marblizeLogEntry(t,i){return t!==1/0?e.TimeFrame.repeat(t)+i:""}static getNotificationEvents(t){const e=t.reduce(((t,e)=>(t[e.frame]||(t[e.frame]=new i(e.frame/10)),t[e.frame].marbles+=r.extractMarble(e),t)),{}),n=Object.keys(e).map((t=>e[t]));return r.encloseGroupEvents(n),n}static extractMarble(t){let e=s[t.notification.kind];return e===n&&(e=t.notification.value),e}static encloseGroupEvents(t){t.forEach((t=>{t.marbles.length>1&&(t.marbles=`${e.GroupStart}${t.marbles}${e.GroupEnd}`)}))}}function o({notification:t}){const e=t.value;return"string"==typeof e&&1===e.length||void 0!==e&&1===JSON.stringify(e).length}const a={toBeNotifications(t,e){let i=t,n=e;(function(...t){return t.every((t=>t.filter((({notification:{kind:t}})=>"N"===t)).every(o)))})(t,e)&&(i=r.marblize(t),n=r.marblize(e));const s=this.equals(i,n);return{message:s?()=>this.utils.matcherHint(".not.toBeNotifications")+"\n\nExpected notifications to not be:\n"+` ${this.utils.printExpected(n)}\nBut got:\n`+` ${this.utils.printReceived(i)}`:()=>{const t=this.utils.diff(n,i,{expand:!0});return this.utils.matcherHint(".toBeNotifications")+"\n\nExpected notifications to be:\n"+` ${this.utils.printExpected(n)}\nBut got:\n`+` ${this.utils.printReceived(i)}`+(t?`\n\nDifference:\n\n${t}`:"")},pass:s}},toBeSubscriptions(t,e){const i=r.marblizeSubscriptions(t),n=r.marblizeSubscriptions(e),s=function(t,e){if(t.length!==e.length)return!1;let i=!0;for(const n of t)if(!e.includes(n)){i=!1;break}return i}(i,n);return{message:s?()=>this.utils.matcherHint(".not.toHaveSubscriptions")+"\n\nExpected observable to not have the following subscription points:\n"+` ${this.utils.printExpected(n)}\nBut got:\n`+` ${this.utils.printReceived(i)}`:()=>{const t=this.utils.diff(n,i,{expand:!0});return this.utils.matcherHint(".toHaveSubscriptions")+"\n\nExpected observable to have the following subscription points:\n"+` ${this.utils.printExpected(n)}\nBut got:\n`+` ${this.utils.printReceived(i)}`+(t?`\n\nDifference:\n\n${t}`:"")},pass:s}},toHaveEmptySubscriptions(t){const e=!(t&&t.length>0);let i;return t&&t.length>0&&(i=r.marblizeSubscriptions(t)),{message:e?()=>this.utils.matcherHint(".not.toHaveNoSubscriptions")+"\n\nExpected observable to have at least one subscription point, but got nothing"+this.utils.printReceived(""):()=>this.utils.matcherHint(".toHaveNoSubscriptions")+"\n\nExpected observable to have no subscription points\nBut got:\n"+` ${this.utils.printReceived(i)}\n\n`,pass:e}}};function c(t,e){e&&(function(t,e){return 0===e.length&&0!==t.length&&void 0!==t[0].subscribedFrame}(t,e)?expect(t).toHaveEmptySubscriptions():function(t,e){return 0===t.length&&0===e.length||0!==e.length&&void 0!==e[0].subscribedFrame}(t,e)?expect(t).toBeSubscriptions(e):expect(t).toBeNotifications(e))}expect.extend(a);class u{static init(){u.instance=new t.TestScheduler(c)}static get(){if(u.instance)return u.instance;throw new Error("Scheduler is not initialized")}static reset(){u.instance=null}static materializeInnerObservable(t,e){return u.get().materializeInnerObservable(t,e)}}function l(t){return t.replace(/^[ ]+/,"")}const p={message:()=>"",pass:!0};expect.extend({toHaveSubscriptions(t,e){const i=Array.isArray(e)?e.map(l):l(e);return u.get().expectSubscriptions(t.getSubscriptions()).toBe(i),p},toHaveNoSubscriptions:t=>(u.get().expectSubscriptions(t.getSubscriptions()).toBe([]),p),toBeObservable:(t,e)=>(u.get().expectObservable(t).toBe(e.marbles,e.values,e.error),p),toBeMarble:(t,e)=>(u.get().expectObservable(t).toBe(l(e)),p),toSatisfyOnFlush(t,e){u.get().expectObservable(t);const i=u.get().flushTests;return i[i.length-1].ready=!0,b.push(e),p}});let b=[];beforeEach((()=>{u.init(),b=[]})),afterEach((()=>{for(u.get().flush();b.length>0;)b.shift()();u.reset()}))})();
//# sourceMappingURL=index.min.js.map