summaryrefslogtreecommitdiffstats
path: root/test/components/dateRange.test.js
blob: 07ae2b5cccdd6f2fda8523d994e30d1579e1d163 (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
/*
 * ============LICENSE_START=======================================================
 * SPARKY (AAI UI service)
 * ================================================================================
 * Copyright © 2017 AT&T Intellectual Property.
 * Copyright © 2017 Amdocs
 * All rights reserved.
 * ================================================================================
 * 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 and OpenECOMP are trademarks
 * and service marks 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 i18n from 'utils/i18n/i18n';

import store from 'app/AppStore.js';
import DateRange from 'generic-components/dateRange/DateRange.jsx';
import {
	dateRangeActionTypes,
	LABEL_START_DATE,
	LABEL_END_DATE,
	DATE_PICKER_PLACEHOLDER,
	IS_AFTER_END_DATE,
	IS_BEFORE_START_DATE,
	END_DATE,
	START_DATE} from 'generic-components/dateRange/DateRangeConstants.js';
import DateRangeActions from 'generic-components/dateRange/DateRangeActions.js';
import reducer from 'generic-components/dateRange/DateRangeReducer.js';
import sinon from 'sinon';

import { moment } from 'moment';

describe('Core Date Range suite', function() {

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

	// test structure
  it('Date Range - Validate start & end lables', function() {
    let labels = TestUtils.scryRenderedDOMComponentsWithTag(this.component, 'label');
    expect(labels.length).to.equal(2);
    expect(labels[0].textContent).to.equal(LABEL_START_DATE + ': ');
    expect(labels[1].textContent).to.equal(LABEL_END_DATE + ': ');
  });

  it('Date Range - Start Date Picker exists', function() {
    let datePicker = TestUtils.findRenderedDOMComponentWithClass(this.component, 'start-date-picker');
    expect(datePicker).to.exist;
    expect(datePicker.type).to.equal('text');
    expect(datePicker.placeholder).to.equal(DATE_PICKER_PLACEHOLDER);
  });

  it('Date Range - End Date Picker exists', function() {
    let datePicker = TestUtils.findRenderedDOMComponentWithClass(this.component, 'end-date-picker');
    expect(datePicker).to.exist;
    expect(datePicker.type).to.equal('text');
    expect(datePicker.placeholder).to.equal(DATE_PICKER_PLACEHOLDER);
  });

	// Reducer Tests
  it('Date Range Reducer ... set start date (no initial dates)', function() {
    var moment = require('moment');
    const initialState = {};
    const dateChangeAction = {type: dateRangeActionTypes.DATE_RANGE_CHANGE, data: {dateRange: {startDate: moment(new Date('05/04/2016'))}, errorMsg: ''}};
    const newState = reducer(initialState, dateChangeAction);
    expect(newState.dateRangeStart).to.exist;
    expect(newState.dateRangeStart.toDate().getTime()).to.equal(new Date('05/04/2016').getTime());
    expect(newState.dateRangeEnd).to.not.exist;
    expect(newState.dateRangeError).to.equal('');
  });
  it('Date Range Reducer ... update start date (no end date)', function() {
    var moment = require('moment');
    const initialStartDate = new Date('05/01/2016');
    const initialState = {dateRange: {startDate: moment(initialStartDate)}};
    const dateChangeAction = {type: dateRangeActionTypes.DATE_RANGE_CHANGE, data: {dateRange: {startDate: moment(new Date('05/04/2016'))}, errorMsg: ''}};
    const newState = reducer(initialState, dateChangeAction);
    expect(newState.dateRangeStart).to.exist;
    expect(newState.dateRangeStart.toDate().getTime()).to.equal(new Date('05/04/2016').getTime());
    expect(newState.dateRangeEnd).to.not.exist;
    expect(newState.dateRangeError).to.equal('');
  });
  it('Date Range Reducer - set end date (no start date)', function() {
    var moment = require('moment');
    const  initialState = {};
    const dateChangeAction = {type: dateRangeActionTypes.DATE_RANGE_CHANGE, data: {dateRange: {endDate: moment(new Date('05/04/2016'))}, errorMsg: ''}};
    const newState = reducer(initialState, dateChangeAction);
    expect(newState.dateRangeEnd).to.exist;
    expect(newState.dateRangeEnd.toDate().getTime()).to.equal(new Date('05/04/2016').getTime());
    expect(newState.dateRangeStart).to.not.exist;
    expect(newState.dateRangeError).to.equal('');
  });
  it('Date Range Reducer - update end date (no start date)', function() {
    var moment = require('moment');
    const initialEndDate = new Date('05/01/2016');
    const initialState = {dateRange: {endDate: moment(initialEndDate)}};
    const dateChangeAction = {type: dateRangeActionTypes.DATE_RANGE_CHANGE, data: {dateRange: {endDate: moment(new Date('05/04/2016'))}, errorMsg: ''}};
    const newState = reducer(initialState, dateChangeAction);
    expect(newState.dateRangeEnd).to.exist;
    expect(newState.dateRangeEnd.toDate().getTime()).to.equal(new Date('05/04/2016').getTime());
    expect(newState.dateRangeStart).to.not.exist;
    expect(newState.dateRangeError).to.equal('');
  });
  it('Date Range Reducer - set end date with initial start date', function() {
    var moment = require('moment');
    const initialStartDate = new Date('05/01/2016');
    const initialState = {dateRange: {startDate: moment(initialStartDate)}};
    const dateChangeAction = {type: dateRangeActionTypes.DATE_RANGE_CHANGE, data: {dateRange: {startDate: moment(new Date('05/01/2016')), endDate: moment(new Date('05/04/2016'))}, errorMsg: ''}};
    const newState = reducer(initialState, dateChangeAction);
    expect(newState.dateRangeStart).to.exist;
    expect(newState.dateRangeStart.toDate().getTime()).to.equal(new Date('05/01/2016').getTime());
    expect(newState.dateRangeEnd).to.exist;
    expect(newState.dateRangeEnd.toDate().getTime()).to.equal(new Date('05/04/2016').getTime());
    expect(newState.dateRangeError).to.equal('');
  });
  it('Date Range Reducer - set start date with initial end date', function() {
    var moment = require('moment');
    const initialEndDate = new Date('05/04/2016');
    const initialState = {dateRange: {endDate: moment(initialEndDate)}};
    const dateChangeAction = {type: dateRangeActionTypes.DATE_RANGE_CHANGE, data: {dateRange: {startDate: moment(new Date('05/01/2016')), endDate: moment(new Date('05/04/2016'))}, errorMsg: ''}};
    const newState = reducer(initialState, dateChangeAction);
    expect(newState.dateRangeStart).to.exist;
    expect(newState.dateRangeStart.toDate().getTime()).to.equal(new Date('05/01/2016').getTime());
    expect(newState.dateRangeEnd).to.exist;
    expect(newState.dateRangeEnd.toDate().getTime()).to.equal(new Date('05/04/2016').getTime());
    expect(newState.dateRangeError).to.equal('');
  });
  it('Date Range Reducer - verify INVALID_DATE_RANGE event', function() {
    var moment = require('moment');
    const errMsg = 'Some error message';
    const initialEndDate = new Date('05/01/2016');
    const initialStartDate = new Date('05/02/2016');
    const initialState = {startDate: moment(initialStartDate), endDate: moment(initialEndDate)};
    const invalidRangeAction = {type: dateRangeActionTypes.INVALID_DATE_RANGE, data: {dateRange: {startDate: moment(initialStartDate), endDate: moment(initialEndDate)}, errorMsg: errMsg}};
    const newState = reducer(initialState, invalidRangeAction);
    expect(newState.endDate.toDate().getTime()).to.equal(new Date('05/01/2016').getTime());
    expect(newState.startDate.toDate().getTime()).to.equal(new Date('05/02/2016').getTime());
    expect(newState.dateRangeError).to.equal(errMsg);
  });

	// test Actions
  it('Date Range Action - valid start date change', function() {
    var moment = require('moment');
    const startDate = moment(new Date('07/19/2016'));
    const endDate = moment(new Date('07/20/2016'));
    const expectedAction = {
      type: dateRangeActionTypes.DATE_RANGE_CHANGE,
      data: {
        dateRange: {
          startDate: startDate,
          endDate: endDate
        },
      }

    };
    const results = DateRangeActions.onStartDateChange(startDate, endDate);

    expect(results.type).to.equal(expectedAction.type);
    expect(results.data.dateRange.startDate).to.equal(expectedAction.data.dateRange.startDate);
    expect(results.data.dateRange.endDate).to.equal(expectedAction.data.dateRange.endDate);
  });
  it('Date Range Action - valid end date change', function() {
    var moment = require('moment');
    const startDate = moment(new Date('07/19/2016'));
    const endDate = moment(new Date('07/20/2016'));
    const expectedAction = {
      type: dateRangeActionTypes.DATE_RANGE_CHANGE,
      data: {
        dateRange: {
          startDate: startDate,
          endDate: endDate
        },
      }

    };
    const results = DateRangeActions.onEndDateChange(startDate, endDate);

    expect(results.type).to.equal(expectedAction.type);
    expect(results.data.dateRange.startDate).to.equal(expectedAction.data.dateRange.startDate);
    expect(results.data.dateRange.endDate).to.equal(expectedAction.data.dateRange.endDate);
  });
  it('Date Range Action - end date before start date', function() {
    var moment = require('moment');
    const startDate = moment(new Date('07/21/2016'));
    const endDate = moment(new Date('07/20/2016'));
    const errorMsg = i18n(END_DATE) + ': ' +
			moment(new Date(endDate)).format(DATE_PICKER_PLACEHOLDER) +
			' ' + i18n(IS_BEFORE_START_DATE);
    const expectedAction = {
      type: dateRangeActionTypes.INVALID_DATE_RANGE,
      data: {
        dateRange: {
          startDate: startDate,
          endDate: endDate
        },
        errorMsg: errorMsg
      }

    };
    const results = DateRangeActions.onEndDateChange(startDate, endDate);

    expect(results.type).to.equal(expectedAction.type);
    expect(results.data.dateRange.startDate).to.equal(expectedAction.data.dateRange.startDate);
    expect(results.data.dateRange.endDate).to.equal(expectedAction.data.dateRange.endDate);
    expect(results.data.errorMsg).to.equal(expectedAction.data.errorMsg);
  });
  it('Date Range Action - start date after date', function() {
    var moment = require('moment');
    const startDate = moment(new Date('07/21/2016'));
    const endDate = moment(new Date('07/20/2016'));
    const errorMsg = i18n(START_DATE) + ': ' +
			moment(new Date(startDate)).format(DATE_PICKER_PLACEHOLDER) +
			' ' + i18n(IS_AFTER_END_DATE);
    const expectedAction = {
      type: dateRangeActionTypes.INVALID_DATE_RANGE,
      data: {
        dateRange: {
          startDate: startDate,
          endDate: endDate
        },
        errorMsg: errorMsg
      }

    };
    const results = DateRangeActions.onStartDateChange(startDate, endDate);

    expect(results.type).to.equal(expectedAction.type);
    expect(results.data.dateRange.startDate).to.equal(expectedAction.data.dateRange.startDate);
    expect(results.data.dateRange.endDate).to.equal(expectedAction.data.dateRange.endDate);
    expect(results.data.errorMsg).to.equal(expectedAction.data.errorMsg);
  });
  it('Date Range Action - confirm onStartDateChange action called on startDate change', function() {
    const spy = sinon.spy(DateRangeActions, 'onStartDateChange');
    let startDatePicker = TestUtils.findRenderedDOMComponentWithClass(this.component, 'start-date-picker');
    startDatePicker.value = '05/09/2016';
    TestUtils.Simulate.change(startDatePicker);
    expect(DateRangeActions.onStartDateChange.calledOnce).to.be.true;
    DateRangeActions.onStartDateChange.restore();
  });
  it('Date Range Action - confirm onEndDateChange action called on endDate change', function() {
    const spy = sinon.spy(DateRangeActions, 'onEndDateChange');
    let endDatePicker = TestUtils.findRenderedDOMComponentWithClass(this.component, 'end-date-picker');
    endDatePicker.value = '05/09/2016';
    TestUtils.Simulate.change(endDatePicker);
    expect(DateRangeActions.onEndDateChange.calledOnce).to.be.true;
    DateRangeActions.onEndDateChange.restore();
  });
});