Skip to content

Commit

Permalink
Completed tests on callback fxns. (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
qodesmith authored Aug 9, 2020
1 parent 4e493a6 commit 7e00f95
Showing 1 changed file with 124 additions and 3 deletions.
127 changes: 124 additions & 3 deletions cypress/integration/callbacks.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// TODO - implement these tests.
import selectors from '../selectors'

const { singleDatepickerInput, single } = selectors

describe('Callback functions provided to datepicker', function() {
beforeEach(function() {
Expand All @@ -13,6 +15,125 @@ describe('Callback functions provided to datepicker', function() {
cy.window().then(global => cy.wrap(global.datepicker).as('datepicker'))
})

describe('Single instance', function() {})
describe('Daterange pair', function() {})
describe('onSelect', function() {
it('should be called after a date has been selected', function() {
const options = { onSelect: () => {} }
const spy = cy.spy(options, 'onSelect')
this.datepicker(singleDatepickerInput, options)

cy.get(singleDatepickerInput).click()
cy.get(`${single.squaresContainer} [data-direction="0"]`).first().click().then(() => {
expect(spy).to.be.calledOnce
})
})

it('should be called with the correct arguments', function() {
let picker
const today = new Date()
const options = {
onSelect: (...args) => {
expect(args.length, 'onSelect arguments length').to.eq(2)
expect(args[0], 'onSelect 1st arg should be the instance').to.eq(picker)

/*
We can't use `instanceof Date` because `Date` is a different constructor
than the one on the window object that Cypress uses. Essentially,
we're dealing with 2 different window object. So it's easier to just do
the whole toString thingy.
*/
expect(({}).toString.call(args[1]), 'onSelect 2nd arg should be a date').to.eq('[object Date]')
expect(args[1].getFullYear(), `onSelect 2nd arg year should be today's year`).to.eq(today.getFullYear())
expect(args[1].getMonth(), `onSelect 2nd arg month should be today's month`).to.eq(today.getMonth())
}
}

picker = this.datepicker(singleDatepickerInput, options)
cy.get(singleDatepickerInput).click()
cy.get(`${single.squaresContainer} [data-direction="0"]`).first().click()
})
})

describe('onShow', function() {
it('should be called after the calendar is shown', function() {
const options = { onShow: () => {} }
const spy = cy.spy(options, 'onShow')
this.datepicker(singleDatepickerInput, options)

expect(options.onShow).not.to.be.called
cy.get(singleDatepickerInput).click().then(() => {
expect(spy).to.be.calledOnce
})
})

it('should be called with the instance as the only argument', function() {
let instance
const options = {
onShow: (...args) => {
expect(args.length, 'onShow arguments length').to.eq(1)
expect(args[0], 'onShow argument should be the instance').to.eq(instance)
}
}

instance = this.datepicker(singleDatepickerInput, options)
cy.get(singleDatepickerInput).click()
})
})

describe('onHide', function() {
it('should be called after the calendar is hidden', function() {
const options = { onHide: () => {} }
const spy = cy.spy(options, 'onHide')
this.datepicker(singleDatepickerInput, options)

cy.get(singleDatepickerInput).click().then(() => {
expect(spy).not.to.be.called

cy.get('body').click().then(() => {
expect(spy).to.be.calledOnce
})
})
})

it('should be called with the instance as the only argument', function() {
let instance
const options = {
onHide: (...args) => {
expect(args.length, 'onHide arguments length').to.eq(1)
expect(args[0], 'onHide argument should be the instance').to.eq(instance)
}
}

instance = this.datepicker(singleDatepickerInput, options)
cy.get(singleDatepickerInput).click()
})
})

describe('onMonthChange', function() {
it('should be called when the arrows are clicked', function() {
const options = { onMonthChange: () => {} }
const spy = cy.spy(options, 'onMonthChange')
this.datepicker(singleDatepickerInput, options)

cy.get(singleDatepickerInput).click()
cy.get(`${single.controls} .qs-arrow.qs-right`).click()
cy.get(`${single.controls} .qs-arrow.qs-left`).click().then(() => {
expect(spy).to.be.calledTwice
})
})

it('should be called with the datepicker instance as the only argument', function() {
let instance
const options = {
onMonthChange: (...args) => {
expect(args.length, 'onMonthChange arguments length').to.eq(1)
expect(args[0], 'onMonthChange argument should be the instance').to.eq(instance)
}
}

instance = this.datepicker(singleDatepickerInput, options)
cy.get(singleDatepickerInput).click()
cy.get(`${single.controls} .qs-arrow.qs-right`).click()
cy.get(`${single.controls} .qs-arrow.qs-left`).click()
})
})
})

0 comments on commit 7e00f95

Please sign in to comment.