Skip to content

Commit 5db81dc

Browse files
committed
Revert cursors to v2.3.0 behavior
1 parent 2eef72e commit 5db81dc

File tree

7 files changed

+344
-812
lines changed

7 files changed

+344
-812
lines changed

__tests__/Cursor.ts

Lines changed: 28 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
jest.autoMockOff();
55

66
import Immutable = require('immutable');
7-
import Map = Immutable.Map;
87

98
jasmine.getEnv().addEqualityTester((a, b) =>
109
a instanceof Immutable.Sequence && b instanceof Immutable.Sequence ?
@@ -20,46 +19,25 @@ describe('Cursor', () => {
2019
var data = Immutable.fromJS(json);
2120
var cursor = data.cursor();
2221

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

2524
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']));
2827
expect(deepCursor.get('c')).toBe(1);
2928

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

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);
5634
});
5735

5836
it('gets return new cursors', () => {
5937
var data = Immutable.fromJS(json);
6038
var cursor = data.cursor();
6139
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']));
6341
});
6442

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

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

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

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

8969
// 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);
9372
expect(onChange).lastCalledWith(
9473
Immutable.fromJS({a:{b:{c:2}}}),
9574
data,
9675
['a', 'b', 'c']
9776
);
9877

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);
10180
expect(onChange).lastCalledWith(
10281
Immutable.fromJS({a:{b:{c:3}}}),
10382
Immutable.fromJS({a:{b:{c:2}}}),
@@ -108,9 +87,9 @@ describe('Cursor', () => {
10887
expect(data.toJS()).toEqual(json);
10988

11089
// 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);
11493
expect(onChange).lastCalledWith(
11594
Immutable.fromJS({a:{b:{c:11}}}),
11695
data,
@@ -129,7 +108,7 @@ describe('Cursor', () => {
129108
var bCursor = aCursor.cursor('b');
130109
var cCursor = bCursor.cursor('c');
131110

132-
expect(bCursor.set('c', 10)).toEqual(
111+
expect(bCursor.set('c', 10).deref()).toEqual(
133112
Immutable.fromJS({ c: 10 })
134113
);
135114
expect(onChange).lastCalledWith(
@@ -142,9 +121,9 @@ describe('Cursor', () => {
142121
it('creates maps as necessary', () => {
143122
var data = Immutable.Map();
144123
var cursor = data.cursor(['a', 'b', 'c']);
145-
expect(cursor).toEqual(Map.empty());
124+
expect(cursor.deref()).toBe(undefined);
146125
cursor = cursor.set('d', 3);
147-
expect(cursor).toEqual(Immutable.Map({d: 3}));
126+
expect(cursor.deref()).toEqual(Immutable.Map({d: 3}));
148127
});
149128

150129
it('has the sequence API', () => {
@@ -158,7 +137,7 @@ describe('Cursor', () => {
158137
var onChange = jest.genMockFunction();
159138
var cursor = data.cursor(onChange);
160139
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!
162141
found = found.set('v', 20);
163142
expect(onChange).lastCalledWith(
164143
Immutable.fromJS({a: {v: 1}, b: {v: 20}, c: {v: 3}}),
@@ -174,8 +153,8 @@ describe('Cursor', () => {
174153
var c1 = data.cursor(onChange);
175154
var c2 = c1.withMutations(m => m.set('b', 2).set('c', 3).set('d', 4));
176155

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});
179158
expect(onChange.mock.calls.length).toBe(1);
180159
});
181160

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

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(
191170
{ x: 1, y: 2, z: 3 }
192171
));
193172
expect(onChange.mock.calls.length).toBe(1);
194173
});
195174

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-
221175
});

0 commit comments

Comments
 (0)