|
| 1 | +//declare const {rxTestScheduler, time, hot, cold, asDiagram, expectObservable, expectSubscriptions, type}; |
| 2 | + |
| 3 | +const booleans = { T: true, F: false }; |
| 4 | + |
| 5 | +/** @test {sequenceEqual} */ |
| 6 | +describe('Observable.prototype.sequenceEqual', () => { |
| 7 | + it('should return true for two equal sequences', () => { |
| 8 | + const s1 = hot('--a--^--b--c--d--e--f--g--|'); |
| 9 | + const s1subs = '^ !'; |
| 10 | + const s2 = hot('-----^-----b--c--d-e-f------g-|'); |
| 11 | + const s2subs = '^ !'; |
| 12 | + const expected = '-------------------------(T|)'; |
| 13 | + |
| 14 | + const source = s1.sequenceEqual(s2); |
| 15 | + |
| 16 | + expectObservable(source).toBe(expected, booleans); |
| 17 | + expectSubscriptions(s1.subscriptions).toBe(s1subs); |
| 18 | + expectSubscriptions(s2.subscriptions).toBe(s2subs); |
| 19 | + }); |
| 20 | + |
| 21 | + it('should error if the comparor errors', () => { |
| 22 | + const s1 = hot('--a--^--b-----c------d--|'); |
| 23 | + const s1subs = '^ !'; |
| 24 | + const s2 = hot('-----^--------x---y---z-------|'); |
| 25 | + const s2subs = '^ !'; |
| 26 | + const expected = '-------------#'; |
| 27 | + |
| 28 | + let i = 0; |
| 29 | + const source = s1.sequenceEqual(s2, (a: any, b: any) => { |
| 30 | + if (++i === 2) { |
| 31 | + throw new Error('shazbot'); |
| 32 | + } |
| 33 | + return a.value === b.value; |
| 34 | + }); |
| 35 | + |
| 36 | + const values = { |
| 37 | + a: null, |
| 38 | + b: { value: 'bees knees' }, |
| 39 | + c: { value: 'carpy dumb' }, |
| 40 | + d: { value: 'derp' }, |
| 41 | + x: { value: 'bees knees', foo: 'lol' }, |
| 42 | + y: { value: 'carpy dumb', scooby: 'doo' }, |
| 43 | + z: { value: 'derp', weCouldBe: 'dancin, yeah' } |
| 44 | + }; |
| 45 | + |
| 46 | + expectObservable(source).toBe(expected, Object.assign({}, booleans, values), new Error('shazbot')); |
| 47 | + expectSubscriptions(s1.subscriptions).toBe(s1subs); |
| 48 | + expectSubscriptions(s2.subscriptions).toBe(s2subs); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should use the provided comparor', () => { |
| 52 | + const s1 = hot('--a--^--b-----c------d--|'); |
| 53 | + const s1subs = '^ !'; |
| 54 | + const s2 = hot('-----^--------x---y---z-------|'); |
| 55 | + const s2subs = '^ !'; |
| 56 | + const expected = '-------------------------(T|)'; |
| 57 | + |
| 58 | + const source = s1.sequenceEqual(s2, (a: any, b: any) => a.value === b.value); |
| 59 | + |
| 60 | + const values = { |
| 61 | + a: null, |
| 62 | + b: { value: 'bees knees' }, |
| 63 | + c: { value: 'carpy dumb' }, |
| 64 | + d: { value: 'derp' }, |
| 65 | + x: { value: 'bees knees', foo: 'lol' }, |
| 66 | + y: { value: 'carpy dumb', scooby: 'doo' }, |
| 67 | + z: { value: 'derp', weCouldBe: 'dancin, yeah' } |
| 68 | + }; |
| 69 | + |
| 70 | + expectObservable(source).toBe(expected, Object.assign({}, booleans, values)); |
| 71 | + expectSubscriptions(s1.subscriptions).toBe(s1subs); |
| 72 | + expectSubscriptions(s2.subscriptions).toBe(s2subs); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should return false for two unequal sequences, compareTo finishing last', () => { |
| 76 | + const s1 = hot('--a--^--b--c--d--e--f--g--|'); |
| 77 | + const s1subs = '^ !'; |
| 78 | + const s2 = hot('-----^-----b--c--d-e-f------z-|'); |
| 79 | + const s2subs = '^ !'; |
| 80 | + const expected = '-----------------------(F|)'; |
| 81 | + |
| 82 | + const source = s1.sequenceEqual(s2); |
| 83 | + |
| 84 | + expectObservable(source).toBe(expected, booleans); |
| 85 | + expectSubscriptions(s1.subscriptions).toBe(s1subs); |
| 86 | + expectSubscriptions(s2.subscriptions).toBe(s2subs); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should return false for two unequal sequences, early wrong value from source', () => { |
| 90 | + const s1 = hot('--a--^--b--c---x-----------|'); |
| 91 | + const s1subs = '^ !'; |
| 92 | + const s2 = hot('-----^--b--c--d--e--f--|'); |
| 93 | + const s2subs = '^ !'; |
| 94 | + const expected = '----------(F|)'; |
| 95 | + |
| 96 | + const source = s1.sequenceEqual(s2); |
| 97 | + |
| 98 | + expectObservable(source).toBe(expected, booleans); |
| 99 | + expectSubscriptions(s1.subscriptions).toBe(s1subs); |
| 100 | + expectSubscriptions(s2.subscriptions).toBe(s2subs); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should return false when the source emits an extra value after the compareTo completes', () => { |
| 104 | + const s1 = hot('--a--^--b--c--d--e--f--g--h--|'); |
| 105 | + const s1subs = '^ !'; |
| 106 | + const s2 = hot('-----^--b--c--d-|'); |
| 107 | + const s2subs = '^ !'; |
| 108 | + const expected = '------------(F|)'; |
| 109 | + |
| 110 | + const source = s1.sequenceEqual(s2); |
| 111 | + |
| 112 | + expectObservable(source).toBe(expected, booleans); |
| 113 | + expectSubscriptions(s1.subscriptions).toBe(s1subs); |
| 114 | + expectSubscriptions(s2.subscriptions).toBe(s2subs); |
| 115 | + }); |
| 116 | + |
| 117 | + it('should return false when the compareTo emits an extra value after the source completes', () => { |
| 118 | + const s1 = hot('--a--^--b--c--d-|'); |
| 119 | + const s1subs = '^ !'; |
| 120 | + const s2 = hot('-----^--b--c--d--e--f--g--h--|'); |
| 121 | + const s2subs = '^ !'; |
| 122 | + const expected = '------------(F|)'; |
| 123 | + |
| 124 | + const source = s1.sequenceEqual(s2); |
| 125 | + |
| 126 | + expectObservable(source).toBe(expected, booleans); |
| 127 | + expectSubscriptions(s1.subscriptions).toBe(s1subs); |
| 128 | + expectSubscriptions(s2.subscriptions).toBe(s2subs); |
| 129 | + }); |
| 130 | + |
| 131 | + it('should return true for two empty observables', () => { |
| 132 | + const s1 = cold('|'); |
| 133 | + const s2 = cold('|'); |
| 134 | + const expected = '(T|)'; |
| 135 | + |
| 136 | + const source = s1.sequenceEqual(s2); |
| 137 | + expectObservable(source).toBe(expected, booleans); |
| 138 | + }); |
| 139 | + |
| 140 | + it('should return false for an empty observable and an observable that emits', () => { |
| 141 | + const s1 = cold('|'); |
| 142 | + const s2 = cold('---a--|'); |
| 143 | + const expected = '---(F|)'; |
| 144 | + |
| 145 | + const source = s1.sequenceEqual(s2); |
| 146 | + expectObservable(source).toBe(expected, booleans); |
| 147 | + }); |
| 148 | + |
| 149 | + it('should return compare hot and cold observables', () => { |
| 150 | + const s1 = hot('---a--^---b---c---d---e---f---g---h---i---j---|'); |
| 151 | + const s2 = cold( '----b---c-|'); |
| 152 | + const expected1 = '------------(F|)'; |
| 153 | + const subs1 = '^ !'; |
| 154 | + const delay = '-------------------|'; |
| 155 | + const s3 = cold( '-f---g---h---i---j---|'); |
| 156 | + const expected2 = ' ---------------------(T|)'; |
| 157 | + const subs2 = ' ^ !'; |
| 158 | + |
| 159 | + const test1 = s1.sequenceEqual(s2); |
| 160 | + const test2 = s1.sequenceEqual(s3); |
| 161 | + |
| 162 | + expectObservable(test1).toBe(expected1, booleans); |
| 163 | + rxTestScheduler.schedule(() => expectObservable(test2).toBe(expected2, booleans), time(delay)); |
| 164 | + expectSubscriptions(s2.subscriptions).toBe(subs1); |
| 165 | + expectSubscriptions(s3.subscriptions).toBe(subs2); |
| 166 | + }); |
| 167 | +}); |
0 commit comments