-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathrender.test.js
123 lines (97 loc) · 3.3 KB
/
render.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { VERSION as SVELTE_VERSION } from 'svelte/compiler'
import { beforeEach, describe, expect, test } from 'vitest'
import { act, render as stlRender } from '..'
import Comp from './fixtures/Comp.svelte'
import CompDefault from './fixtures/Comp2.svelte'
describe('render', () => {
let props
const render = (additional = {}) => {
return stlRender(Comp, {
target: document.body,
props,
...additional,
})
}
beforeEach(() => {
props = {
name: 'World',
}
})
test('renders component into the document', () => {
const { getByText } = render()
expect(getByText('Hello World!')).toBeInTheDocument()
})
// Dear reader, this is not something you generally want to do in your tests.
test('programmatically change props', async () => {
const { component, getByText } = render()
expect(getByText('Hello World!')).toBeInTheDocument()
await act(() => {
component.$set({ name: 'Worlds' })
})
expect(getByText('Hello Worlds!')).toBeInTheDocument()
})
test('change props with accessors', async () => {
const { component, getByText } = render(
SVELTE_VERSION < '5' ? { accessors: true } : {}
)
expect(getByText('Hello World!')).toBeInTheDocument()
expect(component.name).toBe('World')
await act(() => {
component.value = 'Planet'
})
expect(getByText('Hello World!')).toBeInTheDocument()
})
test('should accept props directly', () => {
const { getByText } = stlRender(Comp, { name: 'World' })
expect(getByText('Hello World!')).toBeInTheDocument()
})
test.runIf(SVELTE_VERSION < '5')(
'should accept svelte v4 component options',
() => {
const target = document.createElement('div')
const div = document.createElement('div')
document.body.appendChild(target)
target.appendChild(div)
const { container } = stlRender(Comp, {
target,
anchor: div,
props: { name: 'World' },
context: new Map([['name', 'context']]),
})
expect(container).toMatchSnapshot()
}
)
test.runIf(SVELTE_VERSION >= '5')(
'should accept svelte v5 component options',
() => {
const target = document.createElement('section')
document.body.appendChild(target)
const { container } = stlRender(Comp, {
target,
props: { name: 'World' },
context: new Map([['name', 'context']]),
})
expect(container).toMatchSnapshot()
}
)
test('should throw error when mixing svelte component options and props', () => {
expect(() => {
stlRender(Comp, { props: {}, name: 'World' })
}).toThrow(/Unknown options were found/)
})
test('should return a container object, which contains the DOM of the rendered component', () => {
const { container } = render()
expect(container.innerHTML).toBe(document.body.innerHTML)
})
test('correctly find component constructor on the default property', () => {
const { getByText } = stlRender(CompDefault, { props: { name: 'World' } })
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()
})
})