|
| 1 | +/* globals describe, it, expect, hot, cold, expectObservable, expectSubscriptions */ |
| 2 | +var Rx = require('../../dist/cjs/Rx'); |
| 3 | +var Observable = Rx.Observable; |
| 4 | + |
| 5 | +describe('Observable.prototype.pluck()', function () { |
| 6 | + it('should work for one object', function () { |
| 7 | + var a = cold('--x--|', {x: {prop: 42}}); |
| 8 | + var asubs = '^ !'; |
| 9 | + var expected = '--y--|'; |
| 10 | + |
| 11 | + var r = a.pluck('prop'); |
| 12 | + expectObservable(r).toBe(expected, {y: 42}); |
| 13 | + expectSubscriptions(a.subscriptions).toBe(asubs); |
| 14 | + }); |
| 15 | + |
| 16 | + it('should work for multiple objects', function () { |
| 17 | + var inputs = { |
| 18 | + a: {prop: '1'}, |
| 19 | + b: {prop: '2'}, |
| 20 | + c: {prop: '3'}, |
| 21 | + d: {prop: '4'}, |
| 22 | + e: {prop: '5'}, |
| 23 | + }; |
| 24 | + var a = cold('--a-b--c-d---e-|', inputs); |
| 25 | + var asubs = '^ !'; |
| 26 | + var expected = '--1-2--3-4---5-|'; |
| 27 | + |
| 28 | + var r = a.pluck('prop'); |
| 29 | + expectObservable(r).toBe(expected); |
| 30 | + expectSubscriptions(a.subscriptions).toBe(asubs); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should work with deep nested properties', function () { |
| 34 | + var inputs = { |
| 35 | + a: {a: {b: {c: '1'}}}, |
| 36 | + b: {a: {b: {c: '2'}}}, |
| 37 | + c: {a: {b: {c: '3'}}}, |
| 38 | + d: {a: {b: {c: '4'}}}, |
| 39 | + e: {a: {b: {c: '5'}}}, |
| 40 | + }; |
| 41 | + var a = cold('--a-b--c-d---e-|', inputs); |
| 42 | + var asubs = '^ !'; |
| 43 | + var expected = '--1-2--3-4---5-|'; |
| 44 | + |
| 45 | + var r = a.pluck('a', 'b', 'c'); |
| 46 | + expectObservable(r).toBe(expected); |
| 47 | + expectSubscriptions(a.subscriptions).toBe(asubs); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should work with edge cases of deep nested properties', function () { |
| 51 | + var inputs = { |
| 52 | + a: {a: {b: {c: 1}}}, |
| 53 | + b: {a: {b: 2}}, |
| 54 | + c: {a: {c: {c: 3}}}, |
| 55 | + d: {}, |
| 56 | + e: {a: {b: {c: 5}}}, |
| 57 | + }; |
| 58 | + var a = cold('--a-b--c-d---e-|', inputs); |
| 59 | + var asubs = '^ !'; |
| 60 | + var expected = '--r-x--y-z---w-|'; |
| 61 | + var values = {r: 1, x: undefined, y: undefined, z: undefined, w: 5}; |
| 62 | + |
| 63 | + var r = a.pluck('a', 'b', 'c'); |
| 64 | + expectObservable(r).toBe(expected, values); |
| 65 | + expectSubscriptions(a.subscriptions).toBe(asubs); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should throw an error if not property is passed', function () { |
| 69 | + expect(function () { |
| 70 | + Observable.of({prop: 1}, {prop: 2}).pluck(); |
| 71 | + }).toThrow(new Error('List of properties cannot be empty.')); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should propagate errors from observable that emits only errors', function () { |
| 75 | + var a = cold('#'); |
| 76 | + var asubs = '(^!)'; |
| 77 | + var expected = '#'; |
| 78 | + |
| 79 | + var r = a.pluck('whatever'); |
| 80 | + expectObservable(r).toBe(expected); |
| 81 | + expectSubscriptions(a.subscriptions).toBe(asubs); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should propagate errors from observable that emit values', function () { |
| 85 | + var a = cold('--a--b--#', {a: {prop: '1'}, b: {prop: '2'}}, 'too bad'); |
| 86 | + var asubs = '^ !'; |
| 87 | + var expected = '--1--2--#'; |
| 88 | + |
| 89 | + var r = a.pluck('prop'); |
| 90 | + expectObservable(r).toBe(expected, undefined, 'too bad'); |
| 91 | + expectSubscriptions(a.subscriptions).toBe(asubs); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should not pluck an empty observable', function () { |
| 95 | + var a = cold('|'); |
| 96 | + var asubs = '(^!)'; |
| 97 | + var expected = '|'; |
| 98 | + |
| 99 | + var invoked = 0; |
| 100 | + var r = a |
| 101 | + .pluck('whatever') |
| 102 | + .do(null, null, function () { |
| 103 | + expect(invoked).toBe(0); |
| 104 | + }); |
| 105 | + |
| 106 | + expectObservable(r).toBe(expected); |
| 107 | + expectSubscriptions(a.subscriptions).toBe(asubs); |
| 108 | + }); |
| 109 | + |
| 110 | + it('should allow unsubscribing explicitly and early', function () { |
| 111 | + var a = cold('--a--b--c--|', {a: {prop: '1'}, b: {prop: '2'}}); |
| 112 | + var unsub = ' ! '; |
| 113 | + var asubs = '^ ! '; |
| 114 | + var expected = '--1--2- '; |
| 115 | + |
| 116 | + var r = a.pluck('prop'); |
| 117 | + expectObservable(r, unsub).toBe(expected); |
| 118 | + expectSubscriptions(a.subscriptions).toBe(asubs); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should pluck twice', function () { |
| 122 | + var inputs = { |
| 123 | + a: {a: {b: {c: '1'}}}, |
| 124 | + b: {a: {b: {c: '2'}}}, |
| 125 | + c: {a: {b: {c: '3'}}}, |
| 126 | + d: {a: {b: {c: '4'}}}, |
| 127 | + e: {a: {b: {c: '5'}}}, |
| 128 | + }; |
| 129 | + var a = cold('--a-b--c-d---e-|', inputs); |
| 130 | + var asubs = '^ !'; |
| 131 | + var expected = '--1-2--3-4---5-|'; |
| 132 | + |
| 133 | + var r = a.pluck('a', 'b').pluck('c'); |
| 134 | + expectObservable(r).toBe(expected); |
| 135 | + expectSubscriptions(a.subscriptions).toBe(asubs); |
| 136 | + }); |
| 137 | + |
| 138 | + it('should not break unsubscription chain when unsubscribed explicitly', function () { |
| 139 | + var a = cold('--a--b--c--|', {a: {prop: '1'}, b: {prop: '2'}}); |
| 140 | + var unsub = ' ! '; |
| 141 | + var asubs = '^ ! '; |
| 142 | + var expected = '--1--2- '; |
| 143 | + |
| 144 | + var r = a |
| 145 | + .mergeMap(function (x) { return Observable.of(x); }) |
| 146 | + .pluck('prop') |
| 147 | + .mergeMap(function (x) { return Observable.of(x); }); |
| 148 | + |
| 149 | + expectObservable(r, unsub).toBe(expected); |
| 150 | + expectSubscriptions(a.subscriptions).toBe(asubs); |
| 151 | + }); |
| 152 | +}); |
0 commit comments