Skip to content

Commit

Permalink
Merge pull request #62 from mihar-22/issue-59
Browse files Browse the repository at this point in the history
feat: allow easier passing in of props
  • Loading branch information
mihar-22 authored Dec 6, 2019
2 parents e315af9 + a2ebeaf commit 60c5c69
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,11 @@
"eslint-plugin-svelte3": "^2.7.3",
"husky": "^3.0.8",
"jest": "^24.7.1",
"jest-transform-svelte": "^2.1.0",
"lint-staged": "^9.4.1",
"npm-run-all": "^4.1.5",
"prettier": "^1.18.2",
"svelte": "^3.0.0",
"svelte-test": "^0.4.0"
"svelte-jester": "^1.0.3"
},
"husky": {
"hooks": {
Expand Down Expand Up @@ -122,8 +121,8 @@
],
"transform": {
"^.+\\.js$": "babel-jest",
"^.+\\.svelte$": "jest-transform-svelte",
"^.+\\.html$": "svelte-test/transform"
"^.+\\.svelte$": "svelte-jester",
"^.+\\.html$": "svelte-jester"
},
"moduleFileExtensions": [
"js",
Expand Down
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 60c5c69

Please sign in to comment.