@testing-library/svelte 5.1.0 → 5.2.0-next.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +65 -74
- package/package.json +27 -22
- package/src/core/index.js +27 -0
- package/src/core/legacy.js +46 -0
- package/src/core/modern.svelte.js +50 -0
- package/src/core/validate-options.js +39 -0
- package/src/index.js +5 -4
- package/src/pure.js +146 -129
- package/types/core/index.d.ts +8 -0
- package/types/core/index.d.ts.map +1 -0
- package/types/core/legacy.d.ts +19 -0
- package/types/core/legacy.d.ts.map +1 -0
- package/types/core/modern.svelte.d.ts +15 -0
- package/types/core/modern.svelte.d.ts.map +1 -0
- package/types/core/validate-options.d.ts +5 -0
- package/types/core/validate-options.d.ts.map +1 -0
- package/types/index.d.ts +5 -82
- package/types/index.d.ts.map +1 -0
- package/types/pure.d.ts +95 -0
- package/types/pure.d.ts.map +1 -0
- package/types/vite.d.ts +5 -12
- package/types/vite.d.ts.map +1 -0
- package/types/vitest.d.ts +2 -0
- package/types/vitest.d.ts.map +1 -0
- package/src/__tests__/__snapshots__/auto-cleanup-skip.test.js.snap +0 -3
- package/src/__tests__/_vitest-setup.js +0 -1
- package/src/__tests__/act.test.js +0 -33
- package/src/__tests__/auto-cleanup-skip.test.js +0 -23
- package/src/__tests__/auto-cleanup.test.js +0 -31
- package/src/__tests__/cleanup.test.js +0 -35
- package/src/__tests__/context.test.js +0 -14
- package/src/__tests__/debug.test.js +0 -18
- package/src/__tests__/events.test.js +0 -32
- package/src/__tests__/fixtures/Comp.svelte +0 -17
- package/src/__tests__/fixtures/Context.svelte +0 -7
- package/src/__tests__/fixtures/Mounter.svelte +0 -19
- package/src/__tests__/fixtures/Simple.svelte +0 -7
- package/src/__tests__/fixtures/Transitioner.svelte +0 -18
- package/src/__tests__/mount.test.js +0 -33
- package/src/__tests__/multi-base.test.js +0 -42
- package/src/__tests__/render.test.js +0 -85
- package/src/__tests__/rerender.test.js +0 -50
- package/src/__tests__/transition.test.js +0 -31
- package/src/__tests__/utils.js +0 -7
- package/src/svelte5-index.js +0 -23
- package/src/svelte5.js +0 -30
- package/types/types.test-d.ts +0 -65
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { render } from '@testing-library/svelte'
|
|
2
|
-
import { describe, expect, test } from 'vitest'
|
|
3
|
-
|
|
4
|
-
import Comp from './fixtures/Comp.svelte'
|
|
5
|
-
|
|
6
|
-
describe('auto-cleanup', () => {
|
|
7
|
-
// This just verifies that by importing STL in an
|
|
8
|
-
// environment which supports afterEach (like jest)
|
|
9
|
-
// we'll get automatic cleanup between tests.
|
|
10
|
-
test('first', () => {
|
|
11
|
-
render(Comp, { props: { name: 'world' } })
|
|
12
|
-
})
|
|
13
|
-
|
|
14
|
-
test('second', () => {
|
|
15
|
-
expect(document.body.innerHTML).toEqual('')
|
|
16
|
-
})
|
|
17
|
-
})
|
|
18
|
-
|
|
19
|
-
describe('cleanup of two components', () => {
|
|
20
|
-
// This just verifies that by importing STL in an
|
|
21
|
-
// environment which supports afterEach (like jest)
|
|
22
|
-
// we'll get automatic cleanup between tests.
|
|
23
|
-
test('first', () => {
|
|
24
|
-
render(Comp, { props: { name: 'world' } })
|
|
25
|
-
render(Comp, { props: { name: 'universe' } })
|
|
26
|
-
})
|
|
27
|
-
|
|
28
|
-
test('second', () => {
|
|
29
|
-
expect(document.body.innerHTML).toEqual('')
|
|
30
|
-
})
|
|
31
|
-
})
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { cleanup, render } from '@testing-library/svelte'
|
|
2
|
-
import { describe, expect, test, vi } from 'vitest'
|
|
3
|
-
|
|
4
|
-
import Mounter from './fixtures/Mounter.svelte'
|
|
5
|
-
|
|
6
|
-
const onExecuted = vi.fn()
|
|
7
|
-
const onDestroyed = vi.fn()
|
|
8
|
-
const renderSubject = () => render(Mounter, { onExecuted, onDestroyed })
|
|
9
|
-
|
|
10
|
-
describe('cleanup', () => {
|
|
11
|
-
test('cleanup deletes element', async () => {
|
|
12
|
-
renderSubject()
|
|
13
|
-
cleanup()
|
|
14
|
-
|
|
15
|
-
expect(document.body).toBeEmptyDOMElement()
|
|
16
|
-
})
|
|
17
|
-
|
|
18
|
-
test('cleanup unmounts component', () => {
|
|
19
|
-
renderSubject()
|
|
20
|
-
cleanup()
|
|
21
|
-
|
|
22
|
-
expect(onDestroyed).toHaveBeenCalledOnce()
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
test('cleanup handles unexpected errors during mount', () => {
|
|
26
|
-
onExecuted.mockImplementation(() => {
|
|
27
|
-
throw new Error('oh no!')
|
|
28
|
-
})
|
|
29
|
-
|
|
30
|
-
expect(renderSubject).toThrowError()
|
|
31
|
-
cleanup()
|
|
32
|
-
|
|
33
|
-
expect(document.body).toBeEmptyDOMElement()
|
|
34
|
-
})
|
|
35
|
-
})
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { render } from '@testing-library/svelte'
|
|
2
|
-
import { expect, test } from 'vitest'
|
|
3
|
-
|
|
4
|
-
import Comp from './fixtures/Context.svelte'
|
|
5
|
-
|
|
6
|
-
test('can set a context', () => {
|
|
7
|
-
const message = 'Got it'
|
|
8
|
-
|
|
9
|
-
const { getByText } = render(Comp, {
|
|
10
|
-
context: new Map(Object.entries({ foo: { message } })),
|
|
11
|
-
})
|
|
12
|
-
|
|
13
|
-
expect(getByText(message)).toBeTruthy()
|
|
14
|
-
})
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { prettyDOM } from '@testing-library/dom'
|
|
2
|
-
import { render } from '@testing-library/svelte'
|
|
3
|
-
import { describe, expect, test, vi } from 'vitest'
|
|
4
|
-
|
|
5
|
-
import Comp from './fixtures/Comp.svelte'
|
|
6
|
-
|
|
7
|
-
describe('debug', () => {
|
|
8
|
-
test('pretty prints the base element', () => {
|
|
9
|
-
vi.stubGlobal('console', { log: vi.fn(), warn: vi.fn(), error: vi.fn() })
|
|
10
|
-
|
|
11
|
-
const { baseElement, debug } = render(Comp, { props: { name: 'world' } })
|
|
12
|
-
|
|
13
|
-
debug()
|
|
14
|
-
|
|
15
|
-
expect(console.log).toHaveBeenCalledTimes(1)
|
|
16
|
-
expect(console.log).toHaveBeenCalledWith(prettyDOM(baseElement))
|
|
17
|
-
})
|
|
18
|
-
})
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { fireEvent, render } from '@testing-library/svelte'
|
|
2
|
-
import { describe, expect, test } from 'vitest'
|
|
3
|
-
|
|
4
|
-
import Comp from './fixtures/Comp.svelte'
|
|
5
|
-
|
|
6
|
-
describe('events', () => {
|
|
7
|
-
test('state changes are flushed after firing an event', async () => {
|
|
8
|
-
const { getByText } = render(Comp, { props: { name: 'World' } })
|
|
9
|
-
const button = getByText('Button')
|
|
10
|
-
|
|
11
|
-
const result = fireEvent.click(button)
|
|
12
|
-
|
|
13
|
-
await expect(result).resolves.toBe(true)
|
|
14
|
-
expect(button).toHaveTextContent('Button Clicked')
|
|
15
|
-
})
|
|
16
|
-
|
|
17
|
-
test('calling `fireEvent` directly works too', async () => {
|
|
18
|
-
const { getByText } = render(Comp, { props: { name: 'World' } })
|
|
19
|
-
const button = getByText('Button')
|
|
20
|
-
|
|
21
|
-
const result = fireEvent(
|
|
22
|
-
button,
|
|
23
|
-
new MouseEvent('click', {
|
|
24
|
-
bubbles: true,
|
|
25
|
-
cancelable: true,
|
|
26
|
-
})
|
|
27
|
-
)
|
|
28
|
-
|
|
29
|
-
await expect(result).resolves.toBe(true)
|
|
30
|
-
expect(button).toHaveTextContent('Button Clicked')
|
|
31
|
-
})
|
|
32
|
-
})
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
<svelte:options accessors />
|
|
2
|
-
|
|
3
|
-
<script>
|
|
4
|
-
export let name = 'World'
|
|
5
|
-
|
|
6
|
-
let buttonText = 'Button'
|
|
7
|
-
|
|
8
|
-
function handleClick() {
|
|
9
|
-
buttonText = 'Button Clicked'
|
|
10
|
-
}
|
|
11
|
-
</script>
|
|
12
|
-
|
|
13
|
-
<h1 data-testid="test">Hello {name}!</h1>
|
|
14
|
-
|
|
15
|
-
<button on:click={handleClick}>{buttonText}</button>
|
|
16
|
-
|
|
17
|
-
<style></style>
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
<script>
|
|
2
|
-
import { onDestroy, onMount } from 'svelte'
|
|
3
|
-
|
|
4
|
-
export let onExecuted = undefined
|
|
5
|
-
export let onMounted = undefined
|
|
6
|
-
export let onDestroyed = undefined
|
|
7
|
-
|
|
8
|
-
onExecuted?.()
|
|
9
|
-
|
|
10
|
-
onMount(() => {
|
|
11
|
-
onMounted?.()
|
|
12
|
-
})
|
|
13
|
-
|
|
14
|
-
onDestroy(() => {
|
|
15
|
-
onDestroyed?.()
|
|
16
|
-
})
|
|
17
|
-
</script>
|
|
18
|
-
|
|
19
|
-
<button />
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
<script>
|
|
2
|
-
import { blur } from 'svelte/transition'
|
|
3
|
-
|
|
4
|
-
let show = false
|
|
5
|
-
let introDone = false
|
|
6
|
-
</script>
|
|
7
|
-
|
|
8
|
-
<button on:click={() => (show = true)}>Show</button>
|
|
9
|
-
|
|
10
|
-
{#if show}
|
|
11
|
-
<div in:blur={{ duration: 64 }} on:introend={() => (introDone = true)}>
|
|
12
|
-
{#if introDone}
|
|
13
|
-
<p data-testid="intro-done">Done</p>
|
|
14
|
-
{:else}
|
|
15
|
-
<p data-testid="intro-pending">Pending</p>
|
|
16
|
-
{/if}
|
|
17
|
-
</div>
|
|
18
|
-
{/if}
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { act, render, screen } from '@testing-library/svelte'
|
|
2
|
-
import { describe, expect, test, vi } from 'vitest'
|
|
3
|
-
|
|
4
|
-
import Mounter from './fixtures/Mounter.svelte'
|
|
5
|
-
|
|
6
|
-
const onMounted = vi.fn()
|
|
7
|
-
const onDestroyed = vi.fn()
|
|
8
|
-
const renderSubject = () => render(Mounter, { onMounted, onDestroyed })
|
|
9
|
-
|
|
10
|
-
describe('mount and destroy', () => {
|
|
11
|
-
test('component is mounted', async () => {
|
|
12
|
-
renderSubject()
|
|
13
|
-
|
|
14
|
-
const content = screen.getByRole('button')
|
|
15
|
-
|
|
16
|
-
expect(content).toBeInTheDocument()
|
|
17
|
-
await act()
|
|
18
|
-
expect(onMounted).toHaveBeenCalledOnce()
|
|
19
|
-
})
|
|
20
|
-
|
|
21
|
-
test('component is destroyed', async () => {
|
|
22
|
-
const { unmount } = renderSubject()
|
|
23
|
-
|
|
24
|
-
await act()
|
|
25
|
-
unmount()
|
|
26
|
-
|
|
27
|
-
const content = screen.queryByRole('button')
|
|
28
|
-
|
|
29
|
-
expect(content).not.toBeInTheDocument()
|
|
30
|
-
await act()
|
|
31
|
-
expect(onDestroyed).toHaveBeenCalledOnce()
|
|
32
|
-
})
|
|
33
|
-
})
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { render } from '@testing-library/svelte'
|
|
2
|
-
import { describe, expect, test } from 'vitest'
|
|
3
|
-
|
|
4
|
-
import Comp from './fixtures/Comp.svelte'
|
|
5
|
-
|
|
6
|
-
describe('multi-base', () => {
|
|
7
|
-
const treeA = document.createElement('div')
|
|
8
|
-
const treeB = document.createElement('div')
|
|
9
|
-
|
|
10
|
-
test('container isolates trees from one another', () => {
|
|
11
|
-
const { getByText: getByTextInA } = render(
|
|
12
|
-
Comp,
|
|
13
|
-
{
|
|
14
|
-
target: treeA,
|
|
15
|
-
props: {
|
|
16
|
-
name: 'Tree A',
|
|
17
|
-
},
|
|
18
|
-
},
|
|
19
|
-
{
|
|
20
|
-
baseElement: treeA,
|
|
21
|
-
}
|
|
22
|
-
)
|
|
23
|
-
|
|
24
|
-
const { getByText: getByTextInB } = render(
|
|
25
|
-
Comp,
|
|
26
|
-
{
|
|
27
|
-
target: treeB,
|
|
28
|
-
props: {
|
|
29
|
-
name: 'Tree B',
|
|
30
|
-
},
|
|
31
|
-
},
|
|
32
|
-
{
|
|
33
|
-
baseElement: treeB,
|
|
34
|
-
}
|
|
35
|
-
)
|
|
36
|
-
|
|
37
|
-
expect(() => getByTextInA('Hello Tree A!')).not.toThrow()
|
|
38
|
-
expect(() => getByTextInB('Hello Tree A!')).toThrow()
|
|
39
|
-
expect(() => getByTextInA('Hello Tree B!')).toThrow()
|
|
40
|
-
expect(() => getByTextInB('Hello Tree B!')).not.toThrow()
|
|
41
|
-
})
|
|
42
|
-
})
|
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
import { render } from '@testing-library/svelte'
|
|
2
|
-
import { describe, expect, test } from 'vitest'
|
|
3
|
-
|
|
4
|
-
import Comp from './fixtures/Comp.svelte'
|
|
5
|
-
import { IS_SVELTE_5 } from './utils.js'
|
|
6
|
-
|
|
7
|
-
describe('render', () => {
|
|
8
|
-
const props = { name: 'World' }
|
|
9
|
-
|
|
10
|
-
test('renders component into the document', () => {
|
|
11
|
-
const { getByText } = render(Comp, { props })
|
|
12
|
-
|
|
13
|
-
expect(getByText('Hello World!')).toBeInTheDocument()
|
|
14
|
-
})
|
|
15
|
-
|
|
16
|
-
test('accepts props directly', () => {
|
|
17
|
-
const { getByText } = render(Comp, props)
|
|
18
|
-
expect(getByText('Hello World!')).toBeInTheDocument()
|
|
19
|
-
})
|
|
20
|
-
|
|
21
|
-
test('throws error when mixing svelte component options and props', () => {
|
|
22
|
-
expect(() => {
|
|
23
|
-
render(Comp, { props, name: 'World' })
|
|
24
|
-
}).toThrow(/Unknown options/)
|
|
25
|
-
})
|
|
26
|
-
|
|
27
|
-
test('throws error when mixing target option and props', () => {
|
|
28
|
-
expect(() => {
|
|
29
|
-
render(Comp, { target: document.createElement('div'), name: 'World' })
|
|
30
|
-
}).toThrow(/Unknown options/)
|
|
31
|
-
})
|
|
32
|
-
|
|
33
|
-
test('should return a container object wrapping the DOM of the rendered component', () => {
|
|
34
|
-
const { container, getByTestId } = render(Comp, props)
|
|
35
|
-
const firstElement = getByTestId('test')
|
|
36
|
-
|
|
37
|
-
expect(container.firstChild).toBe(firstElement)
|
|
38
|
-
})
|
|
39
|
-
|
|
40
|
-
test('should return a baseElement object, which holds the container', () => {
|
|
41
|
-
const { baseElement, container } = render(Comp, props)
|
|
42
|
-
|
|
43
|
-
expect(baseElement).toBe(document.body)
|
|
44
|
-
expect(baseElement.firstChild).toBe(container)
|
|
45
|
-
})
|
|
46
|
-
|
|
47
|
-
test('if target is provided, use it as container and baseElement', () => {
|
|
48
|
-
const target = document.createElement('div')
|
|
49
|
-
const { baseElement, container } = render(Comp, { props, target })
|
|
50
|
-
|
|
51
|
-
expect(container).toBe(target)
|
|
52
|
-
expect(baseElement).toBe(target)
|
|
53
|
-
})
|
|
54
|
-
|
|
55
|
-
test('allow baseElement to be specified', () => {
|
|
56
|
-
const customBaseElement = document.createElement('div')
|
|
57
|
-
|
|
58
|
-
const { baseElement, container } = render(
|
|
59
|
-
Comp,
|
|
60
|
-
{ props },
|
|
61
|
-
{ baseElement: customBaseElement }
|
|
62
|
-
)
|
|
63
|
-
|
|
64
|
-
expect(baseElement).toBe(customBaseElement)
|
|
65
|
-
expect(baseElement.firstChild).toBe(container)
|
|
66
|
-
})
|
|
67
|
-
|
|
68
|
-
test.skipIf(IS_SVELTE_5)('should accept anchor option in Svelte v4', () => {
|
|
69
|
-
const baseElement = document.body
|
|
70
|
-
const target = document.createElement('section')
|
|
71
|
-
const anchor = document.createElement('div')
|
|
72
|
-
baseElement.appendChild(target)
|
|
73
|
-
target.appendChild(anchor)
|
|
74
|
-
|
|
75
|
-
const { getByTestId } = render(
|
|
76
|
-
Comp,
|
|
77
|
-
{ props, target, anchor },
|
|
78
|
-
{ baseElement }
|
|
79
|
-
)
|
|
80
|
-
const firstElement = getByTestId('test')
|
|
81
|
-
|
|
82
|
-
expect(target.firstChild).toBe(firstElement)
|
|
83
|
-
expect(target.lastChild).toBe(anchor)
|
|
84
|
-
})
|
|
85
|
-
})
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import { act, render, screen } from '@testing-library/svelte'
|
|
2
|
-
import { VERSION as SVELTE_VERSION } from 'svelte/compiler'
|
|
3
|
-
import { describe, expect, test, vi } from 'vitest'
|
|
4
|
-
|
|
5
|
-
import Comp from './fixtures/Comp.svelte'
|
|
6
|
-
|
|
7
|
-
describe('rerender', () => {
|
|
8
|
-
test('updates props', async () => {
|
|
9
|
-
const { rerender } = render(Comp, { name: 'World' })
|
|
10
|
-
const element = screen.getByText('Hello World!')
|
|
11
|
-
|
|
12
|
-
await rerender({ name: 'Dolly' })
|
|
13
|
-
|
|
14
|
-
expect(element).toHaveTextContent('Hello Dolly!')
|
|
15
|
-
})
|
|
16
|
-
|
|
17
|
-
test('warns if incorrect arguments shape used', async () => {
|
|
18
|
-
vi.stubGlobal('console', { log: vi.fn(), warn: vi.fn(), error: vi.fn() })
|
|
19
|
-
|
|
20
|
-
const { rerender } = render(Comp, { name: 'World' })
|
|
21
|
-
const element = screen.getByText('Hello World!')
|
|
22
|
-
|
|
23
|
-
await rerender({ props: { name: 'Dolly' } })
|
|
24
|
-
|
|
25
|
-
expect(element).toHaveTextContent('Hello Dolly!')
|
|
26
|
-
expect(console.warn).toHaveBeenCalledOnce()
|
|
27
|
-
expect(console.warn).toHaveBeenCalledWith(
|
|
28
|
-
expect.stringMatching(/deprecated/iu)
|
|
29
|
-
)
|
|
30
|
-
})
|
|
31
|
-
|
|
32
|
-
test('change props with accessors', async () => {
|
|
33
|
-
const { component, getByText } = render(
|
|
34
|
-
Comp,
|
|
35
|
-
SVELTE_VERSION < '5'
|
|
36
|
-
? { accessors: true, props: { name: 'World' } }
|
|
37
|
-
: { name: 'World' }
|
|
38
|
-
)
|
|
39
|
-
const element = getByText('Hello World!')
|
|
40
|
-
|
|
41
|
-
expect(element).toBeInTheDocument()
|
|
42
|
-
expect(component.name).toBe('World')
|
|
43
|
-
|
|
44
|
-
await act(() => {
|
|
45
|
-
component.name = 'Planet'
|
|
46
|
-
})
|
|
47
|
-
|
|
48
|
-
expect(element).toHaveTextContent('Hello Planet!')
|
|
49
|
-
})
|
|
50
|
-
})
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { render, screen, waitFor } from '@testing-library/svelte'
|
|
2
|
-
import { userEvent } from '@testing-library/user-event'
|
|
3
|
-
import { beforeEach, describe, expect, test, vi } from 'vitest'
|
|
4
|
-
|
|
5
|
-
import Transitioner from './fixtures/Transitioner.svelte'
|
|
6
|
-
import { IS_JSDOM, IS_SVELTE_5 } from './utils.js'
|
|
7
|
-
|
|
8
|
-
describe.skipIf(IS_SVELTE_5)('transitions', () => {
|
|
9
|
-
beforeEach(() => {
|
|
10
|
-
if (!IS_JSDOM) return
|
|
11
|
-
|
|
12
|
-
const raf = (fn) => setTimeout(() => fn(new Date()), 16)
|
|
13
|
-
vi.stubGlobal('requestAnimationFrame', raf)
|
|
14
|
-
})
|
|
15
|
-
|
|
16
|
-
test('on:introend', async () => {
|
|
17
|
-
const user = userEvent.setup()
|
|
18
|
-
|
|
19
|
-
render(Transitioner)
|
|
20
|
-
const start = screen.getByRole('button')
|
|
21
|
-
await user.click(start)
|
|
22
|
-
|
|
23
|
-
const pending = screen.getByTestId('intro-pending')
|
|
24
|
-
expect(pending).toBeInTheDocument()
|
|
25
|
-
|
|
26
|
-
await waitFor(() => {
|
|
27
|
-
const done = screen.queryByTestId('intro-done')
|
|
28
|
-
expect(done).toBeInTheDocument()
|
|
29
|
-
})
|
|
30
|
-
})
|
|
31
|
-
})
|
package/src/__tests__/utils.js
DELETED
package/src/svelte5-index.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
/* eslint-disable import/export */
|
|
2
|
-
import { act } from './pure.js'
|
|
3
|
-
import { cleanup } from './svelte5.js'
|
|
4
|
-
|
|
5
|
-
// If we're running in a test runner that supports afterEach
|
|
6
|
-
// then we'll automatically run cleanup afterEach test
|
|
7
|
-
// this ensures that tests run in isolation from each other
|
|
8
|
-
// if you don't like this then either import the `pure` module
|
|
9
|
-
// or set the STL_SKIP_AUTO_CLEANUP env variable to 'true'.
|
|
10
|
-
if (typeof afterEach === 'function' && !process.env.STL_SKIP_AUTO_CLEANUP) {
|
|
11
|
-
afterEach(async () => {
|
|
12
|
-
await act()
|
|
13
|
-
cleanup()
|
|
14
|
-
})
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
// export all base queries, screen, etc.
|
|
18
|
-
export * from '@testing-library/dom'
|
|
19
|
-
|
|
20
|
-
// export svelte-specific functions and custom `fireEvent`
|
|
21
|
-
// `fireEvent` must be a named export to take priority over wildcard export above
|
|
22
|
-
export { act, fireEvent } from './pure.js'
|
|
23
|
-
export { cleanup, render } from './svelte5.js'
|
package/src/svelte5.js
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { createClassComponent } from 'svelte/legacy'
|
|
2
|
-
|
|
3
|
-
import { SvelteTestingLibrary } from './pure.js'
|
|
4
|
-
|
|
5
|
-
class Svelte5TestingLibrary extends SvelteTestingLibrary {
|
|
6
|
-
svelteComponentOptions = [
|
|
7
|
-
'target',
|
|
8
|
-
'props',
|
|
9
|
-
'events',
|
|
10
|
-
'context',
|
|
11
|
-
'intro',
|
|
12
|
-
'recover',
|
|
13
|
-
]
|
|
14
|
-
|
|
15
|
-
renderComponent(ComponentConstructor, componentOptions) {
|
|
16
|
-
const component = createClassComponent({
|
|
17
|
-
...componentOptions,
|
|
18
|
-
component: ComponentConstructor,
|
|
19
|
-
})
|
|
20
|
-
|
|
21
|
-
this.componentCache.add(component)
|
|
22
|
-
|
|
23
|
-
return component
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const instance = new Svelte5TestingLibrary()
|
|
28
|
-
|
|
29
|
-
export const render = instance.render.bind(instance)
|
|
30
|
-
export const cleanup = instance.cleanup.bind(instance)
|
package/types/types.test-d.ts
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
import { expectTypeOf } from 'expect-type'
|
|
2
|
-
import type { ComponentProps, SvelteComponent } from 'svelte'
|
|
3
|
-
import { describe, test } from 'vitest'
|
|
4
|
-
|
|
5
|
-
import Simple from '../src/__tests__/fixtures/Simple.svelte'
|
|
6
|
-
import * as subject from './index.js'
|
|
7
|
-
|
|
8
|
-
describe('types', () => {
|
|
9
|
-
test('render is a function that accepts a Svelte component', () => {
|
|
10
|
-
subject.render(Simple, { name: 'Alice', count: 42 })
|
|
11
|
-
subject.render(Simple, { props: { name: 'Alice', count: 42 } })
|
|
12
|
-
})
|
|
13
|
-
|
|
14
|
-
test('rerender is a function that accepts partial props', async () => {
|
|
15
|
-
const { rerender } = subject.render(Simple, { name: 'Alice', count: 42 })
|
|
16
|
-
|
|
17
|
-
await rerender({ name: 'Bob' })
|
|
18
|
-
await rerender({ count: 0 })
|
|
19
|
-
})
|
|
20
|
-
|
|
21
|
-
test('invalid prop types are rejected', () => {
|
|
22
|
-
// @ts-expect-error: name should be a string
|
|
23
|
-
subject.render(Simple, { name: 42 })
|
|
24
|
-
|
|
25
|
-
// @ts-expect-error: name should be a string
|
|
26
|
-
subject.render(Simple, { props: { name: 42 } })
|
|
27
|
-
})
|
|
28
|
-
|
|
29
|
-
test('render result has container and component', () => {
|
|
30
|
-
const result = subject.render(Simple, { name: 'Alice', count: 42 })
|
|
31
|
-
|
|
32
|
-
expectTypeOf(result).toMatchTypeOf<{
|
|
33
|
-
container: HTMLElement
|
|
34
|
-
component: SvelteComponent<{ name: string }>
|
|
35
|
-
debug: (el?: HTMLElement) => void
|
|
36
|
-
rerender: (props: Partial<ComponentProps<Simple>>) => Promise<void>
|
|
37
|
-
unmount: () => void
|
|
38
|
-
}>()
|
|
39
|
-
})
|
|
40
|
-
|
|
41
|
-
test('render result has default queries', () => {
|
|
42
|
-
const result = subject.render(Simple, { name: 'Alice', count: 42 })
|
|
43
|
-
|
|
44
|
-
expectTypeOf(result.getByRole).parameters.toMatchTypeOf<
|
|
45
|
-
[role: subject.ByRoleMatcher, options?: subject.ByRoleOptions]
|
|
46
|
-
>()
|
|
47
|
-
})
|
|
48
|
-
|
|
49
|
-
test('render result can have custom queries', () => {
|
|
50
|
-
const [getByVibes] = subject.buildQueries(
|
|
51
|
-
(_container: HTMLElement, vibes: string) => {
|
|
52
|
-
throw new Error(`unimplemented ${vibes}`)
|
|
53
|
-
},
|
|
54
|
-
() => '',
|
|
55
|
-
() => ''
|
|
56
|
-
)
|
|
57
|
-
const result = subject.render(
|
|
58
|
-
Simple,
|
|
59
|
-
{ name: 'Alice', count: 42 },
|
|
60
|
-
{ queries: { getByVibes } }
|
|
61
|
-
)
|
|
62
|
-
|
|
63
|
-
expectTypeOf(result.getByVibes).parameters.toMatchTypeOf<[vibes: string]>()
|
|
64
|
-
})
|
|
65
|
-
})
|