Skip to content

feat: allow easier passing in of props #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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