Skip to content

Commit cc044a9

Browse files
committed
Reapply cursors in separate branch for experimenting.
This reverts commit 5db81dc.
1 parent 5db81dc commit cc044a9

File tree

7 files changed

+812
-344
lines changed

7 files changed

+812
-344
lines changed

__tests__/Cursor.ts

+74-28
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
jest.autoMockOff();
55

66
import Immutable = require('immutable');
7+
import Map = Immutable.Map;
78

89
jasmine.getEnv().addEqualityTester((a, b) =>
910
a instanceof Immutable.Sequence && b instanceof Immutable.Sequence ?
@@ -19,25 +20,46 @@ describe('Cursor', () => {
1920
var data = Immutable.fromJS(json);
2021
var cursor = data.cursor();
2122

22-
expect(cursor.deref()).toBe(data);
23+
expect(Immutable.unCursor(cursor)).toBe(data);
2324

2425
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']));
2728
expect(deepCursor.get('c')).toBe(1);
2829

2930
var leafCursor = deepCursor.cursor('c');
30-
expect(leafCursor.deref()).toBe(1);
31+
expect(leafCursor).toBe(1);
3132

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);
3456
});
3557

3658
it('gets return new cursors', () => {
3759
var data = Immutable.fromJS(json);
3860
var cursor = data.cursor();
3961
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']));
4163
});
4264

4365
it('can be treated as a value', () => {
@@ -49,12 +71,10 @@ describe('Cursor', () => {
4971
expect(cursor.get('c')).toBe(1);
5072
});
5173

52-
it('can be value compared to a primitive', () => {
74+
it('returns scalars directly', () => {
5375
var data = Immutable.Map({ a: 'A' });
5476
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');
5878
});
5979

6080
it('updates at its path', () => {
@@ -63,20 +83,21 @@ describe('Cursor', () => {
6383
var data = Immutable.fromJS(json);
6484
var aCursor = data.cursor('a', onChange);
6585

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);
6888

6989
// 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+
7293
expect(onChange).lastCalledWith(
7394
Immutable.fromJS({a:{b:{c:2}}}),
7495
data,
7596
['a', 'b', 'c']
7697
);
7798

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);
80101
expect(onChange).lastCalledWith(
81102
Immutable.fromJS({a:{b:{c:3}}}),
82103
Immutable.fromJS({a:{b:{c:2}}}),
@@ -87,9 +108,9 @@ describe('Cursor', () => {
87108
expect(data.toJS()).toEqual(json);
88109

89110
// 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);
93114
expect(onChange).lastCalledWith(
94115
Immutable.fromJS({a:{b:{c:11}}}),
95116
data,
@@ -108,7 +129,7 @@ describe('Cursor', () => {
108129
var bCursor = aCursor.cursor('b');
109130
var cCursor = bCursor.cursor('c');
110131

111-
expect(bCursor.set('c', 10).deref()).toEqual(
132+
expect(bCursor.set('c', 10)).toEqual(
112133
Immutable.fromJS({ c: 10 })
113134
);
114135
expect(onChange).lastCalledWith(
@@ -121,9 +142,9 @@ describe('Cursor', () => {
121142
it('creates maps as necessary', () => {
122143
var data = Immutable.Map();
123144
var cursor = data.cursor(['a', 'b', 'c']);
124-
expect(cursor.deref()).toBe(undefined);
145+
expect(cursor).toEqual(Map.empty());
125146
cursor = cursor.set('d', 3);
126-
expect(cursor.deref()).toEqual(Immutable.Map({d: 3}));
147+
expect(cursor).toEqual(Immutable.Map({d: 3}));
127148
});
128149

129150
it('has the sequence API', () => {
@@ -137,7 +158,7 @@ describe('Cursor', () => {
137158
var onChange = jest.genMockFunction();
138159
var cursor = data.cursor(onChange);
139160
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);
141162
found = found.set('v', 20);
142163
expect(onChange).lastCalledWith(
143164
Immutable.fromJS({a: {v: 1}, b: {v: 20}, c: {v: 3}}),
@@ -153,8 +174,8 @@ describe('Cursor', () => {
153174
var c1 = data.cursor(onChange);
154175
var c2 = c1.withMutations(m => m.set('b', 2).set('c', 3).set('d', 4));
155176

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});
158179
expect(onChange.mock.calls.length).toBe(1);
159180
});
160181

@@ -165,11 +186,36 @@ describe('Cursor', () => {
165186
var c1 = data.cursor(['a', 'b', 'c'], onChange);
166187
var c2 = c1.withMutations(m => m.set('x', 1).set('y', 2).set('z', 3));
167188

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(
170191
{ x: 1, y: 2, z: 3 }
171192
));
172193
expect(onChange.mock.calls.length).toBe(1);
173194
});
174195

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+
175221
});

0 commit comments

Comments
 (0)