koel/resources/assets/js/components/ui/SearchForm.spec.ts
2022-05-13 19:58:38 +02:00

41 lines
1.3 KiB
TypeScript

import { expect, it, vi } from 'vitest'
import router from '@/router'
import UnitTestCase from '@/__tests__/UnitTestCase'
import SearchForm from './SearchForm.vue'
import { fireEvent } from '@testing-library/vue'
import { eventBus } from '@/utils'
new class extends UnitTestCase {
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')
vi.advanceTimersByTime(500)
expect(mock).toHaveBeenCalledWith('SEARCH_KEYWORDS_CHANGED', 'hey')
vi.useRealTimers()
})
}
}