|
| 1 | +import '@testing-library/jest-dom'; |
| 2 | +import { render, screen, fireEvent } from '@testing-library/react'; |
| 3 | +import React from 'react'; |
| 4 | + |
| 5 | +import { getContentLength } from './config'; |
| 6 | +import LinkCard from './LinkCard'; |
| 7 | + |
| 8 | +// Mock the config module |
| 9 | +jest.mock('./config', () => ({ |
| 10 | + getContentLength: jest.fn() |
| 11 | +})); |
| 12 | + |
| 13 | +/** |
| 14 | + * Test suite for the LinkCard component. |
| 15 | + * |
| 16 | + * This test suite verifies that the LinkCard component correctly: |
| 17 | + * - Renders with required props (href, text, description) |
| 18 | + * - Renders the link with proper attributes (href, target, rel) |
| 19 | + * - Displays the description text |
| 20 | + * - Truncates long descriptions based on window width |
| 21 | + * - Shows/hides text properly when the toggle button is clicked |
| 22 | + * - Handles line breaks in the description |
| 23 | + */ |
| 24 | +describe('LinkCard', () => { |
| 25 | + const defaultProps = { |
| 26 | + href: 'https://example.com', |
| 27 | + text: 'Example Link', |
| 28 | + description: 'This is a sample description.' |
| 29 | + }; |
| 30 | + |
| 31 | + beforeEach(() => { |
| 32 | + // Reset mocks |
| 33 | + jest.clearAllMocks(); |
| 34 | + // Mock the getContentLength to return a fixed value |
| 35 | + getContentLength.mockReturnValue(20); |
| 36 | + // Mock window.innerWidth |
| 37 | + Object.defineProperty(window, 'innerWidth', { |
| 38 | + writable: true, |
| 39 | + configurable: true, |
| 40 | + value: 1024 |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + it('renders with required props', () => { |
| 45 | + // Override getContentLength for this test to avoid truncation |
| 46 | + getContentLength.mockReturnValue(100); |
| 47 | + |
| 48 | + render(<LinkCard {...defaultProps} />); |
| 49 | + |
| 50 | + expect(screen.getByText('Example Link')).toBeInTheDocument(); |
| 51 | + expect(screen.getByText('This is a sample description.')).toBeInTheDocument(); |
| 52 | + }); |
| 53 | + |
| 54 | + it('renders the link with proper attributes', () => { |
| 55 | + render(<LinkCard {...defaultProps} />); |
| 56 | + |
| 57 | + const linkElement = screen.getByText('Example Link'); |
| 58 | + expect(linkElement.tagName).toBe('A'); |
| 59 | + expect(linkElement).toHaveAttribute('href', 'https://example.com'); |
| 60 | + expect(linkElement).toHaveAttribute('target', '_blank'); |
| 61 | + expect(linkElement).toHaveAttribute('rel', 'noopener noreferrer'); |
| 62 | + expect(linkElement).toHaveClass('link-card__link'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('applies custom className when provided', () => { |
| 66 | + render( |
| 67 | + <LinkCard |
| 68 | + {...defaultProps} |
| 69 | + className="custom-class" |
| 70 | + data-testid="link-card-container" |
| 71 | + /> |
| 72 | + ); |
| 73 | + |
| 74 | + // Use getByTestId to find the container instead of direct node access |
| 75 | + const cardElement = screen.getByTestId('link-card-container'); |
| 76 | + expect(cardElement).toHaveClass('custom-class'); |
| 77 | + }); |
| 78 | + |
| 79 | + it('truncates long description and shows "Show More" button', () => { |
| 80 | + const longDescription = 'This is a very long description that should be truncated because it exceeds the maximum character limit.'; |
| 81 | + |
| 82 | + render(<LinkCard {...defaultProps} description={longDescription} />); |
| 83 | + |
| 84 | + // Description should be truncated |
| 85 | + expect(screen.queryByText(longDescription)).not.toBeInTheDocument(); |
| 86 | + |
| 87 | + // "Show More" button should be visible |
| 88 | + const showMoreButton = screen.getByText('Show More'); |
| 89 | + expect(showMoreButton).toBeInTheDocument(); |
| 90 | + }); |
| 91 | + |
| 92 | + it('expands truncated description when "Show More" is clicked', () => { |
| 93 | + const longDescription = 'This is a very long description that should be truncated because it exceeds the maximum character limit.'; |
| 94 | + |
| 95 | + render(<LinkCard {...defaultProps} description={longDescription} />); |
| 96 | + |
| 97 | + // Click "Show More" button |
| 98 | + fireEvent.click(screen.getByText('Show More')); |
| 99 | + |
| 100 | + // Full description should now be visible |
| 101 | + expect(screen.getByText(longDescription)).toBeInTheDocument(); |
| 102 | + |
| 103 | + // Button text should change to "Show Less" |
| 104 | + expect(screen.getByText('Show Less')).toBeInTheDocument(); |
| 105 | + }); |
| 106 | + |
| 107 | + it('collapses expanded description when "Show Less" is clicked', () => { |
| 108 | + const longDescription = 'This is a very long description that should be truncated because it exceeds the maximum character limit.'; |
| 109 | + |
| 110 | + render(<LinkCard {...defaultProps} description={longDescription} />); |
| 111 | + |
| 112 | + // First click to expand |
| 113 | + fireEvent.click(screen.getByText('Show More')); |
| 114 | + |
| 115 | + // Second click to collapse |
| 116 | + fireEvent.click(screen.getByText('Show Less')); |
| 117 | + |
| 118 | + // Description should be truncated again |
| 119 | + expect(screen.queryByText(longDescription)).not.toBeInTheDocument(); |
| 120 | + |
| 121 | + // Button text should change back to "Show More" |
| 122 | + expect(screen.getByText('Show More')).toBeInTheDocument(); |
| 123 | + }); |
| 124 | + |
| 125 | + it('handles line breaks in description', () => { |
| 126 | + const descriptionWithLineBreaks = 'Line 1\nLine 2\nLine 3'; |
| 127 | + |
| 128 | + // Override getContentLength for this test to avoid truncation |
| 129 | + getContentLength.mockReturnValue(100); |
| 130 | + |
| 131 | + render(<LinkCard {...defaultProps} description={descriptionWithLineBreaks} />); |
| 132 | + |
| 133 | + // Use the data-testid to directly access the description container |
| 134 | + const descriptionElement = screen.getByTestId('link-card-description'); |
| 135 | + |
| 136 | + // Check if the description container includes all the lines |
| 137 | + expect(descriptionElement).toHaveTextContent('Line 1'); |
| 138 | + expect(descriptionElement).toHaveTextContent('Line 2'); |
| 139 | + expect(descriptionElement).toHaveTextContent('Line 3'); |
| 140 | + }); |
| 141 | + |
| 142 | + it('updates maxChars when window resizes', () => { |
| 143 | + render(<LinkCard {...defaultProps} />); |
| 144 | + |
| 145 | + // Initially called during component mount |
| 146 | + expect(getContentLength).toHaveBeenCalledWith(1024); |
| 147 | + |
| 148 | + // Simulate window resize |
| 149 | + Object.defineProperty(window, 'innerWidth', { |
| 150 | + value: 768 |
| 151 | + }); |
| 152 | + |
| 153 | + // Trigger resize event |
| 154 | + fireEvent(window, new Event('resize')); |
| 155 | + |
| 156 | + // Should be called again with new width |
| 157 | + expect(getContentLength).toHaveBeenCalledWith(768); |
| 158 | + }); |
| 159 | + |
| 160 | + it('handles descriptions that are shorter than maxChars', () => { |
| 161 | + const shortDescription = 'Short description'; |
| 162 | + getContentLength.mockReturnValue(100); // More than the description length |
| 163 | + |
| 164 | + render(<LinkCard {...defaultProps} description={shortDescription} />); |
| 165 | + |
| 166 | + // Description should be displayed without truncation |
| 167 | + expect(screen.getByText(shortDescription)).toBeInTheDocument(); |
| 168 | + |
| 169 | + // No "Show More" button should be present |
| 170 | + expect(screen.queryByText('Show More')).not.toBeInTheDocument(); |
| 171 | + }); |
| 172 | + |
| 173 | + it('cleans up event listeners on unmount', () => { |
| 174 | + const removeEventListenerSpy = jest.spyOn(window, 'removeEventListener'); |
| 175 | + |
| 176 | + const { unmount } = render(<LinkCard {...defaultProps} />); |
| 177 | + unmount(); |
| 178 | + |
| 179 | + expect(removeEventListenerSpy).toHaveBeenCalledWith('resize', expect.any(Function)); |
| 180 | + |
| 181 | + removeEventListenerSpy.mockRestore(); |
| 182 | + }); |
| 183 | +}); |
0 commit comments