Skip to content

test(date): add test for dateToISO utility function #6308

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

Merged
merged 5 commits into from
Jan 20, 2023
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
8 changes: 4 additions & 4 deletions src/components/input-date-picker/input-date-picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -854,18 +854,18 @@ export class InputDatePicker
inputEl.value = newValue;
};

private setRangeValue = (value: Date[] | string): void => {
private setRangeValue = (valueAsDate: Date[]): void => {
if (!this.range) {
return;
}

const { value: oldValue } = this;
const oldValueIsArray = Array.isArray(oldValue);
const valueIsArray = Array.isArray(value);
const valueIsArray = Array.isArray(valueAsDate);

const newStartDate = valueIsArray ? value[0] : "";
const newStartDate = valueIsArray ? valueAsDate[0] : null;
const newStartDateISO = valueIsArray ? dateToISO(newStartDate) : "";
const newEndDate = valueIsArray ? value[1] : "";
const newEndDate = valueIsArray ? valueAsDate[1] : null;
const newEndDateISO = valueIsArray ? dateToISO(newEndDate) : "";

const newValue = newStartDateISO || newEndDateISO ? [newStartDateISO, newEndDateISO] : "";
Expand Down
29 changes: 28 additions & 1 deletion src/utils/date.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { DateLocaleData } from "../components/date-picker/utils";
import { dateFromISO, dateFromRange, getOrder, inRange, nextMonth, parseDateString, prevMonth, sameDate } from "./date";
import {
dateFromISO,
dateFromRange,
dateToISO,
getOrder,
inRange,
nextMonth,
parseDateString,
prevMonth,
sameDate
} from "./date";

import arabic from "../components/date-picker/assets/date-picker/nls/ar.json";
import english from "../components/date-picker/assets/date-picker/nls/en.json";
Expand Down Expand Up @@ -63,6 +73,23 @@ describe("dateFromISO", () => {
});
});

describe("dateToISO", () => {
it("returns empty string from bad input", () => {
expect(dateToISO("" as any)).toEqual("");
expect(dateToISO("asdflkjasdhoui" as any)).toEqual("");
});
it("correctly returns string in simplified ISO format (YYYY-MM-DD)", () => {
const date = new Date(2011, 10, 29);
const expectedValue = "2011-11-29";
expect(dateToISO(date)).toEqual(expectedValue);
});
Copy link
Member

Choose a reason for hiding this comment

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

I think this test is redundant since the one below tests the same base case plus the zero padding. We could probably remove it, what do you think?

it("correctly returns zero-padded month and day values when less than 10", () => {
const date = new Date(2011, 2, 5);
const expectedValue = "2011-03-05";
expect(dateToISO(date)).toEqual(expectedValue);
});
});

describe("sameDate", () => {
it("returns false for bad input", () => {
expect(sameDate(1 as any, "hey" as any)).toEqual(false);
Expand Down
5 changes: 1 addition & 4 deletions src/utils/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,7 @@ export function datePartsFromLocalizedString(
*
* @param date
*/
export function dateToISO(date?: Date | string): string {
if (typeof date === "string") {
return date;
}
export function dateToISO(date?: Date): string {
if (date instanceof Date) {
return new Date(date.getTime() - date.getTimezoneOffset() * 60000).toISOString().split("T")[0];
}
Expand Down