Skip to content

Commit

Permalink
feat: props can now be passed in directly
Browse files Browse the repository at this point in the history
ref: #59
  • Loading branch information
mihar-22 committed Dec 6, 2019
1 parent d1e337d commit a2ebeaf
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/__tests__/__snapshots__/render.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`render should accept svelte component options 1`] = `
<body>
<div>
<h1
data-testid="test"
>
Hello
World
!
</h1>
<button>
Button
</button>
<div />
</div>
</body>
`;
18 changes: 18 additions & 0 deletions src/__tests__/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,24 @@ describe('render', () => {
expect(getByText('Hello Worlds!')).toBeInTheDocument()
})

test('should accept props directly', () => {
const { getByText } = stlRender(Comp, { name: 'World' })
expect(getByText('Hello World!')).toBeInTheDocument()
})

test('should accept svelte component options', () => {
const target = document.createElement('div')
const div = document.createElement('div')
document.body.appendChild(target)
target.appendChild(div)
const { container } = stlRender(Comp, {
target,
anchor: div,
props: { name: 'World' }
})
expect(container).toMatchSnapshot()
})

test('should return a container object, which contains the DOM of the rendered component', () => {
const { container } = render()

Expand Down
5 changes: 4 additions & 1 deletion src/pure.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { tick } from 'svelte'
const containerCache = new Map()
const componentCache = new Set()

const svleteComponentOptions = ['anchor', 'props', 'hydrate', 'intro']

const render = (
Component,
{ target, ...options } = {},
Expand All @@ -13,10 +15,11 @@ const render = (
target = target || container.appendChild(document.createElement('div'))

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

const component = new ComponentConstructor({
target,
...options
...(isProps ? { props: options } : options)
})

containerCache.set(container, { target, component })
Expand Down

0 comments on commit a2ebeaf

Please sign in to comment.