Skip to content

feat: throw error when mixing props and svelte options #63

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 1 commit 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
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