Skip to content

Commit a2ebeaf

Browse files
committed
feat: props can now be passed in directly
ref: #59
1 parent d1e337d commit a2ebeaf

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`render should accept svelte component options 1`] = `
4+
<body>
5+
<div>
6+
<h1
7+
data-testid="test"
8+
>
9+
Hello
10+
World
11+
!
12+
</h1>
13+
14+
<button>
15+
Button
16+
</button>
17+
<div />
18+
</div>
19+
</body>
20+
`;

src/__tests__/render.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,24 @@ describe('render', () => {
4040
expect(getByText('Hello Worlds!')).toBeInTheDocument()
4141
})
4242

43+
test('should accept props directly', () => {
44+
const { getByText } = stlRender(Comp, { name: 'World' })
45+
expect(getByText('Hello World!')).toBeInTheDocument()
46+
})
47+
48+
test('should accept svelte component options', () => {
49+
const target = document.createElement('div')
50+
const div = document.createElement('div')
51+
document.body.appendChild(target)
52+
target.appendChild(div)
53+
const { container } = stlRender(Comp, {
54+
target,
55+
anchor: div,
56+
props: { name: 'World' }
57+
})
58+
expect(container).toMatchSnapshot()
59+
})
60+
4361
test('should return a container object, which contains the DOM of the rendered component', () => {
4462
const { container } = render()
4563

src/pure.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { tick } from 'svelte'
44
const containerCache = new Map()
55
const componentCache = new Set()
66

7+
const svleteComponentOptions = ['anchor', 'props', 'hydrate', 'intro']
8+
79
const render = (
810
Component,
911
{ target, ...options } = {},
@@ -13,10 +15,11 @@ const render = (
1315
target = target || container.appendChild(document.createElement('div'))
1416

1517
const ComponentConstructor = Component.default || Component
18+
const isProps = !Object.keys(options).some(option => svleteComponentOptions.includes(option))
1619

1720
const component = new ComponentConstructor({
1821
target,
19-
...options
22+
...(isProps ? { props: options } : options)
2023
})
2124

2225
containerCache.set(container, { target, component })

0 commit comments

Comments
 (0)