summaryrefslogtreecommitdiffstats
path: root/test/components/dateRangeSelector.test.js
blob: fd578b123356328a24ad00f84cb6168c2d8dcafd (plain)
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 * Copyright © 2017 Amdocs
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 *
 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 */
import React from "react";
import TestUtils from "react-dom/lib/ReactTestUtils";
import {Provider} from "react-redux";
import {expect} from "chai";
import {moment} from "moment";
import store from "app/AppStore.js";
import DateRangeSelector from "generic-components/dateRangeSelector/DateRangeSelector.jsx";
import DateRangeSelectorActions from "generic-components/dateRangeSelector/DateRangeSelectorActions.js";
import {
	dateRangeSelectorActionTypes,
	TODAY,
	YESTERDAY,
	LAST_WEEK,
	LAST_MONTH,
	CUSTOM,
	ICON_CLASS_CALENDAR,
	ICON_CLASS_DOWN_CARET,
	ERROR_UNKNOWN_PERIOD} from "generic-components/dateRangeSelector/DateRangeSelectorConstants.js";
import reducer from "generic-components/dateRangeSelector/DateRangeSelectorReducer.js";
import {dateRangeActionTypes, DATE_PICKER_PLACEHOLDER} from "generic-components/dateRange/DateRangeConstants.js";
import {MESSAGE_LEVEL_DANGER} from "utils/GlobalConstants.js";

describe("Date Range Selector Test Suite", function() {

	beforeEach(function () {
		this.component = TestUtils.renderIntoDocument( <Provider store={store}><DateRangeSelector /></Provider>);
	});

	// test structure
	it("Date Range Selector - Validate selector button", function () {
		var moment = require('moment');
		let currentDate = moment(new Date()).format(DATE_PICKER_PLACEHOLDER);
		let button = TestUtils.findRenderedDOMComponentWithTag(this.component, 'button');
		expect(button).exists;
		expect(button.childNodes.length).to.equal(3);
		expect(button.childNodes[0].className).to.have.string(ICON_CLASS_CALENDAR);
		expect(button.childNodes[1].innerHTML).to.have.string(currentDate + ' - ' + currentDate);
		expect(button.childNodes[2].className).to.have.string(ICON_CLASS_DOWN_CARET);
	});

	it("Date Range Selector - Validate quick pick options", function () {
		let button = TestUtils.findRenderedDOMComponentWithTag(this.component, 'button');
		TestUtils.Simulate.click(button);

		let popoverMenu = document.body.getElementsByClassName('popover-content');
		// TODO - need to figure out how to get the popover menu (above doesn't work)
	});

	// test reducer
	it("Date Range Selector Reducer ... Vaidate DATE_RANGE_CHANGE event)", function() {
		var moment = require('moment');
		const initialState = {
			startDate: moment(new Date('07/20/2016')),
			endDate: moment(new Date('07/09/2016')),
			period: CUSTOM,
			periodErrText: 'some error text',
			periodErrSev: 'some error severity'
		};
		const rangeChangeAction = {
			type: dateRangeActionTypes.DATE_RANGE_CHANGE,
			data: {
				dateRange: {
					startDate: moment(new Date('05/04/2016')),
					endDate: moment(new Date('06/05/2016'))
				},
				errorMsg: ''
			}};
		const newState = reducer(initialState, rangeChangeAction);
		expect(newState.startDate).to.exist;
		expect(newState.startDate.toDate().getTime()).to.equal(new Date('05/04/2016').getTime());
		expect(newState.endDate).to.exist;
		expect(newState.endDate.toDate().getTime()).to.equal(new Date('06/05/2016').getTime());
		expect(newState.period).to.equal(CUSTOM);
		expect(newState.periodErrText).to.equal('');
		expect(newState.periodErrSev).to.equal('');
	});
	it("Date Range Selector Reducer ... Vaidate INVALID_DATE_RANGE event)", function() {
		var moment = require('moment');
		const initialState = {};
		const rangeChangeAction = {
			type: dateRangeActionTypes.INVALID_DATE_RANGE,
			data: {
				dateRange: {
					startDate: moment(new Date('07/04/2016')),
					endDate: moment(new Date('06/05/2016'))
				},
				errorMsg: 'some eror message'
			}};
		const newState = reducer(initialState, rangeChangeAction);
		expect(newState.startDate).to.exist;
		expect(newState.startDate.toDate().getTime()).to.equal(new Date('07/04/2016').getTime());
		expect(newState.endDate).to.exist;
		expect(newState.endDate.toDate().getTime()).to.equal(new Date('06/05/2016').getTime());
		expect(newState.period).to.not.exist;
		expect(newState.periodErrText).to.equal('some eror message');
		expect(newState.periodErrSev).to.equal(MESSAGE_LEVEL_DANGER);
	});
	it("Date Range Selector Reducer ... Vaidate PERIOD_CHANGE event)", function() {
		var moment = require('moment');
		const initialState = {
			startDate: moment(new Date('07/20/2016')),
			endDate: moment(new Date('07/09/2016')),
			period: CUSTOM,
			periodErrText: 'some error text',
			periodErrSev: 'some error severity'
		};
		const rangeChangeAction = {
			type: dateRangeSelectorActionTypes.EVENT_PERIOD_CHANGE,
			data: {
				dateRange: {
					startDate: moment(new Date('07/04/2016')),
					endDate: moment(new Date('07/05/2016'))
				},
				period: YESTERDAY
			}
		};
		const newState = reducer(initialState, rangeChangeAction);
		expect(newState.startDate).to.exist;
		expect(newState.startDate.toDate().getTime()).to.equal(new Date('07/04/2016').getTime());
		expect(newState.endDate).to.exist;
		expect(newState.endDate.toDate().getTime()).to.equal(new Date('07/05/2016').getTime());
		expect(newState.period).to.equal(YESTERDAY);
		expect(newState.periodErrText).to.equal('');
		expect(newState.periodErrSev).to.equal('');
	});

	// test Actions
	it("Date Range Selector Action - test EVENT_PERIOD_CHANGE (period = Today)", function() {
		var moment = require('moment');
		const startDate = moment(new Date());
		const endDate = moment(new Date());
		const period = TODAY;

		const results = DateRangeSelectorActions.onPeriodChange(startDate, endDate, period);

		const expectedStartTime = moment(new Date());
		setTime(expectedStartTime, 0, 0, 0);
		const expectedEndTime = moment(new Date());
		setTime(expectedEndTime, 23, 59, 59);
		const expectedAction = buildExpectedPeriodChangeAction(expectedStartTime, expectedEndTime, TODAY);

		expect(results.type).to.equal(expectedAction.type);
		validateDates(results.data.dateRange.startDate, expectedAction.data.dateRange.startDate);
		validateDates(results.data.dateRange.endDate, expectedAction.data.dateRange.endDate);
		expect(results.data.period).to.equal(expectedAction.data.period);
	});
	it("Date Range Selector Action - test EVENT_PERIOD_CHANGE (period = Yesterday)", function() {
		var moment = require('moment');
		const startDate = moment(new Date());
		const endDate = moment(new Date());
		const period = YESTERDAY;

		const results = DateRangeSelectorActions.onPeriodChange(startDate, endDate, period);

		const expectedStartTime = moment(new Date()).subtract(1, 'days');
		setTime(expectedStartTime, 0, 0, 0);
		const expectedEndTime = moment(new Date());
		setTime(expectedEndTime, 23, 59, 59);
		const expectedAction = buildExpectedPeriodChangeAction(expectedStartTime, expectedEndTime, YESTERDAY);

		expect(results.type).to.equal(expectedAction.type);
		validateDates(results.data.dateRange.startDate, expectedAction.data.dateRange.startDate);
		validateDates(results.data.dateRange.endDate, expectedAction.data.dateRange.endDate);
		expect(results.data.period).to.equal(expectedAction.data.period);
	});
	it("Date Range Selector Action - test EVENT_PERIOD_CHANGE (period = Last Week)", function() {
		var moment = require('moment');
		const startDate = moment(new Date());
		const endDate = moment(new Date());
		const period = LAST_WEEK;

		const results = DateRangeSelectorActions.onPeriodChange(startDate, endDate, period);

		const expectedStartTime = moment(new Date()).subtract(7, 'days');
		setTime(expectedStartTime, 0, 0, 0);
		const expectedEndTime = moment(new Date());
		setTime(expectedEndTime, 23, 59, 59);
		const expectedAction = buildExpectedPeriodChangeAction(expectedStartTime, expectedEndTime, LAST_WEEK);

		expect(results.type).to.equal(expectedAction.type);
		validateDates(results.data.dateRange.startDate, expectedAction.data.dateRange.startDate);
		validateDates(results.data.dateRange.endDate, expectedAction.data.dateRange.endDate);
		expect(results.data.period).to.equal(expectedAction.data.period);
	});
	it("Date Range Selector Action - test EVENT_PERIOD_CHANGE (period = Last Month)", function() {
		var moment = require('moment');
		const startDate = moment(new Date());
		const endDate = moment(new Date());
		const period = LAST_MONTH;

		const results = DateRangeSelectorActions.onPeriodChange(startDate, endDate, period);

		const expectedStartTime = moment(new Date()).subtract(1, 'months');
		setTime(expectedStartTime, 0, 0, 0);
		const expectedEndTime = moment(new Date());
		setTime(expectedEndTime, 23, 59, 59);
		const expectedAction = buildExpectedPeriodChangeAction(expectedStartTime, expectedEndTime, LAST_MONTH);

		expect(results.type).to.equal(expectedAction.type);
		validateDates(results.data.dateRange.startDate, expectedAction.data.dateRange.startDate);
		validateDates(results.data.dateRange.endDate, expectedAction.data.dateRange.endDate);
		expect(results.data.period).to.equal(expectedAction.data.period);
	});
	it("Date Range Selector Action - test EVENT_PERIOD_CHANGE (period = Custom)", function() {
		var moment = require('moment');
		const startDate = moment(new Date()).subtract(3, 'months');
		const endDate = moment(new Date()).add(6, 'days');
		const period = CUSTOM;

		const results = DateRangeSelectorActions.onPeriodChange(startDate, endDate, period);

		setTime(startDate, 0, 0, 0);
		setTime(endDate, 23, 59, 59);
		const expectedAction = buildExpectedPeriodChangeAction(startDate, endDate, CUSTOM);

		expect(results.type).to.equal(expectedAction.type);
		validateDates(results.data.dateRange.startDate, expectedAction.data.dateRange.startDate);
		validateDates(results.data.dateRange.endDate, expectedAction.data.dateRange.endDate);
		expect(results.data.period).to.equal(expectedAction.data.period);
	});
	it("Date Range Selector Action - test EVENT_PERIOD_CHANGE (period = Unknown)", function() {
		var moment = require('moment');
		const startDate = moment(new Date()).subtract(3, 'months');
		const endDate = moment(new Date()).add(6, 'days');
		const period = 'Some Unknown Period';

		const results = DateRangeSelectorActions.onPeriodChange(startDate, endDate, period);

		let expectedErrorMsg = ERROR_UNKNOWN_PERIOD + ': ' + period;
		const expectedAction = buildExpectedUnknownPeriodAction(startDate, endDate, period, expectedErrorMsg);

		expect(results.type).to.deep.equal(expectedAction.type);
		validateDates(results.data.dateRange.startDate, expectedAction.data.dateRange.startDate);
		validateDates(results.data.dateRange.endDate, expectedAction.data.dateRange.endDate);
		expect(results.data.period).to.equal(expectedAction.data.period);
	});

	// TODO - need tests to confirm DateRangeSelectorActions.onPeriodChange is called when clicking any of the 'quick link' periods


	// helper functions
	function setTime(moment, hours, minutes, seconds) {
		moment.toDate();
		moment.hour(hours);
		moment.minute(minutes);
		moment.second(seconds);
	}

	function validateDates(actualDate, expectedDates) {
		expect(actualDate.toDate().getYear()).to.equal(expectedDates.toDate().getYear());
		expect(actualDate.toDate().getMonth()).to.equal(expectedDates.toDate().getMonth());
		expect(actualDate.toDate().getDay()).to.equal(expectedDates.toDate().getDay());
		expect(actualDate.toDate().getHours()).to.equal(expectedDates.toDate().getHours());
		expect(actualDate.toDate().getMinutes()).to.equal(expectedDates.toDate().getMinutes());
	}

	function buildExpectedPeriodChangeAction(start, end, period) {
		return {
			type: dateRangeSelectorActionTypes.EVENT_PERIOD_CHANGE,
			data: {
				dateRange: {
					startDate: start,
					endDate: end
				},
				period: period
			}
		};
	}

	function buildExpectedUnknownPeriodAction(start, end, period, errorMsg) {
		return {
			type: dateRangeSelectorActionTypes.EVENT_PERIOD_ERROR,
			data: {
				dateRange: {
					startDate: start,
					endDate: end
				},
				period: period,
				errorMsg: errorMsg
			}
		};
	}
});