Skip to content

Commit c2fc435

Browse files
authoredMar 15, 2022
feat: add context to the allowed options (#182)
1 parent 138eb68 commit c2fc435

File tree

5 files changed

+41
-11
lines changed

5 files changed

+41
-11
lines changed
 
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`auto-cleanup-skip second 1`] = `"<div><h1 data-testid=\\"test\\">Hello world!</h1> <button>Button</button></div>"`;
3+
exports[`auto-cleanup-skip second 1`] = `"<div><h1 data-testid=\\"test\\">Hello world!</h1> <div>we have undefined</div> <button>Button</button></div>"`;

Diff for: ‎src/__tests__/__snapshots__/render.test.js.snap

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ exports[`render should accept svelte component options 1`] = `
1111
!
1212
</h1>
1313
14+
<div>
15+
we have context
16+
</div>
17+
1418
<button>
1519
Button
1620
</button>

Diff for: ‎src/__tests__/fixtures/Comp.svelte

+6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
<script>
2+
import { getContext } from 'svelte'
3+
24
export let name
35
46
let buttonText = 'Button'
57
8+
const contextName = getContext('name')
9+
610
function handleClick () {
711
buttonText = 'Button Clicked'
812
}
@@ -12,4 +16,6 @@
1216

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

19+
<div>we have {contextName}</div>
20+
1521
<button on:click={handleClick}>{buttonText}</button>

Diff for: ‎src/__tests__/render.test.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ describe('render', () => {
5353
const { container } = stlRender(Comp, {
5454
target,
5555
anchor: div,
56-
props: { name: 'World' }
56+
props: { name: 'World' },
57+
context: new Map([['name', 'context']])
5758
})
5859
expect(container).toMatchSnapshot()
5960
})
@@ -75,4 +76,15 @@ describe('render', () => {
7576

7677
expect(getByText('Hello World!')).toBeInTheDocument()
7778
})
79+
80+
test("accept the 'context' option", () => {
81+
const { getByText } = stlRender(Comp, {
82+
props: {
83+
name: 'Universe'
84+
},
85+
context: new Map([['name', 'context']])
86+
})
87+
88+
expect(getByText('we have context')).toBeInTheDocument()
89+
})
7890
})

Diff for: ‎src/pure.js

+17-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ import { tick } from 'svelte'
44
const containerCache = new Map()
55
const componentCache = new Set()
66

7-
const svleteComponentOptions = ['anchor', 'props', 'hydrate', 'intro']
7+
const svelteComponentOptions = [
8+
'anchor',
9+
'props',
10+
'hydrate',
11+
'intro',
12+
'context'
13+
]
814

915
const render = (
1016
Component,
@@ -17,19 +23,21 @@ const render = (
1723
const ComponentConstructor = Component.default || Component
1824

1925
const checkProps = (options) => {
20-
const isProps = !Object.keys(options).some(option => svleteComponentOptions.includes(option))
26+
const isProps = !Object.keys(options).some((option) =>
27+
svelteComponentOptions.includes(option)
28+
)
2129

2230
// Check if any props and Svelte options were accidentally mixed.
2331
if (!isProps) {
24-
const unrecognizedOptions = Object
25-
.keys(options)
26-
.filter(option => !svleteComponentOptions.includes(option))
32+
const unrecognizedOptions = Object.keys(options).filter(
33+
(option) => !svelteComponentOptions.includes(option)
34+
)
2735

2836
if (unrecognizedOptions.length > 0) {
2937
throw Error(`
30-
Unknown options were found [${unrecognizedOptions}]. This might happen if you've mixed
31-
passing in props with Svelte options into the render function. Valid Svelte options
32-
are [${svleteComponentOptions}]. You can either change the prop names, or pass in your
38+
Unknown options were found [${unrecognizedOptions}]. This might happen if you've mixed
39+
passing in props with Svelte options into the render function. Valid Svelte options
40+
are [${svelteComponentOptions}]. You can either change the prop names, or pass in your
3341
props for that component via the \`props\` option.\n\n
3442
Eg: const { /** Results **/ } = render(MyComponent, { props: { /** props here **/ } })\n\n
3543
`)
@@ -76,7 +84,7 @@ const render = (
7684
}
7785
}
7886

79-
const cleanupAtContainer = container => {
87+
const cleanupAtContainer = (container) => {
8088
const { target, component } = containerCache.get(container)
8189

8290
if (componentCache.has(component)) component.$destroy()

0 commit comments

Comments
 (0)
Please sign in to comment.