diff --git a/package.json b/package.json index f96a81b0..1f62e639 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@civicactions/cmsds-open-data-components", - "version": "3.10.0", + "version": "3.10.1", "description": "Components for the open data catalog frontend using CMS Design System", "main": "dist/main.js", "source": "src/index.ts", diff --git a/src/utilities/format.test.ts b/src/utilities/format.test.ts new file mode 100644 index 00000000..a6be5ad3 --- /dev/null +++ b/src/utilities/format.test.ts @@ -0,0 +1,76 @@ +import { getFormatType } from './format'; +import { DistributionType } from '../types/dataset'; + +describe('getFormatType', () => { + it('should return empty string when dist is null or undefined', () => { + expect(getFormatType(null as any)).toBe(''); + expect(getFormatType(undefined as any)).toBe(''); + expect(getFormatType({} as DistributionType)).toBe(''); + }); + + it('should return lowercase format when data.format exists', () => { + const dist: DistributionType = { + data: { format: 'CSV' } + } as DistributionType; + expect(getFormatType(dist)).toBe('csv'); + }); + + it('should return format from mediaType when format is not available', () => { + const dist: DistributionType = { + data: { mediaType: 'application/json' } + } as DistributionType; + expect(getFormatType(dist)).toBe('json'); + }); + + it('should handle undefined downloadURL cases without throwing errors', () => { + // Undefined downloadURL + expect(getFormatType({ + data: {} + } as DistributionType)).toBe(''); + + // Null downloadURL + expect(getFormatType({ + data: { "%Ref:downloadURL": null } + } as any)).toBe(''); + + // Empty array + expect(getFormatType({ + data: { "%Ref:downloadURL": [] } + } as any)).toBe(''); + + // Undefined first element + expect(getFormatType({ + data: { "%Ref:downloadURL": [undefined] } + } as any)).toBe(''); + + // Missing data property + expect(getFormatType({ + data: { "%Ref:downloadURL": [{}] } + } as any)).toBe(''); + }); + + // Valid downloadURL case + it('should return format from downloadURL mimeType when available', () => { + const dist: DistributionType = { + data: { + "%Ref:downloadURL": [ + { data: { mimeType: 'application/pdf' } } + ] + } + } as any; + expect(getFormatType(dist)).toBe('pdf'); + }); + + it('should prioritize format > mediaType > downloadURL mimeType', () => { + const dist: DistributionType = { + data: { + format: 'CSV', + mediaType: 'application/json', + "%Ref:downloadURL": [ + { data: { mimeType: 'application/pdf' } } + ] + } + } as any; + expect(getFormatType(dist)).toBe('csv'); + }); +}); diff --git a/src/utilities/format.ts b/src/utilities/format.ts index 4c8f3da3..f9d174f2 100644 --- a/src/utilities/format.ts +++ b/src/utilities/format.ts @@ -11,7 +11,7 @@ export function getFormatType(dist : DistributionType) { return mediaType[1].toLowerCase(); } } - if(dist.data["%Ref:downloadURL"].length && dist.data["%Ref:downloadURL"][0].data) { + if(dist.data["%Ref:downloadURL"] && dist.data["%Ref:downloadURL"].length && dist.data["%Ref:downloadURL"][0]?.data) { if(dist.data["%Ref:downloadURL"][0].data.mimeType) { const mimeType = dist.data["%Ref:downloadURL"][0].data.mimeType.split("/"); if (mimeType.length && mimeType[1]) { @@ -21,4 +21,4 @@ export function getFormatType(dist : DistributionType) { } } return ''; -} \ No newline at end of file +}