Use assert for args check
This commit is contained in:
+12
-18
@@ -2,6 +2,7 @@ import getTemperatureShift from './temperature'
|
|||||||
import getMucusShift from './mucus'
|
import getMucusShift from './mucus'
|
||||||
import getPreOvulatoryPhase from './pre-ovulatory'
|
import getPreOvulatoryPhase from './pre-ovulatory'
|
||||||
import { LocalDate } from 'js-joda'
|
import { LocalDate } from 'js-joda'
|
||||||
|
import assert from 'assert'
|
||||||
|
|
||||||
export default function ({ cycle, previousCycle }) {
|
export default function ({ cycle, previousCycle }) {
|
||||||
throwIfArgsAreNotInRequiredFormat(cycle, previousCycle)
|
throwIfArgsAreNotInRequiredFormat(cycle, previousCycle)
|
||||||
@@ -70,23 +71,16 @@ export default function ({ cycle, previousCycle }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function throwIfArgsAreNotInRequiredFormat(cycle, previousCycle) {
|
function throwIfArgsAreNotInRequiredFormat(cycle, previousCycle) {
|
||||||
if (!Array.isArray(cycle) || !cycle.length) throw new Error('Please provide all cycle days as array')
|
[cycle, previousCycle].forEach(cycle => {
|
||||||
if (!Array.isArray(previousCycle) || !previousCycle.length) throw new Error('Please provide previous cycle days as array')
|
assert.ok(Array.isArray(cycle))
|
||||||
if (
|
assert.ok(cycle.length > 0)
|
||||||
!cycle[0].bleeding || typeof cycle[0].bleeding.value != 'number' ||
|
assert.equal(typeof cycle[0].bleeding, 'object')
|
||||||
!previousCycle[0].bleeding || typeof previousCycle[0].bleeding.value != 'number'
|
assert.equal(typeof cycle[0].bleeding.value, 'number')
|
||||||
) throw new Error('Cycle must start with bleeding')
|
cycle.forEach(day => {
|
||||||
|
assert.equal(typeof day.date, 'string')
|
||||||
if ([cycle, previousCycle].some(cycle => {
|
assert.doesNotThrow(() => LocalDate.parse(day.date))
|
||||||
return cycle.some(day => {
|
if (day.temperature) assert.equal(typeof day.temperature.value, 'number')
|
||||||
if (!day.date) return true
|
if (day.mucus) assert.equal(typeof day.mucus.value, 'number')
|
||||||
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')
|
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
import chai from 'chai'
|
import chai from 'chai'
|
||||||
import getSensiplanStatus from '../../lib/sympto'
|
import getSensiplanStatus from '../../lib/sympto'
|
||||||
|
import { AssertionError } from 'assert'
|
||||||
import {
|
import {
|
||||||
cycleWithoutTempShift,
|
cycleWithoutTempShift,
|
||||||
cycleWithTempAndMucusShift,
|
cycleWithTempAndMucusShift,
|
||||||
@@ -141,10 +142,10 @@ describe('sympto', () => {
|
|||||||
describe('when args are wrong', () => {
|
describe('when args are wrong', () => {
|
||||||
it('throws when arg object is not in right format', () => {
|
it('throws when arg object is not in right format', () => {
|
||||||
const wrongObject = { hello: 'world' }
|
const wrongObject = { hello: 'world' }
|
||||||
expect(() => getSensiplanStatus(wrongObject)).to.throw('cycle days as array')
|
expect(() => getSensiplanStatus(wrongObject)).to.throw(AssertionError)
|
||||||
})
|
})
|
||||||
it('throws if cycle array is empty', () => {
|
it('throws if cycle array is empty', () => {
|
||||||
expect(() => getSensiplanStatus({cycle: []})).to.throw('cycle days as array')
|
expect(() => getSensiplanStatus({cycle: []})).to.throw(AssertionError)
|
||||||
})
|
})
|
||||||
it('throws if cycle days are not in right format', () => {
|
it('throws if cycle days are not in right format', () => {
|
||||||
expect(() => getSensiplanStatus({
|
expect(() => getSensiplanStatus({
|
||||||
@@ -156,7 +157,7 @@ describe('sympto', () => {
|
|||||||
date: '1992-09-09',
|
date: '1992-09-09',
|
||||||
bleeding: { value: 0 }
|
bleeding: { value: 0 }
|
||||||
}]
|
}]
|
||||||
})).to.throw('correct format')
|
})).to.throw(AssertionError)
|
||||||
expect(() => getSensiplanStatus({
|
expect(() => getSensiplanStatus({
|
||||||
cycle: [{
|
cycle: [{
|
||||||
date: '2018-04-13',
|
date: '2018-04-13',
|
||||||
@@ -167,7 +168,7 @@ describe('sympto', () => {
|
|||||||
date: '1992-09-09',
|
date: '1992-09-09',
|
||||||
bleeding: { value: 0 }
|
bleeding: { value: 0 }
|
||||||
}]
|
}]
|
||||||
})).to.throw('correct format')
|
})).to.throw(AssertionError)
|
||||||
expect(() => getSensiplanStatus({
|
expect(() => getSensiplanStatus({
|
||||||
cycle: [{
|
cycle: [{
|
||||||
date: '09-14-2017',
|
date: '09-14-2017',
|
||||||
@@ -177,7 +178,7 @@ describe('sympto', () => {
|
|||||||
date: '1992-09-09',
|
date: '1992-09-09',
|
||||||
bleeding: { value: 0 }
|
bleeding: { value: 0 }
|
||||||
}]
|
}]
|
||||||
})).to.throw('ISO')
|
})).to.throw(AssertionError)
|
||||||
})
|
})
|
||||||
it('throws if first cycle day does not have bleeding value', () => {
|
it('throws if first cycle day does not have bleeding value', () => {
|
||||||
expect(() => getSensiplanStatus({
|
expect(() => getSensiplanStatus({
|
||||||
@@ -192,7 +193,7 @@ describe('sympto', () => {
|
|||||||
date: '2017-09-23',
|
date: '2017-09-23',
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
})).to.throw('start with bleeding')
|
})).to.throw(AssertionError)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
Reference in New Issue
Block a user