Skip to content

Commit 44e97d2

Browse files
committed
Merge remote-tracking branch 'origin/master' into benelan/6284-tree-expand
* origin/master: test(date): add test for dateToISO utility function (#6308)
2 parents c274484 + d816cde commit 44e97d2

File tree

3 files changed

+33
-9
lines changed

3 files changed

+33
-9
lines changed

src/components/input-date-picker/input-date-picker.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -854,18 +854,18 @@ export class InputDatePicker
854854
inputEl.value = newValue;
855855
};
856856

857-
private setRangeValue = (value: Date[] | string): void => {
857+
private setRangeValue = (valueAsDate: Date[]): void => {
858858
if (!this.range) {
859859
return;
860860
}
861861

862862
const { value: oldValue } = this;
863863
const oldValueIsArray = Array.isArray(oldValue);
864-
const valueIsArray = Array.isArray(value);
864+
const valueIsArray = Array.isArray(valueAsDate);
865865

866-
const newStartDate = valueIsArray ? value[0] : "";
866+
const newStartDate = valueIsArray ? valueAsDate[0] : null;
867867
const newStartDateISO = valueIsArray ? dateToISO(newStartDate) : "";
868-
const newEndDate = valueIsArray ? value[1] : "";
868+
const newEndDate = valueIsArray ? valueAsDate[1] : null;
869869
const newEndDateISO = valueIsArray ? dateToISO(newEndDate) : "";
870870

871871
const newValue = newStartDateISO || newEndDateISO ? [newStartDateISO, newEndDateISO] : "";

src/utils/date.spec.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
import { DateLocaleData } from "../components/date-picker/utils";
2-
import { dateFromISO, dateFromRange, getOrder, inRange, nextMonth, parseDateString, prevMonth, sameDate } from "./date";
2+
import {
3+
dateFromISO,
4+
dateFromRange,
5+
dateToISO,
6+
getOrder,
7+
inRange,
8+
nextMonth,
9+
parseDateString,
10+
prevMonth,
11+
sameDate
12+
} from "./date";
313

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

76+
describe("dateToISO", () => {
77+
it("returns empty string from bad input", () => {
78+
expect(dateToISO("" as any)).toEqual("");
79+
expect(dateToISO("asdflkjasdhoui" as any)).toEqual("");
80+
});
81+
it("correctly returns string in simplified ISO format (YYYY-MM-DD)", () => {
82+
const date = new Date(2011, 10, 29);
83+
const expectedValue = "2011-11-29";
84+
expect(dateToISO(date)).toEqual(expectedValue);
85+
});
86+
it("correctly returns zero-padded month and day values when less than 10", () => {
87+
const date = new Date(2011, 2, 5);
88+
const expectedValue = "2011-03-05";
89+
expect(dateToISO(date)).toEqual(expectedValue);
90+
});
91+
});
92+
6693
describe("sameDate", () => {
6794
it("returns false for bad input", () => {
6895
expect(sameDate(1 as any, "hey" as any)).toEqual(false);

src/utils/date.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,7 @@ export function datePartsFromLocalizedString(
126126
*
127127
* @param date
128128
*/
129-
export function dateToISO(date?: Date | string): string {
130-
if (typeof date === "string") {
131-
return date;
132-
}
129+
export function dateToISO(date?: Date): string {
133130
if (date instanceof Date) {
134131
return new Date(date.getTime() - date.getTimezoneOffset() * 60000).toISOString().split("T")[0];
135132
}

0 commit comments

Comments
 (0)