|
| 1 | +import { cleanup, render } from '@testing-library/svelte/svelte5'; |
| 2 | +import { afterEach, describe, expect, it } from 'vitest'; |
| 3 | +import Bluesky from './bluesky.svelte'; |
| 4 | + |
| 5 | +describe('Bluesky', () => { |
| 6 | + afterEach(() => cleanup()); |
| 7 | + |
| 8 | + const test_post_id = |
| 9 | + 'did:plc:nlvjelw3dy3pddq7qoglleko/app.bsky.feed.post/3l6ud34tnwn2k'; |
| 10 | + |
| 11 | + it('mounts with default props', async () => { |
| 12 | + const { container } = render(Bluesky); |
| 13 | + expect(container).toBeTruthy(); |
| 14 | + }); |
| 15 | + |
| 16 | + it('renders iframe with correct embed url', async () => { |
| 17 | + const { getByTestId } = render(Bluesky, { |
| 18 | + post_id: test_post_id, |
| 19 | + }); |
| 20 | + |
| 21 | + const iframe = getByTestId('bluesky-embed'); |
| 22 | + const expected_src = `https://embed.bsky.app/embed/${test_post_id}`; |
| 23 | + expect(iframe.getAttribute('src')).toBe(expected_src); |
| 24 | + }); |
| 25 | + |
| 26 | + it('renders with custom width', async () => { |
| 27 | + const { getByTestId } = render(Bluesky, { |
| 28 | + post_id: test_post_id, |
| 29 | + width: '50%', |
| 30 | + }); |
| 31 | + |
| 32 | + const iframe = getByTestId('bluesky-embed'); |
| 33 | + expect(iframe.getAttribute('width')).toBe('50%'); |
| 34 | + }); |
| 35 | + |
| 36 | + it('applies custom iframe styles', async () => { |
| 37 | + const custom_styles = 'border-radius: 8px; background: #f0f0f0;'; |
| 38 | + const { getByTestId } = render(Bluesky, { |
| 39 | + post_id: test_post_id, |
| 40 | + iframe_styles: custom_styles, |
| 41 | + }); |
| 42 | + |
| 43 | + const iframe = getByTestId('bluesky-embed'); |
| 44 | + const style_text = iframe.style.cssText.toLowerCase(); |
| 45 | + expect(style_text).toContain('border-radius: 8px'); |
| 46 | + expect(style_text).toContain('background: rgb(240, 240, 240)'); |
| 47 | + }); |
| 48 | + |
| 49 | + it('has correct default styles', async () => { |
| 50 | + const { getByTestId } = render(Bluesky, { |
| 51 | + post_id: test_post_id, |
| 52 | + }); |
| 53 | + |
| 54 | + const iframe = getByTestId('bluesky-embed'); |
| 55 | + const style_text = iframe.style.cssText.toLowerCase(); |
| 56 | + expect(style_text).toContain('position: absolute'); |
| 57 | + expect(style_text).toContain('top: 0px'); |
| 58 | + expect(style_text).toContain('left: 0px'); |
| 59 | + expect(style_text).toContain('width: 100%'); |
| 60 | + expect(style_text).toContain('height: 100%'); |
| 61 | + expect(style_text).toContain('border: 0px'); |
| 62 | + expect(iframe.getAttribute('frameborder')).toBe('0'); |
| 63 | + expect(iframe.getAttribute('scrolling')).toBe('no'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('combines default and custom iframe styles correctly', async () => { |
| 67 | + const custom_styles = 'border-radius: 8px; margin: 10px;'; |
| 68 | + const { getByTestId } = render(Bluesky, { |
| 69 | + post_id: test_post_id, |
| 70 | + iframe_styles: custom_styles, |
| 71 | + }); |
| 72 | + |
| 73 | + const iframe = getByTestId('bluesky-embed'); |
| 74 | + const style_text = iframe.style.cssText.toLowerCase(); |
| 75 | + |
| 76 | + // Check default styles are preserved |
| 77 | + expect(style_text).toContain('position: absolute'); |
| 78 | + expect(style_text).toContain('width: 100%'); |
| 79 | + expect(style_text).toContain('height: 100%'); |
| 80 | + expect(style_text).toContain('border: 0px'); |
| 81 | + |
| 82 | + // Check custom styles are applied |
| 83 | + expect(style_text).toContain('border-radius: 8px'); |
| 84 | + expect(style_text).toContain('margin: 10px'); |
| 85 | + }); |
| 86 | + |
| 87 | + it('updates height when receiving message from iframe', () => { |
| 88 | + const { getByTestId } = render(Bluesky, { |
| 89 | + post_id: test_post_id, |
| 90 | + }); |
| 91 | + |
| 92 | + const message_event = new MessageEvent('message', { |
| 93 | + data: { type: 'height', height: 500 }, |
| 94 | + origin: 'https://embed.bsky.app', |
| 95 | + }); |
| 96 | + |
| 97 | + window.dispatchEvent(message_event); |
| 98 | + |
| 99 | + const iframe = getByTestId('bluesky-embed'); |
| 100 | + expect(iframe.style.height).toBe('100%'); |
| 101 | + }); |
| 102 | +}); |
0 commit comments