-
Notifications
You must be signed in to change notification settings - Fork 7k
/
inRange.spec.js
53 lines (46 loc) · 1.64 KB
/
inRange.spec.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
import lodashStable from 'lodash';
import { falsey, stubTrue } from './utils';
import inRange from '../src/inRange';
describe('inRange', () => {
it('should work with an `end`', () => {
expect(inRange(3, 5)).toBe(true);
expect(inRange(5, 5)).toBe(false);
expect(inRange(6, 5)).toBe(false);
});
it('should work with a `start` and `end`', () => {
expect(inRange(1, 1, 5)).toBe(true);
expect(inRange(3, 1, 5)).toBe(true);
expect(inRange(0, 1, 5)).toBe(false);
expect(inRange(5, 1, 5)).toBe(false);
});
it('should treat falsey `start` as `0`', () => {
lodashStable.each(falsey, (value, index) => {
if (index) {
expect(inRange(0, value)).toBe(false);
expect(inRange(0, value, 1)).toBe(true);
} else {
expect(inRange(0)).toBe(false);
}
});
});
it('should swap `start` and `end` when `start` > `end`', () => {
expect(inRange(2, 5, 1)).toBe(true);
expect(inRange(-3, -2, -6)).toBe(true);
});
it('should work with a floating point `n` value', () => {
expect(inRange(0.5, 5)).toBe(true);
expect(inRange(1.2, 1, 5)).toBe(true);
expect(inRange(5.2, 5)).toBe(false);
expect(inRange(0.5, 1, 5)).toBe(false);
});
it('should coerce arguments to finite numbers', () => {
const actual = [
inRange(0, '1'),
inRange(0, '0', 1),
inRange(0, 0, '1'),
inRange(0, NaN, 1),
inRange(-1, -1, NaN),
];
expect(actual).toEqual(lodashStable.map(actual,stubTrue));
});
});