forked from ng-bootstrap/ng-bootstrap
/
util.spec.ts
129 lines (97 loc) · 3.61 KB
/
util.spec.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import {toInteger, toString, getValueInRange, isInteger, isString, hasClassName, grandparent} from './util';
describe('util', () => {
describe('toInteger', () => {
it('should be noop for integers', () => {
expect(toInteger(0)).toBe(0);
expect(toInteger(10)).toBe(10);
});
it('should act as Math.floor for numbers', () => {
expect(toInteger(0.1)).toBe(0);
expect(toInteger(0.9)).toBe(0);
});
it('should parse strings', () => {
expect(toInteger('0')).toBe(0);
expect(toInteger('10')).toBe(10);
expect(toInteger('10.1')).toBe(10);
expect(toInteger('10.9')).toBe(10);
});
});
describe('toString', () => {
it('should be noop for strings', () => { expect(toString('foo')).toBe('foo'); });
it('should return empty string for undefined values', () => {
expect(toString(null)).toBe('');
expect(toString(undefined)).toBe('');
});
it('should stringify non-string values', () => {
expect(toString(10)).toBe('10');
expect(toString(false)).toBe('false');
});
});
describe('getValueInRange', () => {
it('should be noop for numbers in range', () => { expect(getValueInRange(5, 10, 0)).toBe(5); });
it('should do corrections in range', () => {
expect(getValueInRange(11, 10, 0)).toBe(10);
expect(getValueInRange(-1, 10, 0)).toBe(0);
});
it('should take 0 as a default min bound', () => {
expect(getValueInRange(11, 10)).toBe(10);
expect(getValueInRange(-1, 10)).toBe(0);
});
});
describe('isInteger', () => {
it('should recognize integers', () => {
expect(isInteger(0)).toBeTruthy();
expect(isInteger(10)).toBeTruthy();
expect(isInteger(-110)).toBeTruthy();
});
it('should recognize non-integers', () => {
expect(isInteger(null)).toBeFalsy();
expect(isString([])).toBeFalsy();
expect(isString(undefined)).toBeFalsy();
expect(isInteger('2048')).toBeFalsy();
expect(isInteger(14.1)).toBeFalsy();
expect(isInteger(-14.1)).toBeFalsy();
});
});
describe('isString', () => {
it('should recognize strings', () => {
expect(isString('string')).toBeTruthy();
expect(isString('')).toBeTruthy();
});
it('should recognize non-strings', () => {
expect(isString(null)).toBeFalsy();
expect(isString(2048)).toBeFalsy();
expect(isString([])).toBeFalsy();
expect(isString(undefined)).toBeFalsy();
});
});
describe('hasClassName', () => {
it('should find classes correctly', () => {
const element = {className: 'foo bar baz'};
expect(hasClassName(element, 'foo')).toBeTruthy();
expect(hasClassName(element, 'bar')).toBeTruthy();
expect(hasClassName(element, 'baz')).toBeTruthy();
expect(hasClassName(element, 'fo')).toBeFalsy();
expect(hasClassName(element, ' ')).toBeFalsy();
});
it('should work with incorrect values', () => {
expect(hasClassName(null, 'foo')).toBeFalsy();
expect(hasClassName({}, 'foo')).toBeFalsy();
expect(hasClassName({className: null}, 'foo')).toBeFalsy();
});
});
describe('grandparent', () => {
it('should return undefined on undefined', () => {
const element = undefined;
expect(grandparent(element)).toBeFalsy();
});
it('should return undefined on undefined parent', () => {
const element = {parentNode: undefined};
expect(grandparent(element)).toBeFalsy();
});
it('should return parent of parent', () => {
const element = {parentNode: {parentNode: {}}};
expect(grandparent(element)).toBe(element.parentNode.parentNode);
});
});
});