Skip to content

Commit 24ac6ca

Browse files
authored
chore: update Svelte tests to be more Testing Library-y (#2174)
1 parent 087aee7 commit 24ac6ca

7 files changed

Lines changed: 35 additions & 43 deletions

File tree

packages/svelte-form/tests/array-swap.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212

1313
<form.Field name="test" mode="array">
1414
{#snippet children(field)}
15-
<div id="val">{JSON.stringify(field.state.value)}</div>
15+
<div data-testid="val">{JSON.stringify(field.state.value)}</div>
1616
<button
17-
id="swap"
17+
data-testid="swap"
1818
type="button"
1919
onclick={() => form.swapFieldValues('test', 0, 1)}
2020
>

packages/svelte-form/tests/array.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313
<form.Field name="test" mode="array">
1414
{#snippet children(field)}
15-
<div id="val">{JSON.stringify(field.state.value)}</div>
16-
<button id="push" type="button" onclick={() => field.pushValue('b')}>
15+
<div data-testid="val">{JSON.stringify(field.state.value)}</div>
16+
<button data-testid="push" type="button" onclick={() => field.pushValue('b')}>
1717
push
1818
</button>
1919
{/snippet}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ const user = userEvent.setup()
88

99
describe('Svelte Field array mode', () => {
1010
it('should support array mode', async () => {
11-
const { container } = render(TestForm)
12-
expect(container.querySelector('#val')!.textContent).toBe('["a"]')
13-
await user.click(container.querySelector<HTMLButtonElement>('#push')!)
14-
expect(container.querySelector('#val')!.textContent).toBe('["a","b"]')
11+
const { getByTestId, getByRole } = render(TestForm)
12+
expect(getByTestId('val')).toHaveTextContent('["a"]')
13+
await user.click(getByRole('button', { name: 'push' }))
14+
expect(getByTestId('val')).toHaveTextContent('["a","b"]')
1515
})
1616
})
1717

1818
describe('Svelte Field array mode swapFieldValues', () => {
1919
it('should rerender on swapFieldValues even when length is unchanged', async () => {
20-
const { container } = render(SwapForm)
21-
expect(container.querySelector('#val')!.textContent).toBe('["a","b"]')
22-
await user.click(container.querySelector<HTMLButtonElement>('#swap')!)
23-
expect(container.querySelector('#val')!.textContent).toBe('["b","a"]')
20+
const { getByTestId, getByRole } = render(SwapForm)
21+
expect(getByTestId('val')).toHaveTextContent('["a","b"]')
22+
await user.click(getByRole('button', { name: 'swap' }))
23+
expect(getByTestId('val')).toHaveTextContent('["b","a"]')
2424
})
2525
})

packages/svelte-form/tests/large.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@
3939
</form.AppField>
4040
</form>
4141

42-
<pre>{JSON.stringify(formState.current, null, 2)}</pre>
42+
<pre data-testid="form-state">{JSON.stringify(formState.current, null, 2)}</pre>

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,17 @@ const user = userEvent.setup()
77

88
describe('Svelte Tests', () => {
99
it('should have initial values', () => {
10-
const { container } = render(TestForm)
11-
expect(container.querySelector<HTMLInputElement>('#firstName')).toHaveValue(
12-
getSampleData().firstName,
13-
)
10+
const { getByLabelText } = render(TestForm)
11+
expect(getByLabelText('First name')).toHaveValue(getSampleData().firstName)
1412
})
1513

1614
it('should mirror user input', async () => {
17-
const { container } = render(TestForm)
18-
const firstName = container.querySelector<HTMLInputElement>('#firstName')!
15+
const { getByLabelText, getByTestId } = render(TestForm)
16+
const firstName = getByLabelText('First name')
1917
const firstNameValue = 'Jobs'
2018
await user.type(firstName, firstNameValue)
2119

22-
const form = JSON.parse(container.querySelector('pre')!.textContent!)
20+
const form = JSON.parse(getByTestId('form-state').textContent!)
2321
expect(form.values.firstName).toBe(
2422
getSampleData().firstName + firstNameValue,
2523
)

packages/svelte-form/tests/simple.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,4 @@
121121
Change Sample Data
122122
</button>
123123

124-
<pre>{JSON.stringify(formState.current, null, 2)}</pre>
124+
<pre data-testid="form-state">{JSON.stringify(formState.current, null, 2)}</pre>

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

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,57 +7,51 @@ const user = userEvent.setup()
77

88
describe('Svelte Tests', () => {
99
it('should have initial values', () => {
10-
const { container } = render(TestForm)
11-
expect(container.querySelector<HTMLInputElement>('#firstName')).toHaveValue(
12-
getSampleData().firstName,
13-
)
14-
expect(container.querySelector<HTMLInputElement>('#lastName')).toHaveValue(
15-
getSampleData().lastName,
16-
)
10+
const { getByLabelText } = render(TestForm)
11+
expect(getByLabelText('First Name')).toHaveValue(getSampleData().firstName)
12+
expect(getByLabelText('Last Name')).toHaveValue(getSampleData().lastName)
1713
})
1814

1915
it('should change initial values when defaults update', async () => {
20-
const { container } = render(TestForm)
21-
await user.click(container.querySelector<HTMLButtonElement>('#change')!)
16+
const { getByLabelText, getByRole } = render(TestForm)
17+
await user.click(getByRole('button', { name: 'Change Sample Data' }))
2218

23-
expect(container.querySelector<HTMLInputElement>('#firstName')).toHaveValue(
24-
getSampleData().firstName,
25-
)
19+
expect(getByLabelText('First Name')).toHaveValue(getSampleData().firstName)
2620
expect(getSampleData().firstName).toBe('Julian')
2721
})
2822

2923
it('should mirror user input', async () => {
30-
const { container } = render(TestForm)
31-
const lastName = container.querySelector<HTMLInputElement>('#lastName')!
24+
const { getByLabelText, getByTestId } = render(TestForm)
25+
const lastName = getByLabelText('Last Name')
3226
const lastNameValue = 'Jobs'
3327
await user.type(lastName, lastNameValue)
3428

35-
const form = JSON.parse(container.querySelector('pre')!.textContent!)
29+
const form = JSON.parse(getByTestId('form-state').textContent!)
3630
expect(form.values.lastName).toBe(lastNameValue)
3731
})
3832

3933
it('Reset form to initial value', async () => {
40-
const { container } = render(TestForm)
41-
const firstName = container.querySelector<HTMLInputElement>('#firstName')!
34+
const { getByLabelText, getByRole } = render(TestForm)
35+
const firstName = getByLabelText('First Name')
4236
await user.type(firstName, '-Joseph')
4337

4438
expect(firstName).toHaveValue(getSampleData().firstName + '-Joseph')
4539

46-
await user.click(container.querySelector<HTMLButtonElement>('#reset')!)
40+
await user.click(getByRole('button', { name: 'Reset' }))
4741
expect(firstName).toHaveValue(getSampleData().firstName)
4842
})
4943

5044
it('should display validation', async () => {
51-
const { container } = render(TestForm)
52-
const lastName = container.querySelector<HTMLInputElement>('#lastName')!
45+
const { getByLabelText, getByText, queryByText } = render(TestForm)
46+
const lastName = getByLabelText('Last Name')
5347
const lastNameValue = 'Jo'
5448
await user.type(lastName, lastNameValue)
5549
expect(lastName).toHaveValue('Jo')
56-
expect(container.querySelector('em')?.textContent).toBe('Not long enough')
50+
expect(getByText('Not long enough')).toBeInTheDocument()
5751

5852
await user.type(lastName, lastNameValue)
5953

6054
expect(lastName.getAttribute('error-text')).toBeFalsy()
61-
expect(container.querySelector('em')).not.toBeInTheDocument()
55+
expect(queryByText('Not long enough')).not.toBeInTheDocument()
6256
})
6357
})

0 commit comments

Comments
 (0)