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