Skip to content

Commit a81fd5d

Browse files
committed
refactor(tests): improve cleanup logic and script verification in Toot component tests
1 parent 45d779c commit a81fd5d

File tree

3 files changed

+23
-33
lines changed

3 files changed

+23
-33
lines changed

packages/sveltekit-embed/src/lib/components/general-observer.svelte.test.ts

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -180,20 +180,6 @@ describe('General Observer', () => {
180180
});
181181

182182
it.skip('should handle multiple intersection entries correctly', async () => {
183-
let intersectionCallback:
184-
| ((entries: any[]) => void)
185-
| undefined;
186-
187-
mockIntersectionObserver.mockImplementation(
188-
(callback, options) => {
189-
intersectionCallback = callback;
190-
return {
191-
observe: mockObserve,
192-
disconnect: mockDisconnect,
193-
};
194-
},
195-
);
196-
197183
const testContent = 'Multiple entries test';
198184
const { container } = render(GeneralObserver, {
199185
disable_observer: false,
@@ -202,11 +188,14 @@ describe('General Observer', () => {
202188
});
203189

204190
// Simulate multiple entries where one meets threshold
205-
intersectionCallback?.([
206-
{ intersectionRatio: 0.3 }, // Below threshold
207-
{ intersectionRatio: 0.7 }, // Above threshold
208-
{ intersectionRatio: 0.1 }, // Below threshold
209-
]);
191+
lastObserverCallback?.(
192+
[
193+
{ intersectionRatio: 0.3 }, // Below threshold
194+
{ intersectionRatio: 0.7 }, // Above threshold
195+
{ intersectionRatio: 0.1 }, // Below threshold
196+
] as IntersectionObserverEntry[],
197+
lastObserverInstance as unknown as IntersectionObserver,
198+
);
210199

211200
await new Promise(resolve => setTimeout(resolve, 10));
212201

packages/sveltekit-embed/src/lib/components/toot.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
};
1919
2020
const remove_mastodon_embed_script = () => {
21-
if (mastodon_embed_script) {
22-
document.head.removeChild(mastodon_embed_script);
21+
if (mastodon_embed_script && mastodon_embed_script.parentNode) {
22+
mastodon_embed_script.parentNode.removeChild(mastodon_embed_script);
2323
mastodon_embed_script = null;
2424
}
2525
};

packages/sveltekit-embed/src/lib/components/toot.svelte.test.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import { page } from 'vitest/browser';
55

66
describe('Toot', () => {
77
afterEach(() => {
8-
// Clean up any mastodon embed scripts before vitest-browser-svelte cleanup
8+
// Cleanup component first (which removes its script)
9+
cleanup();
10+
// Then remove any leftover scripts
911
const scripts = document.head.querySelectorAll(
1012
'script[src*="embed.js"]',
1113
);
1214
scripts.forEach(script => script.remove());
13-
cleanup();
1415
});
1516
it('renders iframe with correct src', async () => {
1617
const instance = 'my-instance';
@@ -102,11 +103,6 @@ describe('Toot', () => {
102103
const username = 'testuser';
103104
const tootId = '123';
104105

105-
// Mock document.createElement and appendChild
106-
const mockScript = document.createElement('script');
107-
const createElementSpy = vi
108-
.spyOn(document, 'createElement')
109-
.mockReturnValue(mockScript);
110106
const appendChildSpy = vi.spyOn(document.head, 'appendChild');
111107

112108
render(Toot, {
@@ -115,12 +111,17 @@ describe('Toot', () => {
115111
tootId,
116112
});
117113

118-
expect(createElementSpy).toHaveBeenCalledWith('script');
119-
expect(mockScript.src).toBe(`https://${instance}/embed.js`);
120-
expect(mockScript.async).toBe(true);
121-
expect(appendChildSpy).toHaveBeenCalledWith(mockScript);
114+
// Verify script was appended
115+
const appendedScript = appendChildSpy.mock.calls.find(
116+
call =>
117+
call[0] instanceof HTMLScriptElement &&
118+
(call[0] as HTMLScriptElement).src.includes('embed.js'),
119+
)?.[0] as HTMLScriptElement;
120+
121+
expect(appendedScript).toBeTruthy();
122+
expect(appendedScript.src).toBe(`https://${instance}/embed.js`);
123+
expect(appendedScript.async).toBe(true);
122124

123-
createElementSpy.mockRestore();
124125
appendChildSpy.mockRestore();
125126
});
126127

0 commit comments

Comments
 (0)