Skip to content

Commit

Permalink
Fix setTimeout linter
Browse files Browse the repository at this point in the history
  • Loading branch information
fdodino committed Dec 11, 2024
1 parent baf1840 commit f8251e2
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 58 deletions.
9 changes: 8 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,12 @@
"editor.defaultFormatter": "dbaeumer.vscode-eslint",
"editor.formatOnSave": true,
"editor.tabSize": 2,
"eslint.validate": ["javascript", "javascriptreact", "typescript"]
"eslint.validate": [
"javascript",
"javascriptreact",
"typescript"
],
"[svelte]": {
"editor.defaultFormatter": "svelte.svelte-vscode"
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ export default defineConfig({
})
```

### Anatomía de un test
### Anatomía de un test e2e

Ahora sí vamos a ver el test, no demasiado diferente a lo que veníamos haciendo:

Expand Down
6 changes: 6 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@ import js from '@eslint/js'
import svelteParser from 'svelte-eslint-parser'
import tsEslint from 'typescript-eslint'
import tsParser from '@typescript-eslint/parser'
import globals from 'globals'

export default [
js.configs.recommended,
...tsEslint.configs.strict,
...eslintPluginSvelte.configs['flat/recommended'],
{
languageOptions: {
globals: {
...globals.browser,
},
},
rules: {
quotes: [
'warn',
Expand Down
120 changes: 64 additions & 56 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,69 +1,77 @@
<script lang='ts'>
import './paises.css'
import { goto } from '$app/navigation'
import { Pais } from '../lib/pais'
import { paisService } from '../lib/paisService'
<script lang="ts">
import './paises.css'
import { goto } from '$app/navigation'
import { Pais } from '../lib/pais'
import { paisService } from '../lib/paisService'
import Bandera from '$lib/Bandera.svelte'
let paisBusqueda = $state('')
let busquedaAutomatica = $state(false)
let paises = $state<Pais[]>([])
let buscarHabilitado = $derived(paisBusqueda.trim() !== '')
let paisBusqueda = $state('')
let busquedaAutomatica = $state(false)
let paises = $state<Pais[]>([])
let buscarHabilitado = $derived(paisBusqueda.trim() !== '')
const buscar = async () => {
paises = await paisService.buscarPais(paisBusqueda)
}
const buscar = async () => {
paises = await paisService.buscarPais(paisBusqueda)
}
const handleKeyDownBuscar = async (event: { keyCode: number }) => {
if (event.keyCode === 13) {
await buscar()
}
}
const handleKeyDownBuscar = async (event: { keyCode: number }) => {
if (event.keyCode === 13) {
await buscar()
}
}
let timeout: ReturnType<typeof setTimeout>
let timeout: ReturnType<typeof setTimeout>
const debounce = (callback: Function, wait: number) => {
return (...args: any[]) => {
clearTimeout(timeout)
timeout = setTimeout(() => { callback(...args) }, wait)
}
}
const debounce = (callback: (...args: unknown[]) => void, wait: number) => {
return (...args: unknown[]) => {
clearTimeout(timeout)
timeout = setTimeout(() => {
callback(...args)
}, wait)
}
}
const handleKeyUpBuscar = async () => {
if (busquedaAutomatica && buscarHabilitado) {
debounce(buscar, 1000)()
}
}
const handleKeyUpBuscar = async () => {
if (busquedaAutomatica && buscarHabilitado) {
debounce(buscar, 1000)()
}
}
</script>

<div class='titulo'>
<h2>Países</h2>
<div class="titulo">
<h2>Países</h2>
</div>
<div class='busqueda'>
<input
data-testid='paisBusqueda'
onkeydown={handleKeyDownBuscar}
onkeyup={handleKeyUpBuscar}
bind:value={paisBusqueda}
placeholder='Ingrese un valor para buscar países'
/>
{#if !busquedaAutomatica}
<button data-testid='buscar' onclick={buscar} disabled={!buscarHabilitado}>Buscar</button>
{/if}
<div class="busqueda">
<input
data-testid="paisBusqueda"
onkeydown={handleKeyDownBuscar}
onkeyup={handleKeyUpBuscar}
bind:value={paisBusqueda}
placeholder="Ingrese un valor para buscar países"
/>
{#if !busquedaAutomatica}
<button data-testid="buscar" onclick={buscar} disabled={!buscarHabilitado}>Buscar</button>
{/if}
</div>
<div class='check'>
<input class='checkbox' type="checkbox" name="busquedaAutomatica" data-testid="busquedaAutomatica" bind:checked={busquedaAutomatica}>
<label for="busquedaAutomatica">Buscar automáticamente</label>
<div class="check">
<input
class="checkbox"
type="checkbox"
name="busquedaAutomatica"
data-testid="busquedaAutomatica"
bind:checked={busquedaAutomatica}
/>
<label for="busquedaAutomatica">Buscar automáticamente</label>
</div>
<div class='paises'>
{#each paises as pais}
<button
class='pais'
data-testid={`pais-${pais.codigo}`}
onclick={() => goto(`/pais/${pais.codigo}`)}
>
<Bandera bandera={pais.bandera}/>
<div class='nombre_pais'>{pais.nombre}</div>
</button>
{/each}
<div class="paises">
{#each paises as pais}
<button
class="pais"
data-testid={`pais-${pais.codigo}`}
onclick={() => goto(`/pais/${pais.codigo}`)}
>
<Bandera bandera={pais.bandera} />
<div class="nombre_pais">{pais.nombre}</div>
</button>
{/each}
</div>

0 comments on commit f8251e2

Please sign in to comment.