Skip to content

Commit 7696462

Browse files
committed
feature: add 'accessors' option
1 parent 8d30f73 commit 7696462

File tree

2 files changed

+42
-21
lines changed

2 files changed

+42
-21
lines changed

src/__tests__/render.test.js

+22-10
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
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

85
describe('render', () => {
96
let props
107

11-
const render = () => {
8+
const render = (additional = {}) => {
129
return stlRender(Comp, {
1310
target: document.body,
14-
props
11+
props,
12+
...additional,
1513
})
1614
}
1715

1816
beforeEach(() => {
1917
props = {
20-
name: 'World'
18+
name: 'World',
2119
}
2220
})
2321

@@ -40,6 +38,20 @@ describe('render', () => {
4038
expect(getByText('Hello Worlds!')).toBeInTheDocument()
4139
})
4240

41+
test('change props with accessors', async () => {
42+
const { component, getByText } = render({ accessors: true })
43+
44+
expect(getByText('Hello World!')).toBeInTheDocument()
45+
46+
expect(component.name).toBe('World')
47+
48+
await act(() => {
49+
component.value = 'Planet'
50+
})
51+
52+
expect(getByText('Hello World!')).toBeInTheDocument()
53+
})
54+
4355
test('should accept props directly', () => {
4456
const { getByText } = stlRender(Comp, { name: 'World' })
4557
expect(getByText('Hello World!')).toBeInTheDocument()
@@ -54,7 +66,7 @@ describe('render', () => {
5466
target,
5567
anchor: div,
5668
props: { name: 'World' },
57-
context: new Map([['name', 'context']])
69+
context: new Map([['name', 'context']]),
5870
})
5971
expect(container).toMatchSnapshot()
6072
})
@@ -80,9 +92,9 @@ describe('render', () => {
8092
test("accept the 'context' option", () => {
8193
const { getByText } = stlRender(Comp, {
8294
props: {
83-
name: 'Universe'
95+
name: 'Universe',
8496
},
85-
context: new Map([['name', 'context']])
97+
context: new Map([['name', 'context']]),
8698
})
8799

88100
expect(getByText('we have context')).toBeInTheDocument()

src/pure.js

+20-11
Original file line numberDiff line numberDiff line change
@@ -1,15 +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

711
const svelteComponentOptions = [
12+
'accessors',
813
'anchor',
914
'props',
1015
'hydrate',
1116
'intro',
12-
'context'
17+
'context',
1318
]
1419

1520
const render = (
@@ -51,13 +56,15 @@ const render = (
5156

5257
let component = new ComponentConstructor({
5358
target,
54-
...checkProps(options)
59+
...checkProps(options),
5560
})
5661

5762
containerCache.set(container, { target, component })
5863
componentCache.add(component)
5964

60-
component.$$.on_destroy.push(() => { componentCache.delete(component) })
65+
component.$$.on_destroy.push(() => {
66+
componentCache.delete(component)
67+
})
6168

6269
return {
6370
container,
@@ -69,18 +76,20 @@ const render = (
6976
// eslint-disable-next-line no-new
7077
component = new ComponentConstructor({
7178
target,
72-
...checkProps(options)
79+
...checkProps(options),
7380
})
7481

7582
containerCache.set(container, { target, component })
7683
componentCache.add(component)
7784

78-
component.$$.on_destroy.push(() => { componentCache.delete(component) })
85+
component.$$.on_destroy.push(() => {
86+
componentCache.delete(component)
87+
})
7988
},
8089
unmount: () => {
8190
if (componentCache.has(component)) component.$destroy()
8291
},
83-
...getQueriesForElement(container, queries)
92+
...getQueriesForElement(container, queries),
8493
}
8594
}
8695

@@ -89,7 +98,9 @@ const cleanupAtContainer = (container) => {
8998

9099
if (componentCache.has(component)) component.$destroy()
91100

92-
if (target.parentNode === document.body) { document.body.removeChild(target) }
101+
if (target.parentNode === document.body) {
102+
document.body.removeChild(target)
103+
}
93104

94105
containerCache.delete(container)
95106
}
@@ -124,6 +135,4 @@ Object.keys(dtlFireEvent).forEach((key) => {
124135

125136
export * from '@testing-library/dom'
126137

127-
export {
128-
render, cleanup, fireEvent, act
129-
}
138+
export { render, cleanup, fireEvent, act }

0 commit comments

Comments
 (0)