Skip to content

Commit

Permalink
chore: fix various test failures
Browse files Browse the repository at this point in the history
  • Loading branch information
jahow committed Jan 6, 2025
1 parent 8a8f9c2 commit 4a7ae84
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
24 changes: 13 additions & 11 deletions libs/util/data-fetcher/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -89,20 +89,22 @@ export function fetchDataAsArrayBuffer(url: string): Promise<ArrayBuffer> {
})
}

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
)
}
Expand Down
6 changes: 6 additions & 0 deletions libs/util/data-fetcher/src/test-setup.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
8 changes: 8 additions & 0 deletions libs/util/shared/src/lib/utils/temporal-extent-union.spec.ts
Original file line number Diff line number Diff line change
@@ -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 = [
Expand Down
7 changes: 6 additions & 1 deletion libs/util/shared/src/lib/utils/temporal-extent-union.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down

0 comments on commit 4a7ae84

Please sign in to comment.