Skip to content

Commit 45bdbe1

Browse files
committed
feat: add context to the allowed options
For #136 Allows 'context' as one of the Svelte options. Note that most of the changes are due to prettier and husky.
1 parent 138eb68 commit 45bdbe1

File tree

9 files changed

+96
-46
lines changed

9 files changed

+96
-46
lines changed
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>"`;

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>

src/__tests__/act.test.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,16 @@ describe('act', () => {
3232
test('findByTestId returns the element', async () => {
3333
const { findByTestId } = render()
3434

35-
expect(await findByTestId('test')).toHaveTextContent(`Hello ${props.name}!`)
35+
expect(await findByTestId('test')).toHaveTextContent(
36+
`Hello ${props.name}!`
37+
)
3638
})
3739

3840
test('accepts async functions', async () => {
39-
const sleep = (ms) => new Promise(resolve => {
40-
setTimeout(() => resolve(), ms)
41-
})
41+
const sleep = (ms) =>
42+
new Promise((resolve) => {
43+
setTimeout(() => resolve(), ms)
44+
})
4245

4346
const { getByText } = render()
4447
const button = getByText('Button')

src/__tests__/fixtures/Comp.html

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<script>
2-
export let name
2+
export let name;
33

4-
let buttonText = "Button Text"
4+
let buttonText = "Button Text";
55

66
function handleClick() {
7-
buttonText = "Button Clicked"
7+
buttonText = "Button Clicked";
88
}
99
</script>
1010

1111
<style></style>
1212

1313
<h1>Hello {name}!</h1>
1414

15-
<button on:click={handleClick}>{buttonText}</button>
15+
<button on:click="{handleClick}">{buttonText}</button>

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>

src/__tests__/multi-base.test.js

+22-14
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,31 @@ describe('multi-base', () => {
66
const treeB = document.createElement('div')
77

88
test('container isolates trees from one another', () => {
9-
const { getByText: getByTextInA } = render(Comp, {
10-
target: treeA,
11-
props: {
12-
name: 'Tree A'
9+
const { getByText: getByTextInA } = render(
10+
Comp,
11+
{
12+
target: treeA,
13+
props: {
14+
name: 'Tree A'
15+
}
16+
},
17+
{
18+
container: treeA
1319
}
14-
}, {
15-
container: treeA
16-
})
20+
)
1721

18-
const { getByText: getByTextInB } = render(Comp, {
19-
target: treeB,
20-
props: {
21-
name: 'Tree B'
22+
const { getByText: getByTextInB } = render(
23+
Comp,
24+
{
25+
target: treeB,
26+
props: {
27+
name: 'Tree B'
28+
}
29+
},
30+
{
31+
container: treeB
2232
}
23-
}, {
24-
container: treeB
25-
})
33+
)
2634

2735
expect(() => getByTextInA('Hello Tree A!')).not.toThrow()
2836
expect(() => getByTextInB('Hello Tree A!')).toThrow()

src/__tests__/render.test.js

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import {
2-
act,
3-
render as stlRender
4-
} from '..'
1+
import { act, render as stlRender } from '..'
52
import Comp from './fixtures/Comp'
63
import CompDefault from './fixtures/Comp.html'
74

@@ -53,7 +50,8 @@ describe('render', () => {
5350
const { container } = stlRender(Comp, {
5451
target,
5552
anchor: div,
56-
props: { name: 'World' }
53+
props: { name: 'World' },
54+
context: new Map([['name', 'context']])
5755
})
5856
expect(container).toMatchSnapshot()
5957
})
@@ -75,4 +73,15 @@ describe('render', () => {
7573

7674
expect(getByText('Hello World!')).toBeInTheDocument()
7775
})
76+
77+
test("accept the 'context' option", () => {
78+
const { getByText } = stlRender(Comp, {
79+
props: {
80+
name: 'Universe'
81+
},
82+
context: new Map([['name', 'context']])
83+
})
84+
85+
expect(getByText('we have context')).toBeInTheDocument()
86+
})
7887
})

src/__tests__/rerender.test.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import Comp from './fixtures/Comp'
33

44
describe('rerender', () => {
55
test('mounts new component successfully', () => {
6-
const { container, rerender } = render(Comp, { props: { name: 'World 1' } })
6+
const { container, rerender } = render(Comp, {
7+
props: { name: 'World 1' }
8+
})
79

810
expect(container.firstChild).toHaveTextContent('Hello World 1!')
911
rerender({ props: { name: 'World 2' } })
@@ -15,7 +17,9 @@ describe('rerender', () => {
1517

1618
const { rerender, component } = render(Comp, { props: { name: '' } })
1719

18-
component.$$.on_destroy.push(() => { isDestroyed = true })
20+
component.$$.on_destroy.push(() => {
21+
isDestroyed = true
22+
})
1923
rerender({ props: { name: '' } })
2024
expect(isDestroyed).toBeTruthy()
2125
})

src/pure.js

+32-16
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
1-
import { fireEvent as dtlFireEvent, getQueriesForElement, prettyDOM } from '@testing-library/dom'
1+
import {
2+
fireEvent as dtlFireEvent,
3+
getQueriesForElement,
4+
prettyDOM
5+
} from '@testing-library/dom'
26
import { tick } from 'svelte'
37

48
const containerCache = new Map()
59
const componentCache = new Set()
610

7-
const svleteComponentOptions = ['anchor', 'props', 'hydrate', 'intro']
11+
const svelteComponentOptions = [
12+
'anchor',
13+
'props',
14+
'hydrate',
15+
'intro',
16+
'context'
17+
]
818

919
const render = (
1020
Component,
@@ -17,19 +27,21 @@ const render = (
1727
const ComponentConstructor = Component.default || Component
1828

1929
const checkProps = (options) => {
20-
const isProps = !Object.keys(options).some(option => svleteComponentOptions.includes(option))
30+
const isProps = !Object.keys(options).some((option) =>
31+
svelteComponentOptions.includes(option)
32+
)
2133

2234
// Check if any props and Svelte options were accidentally mixed.
2335
if (!isProps) {
24-
const unrecognizedOptions = Object
25-
.keys(options)
26-
.filter(option => !svleteComponentOptions.includes(option))
36+
const unrecognizedOptions = Object.keys(options).filter(
37+
(option) => !svelteComponentOptions.includes(option)
38+
)
2739

2840
if (unrecognizedOptions.length > 0) {
2941
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
42+
Unknown options were found [${unrecognizedOptions}]. This might happen if you've mixed
43+
passing in props with Svelte options into the render function. Valid Svelte options
44+
are [${svelteComponentOptions}]. You can either change the prop names, or pass in your
3345
props for that component via the \`props\` option.\n\n
3446
Eg: const { /** Results **/ } = render(MyComponent, { props: { /** props here **/ } })\n\n
3547
`)
@@ -49,7 +61,9 @@ const render = (
4961
containerCache.set(container, { target, component })
5062
componentCache.add(component)
5163

52-
component.$$.on_destroy.push(() => { componentCache.delete(component) })
64+
component.$$.on_destroy.push(() => {
65+
componentCache.delete(component)
66+
})
5367

5468
return {
5569
container,
@@ -67,7 +81,9 @@ const render = (
6781
containerCache.set(container, { target, newComponent })
6882
componentCache.add(newComponent)
6983

70-
newComponent.$$.on_destroy.push(() => { componentCache.delete(newComponent) })
84+
newComponent.$$.on_destroy.push(() => {
85+
componentCache.delete(newComponent)
86+
})
7187
},
7288
unmount: () => {
7389
if (componentCache.has(component)) component.$destroy()
@@ -76,12 +92,14 @@ const render = (
7692
}
7793
}
7894

79-
const cleanupAtContainer = container => {
95+
const cleanupAtContainer = (container) => {
8096
const { target, component } = containerCache.get(container)
8197

8298
if (componentCache.has(component)) component.$destroy()
8399

84-
if (target.parentNode === document.body) { document.body.removeChild(target) }
100+
if (target.parentNode === document.body) {
101+
document.body.removeChild(target)
102+
}
85103

86104
containerCache.delete(container)
87105
}
@@ -116,6 +134,4 @@ Object.keys(dtlFireEvent).forEach((key) => {
116134

117135
export * from '@testing-library/dom'
118136

119-
export {
120-
render, cleanup, fireEvent, act
121-
}
137+
export { render, cleanup, fireEvent, act }

0 commit comments

Comments
 (0)