2020-10-16 14:09:04 -04:00
|
|
|
import {
|
|
|
|
getAverageByMonth,
|
2020-11-06 10:09:14 -05:00
|
|
|
getEarliestDate,
|
|
|
|
generateDataKeys,
|
2021-02-18 10:09:43 -05:00
|
|
|
} from '~/analytics/usage_trends/utils';
|
2020-10-13 08:08:41 -04:00
|
|
|
import {
|
|
|
|
mockCountsData1,
|
|
|
|
mockCountsData2,
|
|
|
|
countsMonthlyChartData1,
|
|
|
|
countsMonthlyChartData2,
|
|
|
|
} from './mock_data';
|
|
|
|
|
|
|
|
describe('getAverageByMonth', () => {
|
|
|
|
it('collects data into average by months', () => {
|
|
|
|
expect(getAverageByMonth(mockCountsData1)).toStrictEqual(countsMonthlyChartData1);
|
|
|
|
expect(getAverageByMonth(mockCountsData2)).toStrictEqual(countsMonthlyChartData2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('it transforms a data point to the first of the month', () => {
|
|
|
|
const item = mockCountsData1[0];
|
|
|
|
const firstOfTheMonth = item.recordedAt.replace(/-[0-9]{2}$/, '-01');
|
|
|
|
expect(getAverageByMonth([item])).toStrictEqual([[firstOfTheMonth, item.count]]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('it uses sane defaults', () => {
|
|
|
|
expect(getAverageByMonth()).toStrictEqual([]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('it errors when passing null', () => {
|
|
|
|
expect(() => {
|
|
|
|
getAverageByMonth(null);
|
|
|
|
}).toThrow();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when shouldRound = true', () => {
|
|
|
|
const options = { shouldRound: true };
|
|
|
|
|
|
|
|
it('rounds the averages', () => {
|
|
|
|
const roundedData1 = countsMonthlyChartData1.map(([date, avg]) => [date, Math.round(avg)]);
|
|
|
|
const roundedData2 = countsMonthlyChartData2.map(([date, avg]) => [date, Math.round(avg)]);
|
|
|
|
expect(getAverageByMonth(mockCountsData1, options)).toStrictEqual(roundedData1);
|
|
|
|
expect(getAverageByMonth(mockCountsData2, options)).toStrictEqual(roundedData2);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2020-10-16 14:09:04 -04:00
|
|
|
|
2020-11-06 10:09:14 -05:00
|
|
|
describe('getEarliestDate', () => {
|
|
|
|
it('returns the date of the final item in the array', () => {
|
|
|
|
expect(getEarliestDate(mockCountsData1)).toBe('2020-06-12');
|
2020-10-22 14:09:01 -04:00
|
|
|
});
|
|
|
|
|
2020-11-06 10:09:14 -05:00
|
|
|
it('returns null for an empty array', () => {
|
|
|
|
expect(getEarliestDate([])).toBeNull();
|
2020-10-22 14:09:01 -04:00
|
|
|
});
|
|
|
|
|
2020-11-06 10:09:14 -05:00
|
|
|
it("returns null if the array has data but `recordedAt` isn't defined", () => {
|
|
|
|
expect(
|
|
|
|
getEarliestDate(mockCountsData1.map(({ recordedAt: date, ...rest }) => ({ date, ...rest }))),
|
|
|
|
).toBeNull();
|
2020-10-16 14:09:04 -04:00
|
|
|
});
|
2020-11-06 10:09:14 -05:00
|
|
|
});
|
2020-10-16 14:09:04 -04:00
|
|
|
|
2020-11-06 10:09:14 -05:00
|
|
|
describe('generateDataKeys', () => {
|
|
|
|
const fakeQueries = [
|
|
|
|
{ identifier: 'from' },
|
|
|
|
{ identifier: 'first' },
|
|
|
|
{ identifier: 'to' },
|
|
|
|
{ identifier: 'last' },
|
|
|
|
];
|
2020-10-16 14:09:04 -04:00
|
|
|
|
2020-11-06 10:09:14 -05:00
|
|
|
const defaultValue = 'default value';
|
|
|
|
const res = generateDataKeys(fakeQueries, defaultValue);
|
2020-10-16 14:09:04 -04:00
|
|
|
|
2020-11-06 10:09:14 -05:00
|
|
|
it('extracts each query identifier and sets them as object keys', () => {
|
|
|
|
expect(Object.keys(res)).toEqual(['from', 'first', 'to', 'last']);
|
2020-10-16 14:09:04 -04:00
|
|
|
});
|
|
|
|
|
2020-11-06 10:09:14 -05:00
|
|
|
it('sets every value to the `defaultValue` provided', () => {
|
|
|
|
expect(Object.values(res)).toEqual(Array(fakeQueries.length).fill(defaultValue));
|
2020-10-16 14:09:04 -04:00
|
|
|
});
|
|
|
|
});
|