|
| 1 | +import React from 'react'; |
| 2 | +import { render, screen } from '@testing-library/react'; |
| 3 | +import '@testing-library/jest-dom'; |
| 4 | +import GridTile, { GridTileProps } from '../GridTile'; |
| 5 | + |
| 6 | +const defaultProps: GridTileProps = { |
| 7 | + widgetType: 'test-widget', |
| 8 | + isDragging: false, |
| 9 | + setIsDragging: jest.fn(), |
| 10 | + setWidgetAttribute: jest.fn(), |
| 11 | + widgetConfig: { |
| 12 | + i: 'test-widget-1', |
| 13 | + x: 0, |
| 14 | + y: 0, |
| 15 | + w: 2, |
| 16 | + h: 3, |
| 17 | + colWidth: 100, |
| 18 | + }, |
| 19 | + removeWidget: jest.fn(), |
| 20 | + children: <div data-testid="widget-content">Widget Content</div>, |
| 21 | + isLoaded: true, |
| 22 | +}; |
| 23 | + |
| 24 | +describe('GridTile - wrapperProps and cardBodyProps', () => { |
| 25 | + describe('wrapperProps', () => { |
| 26 | + it('should pass wrapperProps to the Card wrapper component', () => { |
| 27 | + const wrapperProps = { |
| 28 | + 'data-testid': 'custom-card-wrapper', |
| 29 | + 'aria-label': 'Custom card label', |
| 30 | + }; |
| 31 | + |
| 32 | + render( |
| 33 | + <GridTile |
| 34 | + {...defaultProps} |
| 35 | + widgetConfig={{ |
| 36 | + ...defaultProps.widgetConfig, |
| 37 | + config: { |
| 38 | + wrapperProps, |
| 39 | + }, |
| 40 | + }} |
| 41 | + /> |
| 42 | + ); |
| 43 | + |
| 44 | + const cardWrapper = screen.getByTestId('custom-card-wrapper'); |
| 45 | + expect(cardWrapper).toBeInTheDocument(); |
| 46 | + expect(cardWrapper).toHaveAttribute('aria-label', 'Custom card label'); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should merge custom className with default className in wrapperProps', () => { |
| 50 | + const wrapperProps = { |
| 51 | + className: 'custom-wrapper-class', |
| 52 | + 'data-testid': 'card-with-custom-class', |
| 53 | + }; |
| 54 | + |
| 55 | + render( |
| 56 | + <GridTile |
| 57 | + {...defaultProps} |
| 58 | + widgetConfig={{ |
| 59 | + ...defaultProps.widgetConfig, |
| 60 | + config: { |
| 61 | + wrapperProps, |
| 62 | + }, |
| 63 | + }} |
| 64 | + /> |
| 65 | + ); |
| 66 | + |
| 67 | + const cardWrapper = screen.getByTestId('card-with-custom-class'); |
| 68 | + expect(cardWrapper).toHaveClass('grid-tile'); |
| 69 | + expect(cardWrapper).toHaveClass('custom-wrapper-class'); |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + describe('cardBodyProps', () => { |
| 74 | + it('should pass cardBodyProps to the CardBody component', () => { |
| 75 | + const cardBodyProps = { |
| 76 | + 'data-testid': 'custom-card-body', |
| 77 | + 'aria-label': 'Custom card body label', |
| 78 | + }; |
| 79 | + |
| 80 | + render( |
| 81 | + <GridTile |
| 82 | + {...defaultProps} |
| 83 | + widgetConfig={{ |
| 84 | + ...defaultProps.widgetConfig, |
| 85 | + config: { |
| 86 | + cardBodyProps, |
| 87 | + }, |
| 88 | + }} |
| 89 | + /> |
| 90 | + ); |
| 91 | + |
| 92 | + const cardBody = screen.getByTestId('custom-card-body'); |
| 93 | + expect(cardBody).toBeInTheDocument(); |
| 94 | + expect(cardBody).toHaveAttribute('aria-label', 'Custom card body label'); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should merge custom className with default className in cardBodyProps', () => { |
| 98 | + const cardBodyProps = { |
| 99 | + className: 'custom-body-class', |
| 100 | + 'data-testid': 'body-with-custom-class', |
| 101 | + }; |
| 102 | + |
| 103 | + render( |
| 104 | + <GridTile |
| 105 | + {...defaultProps} |
| 106 | + widgetConfig={{ |
| 107 | + ...defaultProps.widgetConfig, |
| 108 | + config: { |
| 109 | + cardBodyProps, |
| 110 | + }, |
| 111 | + }} |
| 112 | + /> |
| 113 | + ); |
| 114 | + |
| 115 | + const cardBody = screen.getByTestId('body-with-custom-class'); |
| 116 | + expect(cardBody).toHaveClass('pf-v6-u-p-0'); |
| 117 | + expect(cardBody).toHaveClass('custom-body-class'); |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + describe('both props together', () => { |
| 122 | + it('should correctly apply both wrapperProps and cardBodyProps simultaneously', () => { |
| 123 | + const wrapperProps = { |
| 124 | + className: 'custom-wrapper', |
| 125 | + 'data-testid': 'combined-wrapper', |
| 126 | + }; |
| 127 | + |
| 128 | + const cardBodyProps = { |
| 129 | + className: 'custom-body', |
| 130 | + 'data-testid': 'combined-body', |
| 131 | + }; |
| 132 | + |
| 133 | + render( |
| 134 | + <GridTile |
| 135 | + {...defaultProps} |
| 136 | + widgetConfig={{ |
| 137 | + ...defaultProps.widgetConfig, |
| 138 | + config: { |
| 139 | + wrapperProps, |
| 140 | + cardBodyProps, |
| 141 | + }, |
| 142 | + }} |
| 143 | + /> |
| 144 | + ); |
| 145 | + |
| 146 | + const cardWrapper = screen.getByTestId('combined-wrapper'); |
| 147 | + expect(cardWrapper).toHaveClass('grid-tile'); |
| 148 | + expect(cardWrapper).toHaveClass('custom-wrapper'); |
| 149 | + |
| 150 | + const cardBody = screen.getByTestId('combined-body'); |
| 151 | + expect(cardBody).toHaveClass('pf-v6-u-p-0'); |
| 152 | + expect(cardBody).toHaveClass('custom-body'); |
| 153 | + }); |
| 154 | + }); |
| 155 | +}); |
0 commit comments