4
4
jest . autoMockOff ( ) ;
5
5
6
6
import Immutable = require( 'immutable' ) ;
7
+ import Map = Immutable . Map ;
7
8
8
9
jasmine . getEnv ( ) . addEqualityTester ( ( a , b ) =>
9
10
a instanceof Immutable . Sequence && b instanceof Immutable . Sequence ?
@@ -19,25 +20,46 @@ describe('Cursor', () => {
19
20
var data = Immutable . fromJS ( json ) ;
20
21
var cursor = data . cursor ( ) ;
21
22
22
- expect ( cursor . deref ( ) ) . toBe ( data ) ;
23
+ expect ( Immutable . unCursor ( cursor ) ) . toBe ( data ) ;
23
24
24
25
var deepCursor = cursor . cursor ( [ 'a' , 'b' ] ) ;
25
- expect ( deepCursor . deref ( ) . toJS ( ) ) . toEqual ( json . a . b ) ;
26
- expect ( deepCursor . deref ( ) ) . toBe ( data . getIn ( [ 'a' , 'b' ] ) ) ;
26
+ expect ( deepCursor . toJS ( ) ) . toEqual ( json . a . b ) ;
27
+ expect ( deepCursor ) . toEqual ( data . getIn ( [ 'a' , 'b' ] ) ) ;
27
28
expect ( deepCursor . get ( 'c' ) ) . toBe ( 1 ) ;
28
29
29
30
var leafCursor = deepCursor . cursor ( 'c' ) ;
30
- expect ( leafCursor . deref ( ) ) . toBe ( 1 ) ;
31
+ expect ( leafCursor ) . toBe ( 1 ) ;
31
32
32
- var missCursor = leafCursor . cursor ( 'd' ) ;
33
- expect ( missCursor . deref ( ) ) . toBe ( undefined ) ;
33
+ var missCursor = deepCursor . cursor ( 'd' ) ;
34
+ expect ( Immutable . is ( missCursor , Map . empty ( ) ) ) . toBe ( true ) ;
35
+ expect ( Immutable . unCursor ( missCursor ) ) . toEqual ( Map . empty ( ) ) ;
36
+ } ) ;
37
+
38
+ it ( 'appears to be the type it points to' , ( ) => {
39
+ var data = Immutable . fromJS ( { a :[ 1 , 2 , 3 ] } ) ;
40
+ var cursor = data . cursor ( ) ;
41
+ var aCursor = cursor . cursor ( 'a' ) ;
42
+ expect ( cursor instanceof Immutable . Map ) . toBe ( true ) ;
43
+ expect ( aCursor instanceof Immutable . Vector ) . toBe ( true ) ;
44
+ expect (
45
+ aCursor . update ( ( ) => Immutable . Set . empty ( ) ) instanceof Immutable . Set
46
+ ) . toBe ( true ) ;
47
+ expect (
48
+ aCursor . update ( ( ) => Immutable . Stack . empty ( ) ) instanceof Immutable . Stack
49
+ ) . toBe ( true ) ;
50
+ } ) ;
51
+
52
+ it ( 'can detect cursors' , ( ) => {
53
+ var data = Immutable . fromJS ( json ) ;
54
+ expect ( Immutable . isCursor ( data . get ( 'a' ) ) ) . toBe ( false ) ;
55
+ expect ( Immutable . isCursor ( data . cursor ( 'a' ) ) ) . toBe ( true ) ;
34
56
} ) ;
35
57
36
58
it ( 'gets return new cursors' , ( ) => {
37
59
var data = Immutable . fromJS ( json ) ;
38
60
var cursor = data . cursor ( ) ;
39
61
var deepCursor = cursor . getIn ( [ 'a' , 'b' ] ) ;
40
- expect ( deepCursor . deref ( ) ) . toBe ( data . getIn ( [ 'a' , 'b' ] ) ) ;
62
+ expect ( Immutable . unCursor ( deepCursor ) ) . toBe ( data . getIn ( [ 'a' , 'b' ] ) ) ;
41
63
} ) ;
42
64
43
65
it ( 'can be treated as a value' , ( ) => {
@@ -49,12 +71,10 @@ describe('Cursor', () => {
49
71
expect ( cursor . get ( 'c' ) ) . toBe ( 1 ) ;
50
72
} ) ;
51
73
52
- it ( 'can be value compared to a primitive ' , ( ) => {
74
+ it ( 'returns scalars directly ' , ( ) => {
53
75
var data = Immutable . Map ( { a : 'A' } ) ;
54
76
var aCursor = data . cursor ( 'a' ) ;
55
- expect ( aCursor . length ) . toBe ( null ) ;
56
- expect ( aCursor . deref ( ) ) . toBe ( 'A' ) ;
57
- expect ( Immutable . is ( aCursor , 'A' ) ) . toBe ( true ) ;
77
+ expect ( aCursor ) . toBe ( 'A' ) ;
58
78
} ) ;
59
79
60
80
it ( 'updates at its path' , ( ) => {
@@ -63,20 +83,21 @@ describe('Cursor', () => {
63
83
var data = Immutable . fromJS ( json ) ;
64
84
var aCursor = data . cursor ( 'a' , onChange ) ;
65
85
66
- var deepCursor = aCursor . cursor ( [ 'b' , 'c' ] ) ;
67
- expect ( deepCursor . deref ( ) ) . toBe ( 1 ) ;
86
+ var deepCursor = aCursor . cursor ( 'b' ) ;
87
+ expect ( deepCursor . get ( 'c' ) ) . toBe ( 1 ) ;
68
88
69
89
// cursor edits return new cursors:
70
- var newDeepCursor = deepCursor . update ( x => x + 1 ) ;
71
- expect ( newDeepCursor . deref ( ) ) . toBe ( 2 ) ;
90
+ var newDeepCursor = deepCursor . update ( 'c' , x => x + 1 ) ;
91
+ expect ( newDeepCursor . get ( 'c' ) ) . toBe ( 2 ) ;
92
+
72
93
expect ( onChange ) . lastCalledWith (
73
94
Immutable . fromJS ( { a :{ b :{ c :2 } } } ) ,
74
95
data ,
75
96
[ 'a' , 'b' , 'c' ]
76
97
) ;
77
98
78
- var newestDeepCursor = newDeepCursor . update ( x => x + 1 ) ;
79
- expect ( newestDeepCursor . deref ( ) ) . toBe ( 3 ) ;
99
+ var newestDeepCursor = newDeepCursor . update ( 'c' , x => x + 1 ) ;
100
+ expect ( newestDeepCursor . get ( 'c' ) ) . toBe ( 3 ) ;
80
101
expect ( onChange ) . lastCalledWith (
81
102
Immutable . fromJS ( { a :{ b :{ c :3 } } } ) ,
82
103
Immutable . fromJS ( { a :{ b :{ c :2 } } } ) ,
@@ -87,9 +108,9 @@ describe('Cursor', () => {
87
108
expect ( data . toJS ( ) ) . toEqual ( json ) ;
88
109
89
110
// as is the original cursor.
90
- expect ( deepCursor . deref ( ) ) . toBe ( 1 ) ;
91
- var otherNewDeepCursor = deepCursor . update ( x => x + 10 ) ;
92
- expect ( otherNewDeepCursor . deref ( ) ) . toBe ( 11 ) ;
111
+ expect ( deepCursor . get ( 'c' ) ) . toBe ( 1 ) ;
112
+ var otherNewDeepCursor = deepCursor . update ( 'c' , x => x + 10 ) ;
113
+ expect ( otherNewDeepCursor . get ( 'c' ) ) . toBe ( 11 ) ;
93
114
expect ( onChange ) . lastCalledWith (
94
115
Immutable . fromJS ( { a :{ b :{ c :11 } } } ) ,
95
116
data ,
@@ -108,7 +129,7 @@ describe('Cursor', () => {
108
129
var bCursor = aCursor . cursor ( 'b' ) ;
109
130
var cCursor = bCursor . cursor ( 'c' ) ;
110
131
111
- expect ( bCursor . set ( 'c' , 10 ) . deref ( ) ) . toEqual (
132
+ expect ( bCursor . set ( 'c' , 10 ) ) . toEqual (
112
133
Immutable . fromJS ( { c : 10 } )
113
134
) ;
114
135
expect ( onChange ) . lastCalledWith (
@@ -121,9 +142,9 @@ describe('Cursor', () => {
121
142
it ( 'creates maps as necessary' , ( ) => {
122
143
var data = Immutable . Map ( ) ;
123
144
var cursor = data . cursor ( [ 'a' , 'b' , 'c' ] ) ;
124
- expect ( cursor . deref ( ) ) . toBe ( undefined ) ;
145
+ expect ( cursor ) . toEqual ( Map . empty ( ) ) ;
125
146
cursor = cursor . set ( 'd' , 3 ) ;
126
- expect ( cursor . deref ( ) ) . toEqual ( Immutable . Map ( { d : 3 } ) ) ;
147
+ expect ( cursor ) . toEqual ( Immutable . Map ( { d : 3 } ) ) ;
127
148
} ) ;
128
149
129
150
it ( 'has the sequence API' , ( ) => {
@@ -137,7 +158,7 @@ describe('Cursor', () => {
137
158
var onChange = jest . genMockFunction ( ) ;
138
159
var cursor = data . cursor ( onChange ) ;
139
160
var found = cursor . find ( map => map . get ( 'v' ) === 2 ) ;
140
- expect ( typeof found . deref ) . toBe ( 'function' ) ; // is a cursor!
161
+ expect ( Immutable . isCursor ( found ) ) . toBe ( true ) ;
141
162
found = found . set ( 'v' , 20 ) ;
142
163
expect ( onChange ) . lastCalledWith (
143
164
Immutable . fromJS ( { a : { v : 1 } , b : { v : 20 } , c : { v : 3 } } ) ,
@@ -153,8 +174,8 @@ describe('Cursor', () => {
153
174
var c1 = data . cursor ( onChange ) ;
154
175
var c2 = c1 . withMutations ( m => m . set ( 'b' , 2 ) . set ( 'c' , 3 ) . set ( 'd' , 4 ) ) ;
155
176
156
- expect ( c1 . deref ( ) . toObject ( ) ) . toEqual ( { 'a' : 1 } ) ;
157
- expect ( c2 . deref ( ) . toObject ( ) ) . toEqual ( { 'a' : 1 , 'b' : 2 , 'c' : 3 , 'd' : 4 } ) ;
177
+ expect ( c1 . toObject ( ) ) . toEqual ( { 'a' : 1 } ) ;
178
+ expect ( c2 . toObject ( ) ) . toEqual ( { 'a' : 1 , 'b' : 2 , 'c' : 3 , 'd' : 4 } ) ;
158
179
expect ( onChange . mock . calls . length ) . toBe ( 1 ) ;
159
180
} ) ;
160
181
@@ -165,11 +186,36 @@ describe('Cursor', () => {
165
186
var c1 = data . cursor ( [ 'a' , 'b' , 'c' ] , onChange ) ;
166
187
var c2 = c1 . withMutations ( m => m . set ( 'x' , 1 ) . set ( 'y' , 2 ) . set ( 'z' , 3 ) ) ;
167
188
168
- expect ( c1 . deref ( ) ) . toEqual ( undefined ) ;
169
- expect ( c2 . deref ( ) ) . toEqual ( Immutable . fromJS (
189
+ expect ( c1 ) . toEqual ( Map . empty ( ) ) ;
190
+ expect ( c2 ) . toEqual ( Immutable . fromJS (
170
191
{ x : 1 , y : 2 , z : 3 }
171
192
) ) ;
172
193
expect ( onChange . mock . calls . length ) . toBe ( 1 ) ;
173
194
} ) ;
174
195
196
+ it ( 'can create sub-cursors' , ( ) => {
197
+ var onChange = jest . genMockFunction ( ) ;
198
+ var data = Immutable . fromJS ( json ) ;
199
+
200
+ var cursorA = data . cursor ( 'a' , onChange ) ;
201
+ var cursorAB = cursorA . cursor ( 'b' , onChange ) ;
202
+
203
+ cursorAB . update ( 'c' , v => v + 1 ) ;
204
+
205
+ expect ( data . getIn ( [ 'a' , 'b' , 'c' ] ) ) . toBe ( 1 ) ; // persistent
206
+
207
+ expect ( onChange . mock . calls ) . toEqual ( [
208
+ [
209
+ Immutable . fromJS ( { a : { b : { c : 2 } } } ) ,
210
+ Immutable . fromJS ( { a : { b : { c : 1 } } } ) ,
211
+ [ 'a' , 'b' , 'c' ]
212
+ ] ,
213
+ [
214
+ Immutable . fromJS ( { b : { c : 2 } } ) ,
215
+ Immutable . fromJS ( { b : { c : 1 } } ) ,
216
+ [ 'b' , 'c' ]
217
+ ]
218
+ ] ) ;
219
+ } ) ;
220
+
175
221
} ) ;
0 commit comments