Skip to content

Commit c2e4f40

Browse files
committed
test: ✅ refactor anchorfm component with tests
1 parent 565b872 commit c2e4f40

File tree

2 files changed

+54
-40
lines changed

2 files changed

+54
-40
lines changed
Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,36 @@
11
<script lang="ts">
2-
import GeneralObserver from './general-observer.svelte'
2+
import GeneralObserver from './general-observer.svelte'
33
4-
export let episodeUrl: string
5-
export let height: string = '100'
6-
export let width: string = '100'
4+
export let episodeUrl: string
5+
export let height: string = '100'
6+
export let width: string = '100'
7+
export let disable_observer: boolean = false
78
</script>
89

9-
<GeneralObserver>
10-
<div
11-
class="anchor-fm-episode-svelte-embed"
12-
style={`
10+
<GeneralObserver {disable_observer}>
11+
<div
12+
class="anchor-fm-episode-svelte-embed"
13+
style={`
1314
position: relative;
1415
height: ${height}px;
1516
width: ${width}%;
1617
`}
17-
>
18-
<iframe
19-
data-testid="anchor-fm-episode"
20-
title={`anchor-fm-${episodeUrl}`}
21-
src={`https://anchor.fm/${episodeUrl}`}
22-
{height}
23-
{width}
24-
frameborder="0"
25-
scrolling="no"
26-
style={`
18+
>
19+
<iframe
20+
data-testid="anchor-fm-episode"
21+
title={`anchor-fm-${episodeUrl}`}
22+
src={`https://anchor.fm/${episodeUrl}`}
23+
{height}
24+
{width}
25+
frameborder="0"
26+
scrolling="no"
27+
style={`
2728
position: absolute;
2829
top: 0;
2930
left: 0;
3031
width: 100%;
3132
height: 100%;
3233
`}
33-
/>
34-
</div>
34+
/>
35+
</div>
3536
</GeneralObserver>
Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,42 @@
11
import AnchorFm from '$lib/components/anchor-fm.svelte'
22
import { cleanup, render } from '@testing-library/svelte'
3-
import { afterEach, describe, expect, it, vi } from 'vitest'
3+
import { afterEach, describe, expect, it } from 'vitest'
44

5-
globalThis.IntersectionObserver = vi.fn(() => ({
6-
observe: () => null,
7-
unobserve: () => null,
8-
disconnect: () => null,
9-
})) as unknown as typeof globalThis.IntersectionObserver
5+
let episodeUrl =
6+
'purrfect-dev/embed/episodes/1-31---Delivering-Digital-Content-with-GraphCMS-e14g55c/a-a650v9a'
107

118
describe('AnchorFm', () => {
12-
afterEach(() => cleanup())
9+
afterEach(() => cleanup())
1310

14-
it('mounts', async () => {
15-
const { container } = render(AnchorFm)
16-
expect(container).toBeTruthy()
17-
})
11+
it('mounts with episode url', async () => {
12+
const { container } = render(AnchorFm, {
13+
episodeUrl,
14+
disable_observer: true,
15+
})
16+
expect(container).toBeTruthy()
17+
})
1818

19-
it.skip('has episode url', async () => {
20-
const { container } = render(AnchorFm, {
21-
episodeUrl:
22-
'purrfect-dev/embed/episodes/1-31---Delivering-Digital-Content-with-GraphCMS-e14g55c/a-a650v9a',
23-
})
19+
it('renders iframe with episode url', async () => {
20+
const { getByTestId } = render(AnchorFm, {
21+
episodeUrl,
22+
disable_observer: true,
23+
})
24+
const iframe = getByTestId('anchor-fm-episode')
25+
const expectedSrc = `https://anchor.fm/${episodeUrl}`
26+
expect(iframe.getAttribute('src')).toBe(expectedSrc)
27+
})
2428

25-
expect(container.querySelectorAll('h2')).toBe(
26-
'1.31 - Delivering Digital Content with GraphCMS'
27-
)
28-
})
29+
it('sets iframe dimensions based on props', async () => {
30+
const height = '200'
31+
const width = '75'
32+
const { getByTestId } = render(AnchorFm, {
33+
episodeUrl,
34+
height,
35+
width,
36+
disable_observer: true,
37+
})
38+
const iframe = getByTestId('anchor-fm-episode')
39+
expect(iframe.getAttribute('height')).toBe(height)
40+
expect(iframe.getAttribute('width')).toBe(`${width}`)
41+
})
2942
})

0 commit comments

Comments
 (0)