From 805318cc71da36cf471afb5fb06e2fe2851baaf0 Mon Sep 17 00:00:00 2001 From: Rahim Alwer Date: Fri, 6 Dec 2019 14:49:38 +1100 Subject: [PATCH] feat: throw error when mixing props and svelte options --- src/__tests__/render.test.js | 6 ++++++ src/pure.js | 17 +++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/__tests__/render.test.js b/src/__tests__/render.test.js index bf8a58d..efacd1d 100644 --- a/src/__tests__/render.test.js +++ b/src/__tests__/render.test.js @@ -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() diff --git a/src/pure.js b/src/pure.js index 1e6316f..8651539 100644 --- a/src/pure.js +++ b/src/pure.js @@ -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)