Skip to content

Added tests for utils functions isSelectable and mergeModifiers #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions test/utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// import React from 'react'
// import { format } from 'date-fns'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove these comments.


import { isSelectable, mergeModifiers } from '../src/utils'
import { isToday } from 'date-fns'

describe('isSelectable', () => {
it('should return `true` for a date that is within the range of `minimumDate` and `maximumDate`', () => {
const testDate = new Date(2025, 10, 4)
const minimumDate = new Date(2025, 10, 3)
const maximumDate = new Date(2025, 10, 5)
expect(isSelectable(testDate, { minimumDate, maximumDate })).toBe(true)
})
it('should return `true` for a date that is equal to `minimumDate` or `maximumDate`', () => {
const minimumDate = new Date(2025, 10, 3)
const maximumDate = new Date(2025, 10, 5)
expect(isSelectable(minimumDate, { minimumDate, maximumDate })).toBe(true)
expect(isSelectable(maximumDate, { minimumDate, maximumDate })).toBe(true)
})
it('should return `false` for a date that is before `minimumDate` or after `maximumDate`', () => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Purely stylistic (I should add an eslint rule for this) but could you leave a blank line between the end of a block and the start of a new one? In the same way that you do within the mergeModifiers tests.

const beforeTestDate = new Date(2025, 10, 2)
const afterTestDate = new Date(2025, 10, 6)
const minimumDate = new Date(2025, 10, 3)
const maximumDate = new Date(2025, 10, 5)

expect(isSelectable(beforeTestDate, { minimumDate, maximumDate })).toBe(false)
expect(isSelectable(afterTestDate, { minimumDate, maximumDate })).toBe(false)
})
})
describe('mergeModfiers', () => {
it('should return the original modifier object if `newModifiers` is not provided', () => {
const today = new Date(2025, 10, 4)
const mockBaseModifiers = { today: isToday(today) }
expect(mergeModifiers(mockBaseModifiers)).toEqual(mockBaseModifiers)
})

it('should (in effect) merge a provided modifier into the `modifiers` object if the key is not already present', () => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does "in effect" mean?

const mockBaseModifiers = { today: (date) => false }
const newModifier = { thursday: (date) => true }

expect(mergeModifiers(mockBaseModifiers, newModifier)).toEqual({
...mockBaseModifiers,
...newModifier
})
expect(mergeModifiers(mockBaseModifiers, newModifier)).toEqual({
...newModifier,
...mockBaseModifiers
})
})

it('should check both `baseModifier` and `newModifier` of the same name', () => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a better way to test this one, without worrying about what values are passed, might be using toHaveBeenCalled, something like:

const modifierA = jest.fn()
const modifierB = jest.fn()

const baseModifiers = { test: modifierA }
const newModifiers = { test: modifierB }
const mergedModfiers = mergeModifiers(baseModifiers, newModifiers)

mergedModfiers.test()

expect(modifierA).toHaveBeenCalled()
expect(modifierB).toHaveBeenCalled()

const mockBaseModifiers = { test: (num) => num > 1 }
const newModifier = { test: (string) => typeof string === 'string' }
const mergedModfiers = mergeModifiers(mockBaseModifiers, newModifier)
const NOT_A_NUMBER_OVER_1_OR_A_STRING = true

expect(mergedModfiers.test(10)).toBe(true)
expect(mergedModfiers.test(-10)).toBe(false)
expect(mergedModfiers.test('a string')).toBe(true)
expect(mergedModfiers.test(NOT_A_NUMBER_OVER_1_OR_A_STRING)).toBe(false)
})

it('should prefer a `baseModifier` with the same name as a `newModifier`', () => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, this one could be like this:

const modifierA = jest.fn(() => true)
const modifierB = jest.fn()

const baseModifiers = { test: modifierA }
const newModifiers = { test: modifierB }
const mergedModfiers = mergeModifiers(baseModifiers, newModifiers)

mergedModfiers.test()

expect(modifierA).toHaveBeenCalled()
expect(modifierB).not.toHaveBeenCalled()

const mockBaseModifiers = { today: (date) => 'baseModifier' }
const newModifier = { today: (date) => 'newModifier' }
const mergedModfiers = mergeModifiers(mockBaseModifiers, newModifier)

expect(mergedModfiers.today('which one?')).toBe('baseModifier')
})

it('should assign the `newModifier` if no `baseModifier` of the same name is provided', () => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't what's tested here already covered by the should (in effect) merge a provided modifier into the 'modifiers' object if the key is not already present test?

const mockBaseModifiers = { today: (date) => 'baseModifier' }
const newModifier = { tomorrow: (date) => 'newModifier' }
const mergedModifiers = mergeModifiers(mockBaseModifiers, newModifier)
expect(mergedModifiers.tomorrow).toEqual(newModifier.tomorrow)
})
})