vitest-marbles
Version:
Marble testing helpers library for RxJs and Jest
54 lines • 2.27 kB
JavaScript
import { MarblesGlossary } from './marbles-glossary';
import { NotificationEvent } from './notification-event';
import { NotificationKindChars, ValueLiteral } from './notification-kind';
const frameStep = 10;
export class Marblizer {
static marblize(messages) {
const emissions = Marblizer.getNotificationEvents(messages);
let marbles = '';
for (let i = 0, prevEndFrame = 0; i < emissions.length; prevEndFrame = emissions[i].end, i++) {
marbles = `${marbles}${MarblesGlossary.TimeFrame.repeat(emissions[i].start - prevEndFrame) +
emissions[i].marbles}`;
}
return marbles;
}
static marblizeSubscriptions(logs) {
return logs.map(log => this.marblizeLogEntry(log.subscribedFrame / frameStep, MarblesGlossary.Subscription) +
this.marblizeLogEntry((log.unsubscribedFrame - log.subscribedFrame) / frameStep - 1, MarblesGlossary.Unsubscription));
}
static marblizeLogEntry(logPoint, symbol) {
if (logPoint !== Infinity) {
return MarblesGlossary.TimeFrame.repeat(logPoint) + symbol;
}
else {
return '';
}
}
static getNotificationEvents(messages) {
const framesToEmissions = messages.reduce((result, message) => {
if (!result[message.frame]) {
result[message.frame] = new NotificationEvent(message.frame / frameStep);
}
const event = result[message.frame];
event.marbles += Marblizer.extractMarble(message);
return result;
}, {});
const events = Object.keys(framesToEmissions).map(frame => framesToEmissions[frame]);
Marblizer.encloseGroupEvents(events);
return events;
}
static extractMarble(message) {
let marble = NotificationKindChars[message.notification.kind];
if (marble === ValueLiteral)
marble = message.notification.value;
return marble;
}
static encloseGroupEvents(events) {
events.forEach(event => {
if (event.marbles.length > 1) {
event.marbles = `${MarblesGlossary.GroupStart}${event.marbles}${MarblesGlossary.GroupEnd}`;
}
});
}
}
//# sourceMappingURL=marblizer.js.map