Skip to content

Commit

Permalink
Merge pull request #798 from sebgroup/develop
Browse files Browse the repository at this point in the history
next release
  • Loading branch information
eweseong authored Feb 9, 2023
2 parents e22ca30 + 4805ff0 commit f5f690b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
23 changes: 14 additions & 9 deletions lib/src/Datepicker/Datepicker.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fireEvent, render, RenderResult, screen, waitFor } from "@testing-library/react";
import { fireEvent, render, RenderResult, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { advanceTo, clear } from "jest-date-mock";
import React from "react";
Expand Down Expand Up @@ -71,29 +71,34 @@ describe("Component: Datepicker", () => {
expect(props.onChange).toHaveBeenCalled();
});

it("Should fire change event with null when component value is out of range and with latest value when in range", async () => {
const [min, max]: [Date, Date] = [new Date(props.value.getFullYear() - 10, 1, 1), new Date(props.value.getFullYear() + 10, 1, 1)];
const year: number = props.value.getFullYear();
it("Should fire change event with null when component value is out of range and with latest value when in range", () => {
const year = props.value.getFullYear();
const maxYear = year + 10;
const minYear = year - 10;
const [max, min] = [new Date(maxYear, 0, 1), new Date(minYear, 0, 1)];

const { rerender }: RenderResult = render(<Datepicker {...props} min={min} />);
expect(props.onChange).not.toHaveBeenCalled();
changeDate(`${year - 11}-01-01`);
changeDate(`${minYear - 1}-01-01`);
expect(props.onChange).toHaveBeenCalledTimes(2);
expect(props.onChange).toHaveBeenLastCalledWith(null);

rerender(<Datepicker {...props} max={max} />);
changeDate(`${year + 11}-01-01`);
changeDate(`${maxYear + 1}-01-01`);
expect(props.onChange).toHaveBeenCalledTimes(4);
expect(props.onChange).toHaveBeenLastCalledWith(null);

rerender(<Datepicker {...props} min={min} max={max} />);
changeDate(`${year - 11}-01-01`);
changeDate(`${minYear - 1}-01-01`);
expect(props.onChange).toHaveBeenCalledTimes(6);
expect(props.onChange).toHaveBeenLastCalledWith(null);
changeDate(`${year + 11}-01-01`);
changeDate(`${maxYear + 1}-01-01`);
expect(props.onChange).toHaveBeenCalledTimes(8);
expect(props.onChange).toHaveBeenLastCalledWith(null);
changeDate(`${year}-01-01`);
changeDate(`${minYear}-01-01`);
expect(props.onChange).toHaveBeenCalledTimes(9);
changeDate(`${maxYear}-01-01`);
expect(props.onChange).toHaveBeenCalledTimes(10);
});

it("should support fallback custom picker", async () => {
Expand Down
6 changes: 4 additions & 2 deletions lib/src/Datepicker/Datepicker.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { isDateAfter } from "@sebgroup/frontend-tools";
import { isDateBefore } from "@sebgroup/frontend-tools/isDateBefore";
import { randomId } from "@sebgroup/frontend-tools/randomId";
import classnames from "classnames";
import React from "react";
Expand Down Expand Up @@ -454,8 +456,8 @@ function hasModifierKey({ altKey, ctrlKey, metaKey, shiftKey }: React.KeyboardEv
}

function isDateInRange(d: Date, min: Date, max: Date): boolean {
const isAfterMinDate = !min || d >= min;
const isBeforeMaxDate = !max || d <= max;
const isAfterMinDate = !min || !isDateBefore(d, min);
const isBeforeMaxDate = !max || !isDateAfter(d, max);
return isAfterMinDate && isBeforeMaxDate;
}

Expand Down

0 comments on commit f5f690b

Please sign in to comment.