UNPKG

@miller-svt/jest-marbles

Version:

Marble testing helpers library for RxJs and Jest

2 lines 4.58 kB
(()=>{"use strict";const t=rxjs,e=rxjs/testing;var i;!function(t){t.Completion="|",t.Error="#",t.TimeFrame="-",t.Subscription="^",t.Unsubscription="!",t.GroupStart="(",t.GroupEnd=")"}(i||(i={}));class n{constructor(t){this.start=t,this.marbles=""}get end(){return this.start+this.marbles.length}}const s={},r={N:s,C:i.Completion,E:i.Error};class o{static marblize(t){const e=o.getNotificationEvents(t);let n="";for(let t=0,s=0;t<e.length;s=e[t].end,t++)n=`${n}${i.TimeFrame.repeat(e[t].start-s)+e[t].marbles}`;return n}static marblizeSubscriptions(t){return t.map((t=>this.marblizeLogEntry(t.subscribedFrame/10,i.Subscription)+this.marblizeLogEntry((t.unsubscribedFrame-t.subscribedFrame)/10-1,i.Unsubscription)))}static marblizeLogEntry(t,e){return t!==1/0?i.TimeFrame.repeat(t)+e:""}static getNotificationEvents(t){const e=t.reduce(((t,e)=>(t[e.frame]||(t[e.frame]=new n(e.frame/10)),t[e.frame].marbles+=o.extractMarble(e),t)),{}),i=Object.keys(e).map((t=>e[t]));return o.encloseGroupEvents(i),i}static extractMarble(t){let e=r[t.notification.kind];return e===s&&(e=t.notification.value),e}static encloseGroupEvents(t){t.forEach((t=>{t.marbles.length>1&&(t.marbles=`${i.GroupStart}${t.marbles}${i.GroupEnd}`)}))}}function a(t){return t.every((({notification:t})=>{return"C"===t.kind||"E"===t.kind&&"error"===t.error||"N"===t.kind&&("string"==typeof(e=t.value)&&1===e.length||void 0!==e&&1===JSON.stringify(e).length);var e}))}const c={toBeNotifications(t,e){let i=t,n=e;(function(...t){return t.every(a)})(t,e)&&(i=o.marblize(t),n=o.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=o.marblizeSubscriptions(t),n=o.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=o.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 u(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(c);class l{static init(){l.instance=new e.TestScheduler(u)}static get(){if(l.instance)return l.instance;throw new Error("Scheduler is not initialized")}static reset(){l.instance=null}static materializeInnerObservable(t,e){return l.get().materializeInnerObservable(t,e)}}function b(t){return t.replace(/^[ ]+/,"")}t.Observable,t.Observable;const p={message:()=>"",pass:!0};expect.extend({toHaveSubscriptions(t,e){const i=Array.isArray(e)?e.map(b):b(e);return l.get().expectSubscriptions(t.getSubscriptions()).toBe(i),p},toHaveNoSubscriptions:t=>(l.get().expectSubscriptions(t.getSubscriptions()).toBe([]),p),toBeObservable:(t,e)=>(l.get().expectObservable(t).toBe(e.marbles,e.values,e.error),p),toBeMarble:(t,e)=>(l.get().expectObservable(t).toBe(b(e)),p),toSatisfyOnFlush(t,e){l.get().expectObservable(t);const i=l.get().flushTests;return i[i.length-1].ready=!0,h.push(e),p}});let h=[];beforeEach((()=>{l.init(),h=[]})),afterEach((()=>{for(l.get().run((()=>{}));h.length>0;)h.shift()?.();l.reset()}))})(); //# sourceMappingURL=index.min.js.map