|
| 1 | +/* globals describe, it, expect, expectObservable, hot, cold */ |
| 2 | +var Rx = require('../../dist/cjs/Rx'); |
| 3 | + |
| 4 | +describe('Observable.prototype.first()', function() { |
| 5 | + it('should take the first value of an observable with one value', function() { |
| 6 | + var e1 = hot('---(a|)'); |
| 7 | + var expected = '---(a|)'; |
| 8 | + expectObservable(e1.first()).toBe(expected) |
| 9 | + }); |
| 10 | + |
| 11 | + it('should take the first value of an observable with many values', function() { |
| 12 | + var e1 = hot('--a--^--b----c---d--|'); |
| 13 | + var expected = '---(b|)'; |
| 14 | + expectObservable(e1.first()).toBe(expected) |
| 15 | + }); |
| 16 | + |
| 17 | + it('should error on empty', function() { |
| 18 | + var e1 = hot('--a--^----|'); |
| 19 | + var expected = '-----#'; |
| 20 | + expectObservable(e1.first()).toBe(expected, null, new Rx.EmptyError()); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should return the default value if source observable was empty', function() { |
| 24 | + var e1 = hot('-----^----|'); |
| 25 | + var expected = '-----(a|)'; |
| 26 | + expectObservable(e1.first(null, null, 'a')).toBe(expected); |
| 27 | + }); |
| 28 | + |
| 29 | + it('should propagate error from the source observable', function() { |
| 30 | + var e1 = hot('---^---#'); |
| 31 | + var expected = '----#'; |
| 32 | + expectObservable(e1.first()).toBe(expected) |
| 33 | + }); |
| 34 | + |
| 35 | + it('should go on forever on never', function() { |
| 36 | + var e2 = hot('--^-------'); |
| 37 | + var expected = '--------'; |
| 38 | + expectObservable(e2.first()).toBe(expected); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should return first value that matches a predicate', function() { |
| 42 | + var e1 = hot('--a-^--b--c--a--c--|'); |
| 43 | + var expected = '------(c|)'; |
| 44 | + var predicate = function (value) { |
| 45 | + return value === 'c'; |
| 46 | + }; |
| 47 | + expectObservable(e1.first(predicate)).toBe(expected); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should return first value that matches a predicate for odd numbers', function() { |
| 51 | + var e1 = hot('--a-^--b--c--d--e--|', {a: 1, b: 2, c: 3, d: 4, e: 5}); |
| 52 | + var expected = '------(c|)'; |
| 53 | + var predicate = function (value) { |
| 54 | + return value % 2 === 1; |
| 55 | + }; |
| 56 | + expectObservable(e1.first(predicate)).toBe(expected, {c: 3}); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should return first value that matches a predicate using thisArg', function() { |
| 60 | + var e1 = hot('--a-^--b--c--d--e--|', {a: 1, b: 2, c: 3, d: 4, e: 5}); |
| 61 | + var expected = '------(c|)'; |
| 62 | + var predicate = function (value) { |
| 63 | + expect(this).toEqual(42); |
| 64 | + return value % 2 === 1; |
| 65 | + }; |
| 66 | + expectObservable(e1.first(predicate, 42)).toBe(expected, {c: 3}); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should error when no value matches the predicate', function() { |
| 70 | + var e1 = hot('--a-^--b--c--a--c--|'); |
| 71 | + var expected = '---------------#'; |
| 72 | + var predicate = function (value) { |
| 73 | + return value === 's'; |
| 74 | + }; |
| 75 | + expectObservable(e1.first(predicate)).toBe(expected, null, new Rx.EmptyError()); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should return the default value when no value matches the predicate', function() { |
| 79 | + var e1 = hot('--a-^--b--c--a--c--|'); |
| 80 | + var expected = '---------------(d|)'; |
| 81 | + var predicate = function (value) { |
| 82 | + return value === 's'; |
| 83 | + }; |
| 84 | + expectObservable(e1.first(predicate, null, 'd')).toBe(expected); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should propagate error when no value matches the predicate', function() { |
| 88 | + var e1 = hot('--a-^--b--c--a--#'); |
| 89 | + var expected = '------------#'; |
| 90 | + var predicate = function (value) { |
| 91 | + return value === 's'; |
| 92 | + }; |
| 93 | + expectObservable(e1.first(predicate)).toBe(expected); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should return first value that matches the index in the predicate', function() { |
| 97 | + var e1 = hot('--a-^--b--c--a--c--|'); |
| 98 | + var expected = '---------(a|)'; |
| 99 | + var predicate = function (value, index) { |
| 100 | + return index === 2; |
| 101 | + }; |
| 102 | + expectObservable(e1.first(predicate)).toBe(expected); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should propagate error from predicate', function() { |
| 106 | + var e1 = hot('--a-^--b--c--d--e--|', {a: 1, b: 2, c: 3, d: 4, e: 5}); |
| 107 | + var expected = '---------#'; |
| 108 | + var predicate = function (value) { |
| 109 | + if (value < 4) { |
| 110 | + return false; |
| 111 | + } else { |
| 112 | + throw 'error'; |
| 113 | + } |
| 114 | + }; |
| 115 | + expectObservable(e1.first(predicate)).toBe(expected, null, 'error'); |
| 116 | + }); |
| 117 | +}); |
0 commit comments