From 334eef8088683e9d7de6885a4eb146bf69c22fd4 Mon Sep 17 00:00:00 2001 From: Vincent Smedinga Date: Wed, 24 Jan 2024 12:57:08 +0100 Subject: [PATCH] Extract reusable variable for badge colors Source: https://jaketrent.com/post/loop-typescript-union-type/ --- packages/react/src/Badge/Badge.test.tsx | 86 +++---------------------- packages/react/src/Badge/Badge.tsx | 17 ++++- 2 files changed, 24 insertions(+), 79 deletions(-) diff --git a/packages/react/src/Badge/Badge.test.tsx b/packages/react/src/Badge/Badge.test.tsx index 43d84cb5cb..5d029e2d57 100644 --- a/packages/react/src/Badge/Badge.test.tsx +++ b/packages/react/src/Badge/Badge.test.tsx @@ -1,6 +1,6 @@ import { render } from '@testing-library/react' import { createRef } from 'react' -import { Badge } from './Badge' +import { Badge, badgeColors } from './Badge' import '@testing-library/jest-dom' describe('Badge', () => { @@ -55,83 +55,13 @@ describe('Badge', () => { expect(component).toHaveClass('amsterdam-badge--dark-green') }) - it('renders with black color', () => { - const { container } = render() + badgeColors.map((color) => + it(`renders with ${color} color`, () => { + const { container } = render() - const component = container.querySelector(':only-child') - - expect(component).toHaveClass('amsterdam-badge--black') - }) - - it('renders with blue color', () => { - const { container } = render() - - const component = container.querySelector(':only-child') - - expect(component).toHaveClass('amsterdam-badge--blue') - }) - - it('renders with dark-blue color', () => { - const { container } = render() - - const component = container.querySelector(':only-child') - - expect(component).toHaveClass('amsterdam-badge--dark-blue') - }) - - it('renders with dark-green color', () => { - const { container } = render() - - const component = container.querySelector(':only-child') - - expect(component).toHaveClass('amsterdam-badge--dark-green') - }) - - it('renders with green color', () => { - const { container } = render() - - const component = container.querySelector(':only-child') - - expect(component).toHaveClass('amsterdam-badge--green') - }) + const component = container.querySelector(':only-child') - it('renders with magenta color', () => { - const { container } = render() - - const component = container.querySelector(':only-child') - - expect(component).toHaveClass('amsterdam-badge--magenta') - }) - - it('renders with orange color', () => { - const { container } = render() - - const component = container.querySelector(':only-child') - - expect(component).toHaveClass('amsterdam-badge--orange') - }) - - it('renders with purple color', () => { - const { container } = render() - - const component = container.querySelector(':only-child') - - expect(component).toHaveClass('amsterdam-badge--purple') - }) - - it('renders with white color', () => { - const { container } = render() - - const component = container.querySelector(':only-child') - - expect(component).toHaveClass('amsterdam-badge--white') - }) - - it('renders with yellow color', () => { - const { container } = render() - - const component = container.querySelector(':only-child') - - expect(component).toHaveClass('amsterdam-badge--yellow') - }) + expect(component).toHaveClass(`amsterdam-badge--${color}`) + }), + ) }) diff --git a/packages/react/src/Badge/Badge.tsx b/packages/react/src/Badge/Badge.tsx index 170e40734d..fe7b00f62d 100644 --- a/packages/react/src/Badge/Badge.tsx +++ b/packages/react/src/Badge/Badge.tsx @@ -7,8 +7,23 @@ import clsx from 'clsx' import { forwardRef } from 'react' import type { ForwardedRef, HTMLAttributes } from 'react' +export const badgeColors = [ + 'black', + 'blue', + 'dark-blue', + 'dark-green', + 'green', + 'magenta', + 'orange', + 'purple', + 'white', + 'yellow', +] as const + +type BadgeColor = (typeof badgeColors)[number] + export interface BadgeProps extends HTMLAttributes { - color?: 'black' | 'blue' | 'dark-blue' | 'dark-green' | 'green' | 'magenta' | 'orange' | 'purple' | 'white' | 'yellow' + color?: BadgeColor label: string | number }