This repository has been archived by the owner on Jan 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
/
idx.test.js
76 lines (65 loc) · 1.88 KB
/
idx.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
'use strict'; // eslint-disable-line strict
jest.unmock('./idx');
const idx = require('./idx');
describe('idx', () => {
it('returns properties that exist', () => {
const a = {b: {c: 123}};
expect(idx(a, _ => _.b.c)).toEqual(123);
});
it('throws non-"property access" errors', () => {
const error = new Error('Expected error.');
const a = {};
Object.defineProperty(a, 'b', {
get() {
throw error;
},
});
expect(() => idx(a, _ => _.b.c)).toThrow(error);
});
it('throws a `TypeError` when calling non-methods', () => {
const a = {b: 'I am a string'};
expect(() => idx(a, _ => _.b())).toThrow(
new TypeError('_.b is not a function'),
);
});
it('returns null for intermediate null properties', () => {
const a = {b: null};
expect(idx(a, _ => _.b.c)).toEqual(null);
});
it('returns undefined for intermediate undefined properties', () => {
const a = {b: undefined};
expect(idx(a, _ => _.b.c)).toEqual(undefined);
});
it('returns undefined for intermediate undefined array indexes', () => {
const a = {b: []};
expect(idx(a, _ => _.b[0].c)).toEqual(undefined);
});
it('returns null for error in capital case', () => {
const error = new TypeError('b is NULL');
const a = {};
Object.defineProperty(a, 'b', {
get() {
throw error;
},
});
expect(idx(a, _ => _.b.c)).toEqual(null);
});
it('returns undefined for error in capital case', () => {
const error = new TypeError('b is UNDEFINED');
const a = {};
Object.defineProperty(a, 'b', {
get() {
throw error;
},
});
expect(idx(a, _ => _.b.c)).toEqual(undefined);
});
});