File size: 3,110 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import moment from 'moment';
import { addDayToRange } from '../utils';

describe( 'addDayToRange', () => {
	test( 'should return the same range if day is invalid', () => {
		const range = { from: moment( '2023-01-01' ), to: moment( '2023-01-05' ) };
		const result = addDayToRange( null, range );
		expect( result ).toEqual( range );
	} );

	test( 'should set "from" to null if day is the same as "from"', () => {
		const range = { from: moment( '2023-01-01' ), to: moment( '2023-01-05' ) };
		const result = addDayToRange( moment( '2023-01-01' ), range );
		expect( result.from ).toBeNull();
		expect( result.to ).toEqual( range.to );
	} );

	test( 'should set "to" to null if day is the same as "to"', () => {
		const range = { from: moment( '2023-01-01' ), to: moment( '2023-01-05' ) };
		const result = addDayToRange( moment( '2023-01-05' ), range );
		expect( result.from ).toEqual( range.from );
		expect( result.to ).toBeNull();
	} );

	test( 'should set "from" if it is null', () => {
		const range = { from: null, to: moment( '2023-01-05' ) };
		const result = addDayToRange( moment( '2023-01-01' ), range );
		expect( result.from ).toEqual( moment( '2023-01-01' ) );
		expect( result.to ).toEqual( range.to );
	} );

	test( 'should set "to" if it is null', () => {
		const range = { from: moment( '2023-01-01' ), to: null };
		const result = addDayToRange( moment( '2023-01-05' ), range );
		expect( result.from ).toEqual( range.from );
		expect( result.to ).toEqual( moment( '2023-01-05' ) );
	} );

	test( 'should update "from" if day is before current "from"', () => {
		const range = { from: moment( '2023-01-05' ), to: moment( '2023-01-10' ) };
		const result = addDayToRange( moment( '2023-01-01' ), range );
		expect( result.from ).toEqual( moment( '2023-01-01' ) );
		expect( result.to ).toEqual( range.to );
	} );

	test( 'should update "to" if day is after current "to"', () => {
		const range = { from: moment( '2023-01-01' ), to: moment( '2023-01-05' ) };
		const result = addDayToRange( moment( '2023-01-10' ), range );
		expect( result.from ).toEqual( range.from );
		expect( result.to ).toEqual( moment( '2023-01-10' ) );
	} );

	test( 'should update "from" if day is closer to "from" than "to"', () => {
		const range = { from: moment( '2023-01-01' ), to: moment( '2023-01-10' ) };
		const result = addDayToRange( moment( '2023-01-04' ), range );
		expect( result.from ).toEqual( moment( '2023-01-04' ) );
		expect( result.to ).toEqual( range.to );
	} );

	test( 'should update "to" if day is closer to "to" than "from"', () => {
		const range = { from: moment( '2023-01-01' ), to: moment( '2023-01-10' ) };
		const result = addDayToRange( moment( '2023-01-07' ), range );
		expect( result.from ).toEqual( range.from );
		expect( result.to ).toEqual( moment( '2023-01-07' ) );
	} );

	test( 'should ignore time fractions', () => {
		const range = { from: moment( '2023-01-01 11:10' ), to: moment( '2023-01-17 12:00' ) };
		const result = addDayToRange( moment( '2023-01-17 13:00' ), range );
		expect( result.from ).toEqual( range.from );
		expect( result.to ).toBeNull();
	} );
} );