Skip to content

Commit

Permalink
Merge pull request #37 from derekphilipau/dev
Browse files Browse the repository at this point in the history
bug in schema keywords
  • Loading branch information
derekphilipau authored Dec 31, 2023
2 parents 55eef36 + 900ae9e commit 72c0516
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 3 deletions.
87 changes: 87 additions & 0 deletions __tests__/lib/schema.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import type { ArtworkDocument } from '@/types/document';
import { getSchemaVisualArtwork } from '@/lib/schema';

describe('getSchemaVisualArtwork', () => {
it('should return undefined for undefined input', () => {
const result = getSchemaVisualArtwork(undefined);
expect(result).toBeUndefined();
});

it('should correctly transform a minimal ArtworkDocument', () => {
const mockArtwork: ArtworkDocument = {
title: 'Test Artwork',
image: { thumbnailUrl: 'http://example.com/image.jpg' },
description: 'A description of the artwork',
primaryConstituent: { name: 'Artist Name', canonicalName: 'Artist Name' },
medium: ['Oil on canvas'],
classification: 'Painting',
dimensions: '94 1/2 × 44 × 33 in. (240 × 111.8 × 83.8 cm)',
copyright: 'Copyright info',
creditLine: 'Credit info',
formattedDate: '2023-01-01',
keywords: ['keyword1', 'keyword2'],
};

const expectedSchema = {
'@context': 'https://schema.org',
'@type': 'VisualArtwork',
name: 'Test Artwork',
image: 'http://example.com/image.jpg',
abstract: 'A description of the artwork',
creator: [{ '@type': 'Person', name: 'Artist Name' }],
artMedium: 'Oil on canvas',
artform: 'Painting',
height: [{ '@type': 'Distance', name: '240 cm' }],
width: [{ '@type': 'Distance', name: '111.8 cm' }],
depth: [{ '@type': 'Distance', name: '83.8 cm' }],
accessMode: 'visual',
copyrightNotice: 'Copyright info',
creditText: 'Credit info',
dateCreated: '2023-01-01',
inLanguage: 'English',
keywords: 'keyword1, keyword2',
};

const result = getSchemaVisualArtwork(mockArtwork);
expect(result).toEqual(expectedSchema);
});

it('should correctly transform a minimal ArtworkDocument with keywords as string', () => {
const mockArtwork: any = {
title: 'Test Artwork',
image: { thumbnailUrl: 'http://example.com/image.jpg' },
description: 'A description of the artwork',
primaryConstituent: { name: 'Artist Name', canonicalName: 'Artist Name' },
medium: ['Oil on canvas'],
classification: 'Painting',
dimensions: '94 1/2 × 44 × 33 in. (240 × 111.8 × 83.8 cm)',
copyright: 'Copyright info',
creditLine: 'Credit info',
formattedDate: '2023-01-01',
keywords: 'keyword1, keyword2',
};

const expectedSchema = {
'@context': 'https://schema.org',
'@type': 'VisualArtwork',
name: 'Test Artwork',
image: 'http://example.com/image.jpg',
abstract: 'A description of the artwork',
creator: [{ '@type': 'Person', name: 'Artist Name' }],
artMedium: 'Oil on canvas',
artform: 'Painting',
height: [{ '@type': 'Distance', name: '240 cm' }],
width: [{ '@type': 'Distance', name: '111.8 cm' }],
depth: [{ '@type': 'Distance', name: '83.8 cm' }],
accessMode: 'visual',
copyrightNotice: 'Copyright info',
creditText: 'Credit info',
dateCreated: '2023-01-01',
inLanguage: 'English',
keywords: 'keyword1, keyword2',
};

const result = getSchemaVisualArtwork(mockArtwork);
expect(result).toEqual(expectedSchema);
});
});
13 changes: 10 additions & 3 deletions lib/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* Functions to transform data into JSON-LD for Schema.org.
* Currently only supports VisualArtwork.
*/
import { vi } from 'date-fns/locale';
import type { VisualArtwork, WithContext } from 'schema-dts';

import type { ArtworkDocument } from '@/types/document';
Expand Down Expand Up @@ -59,7 +58,11 @@ export function getSchemaVisualArtwork(item: ArtworkDocument | undefined) {
},
];
}
if (item.medium) schema.artMedium = item.medium;
if (Array.isArray(item.medium) && item.medium.length > 0) {
schema.artMedium = item.medium.join(', ');
} else if (typeof item.medium === 'string' && item.medium) {
schema.artMedium = item.medium;
}
if (item.classification) schema.artform = item.classification;
const dimensions = getDimensionsCM(item.dimensions);
if (dimensions?.height)
Expand All @@ -73,7 +76,11 @@ export function getSchemaVisualArtwork(item: ArtworkDocument | undefined) {
if (item.creditLine) schema.creditText = item.creditLine;
if (item.formattedDate) schema.dateCreated = item.formattedDate; // TODO
schema.inLanguage = 'English'; // TODO
if (item.keywords?.length) schema.keywords = item.keywords.join(', ');
if (Array.isArray(item.keywords) && item.keywords.length > 0) {
schema.keywords = item.keywords.join(', ');
} else if (typeof item.keywords === 'string' && item.keywords) {
schema.keywords = item.keywords;
}
return schema;
}

Expand Down

0 comments on commit 72c0516

Please sign in to comment.