test(mergeWithKey): add tests to mergeWithKey

This commit is contained in:
Víctor Oliva 2020-07-16 20:36:54 +02:00 committed by Josep M Sobrepere
parent 13a3de4ae7
commit 367128f238
2 changed files with 40 additions and 0 deletions

View File

@ -0,0 +1,34 @@
import { map } from "rxjs/operators"
import { TestScheduler } from "rxjs/testing"
import { mergeWithKey } from "./mergeWithKey"
const scheduler = () =>
new TestScheduler((actual, expected) => {
expect(actual).toEqual(expected)
})
describe("mergeWithKey", () => {
it("emits the key of the stream that emitted the value", () => {
scheduler().run(({ expectObservable, cold }) => {
const sourceA = cold("a---b---|");
const sourceB = cold("-1--2----3|")
const expected = " mn--(op)-q|"
const result = mergeWithKey({
strings: sourceA,
numbers: sourceB
}).pipe(
map(({ type, payload }) => `${type}:${payload}`),
)
expectObservable(result).toBe(expected, {
m: "strings:a",
n: "numbers:1",
o: "strings:b",
p: "numbers:2",
q: "numbers:3",
})
})
})
})

View File

@ -1,6 +1,12 @@
import { merge, Observable, ObservableInput, from, SchedulerLike } from "rxjs"
import { map } from "rxjs/operators"
/**
* Emits the values from all the streams of the provided object, in a result
* which provides the key of the stream of that emission.
*
* @param input object of streams
*/
export const mergeWithKey: <
O extends { [P in keyof any]: ObservableInput<any> },
OT extends {