-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathcleanup.test.js
36 lines (27 loc) · 954 Bytes
/
cleanup.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { describe, expect, test, vi } from 'vitest'
import { VERSION as SVELTE_VERSION } from 'svelte/compiler'
import { act, cleanup, render } from '@testing-library/svelte'
import Mounter from './fixtures/Mounter.svelte'
const onExecuted = vi.fn()
const onDestroyed = vi.fn()
const renderSubject = () => render(Mounter, { onExecuted, onDestroyed })
describe('cleanup', () => {
test('cleanup deletes element', async () => {
renderSubject()
cleanup()
expect(document.body).toBeEmptyDOMElement()
})
test.runIf(SVELTE_VERSION < '5')('cleanup unmounts component', async () => {
await act(renderSubject)
cleanup()
expect(onDestroyed).toHaveBeenCalledOnce()
})
test('cleanup handles unexpected errors during mount', () => {
onExecuted.mockImplementation(() => {
throw new Error('oh no!')
})
expect(renderSubject).toThrowError()
cleanup()
expect(document.body).toBeEmptyDOMElement()
})
})