|
| 1 | +import { render, screen } from '@testing-library/react'; |
| 2 | +import '@testing-library/jest-dom'; |
| 3 | +import { describe, it, expect, vi } from 'vitest'; |
| 4 | +import ShareButtons from './ShareButtons'; |
| 5 | +import React from 'react'; |
| 6 | + |
| 7 | +describe('ShareButtons - Error Resilience & Hydration Stability', () => { |
| 8 | + it('renders correctly with missing optional props like title', () => { |
| 9 | + render(<ShareButtons url="https://example.com" />); |
| 10 | + const twitterButton = screen.getByLabelText(/Share on X/i); |
| 11 | + expect(twitterButton).toHaveAttribute( |
| 12 | + 'href', |
| 13 | + 'https://x.com/intent/tweet?url=https%3A%2F%2Fexample.com' |
| 14 | + ); |
| 15 | + }); |
| 16 | + |
| 17 | + it('maintains hydration stability when rendered on server vs client', () => { |
| 18 | + const { container } = render(<ShareButtons url="https://example.com" />); |
| 19 | + expect(container.innerHTML).toContain('href'); |
| 20 | + }); |
| 21 | + |
| 22 | + it('safely handles malformed url strings without throwing exceptions', () => { |
| 23 | + // Malformed URLs should be encoded correctly by encodeURIComponent |
| 24 | + const malformedUrl = 'https://example.com/%%%'; |
| 25 | + render(<ShareButtons url={malformedUrl} />); |
| 26 | + const linkedinButton = screen.getByLabelText(/Share on LinkedIn/i); |
| 27 | + expect(linkedinButton).toBeInTheDocument(); |
| 28 | + }); |
| 29 | + |
| 30 | + it('provides fallback behavior if icons fail to load or are missing', () => { |
| 31 | + render(<ShareButtons url="https://example.com" />); |
| 32 | + const linkedinButton = screen.getByLabelText(/Share on LinkedIn/i); |
| 33 | + const svgIcon = linkedinButton.querySelector('svg'); |
| 34 | + expect(svgIcon).toBeInTheDocument(); |
| 35 | + }); |
| 36 | + |
| 37 | + it('ensures exception safety when extreme string lengths are provided', () => { |
| 38 | + const longUrl = 'https://example.com/' + 'a'.repeat(10000); |
| 39 | + expect(() => render(<ShareButtons url={longUrl} />)).not.toThrow(); |
| 40 | + }); |
| 41 | +}); |
0 commit comments