|
| 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 row next to a top-aligned icon', () => { |
| 28 | + const { container } = renderActionCard({ |
| 29 | + header: 'Header', |
| 30 | + icon: <span />, |
| 31 | + iconVerticalAlignment: 'top', |
| 32 | + styleClassNames, |
| 33 | + }); |
| 34 | + const headers = container.querySelectorAll('.b'); |
| 35 | + expect(headers).toHaveLength(1); |
| 36 | + expect(headers[0]).toHaveClass(styles['header-row']); |
| 37 | + expect(container.querySelector(`.${styles.header}`)).not.toHaveClass('b'); |
| 38 | + }); |
| 39 | + |
| 40 | + test('applies the header class to the header row with a top-aligned icon and only a description', () => { |
| 41 | + const { container, wrapper } = renderActionCard({ |
| 42 | + description: 'Description', |
| 43 | + ariaLabel: 'Card', |
| 44 | + icon: <span />, |
| 45 | + iconVerticalAlignment: 'top', |
| 46 | + styleClassNames, |
| 47 | + }); |
| 48 | + const headers = container.querySelectorAll('.b'); |
| 49 | + expect(headers).toHaveLength(1); |
| 50 | + expect(headers[0]).toHaveClass(styles['header-row']); |
| 51 | + expect(headers[0]).toContainElement(wrapper.findIcon()!.getElement()); |
| 52 | + expect(headers[0]).toContainElement(wrapper.findDescription()!.getElement()); |
| 53 | + }); |
| 54 | + |
| 55 | + test('renders the overlay button on the root with a top-aligned icon and only a description', () => { |
| 56 | + const onClick = jest.fn(); |
| 57 | + const { wrapper } = renderActionCard({ |
| 58 | + description: 'Description', |
| 59 | + ariaLabel: 'Card', |
| 60 | + icon: <span />, |
| 61 | + iconVerticalAlignment: 'top', |
| 62 | + onClick, |
| 63 | + styleClassNames, |
| 64 | + }); |
| 65 | + const buttons = wrapper.getElement().querySelectorAll('button'); |
| 66 | + expect(buttons).toHaveLength(1); |
| 67 | + expect(buttons[0]).toHaveClass(styles['overlay-button']); |
| 68 | + expect(buttons[0].parentElement).toBe(wrapper.getElement()); |
| 69 | + wrapper.click(); |
| 70 | + expect(onClick).toHaveBeenCalledTimes(1); |
| 71 | + }); |
| 72 | + |
| 73 | + test('applies the header class to the header row with a top-aligned icon and no content', () => { |
| 74 | + const { container } = render( |
| 75 | + <ActionCard header="Header" icon={<span />} iconVerticalAlignment="top" {...{ styleClassNames }} /> |
| 76 | + ); |
| 77 | + const wrapper = createWrapper(container).findActionCard()!; |
| 78 | + expect(wrapper.findContent()).toBeNull(); |
| 79 | + const headers = container.querySelectorAll('.b'); |
| 80 | + expect(headers).toHaveLength(1); |
| 81 | + expect(headers[0]).toHaveClass(styles['header-row']); |
| 82 | + expect(headers[0]).toContainElement(wrapper.findIcon()!.getElement()); |
| 83 | + }); |
| 84 | + |
| 85 | + test('applies the header class to the header with a centered icon', () => { |
| 86 | + const { container } = renderActionCard({ |
| 87 | + header: 'Header', |
| 88 | + icon: <span />, |
| 89 | + iconVerticalAlignment: 'center', |
| 90 | + styleClassNames, |
| 91 | + }); |
| 92 | + expect(container.querySelectorAll('.b')).toHaveLength(1); |
| 93 | + expect(container.querySelector('.b')).toHaveClass(styles.header); |
| 94 | + expect(container.querySelector(`.${styles['header-row']}`)).toBeNull(); |
| 95 | + }); |
| 96 | + |
| 97 | + test('applies the header class when only a description is set', () => { |
| 98 | + const { container } = renderActionCard({ description: 'Description', styleClassNames }); |
| 99 | + expect(container.querySelectorAll('.b')).toHaveLength(1); |
| 100 | + expect(container.querySelector('.b')).toHaveClass(styles.header); |
| 101 | + }); |
| 102 | + |
| 103 | + test('renders no header class when neither header nor description is set', () => { |
| 104 | + const { container } = renderActionCard({ ariaLabel: 'Card', styleClassNames }); |
| 105 | + expect(container.querySelector('.b')).toBeNull(); |
| 106 | + }); |
| 107 | + |
| 108 | + test('applies the root class with href and when disabled', () => { |
| 109 | + const link = renderActionCard({ header: 'Link', href: '#', styleClassNames }); |
| 110 | + expect(link.wrapper.getElement()).toHaveClass('a'); |
| 111 | + const disabled = renderActionCard({ header: 'Disabled', disabled: true, styleClassNames }); |
| 112 | + expect(disabled.wrapper.getElement()).toHaveClass('a'); |
| 113 | + }); |
| 114 | + |
| 115 | + test('does not leak the styleClassNames prop to the DOM', () => { |
| 116 | + const { container } = renderActionCard({ header: 'Header', styleClassNames }); |
| 117 | + expect(container.querySelector('[styleClassNames]')).toBeNull(); |
| 118 | + }); |
| 119 | +}); |
0 commit comments