From b5d2c60d7d3d9d354076f9f47f240156021b074d Mon Sep 17 00:00:00 2001 From: VWSCoronaDashboard28 Date: Mon, 18 Sep 2023 15:13:31 +0200 Subject: [PATCH] feat(contact-page-redesign): Add utility and unit test. --- .../components/contact/contact-page-link.tsx | 12 +------ .../format-link-according-to-type.spec.ts | 34 +++++++++++++++++++ .../utils/format-link-according-to-type.ts | 13 +++++++ 3 files changed, 48 insertions(+), 11 deletions(-) create mode 100644 packages/app/src/utils/__tests__/format-link-according-to-type.spec.ts create mode 100644 packages/app/src/utils/format-link-according-to-type.ts diff --git a/packages/app/src/components/contact/contact-page-link.tsx b/packages/app/src/components/contact/contact-page-link.tsx index 0794d8acd1..e7896b39f0 100644 --- a/packages/app/src/components/contact/contact-page-link.tsx +++ b/packages/app/src/components/contact/contact-page-link.tsx @@ -2,6 +2,7 @@ import { ChevronRight, External, Telephone } from '@corona-dashboard/icons'; import styled from 'styled-components'; import { ExternalLink } from '~/components/external-link'; import { space } from '~/style/theme'; +import { formatLinkAccordingToType } from '~/utils/format-link-according-to-type'; import { isInternalUrl } from '~/utils/is-internal-url'; import { Link } from '~/utils/link'; import { LinkType } from './types'; @@ -13,17 +14,6 @@ interface ContactPageLinkProps { } export const ContactPageLink = ({ href, label, linkType }: ContactPageLinkProps) => { - const formatLinkAccordingToType = (href: string, linkType: LinkType | undefined) => { - switch (linkType) { - case 'email': - return `mailto:${href}`; - case 'phone': - return `tel:${href.replace(/\s/g, '').replace('-', '')}`; - default: - return href; - } - }; - if (isInternalUrl(href)) { return ( diff --git a/packages/app/src/utils/__tests__/format-link-according-to-type.spec.ts b/packages/app/src/utils/__tests__/format-link-according-to-type.spec.ts new file mode 100644 index 0000000000..4bff96a984 --- /dev/null +++ b/packages/app/src/utils/__tests__/format-link-according-to-type.spec.ts @@ -0,0 +1,34 @@ +import { suite } from 'uvu'; +import * as assert from 'uvu/assert'; +import { formatLinkAccordingToType } from '../format-link-according-to-type'; + +const FormatLinkAccordingToType = suite('formatLinkAccordingToType'); + +FormatLinkAccordingToType('should not do anything to a regular link', () => { + const linkType = 'regular'; + const href = '/'; + + const result = formatLinkAccordingToType(href, linkType); + + assert.is(result, '/'); +}); + +FormatLinkAccordingToType('should format as a telephone link', () => { + const linkType = 'phone'; + const href = '123-456-789'; + + const result = formatLinkAccordingToType(href, linkType); + + assert.is(result, 'tel:123456789'); +}); + +FormatLinkAccordingToType('should format as a email link', () => { + const linkType = 'email'; + const href = 'test@test.com'; + + const result = formatLinkAccordingToType(href, linkType); + + assert.is(result, 'mailto:test@test.com'); +}); + +FormatLinkAccordingToType.run(); diff --git a/packages/app/src/utils/format-link-according-to-type.ts b/packages/app/src/utils/format-link-according-to-type.ts new file mode 100644 index 0000000000..5223ef69f1 --- /dev/null +++ b/packages/app/src/utils/format-link-according-to-type.ts @@ -0,0 +1,13 @@ +/** + * Returns a formatted link based on the link type of email, phone, regular + */ +export const formatLinkAccordingToType = (href: string, linkType: string | undefined) => { + switch (linkType) { + case 'email': + return `mailto:${href}`; + case 'phone': + return `tel:${href.replace(/\s/g, '').replaceAll('-', '')}`; + default: + return href; + } +};