Skip to content

Commit 4117c97

Browse files
committed
improve tests
1 parent b3d3bff commit 4117c97

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

packages/react/src/Image/Image.features.stories.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ export const BorderRadius: StoryFn<typeof Image> = () => (
3333
<Image
3434
key={borderRadius}
3535
src={placeholderImage}
36-
alt="placeholder, blank area with an off-white background color"
3736
width={200}
3837
height={200}
38+
alt="placeholder, blank area with an off-white background color"
3939
borderRadius={borderRadius}
4040
/>
4141
))}

packages/react/src/Image/Image.test.tsx

+25-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {render} from '@testing-library/react'
33
import '@testing-library/jest-dom'
44
import {axe, toHaveNoViolations} from 'jest-axe'
55

6-
import {Image, ImageBorderRadiusOptions} from './'
6+
import {Image, ImageAspectRatio, ImageBorderRadiusOptions} from './'
77

88
expect.extend(toHaveNoViolations)
99

@@ -17,7 +17,7 @@ describe('Image', () => {
1717
expect(results).toHaveNoViolations()
1818
})
1919

20-
it('should render an img element', async () => {
20+
it('should render an img element', () => {
2121
const {getByAltText} = render(
2222
<Image src="https://via.placeholder.com/600x400/d3d9df/d3d9df.png" alt="alternative text" />,
2323
)
@@ -28,7 +28,7 @@ describe('Image', () => {
2828
expect(imageElement.tagName).toBe('IMG')
2929
})
3030

31-
it('should apply animation styles when `animate` is provided', async () => {
31+
it('should apply animation styles when `animate` is provided', () => {
3232
const {getByRole} = render(
3333
<Image src="https://via.placeholder.com/600x400/d3d9df/d3d9df.png" alt="alternative text" animate="scale-in" />,
3434
)
@@ -39,7 +39,7 @@ describe('Image', () => {
3939
expect(imageElement).toHaveClass('Animation--scale-in')
4040
})
4141

42-
it('should forward the `className` prop to the image', async () => {
42+
it('should forward the `className` prop to the image', () => {
4343
const testClass = 'test'
4444

4545
const {getByRole} = render(
@@ -53,7 +53,7 @@ describe('Image', () => {
5353
expect(getByRole('img')).toHaveClass(testClass)
5454
})
5555

56-
it('should forward the `style` prop to the image', async () => {
56+
it('should forward the `style` prop to the image', () => {
5757
const {getByRole} = render(
5858
<Image
5959
src="https://via.placeholder.com/600x400/d3d9df/d3d9df.png"
@@ -65,18 +65,36 @@ describe('Image', () => {
6565
expect(getByRole('img')).toHaveStyle({background: 'red'})
6666
})
6767

68-
it('should forward the `alt` prop to the image', async () => {
68+
it('should forward the `alt` prop to the image', () => {
6969
const {getByAltText} = render(
7070
<Image src="https://via.placeholder.com/600x400/d3d9df/d3d9df.png" alt="alternative text" />,
7171
)
7272

7373
expect(getByAltText('alternative text')).toBeInTheDocument()
7474
})
7575

76-
it.each(ImageBorderRadiusOptions)('should add the appropriate class when `borderRadius="%s"`', async size => {
76+
it.each(ImageBorderRadiusOptions)('should add the appropriate class when `borderRadius="%s"`', size => {
7777
const {getByRole} = render(
7878
<Image src="https://via.placeholder.com/600x400/d3d9df/d3d9df.png" alt="alternative text" borderRadius={size} />,
7979
)
8080
expect(getByRole('img')).toHaveClass(`Image--borderRadius-${size}`)
8181
})
82+
83+
it.each<{aspectRatio: ImageAspectRatio; classSuffix: string}>([
84+
{aspectRatio: '1:1', classSuffix: '1-1'},
85+
{aspectRatio: '16:9', classSuffix: '16-9'},
86+
{aspectRatio: '16:10', classSuffix: '16-10'},
87+
{aspectRatio: '4:3', classSuffix: '4-3'},
88+
{aspectRatio: 'custom', classSuffix: 'custom'},
89+
])('should add the appropriate class when `aspectRatio="$aspectRatio"`', ({aspectRatio, classSuffix}) => {
90+
const {getByRole} = render(
91+
<Image
92+
src="https://via.placeholder.com/600x400/d3d9df/d3d9df.png"
93+
alt="alternative text"
94+
aspectRatio={aspectRatio}
95+
/>,
96+
)
97+
98+
expect(getByRole('img')).toHaveClass(`Image--aspectRatio-${classSuffix}`)
99+
})
82100
})

packages/react/src/Image/Image.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import {AnimateProps, useAnimation} from '../animation'
55

66
export const ImageBorderRadiusOptions = ['small', 'medium', 'large', 'xlarge', 'full'] as const
77
export type ImageBorderRadiusOptions = (typeof ImageBorderRadiusOptions)[number]
8-
export type ImageAspectRatio = '1:1' | '16:9' | '16:10' | '4:3' | 'custom'
8+
export const ImageAspectRatios = ['1:1', '16:9', '16:10', '4:3', 'custom']
9+
export type ImageAspectRatio = (typeof ImageAspectRatios)[number]
910

1011
export type ImageProps = React.ImgHTMLAttributes<HTMLImageElement> & {
1112
alt: string

0 commit comments

Comments
 (0)