|
| 1 | +import AnchorFm from '$lib/components/anchor-fm.svelte'; |
| 2 | +import { describe, expect, it } from 'vitest'; |
| 3 | +import { render } from 'vitest-browser-svelte'; |
| 4 | + |
| 5 | +let episodeUrl = |
| 6 | + 'purrfect-dev/embed/episodes/1-31---Delivering-Digital-Content-with-GraphCMS-e14g55c/a-a650v9a'; |
| 7 | + |
| 8 | +describe('AnchorFm', () => { |
| 9 | + it('mounts with episode url', async () => { |
| 10 | + const { container } = render(AnchorFm, { |
| 11 | + episodeUrl, |
| 12 | + disable_observer: true, |
| 13 | + }); |
| 14 | + expect(container).toBeTruthy(); |
| 15 | + }); |
| 16 | + |
| 17 | + it('renders iframe with episode url', async () => { |
| 18 | + const { getByTestId } = render(AnchorFm, { |
| 19 | + episodeUrl, |
| 20 | + disable_observer: true, |
| 21 | + }); |
| 22 | + const iframe = getByTestId('anchor-fm-episode'); |
| 23 | + const expected_src = `https://anchor.fm/${episodeUrl}`; |
| 24 | + await expect.element(iframe).toHaveAttribute('src', expected_src); |
| 25 | + }); |
| 26 | + |
| 27 | + it('mounts with custom height and width', async () => { |
| 28 | + const { container } = render(AnchorFm, { |
| 29 | + episodeUrl, |
| 30 | + height: '200px', |
| 31 | + width: '50%', |
| 32 | + disable_observer: true, |
| 33 | + }); |
| 34 | + const iframe = container.querySelector('iframe'); |
| 35 | + |
| 36 | + expect(iframe?.parentElement?.style.height).toBe('200px'); |
| 37 | + expect(iframe?.parentElement?.style.width).toBe('50%'); |
| 38 | + }); |
| 39 | + |
| 40 | + it('renders with a GeneralObserver', async () => { |
| 41 | + const { getByTestId } = render(AnchorFm, { |
| 42 | + episodeUrl, |
| 43 | + disable_observer: false, |
| 44 | + }); |
| 45 | + const general_observer = getByTestId('general-observer'); |
| 46 | + await expect.element(general_observer).toBeInTheDocument(); |
| 47 | + }); |
| 48 | + |
| 49 | + // Coverage gaps - test stubs to implement |
| 50 | + it('should handle empty episodeUrl gracefully', async () => { |
| 51 | + // Test edge case: empty or invalid episode URL |
| 52 | + const { getByTestId } = render(AnchorFm, { |
| 53 | + episodeUrl: '', |
| 54 | + disable_observer: true, |
| 55 | + }); |
| 56 | + const iframe = getByTestId('anchor-fm-episode'); |
| 57 | + await expect |
| 58 | + .element(iframe) |
| 59 | + .toHaveAttribute('src', 'https://anchor.fm/'); |
| 60 | + await expect |
| 61 | + .element(iframe) |
| 62 | + .toHaveAttribute('title', 'anchor-fm-'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should apply default height and width when not provided', async () => { |
| 66 | + // Test default prop values (height: '100px', width: '100%') |
| 67 | + const { container } = render(AnchorFm, { |
| 68 | + episodeUrl, |
| 69 | + disable_observer: true, |
| 70 | + }); |
| 71 | + const iframe = container.querySelector('iframe'); |
| 72 | + |
| 73 | + expect(iframe?.parentElement?.style.height).toBe('100px'); |
| 74 | + expect(iframe?.parentElement?.style.width).toBe('100%'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should handle special characters in episodeUrl', async () => { |
| 78 | + // Test URL encoding and special characters |
| 79 | + const specialUrl = |
| 80 | + 'test-podcast/episodes/episode-with-special-chars-!@#$%^&*()'; |
| 81 | + const { getByTestId } = render(AnchorFm, { |
| 82 | + episodeUrl: specialUrl, |
| 83 | + disable_observer: true, |
| 84 | + }); |
| 85 | + const iframe = getByTestId('anchor-fm-episode'); |
| 86 | + const expectedSrc = `https://anchor.fm/${specialUrl}`; |
| 87 | + await expect.element(iframe).toHaveAttribute('src', expectedSrc); |
| 88 | + await expect |
| 89 | + .element(iframe) |
| 90 | + .toHaveAttribute('title', `anchor-fm-${specialUrl}`); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should have proper iframe accessibility attributes', async () => { |
| 94 | + // Test title attribute, aria-labels, etc. |
| 95 | + const { getByTestId } = render(AnchorFm, { |
| 96 | + episodeUrl, |
| 97 | + disable_observer: true, |
| 98 | + }); |
| 99 | + const iframe = getByTestId('anchor-fm-episode'); |
| 100 | + |
| 101 | + await expect |
| 102 | + .element(iframe) |
| 103 | + .toHaveAttribute('title', `anchor-fm-${episodeUrl}`); |
| 104 | + await expect.element(iframe).toHaveAttribute('frameborder', '0'); |
| 105 | + await expect.element(iframe).toHaveAttribute('scrolling', 'no'); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should handle very long episodeUrl values', async () => { |
| 109 | + // Test edge case: extremely long URLs |
| 110 | + const longUrl = 'a'.repeat(1000) + '/episodes/' + 'b'.repeat(500); |
| 111 | + const { getByTestId } = render(AnchorFm, { |
| 112 | + episodeUrl: longUrl, |
| 113 | + disable_observer: true, |
| 114 | + }); |
| 115 | + const iframe = getByTestId('anchor-fm-episode'); |
| 116 | + const expectedSrc = `https://anchor.fm/${longUrl}`; |
| 117 | + await expect.element(iframe).toHaveAttribute('src', expectedSrc); |
| 118 | + }); |
| 119 | + |
| 120 | + it('should apply custom CSS styles correctly', async () => { |
| 121 | + // Test that custom height/width styles are properly applied |
| 122 | + const customHeight = '300px'; |
| 123 | + const customWidth = '75%'; |
| 124 | + const { container } = render(AnchorFm, { |
| 125 | + episodeUrl, |
| 126 | + height: customHeight, |
| 127 | + width: customWidth, |
| 128 | + disable_observer: true, |
| 129 | + }); |
| 130 | + const iframe = container.querySelector('iframe'); |
| 131 | + const wrapperDiv = iframe?.parentElement; |
| 132 | + |
| 133 | + expect(wrapperDiv?.style.height).toBe(customHeight); |
| 134 | + expect(wrapperDiv?.style.width).toBe(customWidth); |
| 135 | + expect(wrapperDiv?.style.position).toBe('relative'); |
| 136 | + |
| 137 | + // Check iframe styles |
| 138 | + expect(iframe?.style.position).toBe('absolute'); |
| 139 | + expect(iframe?.style.top).toBe('0px'); |
| 140 | + expect(iframe?.style.left).toBe('0px'); |
| 141 | + expect(iframe?.style.width).toBe('100%'); |
| 142 | + expect(iframe?.style.height).toBe('100%'); |
| 143 | + }); |
| 144 | + |
| 145 | + it('should handle numeric height and width values', async () => { |
| 146 | + // Test passing numbers instead of strings for dimensions |
| 147 | + const { container } = render(AnchorFm, { |
| 148 | + episodeUrl, |
| 149 | + height: '250px', |
| 150 | + width: '80%', |
| 151 | + disable_observer: true, |
| 152 | + }); |
| 153 | + const iframe = container.querySelector('iframe'); |
| 154 | + |
| 155 | + // Component should handle numeric values by converting them |
| 156 | + expect(iframe?.parentElement?.style.height).toBe('250px'); |
| 157 | + expect(iframe?.parentElement?.style.width).toBe('80%'); |
| 158 | + }); |
| 159 | +}); |
0 commit comments