Commit af12a14a authored by Brandon Labuschagne's avatar Brandon Labuschagne Committed by Phil Hughes

Change return type of getDateInPast to Date

The previous return type was a date string.

This approach allows more flexibility when working
with the response.
parent 4495195c
......@@ -553,14 +553,10 @@ export const calculateRemainingMilliseconds = endDate => {
*
* @param {Date} date the date that we will substract days from
* @param {number} daysInPast number of days that are subtracted from a given date
* @returns {String} Date string in ISO format
* @returns {Date} Date in past as Date object
*/
export const getDateInPast = (date, daysInPast) => {
const dateClone = newDate(date);
return new Date(
dateClone.setTime(dateClone.getTime() - daysInPast * 24 * 60 * 60 * 1000),
).toISOString();
};
export const getDateInPast = (date, daysInPast) =>
new Date(newDate(date).setDate(date.getDate() - daysInPast));
export const beginOfDayTime = 'T00:00:00Z';
export const endOfDayTime = 'T23:59:59Z';
......
---
title: Change return type of getDateInPast to Date
merge_request: 19081
author:
type: changed
......@@ -119,7 +119,7 @@ export default {
},
initDateRange() {
const endDate = new Date(Date.now());
const startDate = new Date(getDateInPast(endDate, DEFAULT_DAYS_IN_PAST));
const startDate = getDateInPast(endDate, DEFAULT_DAYS_IN_PAST);
this.setDateRange({ skipFetch: true, startDate, endDate });
},
},
......
......@@ -22,7 +22,7 @@ export default () => {
const { endpoint, emptyStateSvgPath, noAccessSvgPath } = appContainer.dataset;
const now = new Date(Date.now());
const defaultStartDate = new Date(getDateInPast(now, defaultDaysInPast));
const defaultStartDate = getDateInPast(now, defaultDaysInPast);
const defaultEndDate = now;
let filterManager;
......
......@@ -33,7 +33,7 @@ export const fetchChartData = ({ dispatch, getters, state, rootState }, chartKey
if (chartKey === chartKeys.scatterplot) {
const transformedData = transformScatterData(
data,
new Date(getDateInPast(rootState.filters.startDate, scatterPlotAddonQueryDays)),
getDateInPast(rootState.filters.startDate, scatterPlotAddonQueryDays),
new Date(rootState.filters.endDate),
);
......
......@@ -27,10 +27,7 @@ export const getCommonFilterParams = state => chartKey => {
// for the scatterplot we need to remove 30 days from the state's merged_at_after date
const mergedAtAfterDate =
chartKey && chartKey === chartKeys.scatterplot
? dateFormat(
new Date(getDateInPast(new Date(startDate), scatterPlotAddonQueryDays)),
dateFormats.isoDate,
)
? dateFormat(getDateInPast(startDate, scatterPlotAddonQueryDays), dateFormats.isoDate)
: dateFormat(startDate, dateFormats.isoDate);
return {
......
......@@ -163,7 +163,7 @@ export const getMedianLineData = (data, startDate, endDate, daysOffset) => {
medianData = transformedData.slice(startIndex, i);
flattenedData = _.flatten(medianData);
if (flattenedData.length) {
d = getDateInPast(endDate, len - i);
d = getDateInPast(endDate, len - i).toISOString();
result.push([dateFormat(d, dateFormats.isoDate), median(flattenedData)]);
}
}
......
......@@ -51,7 +51,7 @@ const stageFixtures = defaultStages.reduce((acc, stage) => {
}, {});
export const endDate = new Date(Date.now());
export const startDate = new Date(getDateInPast(endDate, DEFAULT_DAYS_IN_PAST));
export const startDate = getDateInPast(endDate, DEFAULT_DAYS_IN_PAST);
export const issueEvents = stageFixtures.issue;
export const planEvents = stageFixtures.plan;
......
......@@ -31,8 +31,8 @@ describe('Productivity analytics chart actions', () => {
rootState: {
endpoint: `${TEST_HOST}/analytics/productivity_analytics.json`,
filters: {
startDate: '2019-09-01',
endDate: '2091-09-05',
startDate: new Date('2019-09-01'),
endDate: new Date('2091-09-05'),
},
},
getters: {
......
......@@ -428,17 +428,19 @@ describe('newDate', () => {
});
describe('getDateInPast', () => {
const date = new Date(1563235200000); // 2019-07-16T00:00:00.000Z;
const date = new Date('2019-07-16T00:00:00.000Z');
const daysInPast = 90;
it('returns the correct date in the past', () => {
const dateInPast = datetimeUtility.getDateInPast(date, daysInPast);
expect(dateInPast).toBe('2019-04-17T00:00:00.000Z');
const expectedDateInPast = new Date('2019-04-17T00:00:00.000Z');
expect(dateInPast).toStrictEqual(expectedDateInPast);
});
it('does not modifiy the original date', () => {
datetimeUtility.getDateInPast(date, daysInPast);
expect(date).toStrictEqual(new Date(1563235200000));
expect(date).toStrictEqual(new Date('2019-07-16T00:00:00.000Z'));
});
});
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment