add specs for parser utils

This commit is contained in:
Ondřej Synáček 2020-07-15 20:46:14 +02:00
parent 48e012bbc0
commit db0ee12c4f
2 changed files with 63 additions and 0 deletions

14
mocks/dayjs.mock.js Normal file
View File

@ -0,0 +1,14 @@
const dayjs = jest.requireActual('dayjs')
let mockedDay = null
const mockDayJs = (args, options) => {
return mockedDay || dayjs(args, options)
}
const setMockedDay = (args, options) => {
mockedDay = dayjs(args, options)
return mockedDay
}
module.exports = { mockDayJs, setMockedDay }

49
test/parser-utils.spec.js Normal file
View File

@ -0,0 +1,49 @@
const { expect } = require('chai')
const { mockDayJs, setMockedDay } = require('../mocks/dayjs.mock')
const dayjs = jest.requireActual('dayjs')
const parserUtils = require('../lib/parser-utils')
jest.mock('dayjs', () => mockDayJs)
describe('parserUtils', () => {
beforeEach(() => {
setMockedDay(dayjs())
})
describe('parse dates', () => {
it('should format start date', () => {
const { start } = parserUtils.parseDates(dayjs('2020-03-02 15:35:00'))
expect(start).to.deep.equal([ 2020, 3, 2, 15, 35 ])
})
it('should use current date for start date if not available', () => {
setMockedDay(dayjs('2020-01-01 12:00:00'))
const { start } = parserUtils.parseDates()
expect(start).to.deep.equal([ 2020, 1, 1 ])
})
it('should get duration in minutes based on end date', () => {
const { duration } = parserUtils.parseDates(
dayjs('2020-03-02 15:35:00'),
dayjs('2020-03-04 04:30:00')
)
expect(duration).to.deep.equal({ minutes: 2215 })
})
it('should get duration of 120 minutes if end time is missing', () => {
const { duration } = parserUtils.parseDates(
dayjs('2020-03-02 15:35:00'),
)
expect(duration).to.deep.equal({ minutes: 120 })
})
})
})