Skip to content
Snippets Groups Projects
Unverified Commit 8aa6f386 authored by Denis Berezin's avatar Denis Berezin Committed by GitHub
Browse files

Fix issue with first week of month not being visible in calendar selector (#29722)

parent 61d382f1
No related branches found
No related tags found
No related merge requests found
/* eslint-disable react/prop-types */
import React, { Component } from "react"; import React, { Component } from "react";
import cx from "classnames"; import cx from "classnames";
import moment, { Moment } from "moment-timezone"; import moment, { Moment } from "moment-timezone";
...@@ -13,7 +12,7 @@ import { CalendarDay } from "./Calendar.styled"; ...@@ -13,7 +12,7 @@ import { CalendarDay } from "./Calendar.styled";
export type SelectAll = "after" | "before"; export type SelectAll = "after" | "before";
type Props = { export type CalendarProps = {
initial?: Moment; initial?: Moment;
selected?: Moment; selected?: Moment;
selectedEnd?: Moment; selectedEnd?: Moment;
...@@ -33,8 +32,8 @@ type State = { ...@@ -33,8 +32,8 @@ type State = {
current?: Moment; current?: Moment;
}; };
export default class Calendar extends Component<Props, State> { export default class Calendar extends Component<CalendarProps, State> {
constructor(props: Props) { constructor(props: CalendarProps) {
super(props); super(props);
this.state = { this.state = {
current: moment(props.initial || undefined), current: moment(props.initial || undefined),
...@@ -45,7 +44,7 @@ export default class Calendar extends Component<Props, State> { ...@@ -45,7 +44,7 @@ export default class Calendar extends Component<Props, State> {
isRangePicker: true, isRangePicker: true,
}; };
UNSAFE_componentWillReceiveProps(nextProps: Props) { UNSAFE_componentWillReceiveProps(nextProps: CalendarProps) {
if ( if (
// `selected` became null or not null // `selected` became null or not null
(nextProps.selected == null) !== (this.props.selected == null) || (nextProps.selected == null) !== (this.props.selected == null) ||
...@@ -158,6 +157,11 @@ export default class Calendar extends Component<Props, State> { ...@@ -158,6 +157,11 @@ export default class Calendar extends Component<Props, State> {
const firstDayOfWeek = getFirstDayOfWeekIndex(); const firstDayOfWeek = getFirstDayOfWeekIndex();
const date = moment(current).startOf("month").isoWeekday(firstDayOfWeek); const date = moment(current).startOf("month").isoWeekday(firstDayOfWeek);
// if set week doesn't start with 1st day of month, then add the previous week
if (date.date() > 1) {
date.add(-1, "w");
}
let done = false; let done = false;
let monthIndex = date.month(); let monthIndex = date.month();
let count = 0; let count = 0;
......
import React from "react";
import moment from "moment-timezone";
import mockDate from "mockdate";
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import Calendar from "metabase/components/Calendar";
describe("Calendar", () => {
afterEach(() => {
mockDate.reset();
});
it("should switch months correctly", () => {
mockDate.set("2018-01-12T12:00:00Z", 0);
render(<Calendar selected={moment("2018-01-01")} onChange={() => {}} />);
const PREVIOUS = screen.getByRole("img", { name: /chevronleft icon/i });
const NEXT = screen.getByRole("img", { name: /chevronright icon/i });
expect(screen.getByText("January 2018")).toBeInTheDocument();
userEvent.click(PREVIOUS);
expect(screen.getByText("December 2017")).toBeInTheDocument();
userEvent.click(NEXT);
userEvent.click(NEXT);
expect(screen.getByText("February 2018")).toBeInTheDocument();
});
});
import React from "react"; import React from "react";
import moment from "moment-timezone";
import mockDate from "mockdate";
import userEvent from "@testing-library/user-event";
import { render, screen } from "__support__/ui"; import { render, screen } from "__support__/ui";
import MetabaseSettings from "metabase/lib/settings"; import MetabaseSettings from "metabase/lib/settings";
import { updateMomentStartOfWeek } from "metabase/lib/i18n"; import { updateMomentStartOfWeek } from "metabase/lib/i18n";
import Calendar from "./Calendar"; import Calendar, { CalendarProps } from "./Calendar";
describe("Calendar", () => { describe("Calendar", () => {
afterEach(() => {
mockDate.reset();
});
it("should switch months correctly", () => {
mockDate.set("2018-01-12T12:00:00Z", 0);
setup({ selected: moment("2018-01-01") });
const PREVIOUS = screen.getByRole("img", { name: /chevronleft icon/i });
const NEXT = screen.getByRole("img", { name: /chevronright icon/i });
expect(screen.getByText("January 2018")).toBeInTheDocument();
userEvent.click(PREVIOUS);
expect(screen.getByText("December 2017")).toBeInTheDocument();
userEvent.click(NEXT);
userEvent.click(NEXT);
expect(screen.getByText("February 2018")).toBeInTheDocument();
});
it("should render all days of current month by default", () => {
mockDate.set("2023-03-31T12:00:00Z", 0);
setup();
// check that listed dates are correct
expect(screen.getByTestId("calendar-weeks")).toHaveTextContent(
[
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
].join(""), // days in March 2023
);
});
it("should render weekday short names", () => { it("should render weekday short names", () => {
setup(); setup();
...@@ -51,6 +89,6 @@ describe("Calendar", () => { ...@@ -51,6 +89,6 @@ describe("Calendar", () => {
}); });
}); });
function setup() { function setup(props?: Partial<CalendarProps>) {
return render(<Calendar />); return render(<Calendar {...props} />);
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment