Skip to content

Commit 55e8355

Browse files
committed
all tests pass
1 parent 926c269 commit 55e8355

File tree

8 files changed

+38
-469
lines changed

8 files changed

+38
-469
lines changed

src/__tests__/fixtures/Comp.svelte

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
<svelte:options accessors />
22

33
<script>
4+
import { getContext } from 'svelte'
5+
46
export let name
57
68
let buttonText = 'Button'
79
810
function handleClick() {
911
buttonText = 'Button Clicked'
1012
}
13+
14+
const contextName = getContext('name')
1115
</script>
1216

1317
<h1 data-testid="test">Hello {name}!</h1>
1418

1519
<button on:click={handleClick}>{buttonText}</button>
1620

17-
<style></style>
21+
<div>we have {contextName}</div>

src/__tests__/render.test.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { VERSION as SVELTE_VERSION } from 'svelte/compiler'
22
import { describe, expect, test } from 'vitest'
33

4-
import { act, render as stlRender } from '@testing-library/svelte'
4+
import { act, render } from '@testing-library/svelte'
55
import Comp from './fixtures/Comp.svelte'
66

77
describe('render', () => {
@@ -21,13 +21,13 @@ describe('render', () => {
2121
test('throws error when mixing svelte component options and props', () => {
2222
expect(() => {
2323
render(Comp, { props, name: 'World' })
24-
}).toThrow(/Unknown component options/)
24+
}).toThrow(/Unknown options/)
2525
})
2626

2727
test('throws error when mixing target option and props', () => {
2828
expect(() => {
2929
render(Comp, { target: document.createElement('div'), name: 'World' })
30-
}).toThrow(/Unknown component options/)
30+
}).toThrow(/Unknown options/)
3131
})
3232

3333
test('should return a container object wrapping the DOM of the rendered component', () => {
@@ -88,24 +88,24 @@ describe('render', () => {
8888

8989
test('should throw error when mixing svelte component options and props', () => {
9090
expect(() => {
91-
stlRender(Comp, { props: {}, name: 'World' })
91+
render(Comp, { props: {}, name: 'World' })
9292
}).toThrow(/Unknown options were found/)
9393
})
9494

9595
test('should return a container object, which contains the DOM of the rendered component', () => {
96-
const { container } = render()
96+
const { baseElement } = render(Comp)
9797

98-
expect(container.innerHTML).toBe(document.body.innerHTML)
98+
expect(baseElement.innerHTML).toBe(document.body.innerHTML)
9999
})
100100

101101
test('correctly find component constructor on the default property', () => {
102-
const { getByText } = stlRender(CompDefault, { props: { name: 'World' } })
102+
const { getByText } = render(Comp, { props: { name: 'World' } })
103103

104104
expect(getByText('Hello World!')).toBeInTheDocument()
105105
})
106106

107107
test("accept the 'context' option", () => {
108-
const { getByText } = stlRender(Comp, {
108+
const { getByText } = render(Comp, {
109109
props: { name: 'Universe' },
110110
context: new Map([['name', 'context']]),
111111
})

src/__tests__/render.test.js.orig

Lines changed: 0 additions & 122 deletions
This file was deleted.

src/__tests__/rerender.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { VERSION as SVELTE_VERSION } from 'svelte/compiler'
66

77
import { act, screen, render, waitFor } from '@testing-library/svelte'
88

9-
import Comp from './fixtures/Rerender.svelte'
9+
import Comp from './fixtures/Comp.svelte'
1010

1111
describe('rerender', () => {
1212
test('updates props', async () => {

src/__tests__/rerender.test.js.orig

Lines changed: 0 additions & 61 deletions
This file was deleted.

src/pure.js

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,25 +48,32 @@ export class SvelteTestingLibrary {
4848
return { props: options }
4949
}
5050

51-
render(Component, { target, ...options } = {}, { container, queries } = {}) {
52-
container = container || document.body
53-
target = target || container.appendChild(document.createElement('div'))
51+
render(Component, componentOptions = {}, renderOptions = {}) {
52+
componentOptions = this.checkProps(componentOptions)
53+
54+
const baseElement =
55+
renderOptions.baseElement ?? componentOptions.target ?? document.body
56+
57+
const target =
58+
componentOptions.target ??
59+
baseElement.appendChild(document.createElement('div'))
60+
5461
this.targetCache.add(target)
5562

56-
const ComponentConstructor = Component.default || Component
63+
const ComponentConstructor = Component?.default ?? Component
5764

58-
const component = this.renderComponent(
59-
{
60-
target,
61-
ComponentConstructor,
62-
},
63-
options
64-
)
65+
const component = this.renderComponent(ComponentConstructor, {
66+
...componentOptions,
67+
target,
68+
})
69+
70+
this.componentCache.add(component)
6571

6672
return {
67-
container,
73+
baseElement,
74+
container: target,
6875
component,
69-
debug: (el = container) => console.log(prettyDOM(el)),
76+
debug: (el = baseElement) => console.log(prettyDOM(el)),
7077
rerender: async (props) => {
7178
if (props.props) {
7279
console.warn(
@@ -80,20 +87,16 @@ export class SvelteTestingLibrary {
8087
unmount: () => {
8188
this.cleanupComponent(component)
8289
},
83-
...getQueriesForElement(container, queries),
90+
...getQueriesForElement(baseElement, renderOptions.queries),
8491
}
8592
}
8693

87-
renderComponent({ target, ComponentConstructor }, options) {
88-
options = { target, ...this.checkProps(options) }
89-
94+
renderComponent(ComponentConstructor, options) {
9095
if (IS_SVELTE_5)
9196
throw new Error('for Svelte 5, use `@testing-library/svelte/svelte5`')
9297

9398
const component = new ComponentConstructor(options)
9499

95-
this.componentCache.add(component)
96-
97100
// TODO(mcous, 2024-02-11): remove this behavior in the next major version
98101
// It is unnecessary has no path to implementation in Svelte v5
99102
if (!IS_SVELTE_5) {

0 commit comments

Comments
 (0)