File size: 4,185 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
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
import { validateTimeSlot, validatePlugins } from '../schedule-form.helper';

describe( 'Schedule form validation', () => {
	test( 'plugins selection', () => {
		const selectedPlugins = [ 'plugin1', 'plugin2' ];
		const existingPlugins1 = [ [ 'plugin2', 'plugin1', 'plugin3' ] ];
		const existingPlugins2 = [
			[ 'plugin2', 'plugin1', 'plugin3' ],
			[ 'plugin2', 'plugin1' ],
		];

		// Truthy value means there is an error message
		expect( validatePlugins( [] ) ).toBeTruthy();
		expect( validatePlugins( selectedPlugins ) ).toBeFalsy();
		expect( validatePlugins( selectedPlugins, existingPlugins1 ) ).toBeFalsy();
		expect( validatePlugins( selectedPlugins, existingPlugins2 ) ).toBeTruthy();
	} );

	test( 'timestamp / time slot', () => {
		const timestamp = getTwoDaysFutureUTCTime();
		const ts_3am = timestamp + 3 * 60 * 60;
		const ts_9pm = timestamp + 21 * 60 * 60;

		const nextMondayMidnight = getNextMondayMidnightUTCTime();
		const ts_mon_6am = nextMondayMidnight + 6 * 60 * 60;
		const ts_mon_6pm = nextMondayMidnight + 18 * 60 * 60;
		const ts_fri_6am = nextMondayMidnight + 4 * 24 * 60 * 60 + 6 * 60 * 60;
		const ts_fri_6pm = nextMondayMidnight + 4 * 24 * 60 * 60 + 18 * 60 * 60;

		const now = new Date();
		const past = now.getTime() - 1000;

		//
		const existingSchedules = [
			{ frequency: 'daily', timestamp: ts_3am }, // daily at 3:00 am
		];
		const existingSchedules2 = [
			{ frequency: 'weekly', timestamp: ts_mon_6pm }, // weekly on Monday at 6:00 pm
		];
		const existingSchedules3 = [
			{ frequency: 'weekly', timestamp: ts_mon_6pm }, // weekly on Monday at 6:00 pm
			{ frequency: 'daily', timestamp: ts_3am }, // daily at 3:00 am
		];

		// Truthy value means there is an error message
		expect( validateTimeSlot( { frequency: 'daily', timestamp: ts_3am } ) ).toBeFalsy();
		expect( validateTimeSlot( { frequency: 'weekly', timestamp: ts_fri_6am } ) ).toBeFalsy();

		expect(
			validateTimeSlot( { frequency: 'daily', timestamp: ts_9pm }, existingSchedules )
		).toBeFalsy();
		expect(
			validateTimeSlot( { frequency: 'weekly', timestamp: ts_3am }, existingSchedules )
		).toBeTruthy();

		expect(
			validateTimeSlot( { frequency: 'daily', timestamp: ts_mon_6am }, existingSchedules2 )
		).toBeFalsy();
		expect(
			validateTimeSlot( { frequency: 'daily', timestamp: ts_mon_6pm }, existingSchedules2 )
		).toBeTruthy();
		expect(
			validateTimeSlot( { frequency: 'weekly', timestamp: ts_fri_6pm }, existingSchedules2 )
		).toBeFalsy();
		expect(
			validateTimeSlot( { frequency: 'daily', timestamp: ts_fri_6pm }, existingSchedules2 )
		).toBeTruthy();

		expect(
			validateTimeSlot( { frequency: 'daily', timestamp: ts_3am }, existingSchedules3 )
		).toBeTruthy();
		expect(
			validateTimeSlot( { frequency: 'daily', timestamp: ts_9pm }, existingSchedules3 )
		).toBeFalsy();
		expect(
			validateTimeSlot( { frequency: 'daily', timestamp: ts_mon_6am }, existingSchedules3 )
		).toBeFalsy();
		expect(
			validateTimeSlot( { frequency: 'weekly', timestamp: ts_mon_6am }, existingSchedules3 )
		).toBeFalsy();
		expect(
			validateTimeSlot( { frequency: 'daily', timestamp: ts_fri_6pm }, existingSchedules3 )
		).toBeTruthy();
		expect(
			validateTimeSlot( { frequency: 'weekly', timestamp: ts_fri_6pm }, existingSchedules3 )
		).toBeFalsy();

		// One second in the past
		expect(
			validateTimeSlot( { frequency: 'daily', timestamp: past / 1000 }, existingSchedules )
		).toBeTruthy();
	} );

	function getTwoDaysFutureUTCTime() {
		const currentDate = new Date();
		const twoDaysFuture = new Date( currentDate.getTime() + 2 * 24 * 60 * 60 * 1000 );

		twoDaysFuture.setUTCHours( 0, 0, 0, 0 );

		return Math.floor( twoDaysFuture.getTime() / 1000 ); // Returning Unix time in seconds
	}

	function getNextMondayMidnightUTCTime() {
		const currentDate = new Date();
		let daysUntilNextMonday = 1 - currentDate.getUTCDay();

		if ( daysUntilNextMonday <= 0 ) {
			daysUntilNextMonday += 7;
		}

		const nextMonday = new Date(
			currentDate.getTime() + daysUntilNextMonday * 24 * 60 * 60 * 1000
		); // Adding days to get to next Monday
		nextMonday.setUTCHours( 0, 0, 0, 0 );

		return Math.floor( nextMonday.getTime() / 1000 );
	}
} );