From 6b0d20227243ab2a6a79c301f0c89caaf6439ef3 Mon Sep 17 00:00:00 2001 From: Julia Friesel Date: Fri, 21 Dec 2018 09:42:20 +0100 Subject: [PATCH] Respect maxcyclelength in getCycleForStartDay --- lib/cycle.js | 7 +++++-- test/cycle.spec.js | 43 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/lib/cycle.js b/lib/cycle.js index b675f52..8fd7bd1 100644 --- a/lib/cycle.js +++ b/lib/cycle.js @@ -65,17 +65,20 @@ export default function config(opts) { } function getCycleForCycleStartDay(startDay) { + // TODO this needs to be made cycle length aware. all other places can go? const cycleStartIndex = cycleDaysSortedByDate.indexOf(startDay) const i = cycleStartsSortedByDate.indexOf(startDay) const nextMensesStart = cycleStartsSortedByDate[i - 1] + let cycle if (nextMensesStart) { - return cycleDaysSortedByDate.slice( + cycle = cycleDaysSortedByDate.slice( cycleDaysSortedByDate.indexOf(nextMensesStart) + 1, cycleStartIndex + 1, ) } else { - return cycleDaysSortedByDate.slice(0, cycleStartIndex + 1) + cycle = cycleDaysSortedByDate.slice(0, cycleStartIndex + 1) } + return cycle.length > maxCycleLength ? null : cycle } function getCycleForDay(dayOrDate) { diff --git a/test/cycle.spec.js b/test/cycle.spec.js index 77bf379..232e2bc 100644 --- a/test/cycle.spec.js +++ b/test/cycle.spec.js @@ -71,8 +71,6 @@ describe('getCycleDayNumber', () => { const result = getCycleDayNumber(targetDate) expect(result).to.be.null() }) - - }) describe('getPreviousCycle', () => { @@ -433,6 +431,47 @@ describe('getCycleForDay', () => { expect(result).to.eql(null) }) + it('returns null if the cycle is longer than the max', () => { + const { getCycleForDay } = cycleModule({ + cycleDaysSortedByDate, + cycleStartsSortedByDate: cycleDaysSortedByDate.filter(d => { + return cycleStarts.includes(d.date) + }), + maxCycleLength: 3 + }) + const result = getCycleForDay('2018-04-04') + expect(result).to.eql(null) + }) + + it('returns the cycle if the cycle is shorter or equal max', () => { + const { getCycleForDay } = cycleModule({ + cycleDaysSortedByDate, + cycleStartsSortedByDate: cycleDaysSortedByDate.filter(d => { + return cycleStarts.includes(d.date) + }), + maxCycleLength: 4 + }) + const result = getCycleForDay('2018-04-04') + expect(result).to.eql([ + { + date: '2018-04-05', + mucus: { value: 2 } + }, + { + date: '2018-04-04', + mucus: { value: 2 } + }, + { + date: '2018-04-03', + mucus: { value: 2 } + }, + { + date: '2018-04-02', + bleeding: { value: 2 } + }, + ]) + }) + it('gets cycle for day', () => { const result = getCycleForDay('2018-04-04') expect(result.length).to.eql(4)