Skip to content

Commit

Permalink
Merge pull request #63 from mihar-22/pull-62
Browse files Browse the repository at this point in the history
feat: throw error when mixing props and svelte options
  • Loading branch information
mihar-22 authored Dec 6, 2019
2 parents 60c5c69 + 805318c commit 0fd1527
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/__tests__/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ describe('render', () => {
expect(container).toMatchSnapshot()
})

test('should throw error when mixing svelte component options and props', () => {
expect(() => {
stlRender(Comp, { anchor: '', name: 'World' })
}).toThrow(/Unknown options were found/)
})

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

Expand Down
17 changes: 17 additions & 0 deletions src/pure.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,23 @@ const render = (
const ComponentConstructor = Component.default || Component
const isProps = !Object.keys(options).some(option => svleteComponentOptions.includes(option))

// Check if any props and Svelte options were accidentally mixed.
if (!isProps) {
const unrecognizedOptions = Object
.keys(options)
.filter(option => !svleteComponentOptions.includes(option))

if (unrecognizedOptions.length > 0) {
throw Error(`
Unknown options were found [${unrecognizedOptions}]. This might happen if you've mixed
passing in props with Svelte options into the render function. Valid Svelte options
are [${svleteComponentOptions}]. You can either change the prop names, or pass in your
props for that component via the \`props\` option.\n\n
Eg: const { /** Results **/ } = render(MyComponent, { props: { /** props here **/ } })\n\n
`)
}
}

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

0 comments on commit 0fd1527

Please sign in to comment.