Skip to content

Commit

Permalink
format (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
mfix22 authored Nov 18, 2020
1 parent 80e623f commit b999fbf
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 63 deletions.
26 changes: 13 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ const DAYS = {
WEDNESDAY: 3,
THURSDAY: 4,
FRIDAY: 5,
SATURDAY: 6
SATURDAY: 6,
}

const VIEWS = {
DAY: 'day',
WEEK: 'week',
MONTH: 'month',
YEAR: 'year'
YEAR: 'year',
}

const HEADERS = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
Expand All @@ -25,7 +25,7 @@ const DEFAULTS = {
initialNumDays: 35,
events: [],
weekStartsOn: DAYS.SUNDAY,
headers: HEADERS
headers: HEADERS,
}

function format(dateLike) {
Expand Down Expand Up @@ -55,7 +55,7 @@ function getButtonProps({ label, onClick }) {
onClick: () => {
onClick()
userOnClick()
}
},
}
}
}
Expand All @@ -67,7 +67,7 @@ function useCalendar({
numDays: numDaysProp,
events = DEFAULTS.events,
weekStartsOn = DEFAULTS.weekStartsOn,
headers: headersProp = DEFAULTS.headers
headers: headersProp = DEFAULTS.headers,
} = DEFAULTS) {
const [dateState, setDateState] = React.useState(format(initialDate))
const [numDaysState, setNumDaysState] = React.useState(Math.max(initialNumDays, 0))
Expand All @@ -76,7 +76,7 @@ function useCalendar({
const numDays = numDaysProp || numDaysState

const setReferenceDate = React.useCallback(
newDate => {
(newDate) => {
if (!dateProp) {
setDateState(format(newDate))
}
Expand All @@ -85,7 +85,7 @@ function useCalendar({
)

const setNumDays = React.useCallback(
numDays => {
(numDays) => {
if (!numDaysProp) {
setNumDaysState(Math.max(numDays, 0))
}
Expand Down Expand Up @@ -129,7 +129,7 @@ function useCalendar({
const date = React.useMemo(() => new Date(referenceDate), [referenceDate])

// Ensure events dates are stored as Date objects
events.forEach(event => {
events.forEach((event) => {
const { date, startDate, endDate } = event

if (date) {
Expand All @@ -150,9 +150,9 @@ function useCalendar({

const days = getMappedDays(date, numDays, { weekStartsOn, view })

const daysWithEvents = days.map(day => ({
const daysWithEvents = days.map((day) => ({
...day,
events: getDaysEvents(day, events)
events: getDaysEvents(day, events),
}))

if (view === VIEWS.DAY || view === VIEWS.WEEK) {
Expand All @@ -173,7 +173,7 @@ function useCalendar({
const day = (starting + i) % headersProp.length
return {
title: headersProp[day],
day
day,
}
}),
[headersProp, length, starting]
Expand All @@ -193,11 +193,11 @@ function useCalendar({
getPrevButtonProps,
getNextButtonProps,
getTodayButtonProps,
setNumDays
setNumDays,
}
}

function Calendar(props) {
export function Calendar(props) {
const stuff = useCalendar(props)

const Component = props.children || props.render
Expand Down
85 changes: 43 additions & 42 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function render(options) {
rtlRender(React.createElement(Calendar, options, children))
return {
children,
...children.mock.calls[0][0]
...children.mock.calls[0][0],
}
}

Expand Down Expand Up @@ -54,7 +54,7 @@ const BASIC_CASES = {
'2019-02-27',
'2019-02-28',
'2019-03-01',
'2019-03-02'
'2019-03-02',
],
4: ['2019-02-18', '2019-02-19', '2019-02-20', '2019-02-21'],
7: [
Expand All @@ -64,13 +64,13 @@ const BASIC_CASES = {
'2019-02-20',
'2019-02-21',
'2019-02-22',
'2019-02-23'
]
'2019-02-23',
],
}

test('hooks defaults', () => {
const cases = [{}, undefined]
cases.forEach(options => {
cases.forEach((options) => {
function MyCalendar() {
const { date } = Calendar.useCalendar(options)

Expand All @@ -88,22 +88,22 @@ test('defaults', () => {

expect(getDateKey(date)).toBe(getDateKey(new Date()))
expect(numDays).toBe(35)
expect(headers.map(h => h.title)).toEqual([
expect(headers.map((h) => h.title)).toEqual([
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday'
'Saturday',
])
expect(headers.map(h => h.day)).toEqual([0, 1, 2, 3, 4, 5, 6])
expect(headers.map((h) => h.day)).toEqual([0, 1, 2, 3, 4, 5, 6])
})

test('isToday', () => {
const { days } = render({
numDays: 4,
children: () => {}
children: () => {},
})

expect(days[0][0].isToday).toBe(true)
Expand All @@ -122,7 +122,7 @@ describe('year', () => {
})
})

describe.each(['month', 'week', 'day'])('%s view', view => {
describe.each(['month', 'week', 'day'])('%s view', (view) => {
const numDays = view === 'month' ? 35 : view === 'week' ? 7 : 4

test('basic rendering ', () => {
Expand All @@ -133,7 +133,7 @@ describe.each(['month', 'week', 'day'])('%s view', view => {
const flattenedDays = [].concat(...days)

expect(flattenedDays.length).toBe(numDays)
expect(flattenedDays.map(d => getDateKey(d.date))).toEqual(BASIC_CASES[numDays])
expect(flattenedDays.map((d) => getDateKey(d.date))).toEqual(BASIC_CASES[numDays])
expect(children).toHaveBeenCalledWith(
expect.objectContaining({
date: new Date('2019-02-18T08:00:00.000Z'),
Expand All @@ -147,7 +147,7 @@ describe.each(['month', 'week', 'day'])('%s view', view => {
getPrevButtonProps: expect.any(Function),
getNextButtonProps: expect.any(Function),
getTodayButtonProps: expect.any(Function),
setNumDays: expect.any(Function)
setNumDays: expect.any(Function),
}),
expect.anything()
)
Expand All @@ -156,7 +156,7 @@ describe.each(['month', 'week', 'day'])('%s view', view => {
const gridViews = {
35: [5, 7],
7: [1, 7],
4: [1, 4]
4: [1, 4],
}
test('grid size is correct', () => {
const date = '2019-02-18'
Expand All @@ -166,15 +166,15 @@ describe.each(['month', 'week', 'day'])('%s view', view => {
const [numWeeks, weekLength] = gridViews[numDays]

expect(days.length).toBe(numWeeks)
days.map(week => {
days.map((week) => {
expect(week.length).toBe(weekLength)
})
})

const dayInformationMap = {
35: 22,
7: 1,
4: 0
4: 0,
}
test('day information is correct', () => {
const date = '2019-02-18'
Expand All @@ -185,7 +185,7 @@ describe.each(['month', 'week', 'day'])('%s view', view => {
initialDate: referenceDate,
numDays,
children,
events: [event, { invalidEvent: new Date() }]
events: [event, { invalidEvent: new Date() }],
})

const flattenedDays = [].concat(...days)
Expand All @@ -206,18 +206,18 @@ describe.each(['month', 'week', 'day'])('%s view', view => {
event2 = {
startDate: '2019-02-20',
endDate: '2019-02-22',
title: 'Another Multi-day Event Title'
title: 'Another Multi-day Event Title',
}

const { children, days } = render({
initialDate: '2019-02-19',
weekStartsOn: 0,
numDays: 35,
children,
events: [event1, event2]
events: [event1, event2],
})

const getDay = date => {
const getDay = (date) => {
for (const week of days) {
for (const day of week) {
if (isSame(date, day.date, 'day')) {
Expand All @@ -242,30 +242,31 @@ describe.each(['month', 'week', 'day'])('%s view', view => {
})

describe('props', () => {
test.each([[Calendar.days.MONDAY, 0], [Calendar.days.TUESDAY, 6], [Calendar.days.SATURDAY, 2]])(
'test starting week on index: %s',
(weekStartsOn, index) => {
const date = '2019-02-18'
const initialDate = moment(date, 'YYYY-MM-DD')

const { children, days } = render({
initialDate,
numDays: 7,
children,
weekStartsOn
})

expect(getDateKey(days[0][index].date)).toBe(date)
}
)
test.each([
[Calendar.days.MONDAY, 0],
[Calendar.days.TUESDAY, 6],
[Calendar.days.SATURDAY, 2],
])('test starting week on index: %s', (weekStartsOn, index) => {
const date = '2019-02-18'
const initialDate = moment(date, 'YYYY-MM-DD')

const { children, days } = render({
initialDate,
numDays: 7,
children,
weekStartsOn,
})

expect(getDateKey(days[0][index].date)).toBe(date)
})

test('headers', () => {
const initialDate = moment('2019-02-18', 'YYYY-MM-DD')

const { children } = render({
initialDate,
numDays: 35,
weekStartsOn: Calendar.days.THURSDAY
weekStartsOn: Calendar.days.THURSDAY,
})

expect(children).toHaveBeenCalledWith(
Expand All @@ -277,16 +278,16 @@ describe('props', () => {
{ title: 'Sunday', day: 0 },
{ title: 'Monday', day: 1 },
{ title: 'Tuesday', day: 2 },
{ title: 'Wednesday', day: 3 }
]
{ title: 'Wednesday', day: 3 },
],
}),
expect.anything()
)

const { children: children2 } = render({
initialDate: moment('2019-02-20', 'YYYY-MM-DD'),
numDays: 4,
headers: ['Sonntag', 'Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag']
headers: ['Sonntag', 'Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag'],
})

expect(children2).toHaveBeenCalledWith(
Expand All @@ -295,8 +296,8 @@ describe('props', () => {
{ title: 'Mittwoch', day: 3 },
{ title: 'Donnerstag', day: 4 },
{ title: 'Freitag', day: 5 },
{ title: 'Samstag', day: 6 }
]
{ title: 'Samstag', day: 6 },
],
}),
expect.anything()
)
Expand Down Expand Up @@ -335,7 +336,7 @@ describe('actions', () => {
expect(onClick).toHaveBeenCalledTimes(4)

const buttons = ['Next', 'Prev', 'Today']
buttons.forEach(title => {
buttons.forEach((title) => {
const button = getByText(title)

expect(button.getAttribute('aria-label')).toContain(`Go to ${title.toLowerCase()}`)
Expand Down
12 changes: 4 additions & 8 deletions test/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,9 @@ describe('add', () => {
['days', '2019-02-19'],
['day', '2019-02-19'],
['d', '2019-02-19'],
['random value', '2019-02-18']
['random value', '2019-02-18'],
])('add(2019-02-18, 1, %s) === %s', (type, result) => {
expect(
add(initialDate, 1, type)
.toISOString()
.split('T')[0]
).toBe(result)
expect(add(initialDate, 1, type).toISOString().split('T')[0]).toBe(result)
})

test.each([
Expand All @@ -38,15 +34,15 @@ describe('add', () => {
['2019-02-28', 'month', true],
['2019-03-01', 'month', false],
['2019-07-09', 'year', true],
['2020-07-09', 'year', false]
['2020-07-09', 'year', false],
])('isSame(2019-02-18, %s, %s) === %s', (compareDate, precision, result, weekStartsOn = 0) => {
const d1 = moment('2019-02-18', 'YYYY-MM-DD').toDate()
const d2 = moment(compareDate, 'YYYY-MM-DD').toDate()

expect(isSame(d1, d2, precision, weekStartsOn)).toBe(result)
})

describe.each(['day', 'week', 'month', 'year'])('%s', view => {
describe.each(['day', 'week', 'month', 'year'])('%s', (view) => {
Array.from({ length: 367 }).forEach((_, i) => {
const date = moment('2018-01-01', 'YYYY-MM-DD').add(i, 'days')
const day = getDateKey(date.toDate())
Expand Down

0 comments on commit b999fbf

Please sign in to comment.