-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
162 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,29 @@ | ||
<script> | ||
import * as Svelte from 'svelte' | ||
export let context = null | ||
export let onCreate = null | ||
export let onMount = null | ||
export let beforeUpdate = null | ||
export let afterUpdate = null | ||
export let onDestroy = null | ||
if (context) { | ||
Object.keys(context).forEach((key) => { | ||
Svelte.setContext(key, context[key]) | ||
}) | ||
} | ||
if (onCreate) bind(onCreate)() | ||
if (onMount) Svelte.onMount(bind(onMount)) | ||
if (beforeUpdate) Svelte.beforeUpdate(bind(beforeUpdate)) | ||
if (afterUpdate) Svelte.afterUpdate(bind(afterUpdate)) | ||
if (onDestroy) Svelte.onDestroy(bind(onDestroy)) | ||
function bind(callback) { | ||
return () => callback({ props: $$restProps }) | ||
} | ||
</script> | ||
|
||
<slot></slot> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,79 @@ | ||
const { expect, test } = require('@jest/globals') | ||
const { render } = require('@testing-library/svelte') | ||
const { render, act } = require('@testing-library/svelte') | ||
const { getContext } = require('svelte') | ||
|
||
const WithChilds = require('./__fixtures__/with-childs.svelte') | ||
const Fragment = require('./fragment.svelte') | ||
|
||
test('renders all childs', () => { | ||
const { container } = render(WithChilds) | ||
|
||
expect(container.innerHTML).toMatch('<div>A<span>B</span>C</div>') | ||
}) | ||
|
||
test('calls onCreate', () => { | ||
const onCreate = jest.fn() | ||
|
||
render(Fragment, { props: { onCreate, a: 1 } }) | ||
|
||
expect(onCreate).toHaveBeenCalledWith({ props: { a: 1 } }) | ||
}) | ||
|
||
test('calls onMount', () => { | ||
const onMount = jest.fn() | ||
|
||
render(Fragment, { props: { onMount, a: 1 } }) | ||
|
||
expect(onMount).toHaveBeenCalledWith({ props: { a: 1 } }) | ||
}) | ||
|
||
test('calls beforeUpdate', async () => { | ||
const beforeUpdate = jest.fn() | ||
|
||
const { component } = render(Fragment, { props: { beforeUpdate, a: 1 } }) | ||
|
||
expect(beforeUpdate).toHaveBeenCalledWith({ props: { a: 1 } }) | ||
|
||
await act(() => component.$set({ b: 2 })) | ||
|
||
expect(beforeUpdate).toHaveBeenCalledWith({ props: { a: 1, b: 2 } }) | ||
}) | ||
|
||
test('calls afterUpdate', async () => { | ||
const afterUpdate = jest.fn() | ||
|
||
const { component } = render(Fragment, { props: { afterUpdate, a: 1 } }) | ||
|
||
expect(afterUpdate).toHaveBeenCalledWith({ props: { a: 1 } }) | ||
|
||
await act(() => component.$set({ b: 2 })) | ||
|
||
expect(afterUpdate).toHaveBeenCalledWith({ props: { a: 1, b: 2 } }) | ||
}) | ||
|
||
test('calls onDestroy', async () => { | ||
const onDestroy = jest.fn() | ||
|
||
const { component } = render(Fragment, { props: { onDestroy, a: 1 } }) | ||
|
||
await act(() => component.$destroy()) | ||
|
||
expect(onDestroy).toHaveBeenCalledWith({ props: { a: 1 } }) | ||
}) | ||
|
||
test('create context', () => { | ||
let a | ||
let b | ||
|
||
render(Fragment, { | ||
props: { | ||
context: { a: 1, b: 2 }, | ||
onMount: () => { | ||
a = getContext('a') | ||
b = getContext('b') | ||
}, | ||
}, | ||
}) | ||
|
||
expect(a).toBe(1) | ||
expect(b).toBe(2) | ||
}) |