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