1
+ /* globals describe, it, expect, hot, cold, expectObservable */
2
+ var Rx = require ( '../../dist/cjs/Rx' ) ;
3
+ var Observable = Rx . Observable ;
4
+
5
+ describe ( 'Observable.prototype.elementAt' , function ( ) {
6
+ it ( "should return first element by zero-based index" , function ( ) {
7
+ var source = hot ( '--a--b--c--|' ) ;
8
+ var expected = '--(a|)' ;
9
+
10
+ expectObservable ( source . elementAt ( 0 ) ) . toBe ( expected ) ;
11
+ } ) ;
12
+
13
+ it ( "should return non-first element by zero-based index" , function ( ) {
14
+ var source = hot ( '--a--b--c--d--e--f--|' ) ;
15
+ var expected = '-----------(d|)' ;
16
+
17
+ expectObservable ( source . elementAt ( 3 ) ) . toBe ( expected ) ;
18
+ } ) ;
19
+
20
+ it ( "should return last element by zero-based index" , function ( ) {
21
+ var source = hot ( '--a--b--c--|' ) ;
22
+ var expected = '--------(c|)' ;
23
+
24
+ expectObservable ( source . elementAt ( 2 ) ) . toBe ( expected ) ;
25
+ } ) ;
26
+
27
+ it ( "should throw if index is smaller than zero" , function ( ) {
28
+ expect ( function ( ) { Observable . range ( 0 , 10 ) . elementAt ( - 1 ) ; } )
29
+ . toThrow ( new Rx . ArgumentOutOfRangeError ) ;
30
+ } ) ;
31
+
32
+ it ( "should raise error if index is out of range but does not have default value" , function ( ) {
33
+ var source = hot ( '--a--|' ) ;
34
+ var expected = '-----#' ;
35
+
36
+ expectObservable ( source . elementAt ( 3 ) )
37
+ . toBe ( expected , null , new Rx . ArgumentOutOfRangeError ) ;
38
+ } ) ;
39
+
40
+ it ( "should return default value if index is out of range" , function ( ) {
41
+ var source = hot ( '--a--|' ) ;
42
+ var expected = '-----(x|)' ;
43
+ var defaultValue = '42' ;
44
+
45
+ expectObservable ( source . elementAt ( 3 , defaultValue ) ) . toBe ( expected , { x : defaultValue } ) ;
46
+ } ) ;
47
+ } ) ;
0 commit comments