From 4d827445fc45fee3c6556bd74f17fa2a55b4ff4b Mon Sep 17 00:00:00 2001 From: Kazumi Inada Date: Sun, 4 Dec 2022 00:25:53 +0900 Subject: [PATCH] fix: Accept type string for `location` and `address` --- src/types.ts | 18 +++++---- src/utils/schema/__tests__/setAddress.test.ts | 4 ++ .../schema/__tests__/setLocation.test.ts | 37 +++++++++++++++++++ src/utils/schema/setAddress.ts | 3 +- src/utils/schema/setLocation.ts | 6 ++- 5 files changed, 58 insertions(+), 10 deletions(-) create mode 100644 src/utils/schema/__tests__/setLocation.test.ts diff --git a/src/types.ts b/src/types.ts index a52f0348..6245a954 100644 --- a/src/types.ts +++ b/src/types.ts @@ -95,7 +95,7 @@ export interface VirtualLocation { url: string; } -export type Location = Place | VirtualLocation; +export type Location = string | Place | VirtualLocation; export type EventStatus = | 'EventCancelled' @@ -162,13 +162,15 @@ export interface OpenGraphMedia { secureUrl?: string; } -export interface Address { - streetAddress: string; - addressLocality: string; - addressRegion?: string; - postalCode: string; - addressCountry: string; -} +export type Address = + | string + | { + streetAddress: string; + addressLocality: string; + addressRegion?: string; + postalCode: string; + addressCountry: string; + }; export interface Video { name: string; diff --git a/src/utils/schema/__tests__/setAddress.test.ts b/src/utils/schema/__tests__/setAddress.test.ts index 2f22bb24..a38bff09 100644 --- a/src/utils/schema/__tests__/setAddress.test.ts +++ b/src/utils/schema/__tests__/setAddress.test.ts @@ -22,6 +22,10 @@ describe('setAddress', () => { expect(setAddress(undefined)).toBeUndefined(); }); + test('should accepts simple string and returns it', () => { + expect(setAddress('AddressText')).toBe('AddressText'); + }); + test('single address returns correctly', () => { const data = setAddress(addressOne); diff --git a/src/utils/schema/__tests__/setLocation.test.ts b/src/utils/schema/__tests__/setLocation.test.ts new file mode 100644 index 00000000..af114bc4 --- /dev/null +++ b/src/utils/schema/__tests__/setLocation.test.ts @@ -0,0 +1,37 @@ +import { Place, VirtualLocation } from 'src/types'; +import { setLocation } from '../setLocation'; + +const virtualLocation: VirtualLocation = { + name: 'Virtual Location', + sameAs: 'https://example.com', + url: 'https://example.com', +}; + +const place: Place = { + name: 'Place', + address: 'Address String', +}; + +describe('setLocation', () => { + test('should return undefined if location is undefined', () => { + expect(setLocation(undefined)).toBeUndefined(); + }); + + test('should accepts simple string and returns it', () => { + expect(setLocation('LocationText')).toBe('LocationText'); + }); + + test('returns VirtualLocation correctly', () => { + expect(setLocation(virtualLocation)).toMatchObject({ + '@type': 'VirtualLocation', + ...virtualLocation, + }); + }); + + test('returns Place correctly', () => { + expect(setLocation(place)).toMatchObject({ + '@type': 'Place', + ...place, + }); + }); +}); diff --git a/src/utils/schema/setAddress.ts b/src/utils/schema/setAddress.ts index 07dfab2f..16a9c756 100644 --- a/src/utils/schema/setAddress.ts +++ b/src/utils/schema/setAddress.ts @@ -2,7 +2,7 @@ import { Address } from 'src/types'; export function setAddress(address?: Address | Address[]) { if (!address) return undefined; - + if (!Array.isArray(address)) return toPostalAddress(address); // If array of one address, replace with single address @@ -13,5 +13,6 @@ export function setAddress(address?: Address | Address[]) { } function toPostalAddress(address: Address) { + if (typeof address === 'string') return address; return { '@type': 'PostalAddress', ...address }; } diff --git a/src/utils/schema/setLocation.ts b/src/utils/schema/setLocation.ts index 6b3be55d..9574e542 100644 --- a/src/utils/schema/setLocation.ts +++ b/src/utils/schema/setLocation.ts @@ -1,11 +1,15 @@ import { Location, Place, VirtualLocation } from 'src/types'; import { setAddress } from './setAddress'; -export function setLocation(location: Location) { +export function setLocation(location?: Location) { if (!location) { return undefined; } + if (typeof location === 'string') { + return location; + } + if ('url' in location) { return setVirtualLocation(location); } else {