|
| 1 | +import { Observable } from "rxjs" |
| 2 | +import { map, scan } from "rxjs/operators" |
| 3 | +import { TestScheduler } from "rxjs/testing" |
| 4 | +import { combineKeys } from "./" |
| 5 | + |
| 6 | +const scheduler = () => |
| 7 | + new TestScheduler((actual, expected) => { |
| 8 | + expect(actual).toEqual(expected) |
| 9 | + }) |
| 10 | + |
| 11 | +describe("combineKeys", () => { |
| 12 | + it("emits a map with the latest value of the stream of each key", () => { |
| 13 | + scheduler().run(({ expectObservable, cold }) => { |
| 14 | + const keys = cold(" ab---cd---").pipe(scan((acc, v) => [...acc, v], [])) |
| 15 | + const a = cold(" --1---2---") |
| 16 | + const b = cold(" ---------") |
| 17 | + const c = cold(" 1----") |
| 18 | + const d = cold(" 9---") |
| 19 | + const expectedStr = "--e--f(gh)" |
| 20 | + |
| 21 | + const innerStreams = { a, b, c, d } |
| 22 | + |
| 23 | + const result = combineKeys( |
| 24 | + keys, |
| 25 | + (v): Observable<string> => innerStreams[v], |
| 26 | + ).pipe(map((x) => Object.fromEntries(x.entries()))) |
| 27 | + |
| 28 | + expectObservable(result).toBe(expectedStr, { |
| 29 | + e: { a: "1" }, |
| 30 | + f: { a: "1", c: "1" }, |
| 31 | + g: { a: "2", c: "1" }, |
| 32 | + h: { a: "2", c: "1", d: "9" }, |
| 33 | + }) |
| 34 | + }) |
| 35 | + }) |
| 36 | + |
| 37 | + it("removes the entry of a key when that key is removed from the source", () => { |
| 38 | + scheduler().run(({ expectObservable, cold }) => { |
| 39 | + const keys = cold(" a-b---c---").pipe(map((v) => [v])) |
| 40 | + const a = cold(" ----------") |
| 41 | + const b = cold(" -1-2-3-4") |
| 42 | + const c = cold(" -2-3") |
| 43 | + const expectedStr = "---e-fgh-i" |
| 44 | + |
| 45 | + const innerStreams = { a, b, c } |
| 46 | + |
| 47 | + const result = combineKeys( |
| 48 | + keys, |
| 49 | + (v): Observable<string> => innerStreams[v], |
| 50 | + ).pipe(map((x) => Object.fromEntries(x.entries()))) |
| 51 | + |
| 52 | + expectObservable(result).toBe(expectedStr, { |
| 53 | + e: { b: "1" }, |
| 54 | + f: { b: "2" }, |
| 55 | + g: {}, |
| 56 | + h: { c: "2" }, |
| 57 | + i: { c: "3" }, |
| 58 | + }) |
| 59 | + }) |
| 60 | + }) |
| 61 | + |
| 62 | + it("completes when the key stream completes", () => { |
| 63 | + scheduler().run(({ expectObservable, cold }) => { |
| 64 | + const keys = cold(" a-b---|").pipe(scan((acc, v) => [...acc, v], [])) |
| 65 | + const a = cold(" -1-----") |
| 66 | + const b = cold(" -1---") |
| 67 | + const expectedStr = "-e-f--|" |
| 68 | + |
| 69 | + const innerStreams = { a, b } |
| 70 | + |
| 71 | + const result = combineKeys( |
| 72 | + keys, |
| 73 | + (v): Observable<string> => innerStreams[v], |
| 74 | + ).pipe(map((x) => Object.fromEntries(x.entries()))) |
| 75 | + |
| 76 | + expectObservable(result).toBe(expectedStr, { |
| 77 | + e: { a: "1" }, |
| 78 | + f: { a: "1", b: "1" }, |
| 79 | + }) |
| 80 | + }) |
| 81 | + }) |
| 82 | + |
| 83 | + it("propagates errors from the inner streams", () => { |
| 84 | + scheduler().run(({ expectObservable, cold }) => { |
| 85 | + const keys = cold(" a-b---|").pipe(scan((acc, v) => [...acc, v], [])) |
| 86 | + const a = cold(" -1-----") |
| 87 | + const b = cold(" -#") |
| 88 | + const expectedStr = "-e-#" |
| 89 | + |
| 90 | + const innerStreams = { a, b } |
| 91 | + |
| 92 | + const result = combineKeys( |
| 93 | + keys, |
| 94 | + (v): Observable<string> => innerStreams[v], |
| 95 | + ).pipe(map((x) => Object.fromEntries(x.entries()))) |
| 96 | + |
| 97 | + expectObservable(result).toBe(expectedStr, { |
| 98 | + e: { a: "1" }, |
| 99 | + }) |
| 100 | + }) |
| 101 | + }) |
| 102 | + |
| 103 | + it("propagates errors from the key stream", () => { |
| 104 | + scheduler().run(({ expectObservable, cold }) => { |
| 105 | + const keys = cold(" a-b#").pipe(scan((acc, v) => [...acc, v], [])) |
| 106 | + const a = cold(" -1--") |
| 107 | + const b = cold(" -1") |
| 108 | + const expectedStr = "-e-#" |
| 109 | + |
| 110 | + const innerStreams = { a, b } |
| 111 | + |
| 112 | + const result = combineKeys( |
| 113 | + keys, |
| 114 | + (v): Observable<string> => innerStreams[v], |
| 115 | + ).pipe(map((x) => Object.fromEntries(x.entries()))) |
| 116 | + |
| 117 | + expectObservable(result).toBe(expectedStr, { |
| 118 | + e: { a: "1" }, |
| 119 | + }) |
| 120 | + }) |
| 121 | + }) |
| 122 | +}) |
0 commit comments