Skip to content

Commit 140c8f5

Browse files
authored
Additional tests for getIn and hasIn (#1367)
Factors some out of updateIn
1 parent 2c18d0b commit 140c8f5

File tree

3 files changed

+137
-67
lines changed

3 files changed

+137
-67
lines changed

__tests__/getIn.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
///<reference path='../resources/jest.d.ts'/>
9+
10+
import { fromJS, List, Map, Set } from '../';
11+
12+
describe('getIn', () => {
13+
14+
it('deep get', () => {
15+
const m = fromJS({a: {b: {c: 10}}});
16+
expect(m.getIn(['a', 'b', 'c'])).toEqual(10);
17+
});
18+
19+
it('deep get with list as keyPath', () => {
20+
const m = fromJS({a: {b: {c: 10}}});
21+
expect(m.getIn(fromJS(['a', 'b', 'c']))).toEqual(10);
22+
});
23+
24+
it('deep get throws without list or array-like', () => {
25+
// need to cast these as TypeScript first prevents us from such clownery.
26+
expect(() =>
27+
Map().getIn(undefined as any),
28+
).toThrow('Invalid keyPath: expected Ordered Collection or Array: undefined');
29+
expect(() =>
30+
Map().getIn({ a: 1, b: 2 } as any),
31+
).toThrow('Invalid keyPath: expected Ordered Collection or Array: [object Object]');
32+
expect(() =>
33+
Map().getIn('abc' as any),
34+
).toThrow('Invalid keyPath: expected Ordered Collection or Array: abc');
35+
});
36+
37+
it('deep get throws if non-readable path', () => {
38+
const realWarn = console.warn;
39+
const warnings: Array<any> = [];
40+
console.warn = w => warnings.push(w);
41+
42+
try {
43+
const deep = Map({ key: { regular: "jsobj" }, list: List([ Map({num: 10}) ]) });
44+
deep.getIn(["key", "foo", "item"]);
45+
expect(warnings.length).toBe(1);
46+
expect(warnings[0]).toBe(
47+
'Invalid keyPath: Value at ["key"] does not have a .get() method: [object Object]' +
48+
'\nThis warning will throw in a future version',
49+
);
50+
51+
warnings.length = 0;
52+
deep.getIn(["list", 0, "num", "badKey"]);
53+
expect(warnings.length).toBe(1);
54+
expect(warnings[0]).toBe(
55+
'Invalid keyPath: Value at ["list",0,"num"] does not have a .get() method: 10' +
56+
'\nThis warning will throw in a future version',
57+
);
58+
} finally {
59+
console.warn = realWarn;
60+
}
61+
});
62+
63+
it('deep get returns not found if path does not match', () => {
64+
const m = fromJS({a: {b: {c: 10}}});
65+
expect(m.getIn(['a', 'b', 'z'])).toEqual(undefined);
66+
expect(m.getIn(['a', 'b', 'z'], 123)).toEqual(123);
67+
expect(m.getIn(['a', 'y', 'z'])).toEqual(undefined);
68+
expect(m.getIn(['a', 'y', 'z'], 123)).toEqual(123);
69+
});
70+
71+
it('does not use notSetValue when path does exist but value is nullable', () => {
72+
const m = fromJS({a: {b: {c: null, d: undefined}}});
73+
expect(m.getIn(['a', 'b', 'c'])).toEqual(null);
74+
expect(m.getIn(['a', 'b', 'd'])).toEqual(undefined);
75+
expect(m.getIn(['a', 'b', 'c'], 123)).toEqual(null);
76+
expect(m.getIn(['a', 'b', 'd'], 123)).toEqual(undefined);
77+
});
78+
79+
});

__tests__/hasIn.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
///<reference path='../resources/jest.d.ts'/>
9+
10+
import { fromJS, List, Map } from '../';
11+
12+
describe('hasIn', () => {
13+
14+
it('deep has', () => {
15+
const m = fromJS({a: {b: {c: 10}}});
16+
expect(m.hasIn(['a', 'b', 'c'])).toEqual(true);
17+
expect(m.hasIn(['a', 'b', 'z'])).toEqual(false);
18+
expect(m.hasIn(['a', 'y', 'z'])).toEqual(false);
19+
});
20+
21+
it('deep has with list as keyPath', () => {
22+
const m = fromJS({a: {b: {c: 10}}});
23+
expect(m.hasIn(fromJS(['a', 'b', 'c']))).toEqual(true);
24+
expect(m.hasIn(fromJS(['a', 'b', 'z']))).toEqual(false);
25+
expect(m.hasIn(fromJS(['a', 'y', 'z']))).toEqual(false);
26+
});
27+
28+
it('deep has throws without list or array-like', () => {
29+
// need to cast these as TypeScript first prevents us from such clownery.
30+
expect(() =>
31+
Map().hasIn(undefined as any),
32+
).toThrow('Invalid keyPath: expected Ordered Collection or Array: undefined');
33+
expect(() =>
34+
Map().hasIn({ a: 1, b: 2 } as any),
35+
).toThrow('Invalid keyPath: expected Ordered Collection or Array: [object Object]');
36+
expect(() =>
37+
Map().hasIn('abc' as any),
38+
).toThrow('Invalid keyPath: expected Ordered Collection or Array: abc');
39+
});
40+
41+
it('deep has does not throw if non-readable path', () => {
42+
const realWarn = console.warn;
43+
const warnings: Array<any> = [];
44+
console.warn = w => warnings.push(w);
45+
46+
try {
47+
const deep = Map({ key: { regular: "jsobj" }, list: List([ Map({num: 10}) ]) });
48+
expect(deep.hasIn(['key', 'foo', 'item'])).toBe(false);
49+
expect(warnings.length).toBe(0);
50+
51+
expect(deep.hasIn(['list', 0, 'num', 'badKey'])).toBe(false);
52+
expect(warnings.length).toBe(0);
53+
} finally {
54+
console.warn = realWarn;
55+
}
56+
});
57+
58+
});

__tests__/updateIn.ts

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -11,73 +11,6 @@ import { fromJS, List, Map, Set } from '../';
1111

1212
describe('updateIn', () => {
1313

14-
it('deep get', () => {
15-
const m = fromJS({a: {b: {c: 10}}});
16-
expect(m.getIn(['a', 'b', 'c'])).toEqual(10);
17-
});
18-
19-
it('deep get with list as keyPath', () => {
20-
const m = fromJS({a: {b: {c: 10}}});
21-
expect(m.getIn(fromJS(['a', 'b', 'c']))).toEqual(10);
22-
});
23-
24-
it('deep get throws without list or array-like', () => {
25-
// need to cast these as TypeScript first prevents us from such clownery.
26-
expect(() =>
27-
Map().getIn(undefined as any),
28-
).toThrow('Invalid keyPath: expected Ordered Collection or Array: undefined');
29-
expect(() =>
30-
Map().getIn({ a: 1, b: 2 } as any),
31-
).toThrow('Invalid keyPath: expected Ordered Collection or Array: [object Object]');
32-
expect(() =>
33-
Map().getIn('abc' as any),
34-
).toThrow('Invalid keyPath: expected Ordered Collection or Array: abc');
35-
});
36-
37-
it('deep get throws if non-readable path', () => {
38-
const realWarn = console.warn;
39-
const warnings: Array<any> = [];
40-
console.warn = w => warnings.push(w);
41-
42-
try {
43-
const deep = Map({ key: { regular: "jsobj" }, list: List([ Map({num: 10}) ]) });
44-
deep.getIn(["key", "foo", "item"]);
45-
expect(warnings.length).toBe(1);
46-
expect(warnings[0]).toBe(
47-
'Invalid keyPath: Value at ["key"] does not have a .get() method: [object Object]' +
48-
'\nThis warning will throw in a future version',
49-
);
50-
51-
warnings.length = 0;
52-
deep.getIn(["list", 0, "num", "badKey"]);
53-
expect(warnings.length).toBe(1);
54-
expect(warnings[0]).toBe(
55-
'Invalid keyPath: Value at ["list",0,"num"] does not have a .get() method: 10' +
56-
'\nThis warning will throw in a future version',
57-
);
58-
} finally {
59-
console.warn = realWarn;
60-
}
61-
});
62-
63-
it('deep has throws without list or array-like', () => {
64-
// need to cast these as TypeScript first prevents us from such clownery.
65-
expect(() =>
66-
Map().hasIn(undefined as any),
67-
).toThrow('Invalid keyPath: expected Ordered Collection or Array: undefined');
68-
expect(() =>
69-
Map().hasIn({ a: 1, b: 2 } as any),
70-
).toThrow('Invalid keyPath: expected Ordered Collection or Array: [object Object]');
71-
expect(() =>
72-
Map().hasIn('abc' as any),
73-
).toThrow('Invalid keyPath: expected Ordered Collection or Array: abc');
74-
});
75-
76-
it('deep get returns not found if path does not match', () => {
77-
const m = fromJS({a: {b: {c: 10}}});
78-
expect(m.getIn(['a', 'b', 'z'])).toEqual(undefined);
79-
});
80-
8114
it('deep edit', () => {
8215
const m = fromJS({a: {b: {c: 10}}});
8316
expect(

0 commit comments

Comments
 (0)