Throw if args are wrong
This commit is contained in:
+24
-1
@@ -4,7 +4,8 @@ import getPreOvulatoryPhase from './pre-ovulatory'
|
||||
import { LocalDate } from 'js-joda'
|
||||
|
||||
export default function ({ cycle, previousCycle }) {
|
||||
// TODO check for basic stuff, throw if nonexistent
|
||||
throwIfArgsAreNotInRequiredFormat(cycle, previousCycle)
|
||||
|
||||
const status = {
|
||||
assumeFertility: true,
|
||||
phases: {}
|
||||
@@ -67,3 +68,25 @@ export default function ({ cycle, previousCycle }) {
|
||||
|
||||
return status
|
||||
}
|
||||
|
||||
function throwIfArgsAreNotInRequiredFormat(cycle, previousCycle) {
|
||||
if (!Array.isArray(cycle) || !cycle.length) throw new Error('Please provide all cycle days as array')
|
||||
if (!Array.isArray(previousCycle) || !previousCycle.length) throw new Error('Please provide previous cycle days as array')
|
||||
if (
|
||||
!cycle[0].bleeding || typeof cycle[0].bleeding.value != 'number' ||
|
||||
!previousCycle[0].bleeding || typeof previousCycle[0].bleeding.value != 'number'
|
||||
) throw new Error('Cycle must start with bleeding')
|
||||
|
||||
if ([cycle, previousCycle].some(cycle => {
|
||||
return cycle.some(day => {
|
||||
if (!day.date) return true
|
||||
try {
|
||||
LocalDate.parse(day.date)
|
||||
} catch(err) {
|
||||
throw new Error('Please provide dates in ISO8601 format')
|
||||
}
|
||||
if (day.temperature && typeof day.temperature.value != 'number') return true
|
||||
if (day.mucus && typeof day.mucus.value != 'number') return true
|
||||
})
|
||||
})) throw new Error('Cycle days are not in correct format')
|
||||
}
|
||||
@@ -10,10 +10,12 @@ function convertToSymptoFormat(val) {
|
||||
const cycleWithTempShift = [36.6, 36.6, 36.6, 36.6, 36.6, 36.6, 36.8, 36.8, 36.8]
|
||||
.map(num => ({ date: '2018-06-01', temperature: num }))
|
||||
.map(convertToSymptoFormat)
|
||||
cycleWithTempShift.unshift({date: '2018-05-30', bleeding: { value: 2 }})
|
||||
|
||||
const cycleWithoutTempShift = [36.6, 36.6, 36.6, 36.6, 36.6, 36.6, 36.8, 36.8]
|
||||
.map(num => ({ date: '2018-06-01', temperature: num }))
|
||||
.map(convertToSymptoFormat)
|
||||
cycleWithoutTempShift.unshift({date: '2018-05-30', bleeding: { value: 2 }})
|
||||
|
||||
const cycleWithTempAndMucusShift = [
|
||||
{ date: '2018-06-01', temperature: 36.6, bleeding: 2 },
|
||||
|
||||
@@ -7,7 +7,6 @@ import {
|
||||
const expect = chai.expect
|
||||
|
||||
describe('sympto', () => {
|
||||
describe('evaluating mucus and temperature shift together', () => {
|
||||
describe('with no previous higher measurement', () => {
|
||||
it('with no shifts detects only peri-ovulatory', function () {
|
||||
const status = getSensiplanStatus({
|
||||
@@ -50,7 +49,6 @@ describe('sympto', () => {
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
describe('with previous higher measurement', () => {
|
||||
describe('with no shifts detects pre-ovulatory phase', function () {
|
||||
it('according to 5-day-rule', function () {
|
||||
@@ -117,4 +115,61 @@ describe('sympto', () => {
|
||||
|
||||
})
|
||||
})
|
||||
describe('when args are wrong', () => {
|
||||
it('throws when arg object is not in right format', () => {
|
||||
const wrongObject = { hello: 'world' }
|
||||
expect(() => getSensiplanStatus(wrongObject)).to.throw('cycle days as array')
|
||||
})
|
||||
it('throws if cycle array is empty', () => {
|
||||
expect(() => getSensiplanStatus({cycle: []})).to.throw('cycle days as array')
|
||||
})
|
||||
it('throws if cycle days are not in right format', () => {
|
||||
expect(() => getSensiplanStatus({
|
||||
cycle: [{
|
||||
hello: 'world',
|
||||
bleeding: { value: 0 }
|
||||
}],
|
||||
previousCycle: [{
|
||||
date: '1992-09-09',
|
||||
bleeding: { value: 0 }
|
||||
}]
|
||||
})).to.throw('correct format')
|
||||
expect(() => getSensiplanStatus({
|
||||
cycle: [{
|
||||
date: '2018-04-13',
|
||||
temperature: {value: '35'},
|
||||
bleeding: { value: 0 }
|
||||
}],
|
||||
previousCycle: [{
|
||||
date: '1992-09-09',
|
||||
bleeding: { value: 0 }
|
||||
}]
|
||||
})).to.throw('correct format')
|
||||
expect(() => getSensiplanStatus({
|
||||
cycle: [{
|
||||
date: '09-14-2017',
|
||||
bleeding: { value: 0 }
|
||||
}],
|
||||
previousCycle: [{
|
||||
date: '1992-09-09',
|
||||
bleeding: { value: 0 }
|
||||
}]
|
||||
})).to.throw('ISO')
|
||||
})
|
||||
it('throws if first cycle day does not have bleeding value', () => {
|
||||
expect(() => getSensiplanStatus({
|
||||
cycle: [{
|
||||
date: '2017-01-01',
|
||||
bleeding: {
|
||||
value: 'medium'
|
||||
}
|
||||
}],
|
||||
previousCycle: [
|
||||
{
|
||||
date: '2017-09-23',
|
||||
}
|
||||
]
|
||||
})).to.throw('start with bleeding')
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user