|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +import React from 'react'; |
| 4 | +import { render } from '@testing-library/react'; |
| 5 | + |
| 6 | +import ItemCard, { ItemCardProps } from '../../../lib/components/item-card'; |
| 7 | +import createWrapper from '../../../lib/components/test-utils/dom'; |
| 8 | + |
| 9 | +import styles from '../../../lib/components/item-card/styles.selectors.js'; |
| 10 | + |
| 11 | +function renderItemCard(props: ItemCardProps & { styleClassNames?: Record<string, string> } = {}) { |
| 12 | + const { container } = render(<ItemCard {...props}>Content</ItemCard>); |
| 13 | + return { container, wrapper: createWrapper(container).findItemCard()! }; |
| 14 | +} |
| 15 | + |
| 16 | +const styleClassNames = { root: 'a', header: 'b', footer: 'c', unknown: 'd' }; |
| 17 | + |
| 18 | +describe('ItemCard Style API v2', () => { |
| 19 | + test('applies each slot class to its element and ignores unknown keys', () => { |
| 20 | + const { container, wrapper } = renderItemCard({ header: 'Header', footer: 'Footer', styleClassNames }); |
| 21 | + expect(wrapper.getElement()).toHaveClass('a'); |
| 22 | + expect(wrapper.findByClassName(styles.header)!.getElement()).toHaveClass('b'); |
| 23 | + expect(wrapper.findFooter()!.getElement()).toHaveClass('c'); |
| 24 | + expect(container.querySelectorAll('.a, .b, .c')).toHaveLength(3); |
| 25 | + expect(container.querySelector('.d')).toBeNull(); |
| 26 | + }); |
| 27 | + |
| 28 | + test.each(['default', 'embedded'] as const)('applies the header class in the %s variant', variant => { |
| 29 | + const { container, wrapper } = renderItemCard({ variant, description: 'Description', styleClassNames }); |
| 30 | + expect(wrapper.findByClassName(styles.header)!.getElement()).toHaveClass('b'); |
| 31 | + expect(container.querySelectorAll('.b')).toHaveLength(1); |
| 32 | + }); |
| 33 | + |
| 34 | + test('does not render the header class when the header row is empty', () => { |
| 35 | + const { container, wrapper } = renderItemCard({ footer: 'Footer', styleClassNames }); |
| 36 | + expect(wrapper.findByClassName(styles.header)).toBeNull(); |
| 37 | + expect(container.querySelector('.b')).toBeNull(); |
| 38 | + }); |
| 39 | + |
| 40 | + test('does not leak the styleClassNames prop to the DOM', () => { |
| 41 | + const { container } = renderItemCard({ header: 'Header', footer: 'Footer', styleClassNames }); |
| 42 | + expect(container.querySelector('[styleClassNames]')).toBeNull(); |
| 43 | + }); |
| 44 | +}); |
0 commit comments