mirror of
https://github.com/koel/koel
synced 2024-11-24 13:13:05 +00:00
feat(test): add SearchForm component tests
This commit is contained in:
parent
6c5db09425
commit
a60cf1eb94
3 changed files with 44 additions and 5 deletions
|
@ -23,7 +23,7 @@ new class extends ComponentTestCase {
|
|||
it('toggles search form (mobile only)', async () => {
|
||||
isMobile.any = true
|
||||
|
||||
const { getByTitle, getByTestId, queryByTestId } = this.render(AppHeader, {
|
||||
const { getByTitle, getByRole, queryByRole } = this.render(AppHeader, {
|
||||
global: {
|
||||
stubs: {
|
||||
SearchForm
|
||||
|
@ -31,12 +31,12 @@ new class extends ComponentTestCase {
|
|||
}
|
||||
})
|
||||
|
||||
expect(await queryByTestId('search-form')).toBeNull()
|
||||
expect(await queryByRole('search')).toBeNull()
|
||||
|
||||
await fireEvent.click(getByTitle('Show or hide the search form'))
|
||||
await this.tick()
|
||||
|
||||
getByTestId('search-form')
|
||||
getByRole('search-form')
|
||||
})
|
||||
|
||||
it.each([[true, true, true], [false, true, false], [true, false, false], [false, false, false]])(
|
||||
|
|
39
resources/assets/js/components/ui/SearchForm.spec.ts
Normal file
39
resources/assets/js/components/ui/SearchForm.spec.ts
Normal file
|
@ -0,0 +1,39 @@
|
|||
import { expect, it, vi } from 'vitest'
|
||||
import router from '@/router'
|
||||
import ComponentTestCase from '@/__tests__/ComponentTestCase'
|
||||
import SearchForm from './SearchForm.vue'
|
||||
import { fireEvent } from '@testing-library/vue'
|
||||
import { eventBus } from '@/utils'
|
||||
|
||||
new class extends ComponentTestCase {
|
||||
protected test () {
|
||||
// skipping because of unstable getRootNode() issues
|
||||
it.skip('sets focus into search box when requested', async () => {
|
||||
const { getByRole } = this.render(SearchForm)
|
||||
|
||||
eventBus.emit('FOCUS_SEARCH_FIELD')
|
||||
|
||||
expect(getByRole('searchbox')).toBe(document.activeElement)
|
||||
})
|
||||
|
||||
it('goes to search screen when search box is focused', async () => {
|
||||
const mock = this.mock(router, 'go')
|
||||
const { getByRole } = this.render(SearchForm)
|
||||
|
||||
await fireEvent.focus(getByRole('searchbox'))
|
||||
|
||||
expect(mock).toHaveBeenCalledWith('search')
|
||||
})
|
||||
|
||||
it('emits an event when search query is changed', async () => {
|
||||
vi.useFakeTimers()
|
||||
const mock = this.mock(eventBus, 'emit')
|
||||
const { getByRole } = this.render(SearchForm)
|
||||
|
||||
await fireEvent.update(getByRole('searchbox'), 'hey')
|
||||
|
||||
expect(mock).toHaveBeenCalledWith('SEARCH_KEYWORDS_CHANGED', 'hey')
|
||||
vi.useRealTimers()
|
||||
})
|
||||
}
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
<template>
|
||||
<div id="searchForm" class="side search" data-testid="search-form" role="search">
|
||||
<div id="searchForm" class="side search" role="search">
|
||||
<input
|
||||
ref="input"
|
||||
v-model="q"
|
||||
|
@ -29,7 +29,7 @@ const onInput = debounce(() => {
|
|||
_q && eventBus.emit('SEARCH_KEYWORDS_CHANGED', _q)
|
||||
}, 500)
|
||||
|
||||
const goToSearchScreen = () => router.go('/search')
|
||||
const goToSearchScreen = () => router.go('search')
|
||||
|
||||
eventBus.on({
|
||||
FOCUS_SEARCH_FIELD () {
|
||||
|
|
Loading…
Reference in a new issue