diff --git a/lib/cycle.js b/lib/cycle.js index 872560d..dcac869 100644 --- a/lib/cycle.js +++ b/lib/cycle.js @@ -47,8 +47,8 @@ export default function config(opts) { function getPreviousCycle(dateString) { const cycleStart = getLastMensesStartForDay(dateString) if (!cycleStart) return null - const i = cycleDaysSortedByDate.indexOf(cycleStart) - const earlierCycleStart = cycleDaysSortedByDate[i - 1] + const i = cycleStartsSortedByDate.indexOf(cycleStart) + const earlierCycleStart = cycleStartsSortedByDate[i + 1] if (!earlierCycleStart) return null return getCycleForStartDay(earlierCycleStart) } diff --git a/test/cycle.spec.js b/test/cycle.spec.js index 6af3208..8508748 100644 --- a/test/cycle.spec.js +++ b/test/cycle.spec.js @@ -5,8 +5,6 @@ import cycleModule from '../lib/cycle' const expect = chai.expect chai.use(dirtyChai) -// TODO getPreviousCycle - describe('getCycleDayNumber', () => { it('works for a simple example', () => { const cycleStarts = [{ @@ -77,6 +75,179 @@ describe('getCycleDayNumber', () => { }) +describe('getPreviousCycle', () => { + it('gets previous cycle', () => { + const cycleDaysSortedByDate = [ + { + date: '2018-07-05', + bleeding: { value: 2 } + }, + { + date: '2018-06-05', + bleeding: { value: 2 } + }, + { + date: '2018-05-05', + mucus: { value: 2 } + }, + { + date: '2018-05-04', + bleeding: { value: 2 } + }, + { + date: '2018-05-03', + bleeding: { value: 2 } + }, + { + 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 } + }, + ] + + const cycleStarts = [ + '2018-07-05', + '2018-06-05', + '2018-05-03', + '2018-04-02' + ] + + const { getPreviousCycle } = cycleModule({ + cycleDaysSortedByDate, + cycleStartsSortedByDate: cycleDaysSortedByDate.filter(d => { + return cycleStarts.includes(d.date) + }) + }) + const result = getPreviousCycle('2018-06-08') + expect(result).to.eql([ + { + date: '2018-05-05', + mucus: { value: 2 } + }, + { + date: '2018-05-04', + bleeding: { value: 2 } + }, + { + date: '2018-05-03', + bleeding: { value: 2 } + } + ]) + }) + + it('returns null when target days is not in a cyle', () => { + const cycleDaysSortedByDate = [ + { + date: '2018-07-05', + }, + { + date: '2018-06-05', + }, + { + date: '2018-05-05', + }, + { + date: '2018-05-04', + }, + { + date: '2018-05-03', + }, + { + date: '2018-04-05', + }, + { + date: '2018-04-04', + mucus: { value: 2 } + }, + { + date: '2018-04-03', + }, + { + date: '2018-04-02', + }, + ] + + const cycleStarts = [] + + const { getPreviousCycle } = cycleModule({ + cycleDaysSortedByDate, + cycleStartsSortedByDate: cycleDaysSortedByDate.filter(d => { + return cycleStarts.includes(d.date) + }) + }) + const result = getPreviousCycle('2018-06-08') + expect(result).to.eql(null) + }) + + it('returns null when there is no previous cycle', () => { + const cycleDaysSortedByDate = [ + { + date: '2018-07-05', + bleeding: { value: 2 } + }, + { + date: '2018-06-05', + bleeding: { value: 2 } + }, + { + date: '2018-05-05', + mucus: { value: 2 } + }, + { + date: '2018-05-04', + bleeding: { value: 2 } + }, + { + date: '2018-05-03', + bleeding: { value: 2 } + }, + { + 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 } + }, + ] + + const cycleStarts = [ + '2018-07-05', + '2018-06-05', + '2018-05-03', + '2018-04-02' + ] + + const { getPreviousCycle } = cycleModule({ + cycleDaysSortedByDate, + cycleStartsSortedByDate: cycleDaysSortedByDate.filter(d => { + return cycleStarts.includes(d.date) + }) + }) + const result = getPreviousCycle('2018-04-18') + expect(result).to.eql(null) + }) +}) + describe('getCyclesBefore', () => { it('gets previous cycles', () => { const cycleDaysSortedByDate = [