Skip to content

Commit

Permalink
fix(trackerUtils): update getAggregatedCompletions to accept 2 dates
Browse files Browse the repository at this point in the history
  • Loading branch information
Clm-Roig committed Jun 13, 2022
1 parent ba44fe4 commit 35d6e6b
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 6 deletions.
98 changes: 97 additions & 1 deletion src/store/trackers/FAKE_DATA.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { subDays } from 'date-fns';
import { addDays, subDays } from 'date-fns';

import { SEVEN_DAYS_AGO_STRING } from '../../config/Constants';
import Tracker from '../../models/Tracker';
Expand Down Expand Up @@ -143,3 +143,99 @@ export const testEntry3: TrackerEntry = {
date: SEVEN_DAYS_AGO_STRING,
trackerId: testTracker1Id
};

export const todayEntries = [
{
id: '2',
completions: [
{
quantity: 3,
unit: 'x'
},
{
quantity: 6,
unit: 'y'
}
],
date: new Date().toString(),
trackerId: '1'
} as TrackerEntry,
{
id: '3',
completions: [
{
quantity: 7,
unit: 'x'
},
{
quantity: 10,
unit: 'y'
}
],
date: new Date().toString(),
trackerId: '1'
} as TrackerEntry
];

export const variousEntries = [
{
id: '2',
completions: [
{
quantity: 3,
unit: 'x'
},
{
quantity: 6,
unit: 'y'
}
],
date: subDays(new Date(), 3).toString(),
trackerId: '1'
} as TrackerEntry,
{
id: '3',
completions: [
{
quantity: 7,
unit: 'x'
},
{
quantity: 10,
unit: 'y'
}
],
date: addDays(new Date(), 3).toString(),
trackerId: '1'
} as TrackerEntry,
{
id: '4',
completions: [
{
quantity: 7,
unit: 'x'
},
{
quantity: 10,
unit: 'y'
}
],
date: new Date().toString(),
trackerId: '1'
} as TrackerEntry,
{
id: '5',
completions: [
{
quantity: 7,
unit: 'x'
},
{
quantity: 10,
unit: 'y'
}
],
date: new Date().toString(),
trackerId: '1'
} as TrackerEntry
];
49 changes: 47 additions & 2 deletions src/store/trackers/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,61 @@
import { subDays } from 'date-fns';
import { addDays, subDays } from 'date-fns';

import TrackerStatus from '../../models/TrackerStatus';
import { testEntry1, testEntry2, testTracker1 } from './FAKE_DATA';
import { testEntry1, testEntry2, testTracker1, todayEntries, variousEntries } from './FAKE_DATA';
import {
computeIfDone,
computeNewStatus,
computeRemainingDays,
formatTrackers,
getAggregatedCompletions,
removeArchivedTrackers,
removeHiddenTrackers
} from './utils';

describe('getAggregatedCompletions()', () => {
it('should return the aggregated completions from an entries list', () => {
expect(getAggregatedCompletions(todayEntries)).toStrictEqual([
{
quantity: 10,
unit: 'x'
},
{
quantity: 16,
unit: 'y'
}
]);
});

it('should return the aggregated completions of today from an entries list', () => {
expect(getAggregatedCompletions(variousEntries, new Date())).toStrictEqual([
// Entries 4 & 5 with same quantity (7 & 10)
{
quantity: 14,
unit: 'x'
},
{
quantity: 20,
unit: 'y'
}
]);
});
it('should return the aggregated completions between tw odates from an entries list', () => {
expect(
getAggregatedCompletions(variousEntries, subDays(new Date(), 4), addDays(new Date(), 4))
).toStrictEqual([
// All entries
{
quantity: 24,
unit: 'x'
},
{
quantity: 36,
unit: 'y'
}
]);
});
});

describe('computeRemainingDays()', () => {
it('should return the appropriate remaining days', () => {
expect(computeRemainingDays(subDays(new Date(), 10).toString(), 3)).toBe(-7); // started in the past, finished in the past
Expand Down
22 changes: 19 additions & 3 deletions src/store/trackers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,26 @@ const aggregateCompletions = (completions: Completion[]) => {
}, []);
};

export const getAggregatedCompletions = (entries: TrackerEntry[], date?: Date): Completion[] => {
/**
* - If no date provided, return the aggregated completions of the provided entries.
* - If only one date is provided, return the completions of this day.
* - If two dates are provided, return the completions between both days (included).
*
* @param {TrackerEntry[]} entries
* @param {Date?} date1
* @param {Date?} date2
* @return {*} {Completion[]}
*/
export const getAggregatedCompletions = (
entries: TrackerEntry[],
date1?: Date,
date2?: Date
): Completion[] => {
let filteredEntries = entries;
if (date) {
filteredEntries = entries.filter((e) => isSameDay(new Date(e.date), date));
if (date1 && date2) {
filteredEntries = entries.filter((e) => new Date(e.date) > date1 && new Date(e.date) < date2);
} else if (date1 && !date2) {
filteredEntries = entries.filter((e) => isSameDay(new Date(e.date), date1));
}
return aggregateCompletions(filteredEntries.flatMap((e) => e.completions));
};
Expand Down

0 comments on commit 35d6e6b

Please sign in to comment.