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