|
1 |
| -const { expect, test } = require('@jest/globals') |
2 |
| -const { render } = require('@testing-library/svelte') |
| 1 | +const { render, act } = require('@testing-library/svelte') |
| 2 | +const { getContext } = require('svelte') |
3 | 3 |
|
4 | 4 | const WithChilds = require('./__fixtures__/with-childs.svelte')
|
| 5 | +const Fragment = require('./fragment.svelte') |
5 | 6 |
|
6 | 7 | test('renders all childs', () => {
|
7 | 8 | const { container } = render(WithChilds)
|
8 | 9 |
|
9 | 10 | expect(container.innerHTML).toMatch('<div>A<span>B</span>C</div>')
|
10 | 11 | })
|
| 12 | + |
| 13 | +test('calls onCreate', () => { |
| 14 | + const onCreate = jest.fn() |
| 15 | + |
| 16 | + render(Fragment, { props: { onCreate, a: 1 } }) |
| 17 | + |
| 18 | + expect(onCreate).toHaveBeenCalledWith({ props: { a: 1 } }) |
| 19 | +}) |
| 20 | + |
| 21 | +test('calls onMount', () => { |
| 22 | + const onMount = jest.fn() |
| 23 | + |
| 24 | + render(Fragment, { props: { onMount, a: 1 } }) |
| 25 | + |
| 26 | + expect(onMount).toHaveBeenCalledWith({ props: { a: 1 } }) |
| 27 | +}) |
| 28 | + |
| 29 | +test('calls beforeUpdate', async () => { |
| 30 | + const beforeUpdate = jest.fn() |
| 31 | + |
| 32 | + const { component } = render(Fragment, { props: { beforeUpdate, a: 1 } }) |
| 33 | + |
| 34 | + expect(beforeUpdate).toHaveBeenCalledWith({ props: { a: 1 } }) |
| 35 | + |
| 36 | + await act(() => component.$set({ b: 2 })) |
| 37 | + |
| 38 | + expect(beforeUpdate).toHaveBeenCalledWith({ props: { a: 1, b: 2 } }) |
| 39 | +}) |
| 40 | + |
| 41 | +test('calls afterUpdate', async () => { |
| 42 | + const afterUpdate = jest.fn() |
| 43 | + |
| 44 | + const { component } = render(Fragment, { props: { afterUpdate, a: 1 } }) |
| 45 | + |
| 46 | + expect(afterUpdate).toHaveBeenCalledWith({ props: { a: 1 } }) |
| 47 | + |
| 48 | + await act(() => component.$set({ b: 2 })) |
| 49 | + |
| 50 | + expect(afterUpdate).toHaveBeenCalledWith({ props: { a: 1, b: 2 } }) |
| 51 | +}) |
| 52 | + |
| 53 | +test('calls onDestroy', async () => { |
| 54 | + const onDestroy = jest.fn() |
| 55 | + |
| 56 | + const { component } = render(Fragment, { props: { onDestroy, a: 1 } }) |
| 57 | + |
| 58 | + await act(() => component.$destroy()) |
| 59 | + |
| 60 | + expect(onDestroy).toHaveBeenCalledWith({ props: { a: 1 } }) |
| 61 | +}) |
| 62 | + |
| 63 | +test('create context', () => { |
| 64 | + let a |
| 65 | + let b |
| 66 | + |
| 67 | + render(Fragment, { |
| 68 | + props: { |
| 69 | + context: { a: 1, b: 2 }, |
| 70 | + onMount: () => { |
| 71 | + a = getContext('a') |
| 72 | + b = getContext('b') |
| 73 | + }, |
| 74 | + }, |
| 75 | + }) |
| 76 | + |
| 77 | + expect(a).toBe(1) |
| 78 | + expect(b).toBe(2) |
| 79 | +}) |
0 commit comments