Skip to content

Commit b8e19ba

Browse files
Sync svelte docs (sveltejs#1095)
sync svelte docs Co-authored-by: Rich-Harris <[email protected]>
1 parent 8b56182 commit b8e19ba

File tree

3 files changed

+77
-2
lines changed

3 files changed

+77
-2
lines changed

apps/svelte.dev/content/docs/svelte/02-runes/03-$derived.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,20 @@ In essence, `$derived(expression)` is equivalent to `$derived.by(() => expressio
5252
Anything read synchronously inside the `$derived` expression (or `$derived.by` function body) is considered a _dependency_ of the derived state. When the state changes, the derived will be marked as _dirty_ and recalculated when it is next read.
5353

5454
To exempt a piece of state from being treated as a dependency, use [`untrack`](svelte#untrack).
55+
56+
## Update propagation
57+
58+
Svelte uses something called _push-pull reactivity_ — when state is updated, everything that depends on the state (whether directly or indirectly) is immediately notified of the change (the 'push'), but derived values are not re-evaluated until they are actually read (the 'pull').
59+
60+
If the new value of a derived is referentially identical to its previous value, downstream updates will be skipped. In other words, Svelte will only update the text inside the button when `large` changes, not when `count` changes, even though `large` depends on `count`:
61+
62+
```svelte
63+
<script>
64+
let count = $state(0);
65+
let large = $derived(count > 10);
66+
</script>
67+
68+
<button onclick={() => count++}>
69+
{large}
70+
</button>
71+
```

apps/svelte.dev/content/docs/svelte/07-misc/02-testing.md

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ You can now write unit tests for code inside your `.js/.ts` files:
4141
/// file: multiplier.svelte.test.js
4242
import { flushSync } from 'svelte';
4343
import { expect, test } from 'vitest';
44-
import { multiplier } from './multiplier.js';
44+
import { multiplier } from './multiplier.svelte.js';
4545

4646
test('Multiplier', () => {
4747
let double = multiplier(0, 2);
@@ -54,9 +54,30 @@ test('Multiplier', () => {
5454
});
5555
```
5656

57+
```js
58+
/// file: multiplier.svelte.js
59+
/**
60+
* @param {number} initial
61+
* @param {number} k
62+
*/
63+
export function multiplier(initial, k) {
64+
let count = $state(initial);
65+
66+
return {
67+
get value() {
68+
return count * k;
69+
},
70+
/** @param {number} c */
71+
set: (c) => {
72+
count = c;
73+
}
74+
};
75+
}
76+
```
77+
5778
### Using runes inside your test files
5879

59-
It is possible to use runes inside your test files. First ensure your bundler knows to route the file through the Svelte compiler before running the test by adding `.svelte` to the filename (e.g `multiplier.svelte.test.js`). After that, you can use runes inside your tests.
80+
Since Vitest processes your test files the same way as your source files, you can use runes inside your tests as long as the filename includes `.svelte`:
6081

6182
```js
6283
/// file: multiplier.svelte.test.js
@@ -76,6 +97,21 @@ test('Multiplier', () => {
7697
});
7798
```
7899

100+
```js
101+
/// file: multiplier.svelte.js
102+
/**
103+
* @param {() => number} getCount
104+
* @param {number} k
105+
*/
106+
export function multiplier(getCount, k) {
107+
return {
108+
get value() {
109+
return getCount() * k;
110+
}
111+
};
112+
}
113+
```
114+
79115
If the code being tested uses effects, you need to wrap the test inside `$effect.root`:
80116

81117
```js
@@ -106,6 +142,27 @@ test('Effect', () => {
106142
});
107143
```
108144

145+
```js
146+
/// file: logger.svelte.js
147+
/**
148+
* @param {() => any} getValue
149+
*/
150+
export function logger(getValue) {
151+
/** @type {any[]} */
152+
let log = $state([]);
153+
154+
$effect(() => {
155+
log.push(getValue());
156+
});
157+
158+
return {
159+
get value() {
160+
return log;
161+
}
162+
};
163+
}
164+
```
165+
109166
### Component testing
110167

111168
It is possible to test your components in isolation using Vitest.

apps/svelte.dev/content/docs/svelte/07-misc/04-custom-elements.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,4 @@ Custom elements can be a useful way to package components for consumption in a n
126126
- The deprecated `let:` directive has no effect, because custom elements do not have a way to pass data to the parent component that fills the slot
127127
- Polyfills are required to support older browsers
128128
- You can use Svelte's context feature between regular Svelte components within a custom element, but you can't use them across custom elements. In other words, you can't use `setContext` on a parent custom element and read that with `getContext` in a child custom element.
129+
- Don't declare properties or attributes starting with `on`, as their usage will be interpreted as an event listener. In other words, Svelte treats `<custom-element oneworld={true}></custom-element>` as `customElement.addEventListener('eworld', true)` (and not as `customElement.oneworld = true`)

0 commit comments

Comments
 (0)