diff --git a/apps/datahub/src/app/record/record-downloads/record-downloads.component.spec.ts b/apps/datahub/src/app/record/record-downloads/record-downloads.component.spec.ts index b298d13cee..2e5a12d4cb 100644 --- a/apps/datahub/src/app/record/record-downloads/record-downloads.component.spec.ts +++ b/apps/datahub/src/app/record/record-downloads/record-downloads.component.spec.ts @@ -13,11 +13,11 @@ import { MdViewFacade } from '@geonetwork-ui/feature/record' import { MockBuilder } from 'ng-mocks' import { PopupAlertComponent } from '@geonetwork-ui/ui/widgets' -// This is used to work around a very weird bug when comparing URL objects would fail -// if the `searchParams` of the object wasn't accessed beforehand in some cases... +// This is used to work around this issue with URL in JSDom: +// https://github.com/jestjs/jest/issues/14012 const newUrl = (url: string) => { const obj = new URL(url) - // obj.searchParams // try commenting this out to see the bug + obj.searchParams // calling the getter once to fill query params return obj } diff --git a/libs/api/metadata-converter/src/lib/iso19139/write-parts.ts b/libs/api/metadata-converter/src/lib/iso19139/write-parts.ts index b41c13e5c1..32fc7a34e8 100644 --- a/libs/api/metadata-converter/src/lib/iso19139/write-parts.ts +++ b/libs/api/metadata-converter/src/lib/iso19139/write-parts.ts @@ -18,7 +18,7 @@ import { UpdateFrequencyCustom, } from '@geonetwork-ui/common/domain/model/record' import { ThesaurusModel } from '@geonetwork-ui/common/domain/model/thesaurus' -import format from 'date-fns/format' +import { format } from 'date-fns/format' import { Geometry } from 'geojson' import { ChainableFunction, diff --git a/libs/util/data-fetcher/src/lib/utils.ts b/libs/util/data-fetcher/src/lib/utils.ts index c00e278e68..6d6e16cd49 100644 --- a/libs/util/data-fetcher/src/lib/utils.ts +++ b/libs/util/data-fetcher/src/lib/utils.ts @@ -8,8 +8,8 @@ import { } from './model' import { sharedFetch, useCache } from '@camptocamp/ogc-client' import { parseHeaders } from './headers' -import parseDate from 'date-fns/parse' -import parseIsoDate from 'date-fns/parseISO' +import { parse as parseDate } from 'date-fns/parse' +import { parseISO as parseIsoDate } from 'date-fns/parseISO' export async function inferDatasetType( url: string, @@ -89,20 +89,22 @@ export function fetchDataAsArrayBuffer(url: string): Promise { }) } -export function tryParseDate(input: string): Date | null { - function tryIso() { - const parsed = parseIsoDate(input) +export function tryParseDate(input: unknown): Date | null { + if (typeof input !== 'string') return null + + function tryIso(value: string) { + const parsed = parseIsoDate(value) return isNaN(parsed.getDate()) ? null : parsed } - function tryFormat(format: string) { - const parsed = parseDate(input, format, new Date()) + function tryFormat(value: string, format: string) { + const parsed = parseDate(value, format, new Date()) return isNaN(parsed.getDate()) ? null : parsed } return ( - tryIso() || - tryFormat('dd/MM/yyyy') || - tryFormat('dd.MM.yyyy') || - tryFormat('MM/dd/yyyy') || + tryIso(input) || + tryFormat(input, 'dd/MM/yyyy') || + tryFormat(input, 'dd.MM.yyyy') || + tryFormat(input, 'MM/dd/yyyy') || null ) } diff --git a/libs/util/data-fetcher/src/test-setup.ts b/libs/util/data-fetcher/src/test-setup.ts index 687a56a558..ff658f77f6 100644 --- a/libs/util/data-fetcher/src/test-setup.ts +++ b/libs/util/data-fetcher/src/test-setup.ts @@ -1,8 +1,14 @@ /* eslint-disable */ import fetch from 'fetch-mock-jest' +import { TextDecoder, TextEncoder } from 'util' global.fetch = fetch as never +// this is needed because jsdom does not include these as globals by default +// see https://github.com/jsdom/jsdom/issues/2524 +global.TextEncoder = TextEncoder +global.TextDecoder = TextDecoder + global.Headers = class { _value = {} constructor(initValue) { diff --git a/libs/util/shared/src/lib/utils/temporal-extent-union.spec.ts b/libs/util/shared/src/lib/utils/temporal-extent-union.spec.ts index b85a40693e..8bd8ddc550 100644 --- a/libs/util/shared/src/lib/utils/temporal-extent-union.spec.ts +++ b/libs/util/shared/src/lib/utils/temporal-extent-union.spec.ts @@ -1,5 +1,13 @@ import { getTemporalRangeUnion } from './temporal-extent-union' +// lock locale to en +beforeAll(() => { + const originalFn = Date.prototype.toLocaleDateString + Date.prototype.toLocaleDateString = function () { + return originalFn.call(this, 'en') + } +}) + describe('getTemporalRangeUnion', () => { it('should return the union of temporal ranges', () => { const ranges = [ diff --git a/libs/util/shared/src/lib/utils/temporal-extent-union.ts b/libs/util/shared/src/lib/utils/temporal-extent-union.ts index 5016527434..691dccf036 100644 --- a/libs/util/shared/src/lib/utils/temporal-extent-union.ts +++ b/libs/util/shared/src/lib/utils/temporal-extent-union.ts @@ -1,4 +1,9 @@ -export function getTemporalRangeUnion(ranges) { +export function getTemporalRangeUnion( + ranges: { + start?: Date + end?: Date + }[] +) { let earliestStartDate = Infinity let latestEndDate = -Infinity if (ranges.length) {