-
Notifications
You must be signed in to change notification settings - Fork 33
docs(examples): add examples to repository #440
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
mcous
wants to merge
1
commit into
main
Choose a base branch
from
docs/examples
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<script> | ||
let { name } = $props() | ||
|
||
let showGreeting = $state(false) | ||
|
||
const onclick = () => (showGreeting = true) | ||
</script> | ||
|
||
<button {onclick}>Greet</button> | ||
|
||
{#if showGreeting} | ||
<p>Hello {name}</p> | ||
{/if} |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { render, screen } from '@testing-library/svelte' | ||
import { userEvent } from '@testing-library/user-event' | ||
import { expect, test } from 'vitest' | ||
|
||
import Subject from './basic.svelte' | ||
|
||
test('no initial greeting', () => { | ||
render(Subject, { name: 'World' }) | ||
|
||
const button = screen.getByRole('button', { name: 'Greet' }) | ||
const greeting = screen.queryByText(/hello/iu) | ||
|
||
expect(button).toBeInTheDocument() | ||
expect(greeting).not.toBeInTheDocument() | ||
}) | ||
|
||
test('greeting appears on click', async () => { | ||
const user = userEvent.setup() | ||
render(Subject, { name: 'World' }) | ||
|
||
const button = screen.getByRole('button') | ||
await user.click(button) | ||
const greeting = screen.getByText(/hello world/iu) | ||
|
||
expect(greeting).toBeInTheDocument() | ||
}) |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# Basic | ||
|
||
This basic example demonstrates how to: | ||
|
||
- Pass props to your Svelte component using [render()] | ||
- [Query][] the structure of your component's DOM elements using screen | ||
- Interact with your component using [@testing-library/user-event][] | ||
- Make assertions using expect, using matchers from | ||
[@testing-library/jest-dom][] | ||
|
||
[query]: https://testing-library.com/docs/queries/about | ||
[render()]: https://testing-library.com/docs/svelte-testing-library/api#render | ||
[@testing-library/user-event]: https://testing-library.com/docs/user-event/intro | ||
[@testing-library/jest-dom]: https://github.com/testing-library/jest-dom | ||
|
||
## Table of contents | ||
|
||
- [`basic.svelte`](#basicsvelte) | ||
- [`basic.test.js`](#basictestjs) | ||
|
||
## `basic.svelte` | ||
|
||
```svelte file=./basic.svelte | ||
<script> | ||
let { name } = $props() | ||
|
||
let showGreeting = $state(false) | ||
|
||
const onclick = () => (showGreeting = true) | ||
</script> | ||
|
||
<button {onclick}>Greet</button> | ||
|
||
{#if showGreeting} | ||
<p>Hello {name}</p> | ||
{/if} | ||
``` | ||
|
||
## `basic.test.js` | ||
|
||
```js file=./basic.test.js | ||
import { render, screen } from '@testing-library/svelte' | ||
import { userEvent } from '@testing-library/user-event' | ||
import { expect, test } from 'vitest' | ||
|
||
import Subject from './basic.svelte' | ||
|
||
test('no initial greeting', () => { | ||
render(Subject, { name: 'World' }) | ||
|
||
const button = screen.getByRole('button', { name: 'Greet' }) | ||
const greeting = screen.queryByText(/hello/iu) | ||
|
||
expect(button).toBeInTheDocument() | ||
expect(greeting).not.toBeInTheDocument() | ||
}) | ||
|
||
test('greeting appears on click', async () => { | ||
const user = userEvent.setup() | ||
render(Subject, { name: 'World' }) | ||
|
||
const button = screen.getByRole('button') | ||
await user.click(button) | ||
const greeting = screen.getByText(/hello world/iu) | ||
|
||
expect(greeting).toBeInTheDocument() | ||
}) | ||
``` |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<script> | ||
let { value = $bindable('') } = $props() | ||
</script> | ||
|
||
<input type="text" bind:value /> |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { render, screen } from '@testing-library/svelte' | ||
import { userEvent } from '@testing-library/user-event' | ||
import { expect, test } from 'vitest' | ||
|
||
import Subject from './bind.svelte' | ||
|
||
test('value binding', async () => { | ||
const user = userEvent.setup() | ||
let value = '' | ||
|
||
render(Subject, { | ||
get value() { | ||
return value | ||
}, | ||
set value(nextValue) { | ||
value = nextValue | ||
}, | ||
}) | ||
|
||
const input = screen.getByRole('textbox') | ||
await user.type(input, 'hello world') | ||
|
||
expect(value).toBe('hello world') | ||
}) |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<script> | ||
let { value, onInput } = $props() | ||
|
||
const oninput = (event) => { | ||
onInput(event.target.value) | ||
} | ||
</script> | ||
|
||
<input type="text" {value} {oninput} /> |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# Binds | ||
|
||
Two-way data binding using [bindable() props][] is difficult to test directly. | ||
It's usually easier to structure your code so that you can test user-facing | ||
results, leaving the binding as an implementation detail. | ||
|
||
However, if two-way binding is an important developer-facing API of your | ||
component, you can use setters to test your binding. | ||
|
||
[bindable() props]: https://svelte.dev/docs/svelte/$bindable | ||
|
||
## Table of contents | ||
|
||
- [`bind.svelte`](#bindsvelte) | ||
- [`bind.test.js`](#bindtestjs) | ||
- [Consider avoiding binding](#consider-avoiding-binding) | ||
|
||
## `bind.svelte` | ||
|
||
```svelte file=./bind.svelte | ||
<script> | ||
let { value = $bindable('') } = $props() | ||
</script> | ||
|
||
<input type="text" bind:value /> | ||
``` | ||
|
||
## `bind.test.js` | ||
|
||
```svelte file=./bind.test.js | ||
import { render, screen } from '@testing-library/svelte' | ||
import { userEvent } from '@testing-library/user-event' | ||
import { expect, test } from 'vitest' | ||
|
||
import Subject from './bind.svelte' | ||
|
||
test('value binding', async () => { | ||
const user = userEvent.setup() | ||
let value = '' | ||
|
||
render(Subject, { | ||
get value() { | ||
return value | ||
}, | ||
set value(nextValue) { | ||
value = nextValue | ||
}, | ||
}) | ||
|
||
const input = screen.getByRole('textbox') | ||
await user.type(input, 'hello world') | ||
|
||
expect(value).toBe('hello world') | ||
}) | ||
``` | ||
|
||
## Consider avoiding binding | ||
|
||
Before embarking on writing tests for bindable props, consider avoiding | ||
`bindable()` entirely. Two-way data binding can make your data flows and state | ||
changes difficult to reason about and test effectively. Instead, you can use | ||
value props to pass data down and callback props to pass changes back up to the | ||
parent. | ||
|
||
> Well-written applications use bindings very sparingly — the vast majority of | ||
> data flow should be top-down -- | ||
> <cite>[Rich Harris](https://github.com/sveltejs/svelte/issues/10768#issue-2181814844)</cite> | ||
|
||
For example, rather than using a `bindable()` prop, use a value prop to pass the | ||
value down and callback prop to send changes back up to the parent: | ||
|
||
```svelte file=./no-bind.svelte | ||
<script> | ||
let { value, onInput } = $props() | ||
|
||
const oninput = (event) => { | ||
onInput(event.target.value) | ||
} | ||
</script> | ||
|
||
<input type="text" {value} {oninput} /> | ||
``` |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<script> | ||
import { getContext } from 'svelte' | ||
|
||
let { label } = $props() | ||
|
||
const messages = getContext('messages') | ||
</script> | ||
|
||
<div role="status" aria-label={label}> | ||
{#each messages.current as message (message.id)} | ||
<p>{message.text}</p> | ||
<hr /> | ||
{/each} | ||
</div> |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { render, screen } from '@testing-library/svelte' | ||
import { expect, test } from 'vitest' | ||
|
||
import Subject from './context.svelte' | ||
|
||
test('notifications with messages from context', async () => { | ||
const messages = { | ||
get current() { | ||
return [ | ||
{ id: 'abc', text: 'hello' }, | ||
{ id: 'def', text: 'world' }, | ||
] | ||
}, | ||
} | ||
|
||
render(Subject, { | ||
context: new Map([['messages', messages]]), | ||
props: { label: 'Notifications' }, | ||
}) | ||
|
||
const status = screen.getByRole('status', { name: 'Notifications' }) | ||
|
||
expect(status).toHaveTextContent('hello world') | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.