Skip to content

Commit

Permalink
feat: add context to the allowed options (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
yanick authored Mar 15, 2022
1 parent 138eb68 commit c2fc435
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/__tests__/__snapshots__/auto-cleanup-skip.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`auto-cleanup-skip second 1`] = `"<div><h1 data-testid=\\"test\\">Hello world!</h1> <button>Button</button></div>"`;
exports[`auto-cleanup-skip second 1`] = `"<div><h1 data-testid=\\"test\\">Hello world!</h1> <div>we have undefined</div> <button>Button</button></div>"`;
4 changes: 4 additions & 0 deletions src/__tests__/__snapshots__/render.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ exports[`render should accept svelte component options 1`] = `
!
</h1>
<div>
we have context
</div>
<button>
Button
</button>
Expand Down
6 changes: 6 additions & 0 deletions src/__tests__/fixtures/Comp.svelte
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
<script>
import { getContext } from 'svelte'
export let name
let buttonText = 'Button'
const contextName = getContext('name')
function handleClick () {
buttonText = 'Button Clicked'
}
Expand All @@ -12,4 +16,6 @@

<h1 data-testid="test">Hello {name}!</h1>

<div>we have {contextName}</div>

<button on:click={handleClick}>{buttonText}</button>
14 changes: 13 additions & 1 deletion src/__tests__/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ describe('render', () => {
const { container } = stlRender(Comp, {
target,
anchor: div,
props: { name: 'World' }
props: { name: 'World' },
context: new Map([['name', 'context']])
})
expect(container).toMatchSnapshot()
})
Expand All @@ -75,4 +76,15 @@ describe('render', () => {

expect(getByText('Hello World!')).toBeInTheDocument()
})

test("accept the 'context' option", () => {
const { getByText } = stlRender(Comp, {
props: {
name: 'Universe'
},
context: new Map([['name', 'context']])
})

expect(getByText('we have context')).toBeInTheDocument()
})
})
26 changes: 17 additions & 9 deletions src/pure.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import { tick } from 'svelte'
const containerCache = new Map()
const componentCache = new Set()

const svleteComponentOptions = ['anchor', 'props', 'hydrate', 'intro']
const svelteComponentOptions = [
'anchor',
'props',
'hydrate',
'intro',
'context'
]

const render = (
Component,
Expand All @@ -17,19 +23,21 @@ const render = (
const ComponentConstructor = Component.default || Component

const checkProps = (options) => {
const isProps = !Object.keys(options).some(option => svleteComponentOptions.includes(option))
const isProps = !Object.keys(options).some((option) =>
svelteComponentOptions.includes(option)
)

// Check if any props and Svelte options were accidentally mixed.
if (!isProps) {
const unrecognizedOptions = Object
.keys(options)
.filter(option => !svleteComponentOptions.includes(option))
const unrecognizedOptions = Object.keys(options).filter(
(option) => !svelteComponentOptions.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
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 [${svelteComponentOptions}]. 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
`)
Expand Down Expand Up @@ -76,7 +84,7 @@ const render = (
}
}

const cleanupAtContainer = container => {
const cleanupAtContainer = (container) => {
const { target, component } = containerCache.get(container)

if (componentCache.has(component)) component.$destroy()
Expand Down

0 comments on commit c2fc435

Please sign in to comment.