Client - init utils methods needed for charts
This commit is contained in:
25
fittrackee_client/tests/unit/utils/constants.ts
Normal file
25
fittrackee_client/tests/unit/utils/constants.ts
Normal file
@ -0,0 +1,25 @@
|
||||
import { ISport } from '@/types/workouts'
|
||||
|
||||
export const sports: ISport[] = [
|
||||
{
|
||||
has_workouts: false,
|
||||
id: 1,
|
||||
img: '/img/sports/cycling-sport.png',
|
||||
is_active: true,
|
||||
label: 'Cycling (Sport)',
|
||||
},
|
||||
{
|
||||
has_workouts: false,
|
||||
id: 2,
|
||||
img: '/img/sports/cycling-transport.png',
|
||||
is_active: false,
|
||||
label: 'Cycling (Transport)',
|
||||
},
|
||||
{
|
||||
has_workouts: true,
|
||||
id: 3,
|
||||
img: '/img/sports/hiking.png',
|
||||
is_active: true,
|
||||
label: 'Hiking',
|
||||
},
|
||||
]
|
137
fittrackee_client/tests/unit/utils/dates.spec.ts
Normal file
137
fittrackee_client/tests/unit/utils/dates.spec.ts
Normal file
@ -0,0 +1,137 @@
|
||||
import { assert, expect } from 'chai'
|
||||
|
||||
import { incrementDate, startDate } from '@/utils/dates'
|
||||
|
||||
describe('startDate (week starting Sunday)', () => {
|
||||
const testsParams = [
|
||||
{
|
||||
description: "returns start day for 'month' duration",
|
||||
inputDuration: 'month',
|
||||
inputDate: 'August 21, 2021 20:00:00',
|
||||
expectedDate: 'August 1, 2021 00:00:00',
|
||||
},
|
||||
{
|
||||
description: "returns start day for 'week' duration",
|
||||
inputDuration: 'week',
|
||||
inputDate: 'August 21, 2021 20:00:00',
|
||||
expectedDate: 'August 15, 2021 00:00:00',
|
||||
},
|
||||
{
|
||||
description: "returns start day for 'year' duration",
|
||||
inputDuration: 'year',
|
||||
inputDate: 'August 21, 2021 20:00:00',
|
||||
expectedDate: 'January 1, 2021 00:00:00',
|
||||
},
|
||||
]
|
||||
|
||||
testsParams.map((testParams) =>
|
||||
it(testParams.description, () => {
|
||||
const day: Date = new Date(testParams.inputDate)
|
||||
const expected: Date = new Date(testParams.expectedDate)
|
||||
assert.deepEqual(
|
||||
startDate(testParams.inputDuration, day, false),
|
||||
expected
|
||||
)
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
describe('startDate (week starting Monday)', () => {
|
||||
const testsParams = [
|
||||
{
|
||||
description: "returns start day for 'month' duration",
|
||||
inputDuration: 'month',
|
||||
inputDate: 'August 21, 2021 20:00:00',
|
||||
expectedDate: 'August 1, 2021 00:00:00',
|
||||
},
|
||||
{
|
||||
description: 'returns start day for week duration',
|
||||
inputDuration: 'week',
|
||||
inputDate: 'August 21, 2021 20:00:00',
|
||||
expectedDate: 'August 16, 2021 00:00:00',
|
||||
},
|
||||
{
|
||||
description: "returns start day for 'year' duration",
|
||||
inputDuration: 'year',
|
||||
inputDate: 'August 21, 2021 20:00:00',
|
||||
expectedDate: 'January 1, 2021 00:00:00',
|
||||
},
|
||||
]
|
||||
|
||||
testsParams.map((testParams) =>
|
||||
it(testParams.description, () => {
|
||||
const day: Date = new Date(testParams.inputDate)
|
||||
const expected: Date = new Date(testParams.expectedDate)
|
||||
assert.deepEqual(startDate(testParams.inputDuration, day, true), expected)
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
describe('startDate (week starting Monday)', () => {
|
||||
it('it throws an exception if duration is invalid', () => {
|
||||
const day: Date = new Date('August 21, 2021 20:00:00')
|
||||
expect(() => startDate('invalid duration', day, true)).to.throw(
|
||||
'Invalid duration, expected: "week", "month", "year", got: "invalid duration"'
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe('dateIncrement', () => {
|
||||
const testsParams = [
|
||||
{
|
||||
description: "returns incremented day for 'month' duration",
|
||||
inputDuration: 'month',
|
||||
inputDate: 'August 21, 2021 20:00:00',
|
||||
expectedDate: 'September 21, 2021 20:00:00',
|
||||
},
|
||||
{
|
||||
description: 'returns incremented day for week duration',
|
||||
inputDuration: 'week',
|
||||
inputDate: 'August 21, 2021 20:00:00',
|
||||
expectedDate: 'August 28, 2021 20:00:00',
|
||||
},
|
||||
{
|
||||
description:
|
||||
'returns incremented day for week duration (February w/ 28 days)',
|
||||
inputDuration: 'week',
|
||||
inputDate: 'February 22, 2021 20:00:00',
|
||||
expectedDate: 'March 1, 2021 20:00:00',
|
||||
},
|
||||
{
|
||||
description:
|
||||
'returns incremented day for week duration (February w/ 29 days)',
|
||||
inputDuration: 'week',
|
||||
inputDate: 'February 22, 2020 20:00:00',
|
||||
expectedDate: 'February 29, 2020 20:00:00',
|
||||
},
|
||||
{
|
||||
description: "returns incremented day for 'year' duration",
|
||||
inputDuration: 'year',
|
||||
inputDate: 'August 21, 2021 20:00:00',
|
||||
expectedDate: 'August 21, 2022 20:00:00',
|
||||
},
|
||||
{
|
||||
description: "returns incremented day for 'year' duration (February, 29)",
|
||||
inputDuration: 'year',
|
||||
inputDate: 'February 29, 2020 20:00:00',
|
||||
expectedDate: 'February 28, 2021 20:00:00',
|
||||
},
|
||||
]
|
||||
|
||||
testsParams.map((testParams) =>
|
||||
it(testParams.description, () => {
|
||||
const day: Date = new Date(testParams.inputDate)
|
||||
const expected: Date = new Date(testParams.expectedDate)
|
||||
assert.deepEqual(incrementDate(testParams.inputDuration, day), expected)
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
describe('dateIncrement', () => {
|
||||
it('it throws an exception if duration is invalid', () => {
|
||||
const day: Date = new Date('August 21, 2021 20:00:00')
|
||||
expect(() => incrementDate('invalid duration', day)).to.throw(
|
||||
'Invalid duration, expected: "week", "month", "year", got: "invalid duration"'
|
||||
)
|
||||
})
|
||||
})
|
516
fittrackee_client/tests/unit/utils/statistics.spec.ts
Normal file
516
fittrackee_client/tests/unit/utils/statistics.spec.ts
Normal file
@ -0,0 +1,516 @@
|
||||
import { assert } from 'chai'
|
||||
|
||||
import { sports } from './constants'
|
||||
|
||||
import {
|
||||
IStatisticsChartData,
|
||||
TStatisticsDatasets,
|
||||
TStatisticsFromApi,
|
||||
} from '@/types/statistics'
|
||||
import { formatStats, getDateKeys, getDatasets } from '@/utils/statistics'
|
||||
|
||||
describe('getDateKeys (week starting Sunday)', () => {
|
||||
const testsParams = [
|
||||
{
|
||||
description: 'returns date keys for "week" duration',
|
||||
inputParams: {
|
||||
duration: 'week',
|
||||
start: new Date('August 01, 2021 00:00:00'),
|
||||
end: new Date('August 31, 2021 23:59:59.999'),
|
||||
},
|
||||
inputWeekStartingMonday: false,
|
||||
expected: [
|
||||
'August 1, 2021 00:00:00',
|
||||
'August 8, 2021 00:00:00',
|
||||
'August 15, 2021 00:00:00',
|
||||
'August 22, 2021 00:00:00',
|
||||
'August 29, 2021 00:00:00',
|
||||
],
|
||||
},
|
||||
{
|
||||
description: 'returns date keys for "month" duration',
|
||||
inputParams: {
|
||||
duration: 'month',
|
||||
start: new Date('September 01, 2020 00:00:00'),
|
||||
end: new Date('August 31, 2021 23:59:59.999'),
|
||||
},
|
||||
inputWeekStartingMonday: false,
|
||||
expected: [
|
||||
'September 1, 2020 00:00:00',
|
||||
'October 1, 2020 00:00:00',
|
||||
'November 1, 2020 00:00:00',
|
||||
'December 1, 2020 00:00:00',
|
||||
'January 1, 2021 00:00:00',
|
||||
'February 1, 2021 00:00:00',
|
||||
'March 1, 2021 00:00:00',
|
||||
'April 1, 2021 00:00:00',
|
||||
'May 1, 2021 00:00:00',
|
||||
'June 1, 2021 00:00:00',
|
||||
'July 1, 2021 00:00:00',
|
||||
'August 1, 2021 00:00:00',
|
||||
],
|
||||
},
|
||||
{
|
||||
description: 'returns date keys for "month" duration',
|
||||
inputParams: {
|
||||
duration: 'year',
|
||||
start: new Date('January 1, 2012 00:00:00'),
|
||||
end: new Date('December 31, 2021 23:59:59.999'),
|
||||
},
|
||||
inputWeekStartingMonday: false,
|
||||
expected: [
|
||||
'January 1, 2012 00:00:00',
|
||||
'January 1, 2013 00:00:00',
|
||||
'January 1, 2014 00:00:00',
|
||||
'January 1, 2015 00:00:00',
|
||||
'January 1, 2016 00:00:00',
|
||||
'January 1, 2017 00:00:00',
|
||||
'January 1, 2018 00:00:00',
|
||||
'January 1, 2019 00:00:00',
|
||||
'January 1, 2020 00:00:00',
|
||||
'January 1, 2021 00:00:00',
|
||||
],
|
||||
},
|
||||
]
|
||||
|
||||
testsParams.map((testParams) =>
|
||||
it(testParams.description, () => {
|
||||
const expected: Date[] = testParams.expected.map(
|
||||
(date_string: string) => new Date(date_string)
|
||||
)
|
||||
assert.deepEqual(getDateKeys(testParams.inputParams, false), expected)
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
describe('getDateKeys (week starting Monday)', () => {
|
||||
const testsParams = [
|
||||
{
|
||||
description: 'returns date keys for "week" duration',
|
||||
inputParams: {
|
||||
duration: 'week',
|
||||
start: new Date('August 01, 2021 00:00:00'),
|
||||
end: new Date('August 31, 2021 23:59:59.999'),
|
||||
},
|
||||
inputWeekStartingMonday: false,
|
||||
expected: [
|
||||
'July 26, 2021 00:00:00',
|
||||
'August 2, 2021 00:00:00',
|
||||
'August 9, 2021 00:00:00',
|
||||
'August 16, 2021 00:00:00',
|
||||
'August 23, 2021 00:00:00',
|
||||
'August 30, 2021 00:00:00',
|
||||
],
|
||||
},
|
||||
{
|
||||
description: 'returns date keys for "month" duration',
|
||||
inputParams: {
|
||||
duration: 'month',
|
||||
start: new Date('September 01, 2020 00:00:00'),
|
||||
end: new Date('August 31, 2021 23:59:59.999'),
|
||||
},
|
||||
inputWeekStartingMonday: false,
|
||||
expected: [
|
||||
'September 1, 2020 00:00:00',
|
||||
'October 1, 2020 00:00:00',
|
||||
'November 1, 2020 00:00:00',
|
||||
'December 1, 2020 00:00:00',
|
||||
'January 1, 2021 00:00:00',
|
||||
'February 1, 2021 00:00:00',
|
||||
'March 1, 2021 00:00:00',
|
||||
'April 1, 2021 00:00:00',
|
||||
'May 1, 2021 00:00:00',
|
||||
'June 1, 2021 00:00:00',
|
||||
'July 1, 2021 00:00:00',
|
||||
'August 1, 2021 00:00:00',
|
||||
],
|
||||
},
|
||||
{
|
||||
description: 'returns date keys for "month" duration',
|
||||
inputParams: {
|
||||
duration: 'year',
|
||||
start: new Date('January 1, 2012 00:00:00'),
|
||||
end: new Date('December 31, 2021 23:59:59.999'),
|
||||
},
|
||||
inputWeekStartingMonday: false,
|
||||
expected: [
|
||||
'January 1, 2012 00:00:00',
|
||||
'January 1, 2013 00:00:00',
|
||||
'January 1, 2014 00:00:00',
|
||||
'January 1, 2015 00:00:00',
|
||||
'January 1, 2016 00:00:00',
|
||||
'January 1, 2017 00:00:00',
|
||||
'January 1, 2018 00:00:00',
|
||||
'January 1, 2019 00:00:00',
|
||||
'January 1, 2020 00:00:00',
|
||||
'January 1, 2021 00:00:00',
|
||||
],
|
||||
},
|
||||
]
|
||||
|
||||
testsParams.map((testParams) =>
|
||||
it(testParams.description, () => {
|
||||
const expected: Date[] = testParams.expected.map(
|
||||
(date_string: string) => new Date(date_string)
|
||||
)
|
||||
assert.deepEqual(getDateKeys(testParams.inputParams, true), expected)
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
describe('getDatasets', () => {
|
||||
it('returns chart datasets (when no displayed data provided)', () => {
|
||||
const expected: TStatisticsDatasets = {
|
||||
nb_workouts: [
|
||||
{
|
||||
label: 'Cycling (Sport)',
|
||||
backgroundColor: [],
|
||||
data: [],
|
||||
},
|
||||
{
|
||||
label: 'Cycling (Transport)',
|
||||
backgroundColor: [],
|
||||
data: [],
|
||||
},
|
||||
{
|
||||
label: 'Hiking',
|
||||
backgroundColor: [],
|
||||
data: [],
|
||||
},
|
||||
],
|
||||
total_distance: [
|
||||
{
|
||||
label: 'Cycling (Sport)',
|
||||
backgroundColor: [],
|
||||
data: [],
|
||||
},
|
||||
{
|
||||
label: 'Cycling (Transport)',
|
||||
backgroundColor: [],
|
||||
data: [],
|
||||
},
|
||||
{
|
||||
label: 'Hiking',
|
||||
backgroundColor: [],
|
||||
data: [],
|
||||
},
|
||||
],
|
||||
total_duration: [
|
||||
{
|
||||
label: 'Cycling (Sport)',
|
||||
backgroundColor: [],
|
||||
data: [],
|
||||
},
|
||||
{
|
||||
label: 'Cycling (Transport)',
|
||||
backgroundColor: [],
|
||||
data: [],
|
||||
},
|
||||
{
|
||||
label: 'Hiking',
|
||||
backgroundColor: [],
|
||||
data: [],
|
||||
},
|
||||
],
|
||||
}
|
||||
assert.deepEqual(getDatasets(sports), expected)
|
||||
})
|
||||
it('returns chart datasets with only displayed sports', () => {
|
||||
const expected: TStatisticsDatasets = {
|
||||
nb_workouts: [
|
||||
{
|
||||
label: 'Cycling (Transport)',
|
||||
backgroundColor: [],
|
||||
data: [],
|
||||
},
|
||||
],
|
||||
total_distance: [
|
||||
{
|
||||
label: 'Cycling (Transport)',
|
||||
backgroundColor: [],
|
||||
data: [],
|
||||
},
|
||||
],
|
||||
total_duration: [
|
||||
{
|
||||
label: 'Cycling (Transport)',
|
||||
backgroundColor: [],
|
||||
data: [],
|
||||
},
|
||||
],
|
||||
}
|
||||
assert.deepEqual(getDatasets([sports[1]]), expected)
|
||||
})
|
||||
})
|
||||
|
||||
describe('formatStats', () => {
|
||||
it('returns empty datasets if no data and no displayed sport provided', () => {
|
||||
const inputStats: TStatisticsFromApi = {}
|
||||
const inputParams = {
|
||||
duration: 'month',
|
||||
start: new Date('May 1, 2021 00:00:00'),
|
||||
end: new Date('July 31, 2021 23:59:59.999'),
|
||||
}
|
||||
const expected: IStatisticsChartData = {
|
||||
labels: ['2021-05', '2021-06', '2021-07'],
|
||||
datasets: {
|
||||
nb_workouts: [
|
||||
{
|
||||
label: 'Cycling (Sport)',
|
||||
backgroundColor: [],
|
||||
data: [null, null, null],
|
||||
},
|
||||
{
|
||||
label: 'Cycling (Transport)',
|
||||
backgroundColor: [],
|
||||
data: [null, null, null],
|
||||
},
|
||||
{
|
||||
label: 'Hiking',
|
||||
backgroundColor: [],
|
||||
data: [null, null, null],
|
||||
},
|
||||
],
|
||||
total_distance: [
|
||||
{
|
||||
label: 'Cycling (Sport)',
|
||||
backgroundColor: [],
|
||||
data: [null, null, null],
|
||||
},
|
||||
{
|
||||
label: 'Cycling (Transport)',
|
||||
backgroundColor: [],
|
||||
data: [null, null, null],
|
||||
},
|
||||
{
|
||||
label: 'Hiking',
|
||||
backgroundColor: [],
|
||||
data: [null, null, null],
|
||||
},
|
||||
],
|
||||
total_duration: [
|
||||
{
|
||||
label: 'Cycling (Sport)',
|
||||
backgroundColor: [],
|
||||
data: [null, null, null],
|
||||
},
|
||||
{
|
||||
label: 'Cycling (Transport)',
|
||||
backgroundColor: [],
|
||||
data: [null, null, null],
|
||||
},
|
||||
{
|
||||
label: 'Hiking',
|
||||
backgroundColor: [],
|
||||
data: [null, null, null],
|
||||
},
|
||||
],
|
||||
},
|
||||
}
|
||||
assert.deepEqual(
|
||||
formatStats(inputParams, false, sports, [], inputStats),
|
||||
expected
|
||||
)
|
||||
})
|
||||
|
||||
it('returns empty datasets if no data and displayed sport provided', () => {
|
||||
const inputStats: TStatisticsFromApi = {}
|
||||
const inputParams = {
|
||||
duration: 'month',
|
||||
start: new Date('May 1, 2021 00:00:00'),
|
||||
end: new Date('July 31, 2021 23:59:59.999'),
|
||||
}
|
||||
const expected: IStatisticsChartData = {
|
||||
labels: ['2021-05', '2021-06', '2021-07'],
|
||||
datasets: {
|
||||
nb_workouts: [
|
||||
{
|
||||
label: 'Cycling (Transport)',
|
||||
backgroundColor: [],
|
||||
data: [null, null, null],
|
||||
},
|
||||
],
|
||||
total_distance: [
|
||||
{
|
||||
label: 'Cycling (Transport)',
|
||||
backgroundColor: [],
|
||||
data: [null, null, null],
|
||||
},
|
||||
],
|
||||
total_duration: [
|
||||
{
|
||||
label: 'Cycling (Transport)',
|
||||
backgroundColor: [],
|
||||
data: [null, null, null],
|
||||
},
|
||||
],
|
||||
},
|
||||
}
|
||||
assert.deepEqual(
|
||||
formatStats(inputParams, false, sports, [2], inputStats),
|
||||
expected
|
||||
)
|
||||
})
|
||||
|
||||
it('returns empty datasets if data and no displayed sport provided', () => {
|
||||
const inputStats: TStatisticsFromApi = {
|
||||
'2021-05': {
|
||||
1: {
|
||||
nb_workouts: 1,
|
||||
total_distance: 10,
|
||||
total_duration: 3000,
|
||||
},
|
||||
},
|
||||
'2021-06': {
|
||||
1: {
|
||||
nb_workouts: 1,
|
||||
total_distance: 15,
|
||||
total_duration: 3500,
|
||||
},
|
||||
2: {
|
||||
nb_workouts: 2,
|
||||
total_distance: 20,
|
||||
total_duration: 3000,
|
||||
},
|
||||
},
|
||||
'2021-07': {
|
||||
3: {
|
||||
nb_workouts: 2,
|
||||
total_distance: 12,
|
||||
total_duration: 5000,
|
||||
},
|
||||
},
|
||||
}
|
||||
const inputParams = {
|
||||
duration: 'month',
|
||||
start: new Date('May 1, 2021 00:00:00'),
|
||||
end: new Date('July 31, 2021 23:59:59.999'),
|
||||
}
|
||||
const expected: IStatisticsChartData = {
|
||||
labels: ['2021-05', '2021-06', '2021-07'],
|
||||
datasets: {
|
||||
nb_workouts: [
|
||||
{
|
||||
label: 'Cycling (Sport)',
|
||||
backgroundColor: [],
|
||||
data: [1, 1, null],
|
||||
},
|
||||
{
|
||||
label: 'Cycling (Transport)',
|
||||
backgroundColor: [],
|
||||
data: [null, 2, null],
|
||||
},
|
||||
{
|
||||
label: 'Hiking',
|
||||
backgroundColor: [],
|
||||
data: [null, null, 2],
|
||||
},
|
||||
],
|
||||
total_distance: [
|
||||
{
|
||||
label: 'Cycling (Sport)',
|
||||
backgroundColor: [],
|
||||
data: [10, 15, null],
|
||||
},
|
||||
{
|
||||
label: 'Cycling (Transport)',
|
||||
backgroundColor: [],
|
||||
data: [null, 20, null],
|
||||
},
|
||||
{
|
||||
label: 'Hiking',
|
||||
backgroundColor: [],
|
||||
data: [null, null, 12],
|
||||
},
|
||||
],
|
||||
total_duration: [
|
||||
{
|
||||
label: 'Cycling (Sport)',
|
||||
backgroundColor: [],
|
||||
data: [3000, 3500, null],
|
||||
},
|
||||
{
|
||||
label: 'Cycling (Transport)',
|
||||
backgroundColor: [],
|
||||
data: [null, 3000, null],
|
||||
},
|
||||
{
|
||||
label: 'Hiking',
|
||||
backgroundColor: [],
|
||||
data: [null, null, 5000],
|
||||
},
|
||||
],
|
||||
},
|
||||
}
|
||||
assert.deepEqual(
|
||||
formatStats(inputParams, false, sports, [], inputStats),
|
||||
expected
|
||||
)
|
||||
})
|
||||
|
||||
it('returns empty datasets if data and displayed sport provided', () => {
|
||||
const inputStats: TStatisticsFromApi = {
|
||||
'2021-05': {
|
||||
1: {
|
||||
nb_workouts: 1,
|
||||
total_distance: 10,
|
||||
total_duration: 3000,
|
||||
},
|
||||
},
|
||||
'2021-06': {
|
||||
1: {
|
||||
nb_workouts: 1,
|
||||
total_distance: 15,
|
||||
total_duration: 3500,
|
||||
},
|
||||
2: {
|
||||
nb_workouts: 2,
|
||||
total_distance: 20,
|
||||
total_duration: 3000,
|
||||
},
|
||||
},
|
||||
'2021-07': {
|
||||
3: {
|
||||
nb_workouts: 2,
|
||||
total_distance: 20,
|
||||
total_duration: 3000,
|
||||
},
|
||||
},
|
||||
}
|
||||
const inputParams = {
|
||||
duration: 'month',
|
||||
start: new Date('May 1, 2021 00:00:00'),
|
||||
end: new Date('July 31, 2021 23:59:59.999'),
|
||||
}
|
||||
const expected: IStatisticsChartData = {
|
||||
labels: ['2021-05', '2021-06', '2021-07'],
|
||||
datasets: {
|
||||
nb_workouts: [
|
||||
{
|
||||
label: 'Cycling (Sport)',
|
||||
backgroundColor: [],
|
||||
data: [1, 1, null],
|
||||
},
|
||||
],
|
||||
total_distance: [
|
||||
{
|
||||
label: 'Cycling (Sport)',
|
||||
backgroundColor: [],
|
||||
data: [10, 15, null],
|
||||
},
|
||||
],
|
||||
total_duration: [
|
||||
{
|
||||
label: 'Cycling (Sport)',
|
||||
backgroundColor: [],
|
||||
data: [3000, 3500, null],
|
||||
},
|
||||
],
|
||||
},
|
||||
}
|
||||
assert.deepEqual(
|
||||
formatStats(inputParams, false, sports, [1], inputStats),
|
||||
expected
|
||||
)
|
||||
})
|
||||
})
|
Reference in New Issue
Block a user