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