Skip to content

Commit 429159f

Browse files
committed
Add tests
1 parent b70a3a3 commit 429159f

File tree

8 files changed

+138
-9
lines changed

8 files changed

+138
-9
lines changed

src/routes/csr/+page.svelte

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ let palabra = $state('')
88
</div>
99

1010
<div class="row">
11-
<input name="palabra" bind:value={palabra} placeholder="Escribí una palabra" required />
11+
<input name="palabra" data-testid="palabra" bind:value={palabra} placeholder="Escribí una palabra" required />
1212
</div>
1313
<div class="row">
14-
<a class="link" href="/">Volver</a>
14+
<a class="link" data-testid="back-link" href="/">Volver</a>
1515
</div>
1616

1717
{#if palabra}
1818
<div class="row">
19-
<p>La palabra "{palabra}" tiene {palabra.length} letras.</p>
19+
<p data-testid="resultado">La palabra "{palabra}" tiene {palabra.length} letras.</p>
2020
</div>
2121
{/if}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { describe, it, expect, vi, beforeEach } from 'vitest'
2+
import { render, screen } from '@testing-library/svelte'
3+
import Counter from './+page.svelte'
4+
import userEvent from '@testing-library/user-event'
5+
6+
describe('count letters', () => {
7+
beforeEach(() => {
8+
vi.clearAllMocks()
9+
})
10+
11+
it('should start with empty string and should show no result', () => {
12+
render(Counter)
13+
14+
const text = screen.getByTestId('palabra') as HTMLInputElement
15+
expect(text.value).to.equal('')
16+
expect(screen.queryByTestId('resultado')).not.toBeTruthy()
17+
})
18+
19+
it('should count the word length', async () => {
20+
render(Counter)
21+
22+
const text = screen.getByTestId('palabra') as HTMLInputElement
23+
await userEvent.type(text, 'hola mundo')
24+
25+
const resultado = screen.getByTestId('resultado') as HTMLInputElement
26+
expect(resultado.innerHTML).to.equal('La palabra "hola mundo" tiene 10 letras.')
27+
})
28+
29+
it('should navigate to the home page when link is clicked', async () => {
30+
render(Counter)
31+
const backLink = screen.getByTestId('back-link') as HTMLAnchorElement
32+
await userEvent.click(backLink)
33+
expect(window.location.pathname).toBe('/')
34+
})
35+
})

src/routes/ssr/+page.server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Actions } from "./$types"
1+
import type { Actions } from './$types'
22

33
export const actions: Actions = {
44
default: async ({ request }) => {

src/routes/ssr/+page.svelte

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@
1111

1212
<div class="row">
1313
<form method="POST" use:enhance>
14-
<input name="palabra" placeholder="Escribí una palabra" required />
15-
<button type="submit">Contar letras</button>
14+
<input name="palabra" placeholder="Escribí una palabra" data-testid="palabra" required />
15+
<button type="submit" data-testid="submit">Contar letras</button>
1616
</form>
1717
</div>
1818

1919
{#if form?.palabra}
2020
<div class="row">
21-
<p>La palabra "{form.palabra}" tiene {form.longitud} letras.</p>
21+
<p data-testid="resultado">La palabra "{form.palabra}" tiene {form.longitud} letras.</p>
2222
</div>
2323
{/if}
2424

2525
<div class="row">
26-
<a class="link" href="/">Volver</a>
26+
<a data-testid="back-link" class="link" href="/">Volver</a>
2727
</div>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { describe, it, expect, vi, beforeEach } from 'vitest'
2+
import { render, screen } from '@testing-library/svelte'
3+
import Counter from './+page.svelte'
4+
import userEvent from '@testing-library/user-event'
5+
6+
describe('count letters', () => {
7+
beforeEach(() => {
8+
vi.clearAllMocks()
9+
})
10+
11+
it('should start with empty string and should show no result', () => {
12+
render(Counter)
13+
14+
const text = screen.getByTestId('palabra') as HTMLInputElement
15+
expect(text.value).to.equal('')
16+
expect(screen.queryByTestId('resultado')).not.toBeTruthy()
17+
})
18+
19+
it('should show the word length when a word is passed as props', async () => {
20+
render(Counter, {
21+
props: {
22+
form: {
23+
palabra: 'hola',
24+
longitud: 4
25+
}
26+
}
27+
})
28+
const palabra = await screen.getByTestId('palabra') as HTMLInputElement
29+
expect(palabra.value).to.equal('')
30+
expect(screen.getByTestId('resultado').textContent).to.equal('La palabra "hola" tiene 4 letras.')
31+
})
32+
33+
it('should navigate to the home page when link is clicked', async () => {
34+
render(Counter)
35+
const backLink = screen.getByTestId('back-link') as HTMLAnchorElement
36+
await userEvent.click(backLink)
37+
expect(window.location.pathname).toBe('/')
38+
})
39+
40+
it('envía la palabra correctamente al hacer submit', async () => {
41+
const mockFetch = vi.fn().mockResolvedValue({
42+
ok: true,
43+
json: async () => ({
44+
type: 'success',
45+
data: { palabra: 'hola', longitud: 4 }
46+
})
47+
})
48+
49+
globalThis.fetch = mockFetch
50+
51+
render(Counter, { props: { form: null } })
52+
53+
const input = screen.getByTestId('palabra')
54+
const submit = screen.getByTestId('submit')
55+
56+
await userEvent.type(input, 'hola')
57+
await userEvent.click(submit)
58+
59+
// Verificar que se llamó fetch
60+
expect(mockFetch).toHaveBeenCalled()
61+
62+
// Verificamos el FormData enviado
63+
const fetchCall = mockFetch.mock.calls[0]
64+
const requestInit = fetchCall[1] as RequestInit
65+
expect(requestInit.body).toBeInstanceOf(URLSearchParams)
66+
const params = new URLSearchParams(requestInit.body as string)
67+
expect(params.get('palabra')).toBe('hola')
68+
})
69+
70+
71+
})

src/routes/ssr/page.server.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// tests/page.server.test.ts
2+
import { describe, it, expect } from 'vitest'
3+
import { actions } from './+page.server'
4+
import type { RequestEvent } from '@sveltejs/kit'
5+
import type { RouteParams } from './$types'
6+
7+
describe('actions.default', () => {
8+
it('debería devolver palabra y longitud', async () => {
9+
const mockRequest = {
10+
formData: async () =>
11+
new Map([['palabra', 'svelte']]) as unknown as FormData
12+
}
13+
14+
// Simulamos el objeto RequestEvent mínimamente
15+
const event = { request: mockRequest } as unknown as RequestEvent<RouteParams, '/ssr'>
16+
17+
const result = await actions.default(event)
18+
expect(result).toEqual({
19+
palabra: 'svelte',
20+
longitud: 6
21+
})
22+
})
23+
})

vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export default defineConfig({
99
include: ['src/**/*.{test,spec}.{js,ts}'],
1010
globals: true,
1111
environment: 'jsdom',
12-
setupFiles: ['./vitest-setup.js'],
12+
setupFiles: ['./vitest-setup.ts'],
1313
coverage: {
1414
include: ['src']
1515
}
File renamed without changes.

0 commit comments

Comments
 (0)