forked from invertase/docs.page
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.test.ts
99 lines (85 loc) · 2.37 KB
/
utils.test.ts
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { getString, getNumber, getBoolean } from '../src/utils';
describe('utils', () => {
describe('getString', () => {
it('returns a string value if provided', () => {
const obj = {
foo: {
bar: 'foo',
},
bar: 'foo',
baz: [
{
foo: 'foo',
},
'foo',
],
};
expect(getString(obj, 'foo.bar', 'baz')).toBe('foo');
expect(getString(obj, 'bar', 'baz')).toBe('foo');
expect(getString(obj, 'baz[0].foo', 'baz')).toBe('foo');
expect(getString(obj, 'baz[1]', 'baz')).toBe('foo');
});
it('returns a default string value if provided value is not a string', () => {
const obj = {
foo: {
bar: 123,
},
};
expect(getString(obj, 'foo.bar', 'foo')).toBe('foo');
});
});
describe('getNumber', () => {
it('returns a number value if provided', () => {
const obj = {
foo: {
bar: 1234,
},
bar: 1234,
baz: [
{
foo: 1234,
},
1234,
],
};
expect(getNumber(obj, 'foo.bar', 9876)).toBe(1234);
expect(getNumber(obj, 'bar', 9876)).toBe(1234);
expect(getNumber(obj, 'baz[0].foo', 9876)).toBe(1234);
expect(getNumber(obj, 'baz[1]', 9876)).toBe(1234);
});
it('returns a default number value if provided value is not a number', () => {
const obj = {
foo: {
bar: '123',
},
};
expect(getNumber(obj, 'foo.bar', 9876)).toBe(9876);
});
});
describe('getBoolean', () => {
it.only('returns a boolean value if provided', () => {
const obj = {
foo: true,
bar: false,
};
expect(getBoolean(obj, 'foo', false)).toBe(true);
expect(getBoolean(obj, 'bar', true)).toBe(false);
});
it('returns a boolean value if returned value is string truthy', () => {
const obj = {
foo: 'true',
bar: 'false',
};
expect(getBoolean(obj, 'foo', false)).toBe(true);
expect(getBoolean(obj, 'bar', true)).toBe(false);
});
it('returns a default number value if provided value is not a boolean', () => {
const obj = {
bar: 'foo',
baz: 1,
};
expect(getBoolean(obj, 'foo', true)).toBe(true);
expect(getBoolean(obj, 'baz', false)).toBe(false);
});
});
});