Skip to content

Commit

Permalink
review suggestions and unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
dgodinez-dh committed Jul 25, 2024
1 parent f5b48ef commit 73131cc
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
41 changes: 41 additions & 0 deletions plugins/ui/src/js/src/elements/hooks/useDatepickerProps.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { parseDateValue } from './useDatepickerProps';

describe('parseDateValue', () => {
const isoDate = '2021-02-03';
const isoDateTime = '2021-03-03T04:05:06';
const isoZonedDateTime = '2021-04-04T05:06:07-04:00[America/New_York]';
const nonIsoZonedDateTime = '2021-04-04T05:06:07 America/New_York';
const invalidDate = 'invalid-date';

it('should return null if the value is null', () => {
expect(parseDateValue(null)).toBeNull();
});

it('should return undefined if the value is undefined', () => {
expect(parseDateValue(undefined)).toBeUndefined();
});

it('should parse an ISO 8601 date string', () => {
expect(parseDateValue(isoDate)?.toString()).toEqual(isoDate);
});

it('should parse an ISO 8601 date time string', () => {
expect(parseDateValue(isoDateTime)?.toString()).toEqual(isoDateTime);
});

it('should parse an ISO 8601 zoned date time string', () => {
expect(parseDateValue(isoZonedDateTime)?.toString()).toEqual(
isoZonedDateTime
);
});

it('should parse a non-ISO 8601 zoned date time string', () => {
expect(parseDateValue(nonIsoZonedDateTime)?.toString()).toEqual(
isoZonedDateTime
);
});

it('should throw an error if the value is invalid', () => {
expect(() => parseDateValue(invalidDate)).toThrow();
});
});
7 changes: 5 additions & 2 deletions plugins/ui/src/js/src/elements/hooks/useDatepickerProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,10 @@ export function useOnChangeCallback(
): (value: MappedDateValue<DateValue>) => void {
return useCallback(
(value: MappedDateValue<DateValue>) => {
callback?.(serializeDateValue(value));
if (callback == null) {
return;
}
callback(serializeDateValue(value));
},
[callback]
);
Expand All @@ -168,7 +171,7 @@ export function useDateValueMemo(
export function parseDateValue(
value?: string | null
): DateValue | null | undefined {
if (value === undefined || value === null) {
if (value == null) {
return value;
}

Expand Down

0 comments on commit 73131cc

Please sign in to comment.