UNPKG

swirly-diagram-testrunner

Version:

Testrunner that runs observables and prepares diagram generation for swirly

75 lines 3.54 kB
import { TestScheduler } from "rxjs/testing"; import { createOperatorSpecification, createStreamSpecification } from "@swirly/parser"; import { styles as defaultStyles } from "@swirly/theme-default-light"; import { assert } from "chai"; import * as util from "util"; export class DiagramTestScheduler extends TestScheduler { constructor() { super((actual, expected) => { if (Array.isArray(actual) && Array.isArray(expected)) { this.outputStreams.push({ messages: actual.map(m => scaleTestMessageTime(m, 10)), subscription: { start: 0, end: "100%" } }); const failureMessage = `\nExpected\n${actual.map(a => `\t${JSON.stringify(a)}`).join("\n")}\t\nto deep equal\n${expected.map(e => `\t${JSON.stringify(e)}`).join("\n")}`; assert.deepEqual(deleteErrorNotificationStack(actual), deleteErrorNotificationStack(expected), failureMessage); } else { assert.deepEqual(actual, expected); } }); this.inputStreams = []; this.outputStreams = []; } runAsDiagram(operatorTitle, fn, styles = defaultStyles) { return this.run(helpers => { fn(helpers); this.inputStreams = this.getHotTestStream().concat(this.getColdTestStream()); this.flush(); this.inputStreams = updateInputStreamsPostFlush(this.inputStreams); const inputs = this.inputStreams.map(s => createStreamSpecification(s.messages.map(stringifyTestMessage))); const operator = createOperatorSpecification(operatorTitle); const outputs = this.outputStreams.map(s => createStreamSpecification(s.messages.map(stringifyTestMessage))); return { content: [...inputs, operator, ...outputs], styles }; }); } getHotTestStream() { return this.hotObservables.map(h => ({ messages: h.messages.map(m => scaleTestMessageTime(m, 10)), subscription: { start: 0, end: "100%" }, })); } getColdTestStream() { return this.coldObservables.map(c => ({ messages: c.messages.map(m => scaleTestMessageTime(m, 10)), cold: c })); } } export function scaleTestMessageTime(testMessage, factor) { return Object.assign(Object.assign({}, testMessage), { frame: testMessage.frame * factor }); } export function updateInputStreamsPostFlush(inputStreams) { return inputStreams.map(singleInputStream => { if (singleInputStream.cold && singleInputStream.cold.subscriptions.length) { singleInputStream.subscription = { start: singleInputStream.cold.subscriptions[0].subscribedFrame, end: singleInputStream.cold.subscriptions[0].unsubscribedFrame, }; } return singleInputStream; }); } export function deleteErrorNotificationStack(marble) { const { notification } = marble; if (notification) { const { kind, error } = notification; if (kind === "E" && error instanceof Error) { notification.error = { name: error.name, message: error.message }; } } return marble; } export function stringifyTestMessage(message) { return message.isGhost || !message.notification.hasValue ? message : Object.assign(Object.assign({}, message), { notification: Object.assign(Object.assign({}, message.notification), { value: util.inspect(message.notification.value) }) }); } //# sourceMappingURL=diagram-test-scheduler.js.map