|
| 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 ActionCard, { ActionCardProps } from '../../../lib/components/action-card'; |
| 7 | +import createWrapper from '../../../lib/components/test-utils/dom'; |
| 8 | + |
| 9 | +import styles from '../../../lib/components/action-card/styles.css.js'; |
| 10 | + |
| 11 | +function renderActionCard(props: ActionCardProps & { styleClassNames?: Record<string, string> }) { |
| 12 | + const { container } = render(<ActionCard {...props}>Content</ActionCard>); |
| 13 | + return { container, wrapper: createWrapper(container).findActionCard()! }; |
| 14 | +} |
| 15 | + |
| 16 | +const styleClassNames = { root: 'a', header: 'b', unknown: 'c' }; |
| 17 | + |
| 18 | +describe('ActionCard Style API v2', () => { |
| 19 | + test('applies each slot class to its element and ignores unknown keys', () => { |
| 20 | + const { container, wrapper } = renderActionCard({ header: 'Header', description: 'Description', styleClassNames }); |
| 21 | + expect(wrapper.getElement()).toHaveClass('a'); |
| 22 | + expect(container.querySelector('.b')).toHaveClass(styles.header); |
| 23 | + expect(container.querySelectorAll('.a, .b')).toHaveLength(2); |
| 24 | + expect(container.querySelector('.c')).toBeNull(); |
| 25 | + }); |
| 26 | + |
| 27 | + test('applies the header class to the header next to a top-aligned icon', () => { |
| 28 | + const { container } = renderActionCard({ |
| 29 | + header: 'Header', |
| 30 | + icon: <span />, |
| 31 | + iconVerticalAlignment: 'top', |
| 32 | + styleClassNames, |
| 33 | + }); |
| 34 | + const header = container.querySelector('.b')!; |
| 35 | + expect(header).toHaveClass(styles.header); |
| 36 | + expect(header.parentElement).toHaveClass(styles['header-row']); |
| 37 | + expect(container.querySelectorAll('.b')).toHaveLength(1); |
| 38 | + }); |
| 39 | + |
| 40 | + test('applies the header class when only a description is set', () => { |
| 41 | + const { container } = renderActionCard({ description: 'Description', styleClassNames }); |
| 42 | + expect(container.querySelector('.b')).toHaveClass(styles.header); |
| 43 | + }); |
| 44 | + |
| 45 | + test('renders no header class when neither header nor description is set', () => { |
| 46 | + const { container } = renderActionCard({ ariaLabel: 'Card', styleClassNames }); |
| 47 | + expect(container.querySelector('.b')).toBeNull(); |
| 48 | + }); |
| 49 | + |
| 50 | + test('applies the root class with href and when disabled', () => { |
| 51 | + const link = renderActionCard({ header: 'Link', href: '#', styleClassNames }); |
| 52 | + expect(link.wrapper.getElement()).toHaveClass('a'); |
| 53 | + const disabled = renderActionCard({ header: 'Disabled', disabled: true, styleClassNames }); |
| 54 | + expect(disabled.wrapper.getElement()).toHaveClass('a'); |
| 55 | + }); |
| 56 | + |
| 57 | + test('does not leak the styleClassNames prop to the DOM', () => { |
| 58 | + const { container } = renderActionCard({ header: 'Header', styleClassNames }); |
| 59 | + expect(container.querySelector('[styleClassNames]')).toBeNull(); |
| 60 | + }); |
| 61 | +}); |
0 commit comments