Skip to content

Commit 8d98aa5

Browse files
authoredMar 17, 2024··
fix!: align target and baseElement options with testing-library (#325)
Fixes #312, fixes #313 BREAKING CHANGES: The `container` option has been renamed to `baseElement`, `result.container` is now set to `target` rather than `baseElement`, and `render` will now throw if you mix props with the `target` option.
1 parent 0593819 commit 8d98aa5

17 files changed

+165
-271
lines changed
 

Diff for: ‎.eslintrc.cjs

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module.exports = {
1414
plugins: ['svelte', 'simple-import-sort', 'json-files'],
1515
rules: {
1616
'simple-import-sort/imports': 'error',
17+
'simple-import-sort/exports': 'error',
1718
},
1819
overrides: [
1920
{

Diff for: ‎src/__tests__/__snapshots__/render.test.js.snap

-48
This file was deleted.

Diff for: ‎src/__tests__/debug.test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@ import Comp from './fixtures/Comp.svelte'
66

77
describe('debug', () => {
88
beforeEach(() => {
9-
vi.spyOn(console, 'log').mockImplementation(() => { })
9+
vi.spyOn(console, 'log').mockImplementation(() => {})
1010
})
1111

1212
afterEach(() => {
1313
console.log.mockRestore()
1414
})
1515

16-
test('pretty prints the container', () => {
17-
const { container, debug } = render(Comp, { props: { name: 'world' } })
16+
test('pretty prints the base element', () => {
17+
const { baseElement, debug } = render(Comp, { props: { name: 'world' } })
1818

1919
debug()
2020

2121
expect(console.log).toHaveBeenCalledTimes(1)
22-
expect(console.log).toHaveBeenCalledWith(prettyDOM(container))
22+
expect(console.log).toHaveBeenCalledWith(prettyDOM(baseElement))
2323
})
2424
})

Diff for: ‎src/__tests__/fixtures/Comp.svelte

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
11
<svelte:options accessors />
22

33
<script>
4-
import { getContext } from 'svelte'
5-
64
export let name
75
86
let buttonText = 'Button'
97
10-
const contextName = getContext('name')
11-
12-
function handleClick () {
8+
function handleClick() {
139
buttonText = 'Button Clicked'
1410
}
1511
</script>
1612

1713
<h1 data-testid="test">Hello {name}!</h1>
1814

19-
<div>we have {contextName}</div>
20-
2115
<button on:click={handleClick}>{buttonText}</button>
2216

2317
<style></style>

Diff for: ‎src/__tests__/fixtures/Comp2.svelte

-15
This file was deleted.

Diff for: ‎src/__tests__/fixtures/Rerender.svelte

-17
This file was deleted.

Diff for: ‎src/__tests__/fixtures/Simple.svelte

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<script lang="ts">
22
export let name: string
3+
export let count: number
34
</script>
45

56
<h1>hello {name}</h1>
7+
<p>count: {count}</p>

Diff for: ‎src/__tests__/multi-base.test.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import { render } from '@testing-library/svelte'
12
import { describe, expect, test } from 'vitest'
23

3-
import { render } from '@testing-library/svelte'
44
import Comp from './fixtures/Comp.svelte'
55

66
describe('multi-base', () => {
@@ -13,11 +13,11 @@ describe('multi-base', () => {
1313
{
1414
target: treeA,
1515
props: {
16-
name: 'Tree A'
17-
}
16+
name: 'Tree A',
17+
},
1818
},
1919
{
20-
container: treeA
20+
baseElement: treeA,
2121
}
2222
)
2323

@@ -26,11 +26,11 @@ describe('multi-base', () => {
2626
{
2727
target: treeB,
2828
props: {
29-
name: 'Tree B'
30-
}
29+
name: 'Tree B',
30+
},
3131
},
3232
{
33-
container: treeB
33+
baseElement: treeB,
3434
}
3535
)
3636

Diff for: ‎src/__tests__/render.test.js

+58-93
Original file line numberDiff line numberDiff line change
@@ -1,123 +1,88 @@
1+
import { render } from '@testing-library/svelte'
12
import { VERSION as SVELTE_VERSION } from 'svelte/compiler'
2-
import { beforeEach, describe, expect, test } from 'vitest'
3+
import { describe, expect, test } from 'vitest'
34

4-
import { act, render as stlRender } from '@testing-library/svelte'
55
import Comp from './fixtures/Comp.svelte'
6-
import CompDefault from './fixtures/Comp2.svelte'
76

87
describe('render', () => {
9-
let props
10-
11-
const render = (additional = {}) => {
12-
return stlRender(Comp, {
13-
target: document.body,
14-
props,
15-
...additional,
16-
})
17-
}
18-
19-
beforeEach(() => {
20-
props = {
21-
name: 'World',
22-
}
23-
})
8+
const props = { name: 'World' }
249

2510
test('renders component into the document', () => {
26-
const { getByText } = render()
11+
const { getByText } = render(Comp, { props })
2712

2813
expect(getByText('Hello World!')).toBeInTheDocument()
2914
})
3015

31-
// Dear reader, this is not something you generally want to do in your tests.
32-
test('programmatically change props', async () => {
33-
const { component, getByText } = render()
34-
16+
test('accepts props directly', () => {
17+
const { getByText } = render(Comp, props)
3518
expect(getByText('Hello World!')).toBeInTheDocument()
36-
37-
await act(() => {
38-
component.$set({ name: 'Worlds' })
39-
})
40-
41-
expect(getByText('Hello Worlds!')).toBeInTheDocument()
4219
})
4320

44-
test('change props with accessors', async () => {
45-
const { component, getByText } = render(
46-
SVELTE_VERSION < '5' ? { accessors: true } : {}
47-
)
48-
49-
expect(getByText('Hello World!')).toBeInTheDocument()
50-
51-
expect(component.name).toBe('World')
52-
53-
await act(() => {
54-
component.value = 'Planet'
55-
})
56-
57-
expect(getByText('Hello World!')).toBeInTheDocument()
21+
test('throws error when mixing svelte component options and props', () => {
22+
expect(() => {
23+
render(Comp, { props, name: 'World' })
24+
}).toThrow(/Unknown options/)
5825
})
5926

60-
test('should accept props directly', () => {
61-
const { getByText } = stlRender(Comp, { name: 'World' })
62-
expect(getByText('Hello World!')).toBeInTheDocument()
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/)
6331
})
6432

65-
test.runIf(SVELTE_VERSION < '5')(
66-
'should accept svelte v4 component options',
67-
() => {
68-
const target = document.createElement('div')
69-
const div = document.createElement('div')
70-
document.body.appendChild(target)
71-
target.appendChild(div)
72-
const { container } = stlRender(Comp, {
73-
target,
74-
anchor: div,
75-
props: { name: 'World' },
76-
context: new Map([['name', 'context']]),
77-
})
78-
expect(container).toMatchSnapshot()
79-
}
80-
)
81-
82-
test.runIf(SVELTE_VERSION >= '5')(
83-
'should accept svelte v5 component options',
84-
() => {
85-
const target = document.createElement('section')
86-
document.body.appendChild(target)
87-
88-
const { container } = stlRender(Comp, {
89-
target,
90-
props: { name: 'World' },
91-
context: new Map([['name', 'context']]),
92-
})
93-
expect(container).toMatchSnapshot()
94-
}
95-
)
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')
9636

97-
test('should throw error when mixing svelte component options and props', () => {
98-
expect(() => {
99-
stlRender(Comp, { props: {}, name: 'World' })
100-
}).toThrow(/Unknown options were found/)
37+
expect(container.firstChild).toBe(firstElement)
10138
})
10239

103-
test('should return a container object, which contains the DOM of the rendered component', () => {
104-
const { container } = render()
40+
test('should return a baseElement object, which holds the container', () => {
41+
const { baseElement, container } = render(Comp, props)
10542

106-
expect(container.innerHTML).toBe(document.body.innerHTML)
43+
expect(baseElement).toBe(document.body)
44+
expect(baseElement.firstChild).toBe(container)
10745
})
10846

109-
test('correctly find component constructor on the default property', () => {
110-
const { getByText } = stlRender(CompDefault, { props: { name: 'World' } })
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 })
11150

112-
expect(getByText('Hello World!')).toBeInTheDocument()
51+
expect(container).toBe(target)
52+
expect(baseElement).toBe(target)
11353
})
11454

115-
test("accept the 'context' option", () => {
116-
const { getByText } = stlRender(Comp, {
117-
props: { name: 'Universe' },
118-
context: new Map([['name', 'context']]),
119-
})
55+
test('allow baseElement to be specified', () => {
56+
const customBaseElement = document.createElement('div')
12057

121-
expect(getByText('we have context')).toBeInTheDocument()
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)
12266
})
67+
68+
test.runIf(SVELTE_VERSION < '5')(
69+
'should accept anchor option in Svelte v4',
70+
() => {
71+
const baseElement = document.body
72+
const target = document.createElement('section')
73+
const anchor = document.createElement('div')
74+
baseElement.appendChild(target)
75+
target.appendChild(anchor)
76+
77+
const { getByTestId } = render(
78+
Comp,
79+
{ props, target, anchor },
80+
{ baseElement }
81+
)
82+
const firstElement = getByTestId('test')
83+
84+
expect(target.firstChild).toBe(firstElement)
85+
expect(target.lastChild).toBe(anchor)
86+
}
87+
)
12388
})

0 commit comments

Comments
 (0)
Please sign in to comment.