Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,20 @@ const yearFormat = (date: Date) => date.getFullYear();

const buildCalendar = (year: number, month: number, weekStart: number, validators: ((date: Date) => boolean)[]) => {
const defaultDate = new Date(year, month);

const firstDayOfWeek = new Date(defaultDate);
firstDayOfWeek.setDate(firstDayOfWeek.getDate() - firstDayOfWeek.getDay() + weekStart);

// We will show a maximum of 6 weeks like Google calendar
// Assume we just want the numbers for now...
const calendarWeeks = [];

if (firstDayOfWeek.getMonth() === defaultDate.getMonth() && firstDayOfWeek.getDate() !== 1) {
firstDayOfWeek.setDate(firstDayOfWeek.getDate() - 7);
}
for (let i = 0; i < 6; i++) {
const week = [];

for (let j = 0; j < 7; j++) {
const date = new Date(firstDayOfWeek);
week.push({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React from 'react';

import { render, screen } from '@testing-library/react';

import { CalendarMonth } from '../CalendarMonth';

test('Renders the first date in a month when a custom weekStart is passed', () => {
// custom aria label generation function because of bug with default aria label generation
// can be removed once the bug is fixed
const formatAria = (date: Date) =>
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can you add a comment here just stating that this custom aria label is needed because of a bug with automatic aria label generation as it is now, and that it can be removed once that bug is fixed?

Copy link
Copy Markdown
Contributor Author

@dominik-petrik dominik-petrik Jul 20, 2022

Choose a reason for hiding this comment

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

I think that'd it be actually a good thing to keep custom aria label because format of default generated aria label might change in future and the test would fail for seemingly unrelated reason.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

That's a fair point, though the same could be said for this approach and its reliance on the cellAriaLabel prop functioning properly. This line of thinking could also extend to many other behaviors of the component, such as the weekStart or date props working properly, or the structure of the component.

I totally get not wanting this test to be able to fail for something that isn't the actual behavior under test, but I think that it's generally reasonable for a test to rely on other behavior of the component under test since totally avoiding that would be next to impossible.

Plus if the generated aria labels are properly tested developers should be able to see that test failing as well if the generated aria label is changed, which should make it apparent what the true cause of the failure here is.

All of that being said I don't think what you've got here is bad by any means and I don't think it neccesarily needs to go, but I would still like a comment saying why it's here and that it shouldn't be needed once the bug is fixed 🙂

`${date.getDate()} ${date.toLocaleDateString(undefined, { month: 'long' })} ${date.getFullYear()}`;

render(<CalendarMonth cellAriaLabel={formatAria} weekStart={1} date={new Date(2023, 0)} />);

const firstDate = screen.queryByRole('button', { name: '1 January 2023' });
expect(firstDate).toBeVisible();
});

test('Renders the last date in a month when a custom weekStart is passed', () => {
// custom aria label generation function because of bug with default aria labels
// can be removed once the bug is fixed
const formatAria = (date: Date) =>
`${date.getDate()} ${date.toLocaleDateString(undefined, { month: 'long' })} ${date.getFullYear()}`;

render(<CalendarMonth cellAriaLabel={formatAria} weekStart={1} date={new Date(2023, 0)} />);

const lastDate = screen.queryByRole('button', { name: '31 January 2023' });
expect(lastDate).toBeVisible();
});