Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Commit

Permalink
feat(contact-page-redesign): Add utility and unit test.
Browse files Browse the repository at this point in the history
  • Loading branch information
APW26 committed Sep 18, 2023
1 parent d36598e commit b5d2c60
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 11 deletions.
12 changes: 1 addition & 11 deletions packages/app/src/components/contact/contact-page-link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 (
<LinkWrapper iconMargin={`0 0 0 ${space[2]}`}>
Expand Down
Original file line number Diff line number Diff line change
@@ -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 = '[email protected]';

const result = formatLinkAccordingToType(href, linkType);

assert.is(result, 'mailto:[email protected]');
});

FormatLinkAccordingToType.run();
13 changes: 13 additions & 0 deletions packages/app/src/utils/format-link-according-to-type.ts
Original file line number Diff line number Diff line change
@@ -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;
}
};

0 comments on commit b5d2c60

Please sign in to comment.