Skip to content

Commit f01a213

Browse files
committed
resolve todo, tweak code, add test
1 parent 55e5c82 commit f01a213

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

packages/svelte-form/src/Field.svelte

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,10 @@
9393
}
9494
})
9595
96-
// TODO (43081j): does this do what i think? we don't access anything
97-
// svelte is aware of, so maybe it'll never call this?
9896
$effect.pre(() => {
97+
// Invoke options function before mounted check, else it wouldn't rerun on changes to options.
98+
// Changes to options are seen by the effect because signals inside them are picked up.
9999
const current = opts()
100-
Object.values(current) // make sure we're watching all the things
101100
if (!mounted) return
102101
api.update(current)
103102
})

packages/svelte-form/tests/simple.svelte

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1+
<svelte:options runes />
2+
13
<script module lang="ts">
24
interface Employee {
35
firstName: string
46
lastName: string
57
}
68
7-
export const sampleData: Employee = {
9+
let sampleData: Employee = $state({
810
firstName: 'Christian',
911
lastName: '',
10-
}
12+
})
13+
14+
export const getSampleData = () => sampleData
1115
</script>
1216

1317
<script lang="ts">
@@ -21,7 +25,7 @@
2125
},
2226
}))
2327
24-
const state = form.useStore()
28+
const formState = form.useStore()
2529
</script>
2630

2731
<form
@@ -110,4 +114,11 @@
110114
</div>
111115
</form>
112116

113-
<pre>{JSON.stringify(state.current, null, 2)}</pre>
117+
<button
118+
id="change"
119+
onclick={() => (sampleData = { firstName: 'Julian', lastName: '' })}
120+
>
121+
Change Sample Data
122+
</button>
123+
124+
<pre>{JSON.stringify(formState.current, null, 2)}</pre>

packages/svelte-form/tests/simple.test.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import '@testing-library/jest-dom'
44
import { userEvent } from '@testing-library/user-event'
55
import { mount, unmount } from 'svelte'
66
// @ts-ignore tsc doesn't know about named exports from svelte files
7-
import TestForm, { sampleData } from './simple.svelte'
7+
import TestForm, { getSampleData } from './simple.svelte'
88

99
describe('Svelte Tests', () => {
1010
let element: HTMLDivElement
@@ -24,13 +24,22 @@ describe('Svelte Tests', () => {
2424

2525
it('should have initial values', async () => {
2626
expect(element.querySelector<HTMLInputElement>('#firstName')).toHaveValue(
27-
sampleData.firstName,
27+
getSampleData().firstName,
2828
)
2929
expect(element.querySelector<HTMLInputElement>('#lastName')).toHaveValue(
30-
sampleData.lastName,
30+
getSampleData().lastName,
3131
)
3232
})
3333

34+
it('should change initial values when defaults update', async () => {
35+
await userEvent.click(element.querySelector<HTMLButtonElement>('#change')!)
36+
37+
expect(element.querySelector<HTMLInputElement>('#firstName')).toHaveValue(
38+
getSampleData().firstName,
39+
)
40+
expect(getSampleData().firstName).toBe('Julian')
41+
})
42+
3443
it('should mirror user input', async () => {
3544
const lastName = element.querySelector<HTMLInputElement>('#lastName')!
3645
const lastNameValue = 'Jobs'
@@ -44,10 +53,10 @@ describe('Svelte Tests', () => {
4453
const firstName = element.querySelector<HTMLInputElement>('#firstName')!
4554
await userEvent.type(firstName, '-Joseph')
4655

47-
expect(firstName).toHaveValue(sampleData.firstName + '-Joseph')
56+
expect(firstName).toHaveValue(getSampleData().firstName + '-Joseph')
4857

4958
await userEvent.click(element.querySelector<HTMLButtonElement>('#reset')!)
50-
expect(firstName).toHaveValue(sampleData.firstName)
59+
expect(firstName).toHaveValue(getSampleData().firstName)
5160
})
5261

5362
it('should display validation', async () => {

0 commit comments

Comments
 (0)