Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
76 changes: 76 additions & 0 deletions src/utilities/format.test.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
4 changes: 2 additions & 2 deletions src/utilities/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {
Expand All @@ -21,4 +21,4 @@ export function getFormatType(dist : DistributionType) {
}
}
return '';
}
}