2024-01-18 11:13:05 +00:00
|
|
|
import Router from '@/router'
|
2022-05-15 20:44:19 +00:00
|
|
|
import { expect, it } from 'vitest'
|
2022-05-13 17:58:38 +00:00
|
|
|
import UnitTestCase from '@/__tests__/UnitTestCase'
|
2022-11-29 10:18:58 +00:00
|
|
|
import { screen } from '@testing-library/vue'
|
2022-05-12 09:51:58 +00:00
|
|
|
import { eventBus } from '@/utils'
|
2022-07-21 10:45:23 +00:00
|
|
|
import SearchForm from './SearchForm.vue'
|
2022-05-12 09:51:58 +00:00
|
|
|
|
2022-05-13 17:58:38 +00:00
|
|
|
new class extends UnitTestCase {
|
2022-05-12 09:51:58 +00:00
|
|
|
protected test () {
|
2022-07-21 10:45:23 +00:00
|
|
|
it('sets focus into search box when requested', async () => {
|
2022-11-29 10:18:58 +00:00
|
|
|
this.render(SearchForm)
|
2022-05-12 09:51:58 +00:00
|
|
|
|
|
|
|
eventBus.emit('FOCUS_SEARCH_FIELD')
|
|
|
|
|
2022-11-29 10:18:58 +00:00
|
|
|
expect(screen.getByRole('searchbox')).toBe(document.activeElement)
|
2022-05-12 09:51:58 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('goes to search screen when search box is focused', async () => {
|
2024-01-18 11:13:05 +00:00
|
|
|
const mock = this.mock(Router, 'go')
|
2022-11-29 10:18:58 +00:00
|
|
|
this.render(SearchForm)
|
2022-05-12 09:51:58 +00:00
|
|
|
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.user.click(screen.getByRole('searchbox'))
|
2022-05-12 09:51:58 +00:00
|
|
|
|
|
|
|
expect(mock).toHaveBeenCalledWith('search')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('emits an event when search query is changed', async () => {
|
|
|
|
const mock = this.mock(eventBus, 'emit')
|
2022-11-29 10:18:58 +00:00
|
|
|
this.render(SearchForm)
|
2022-05-12 09:51:58 +00:00
|
|
|
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.type(screen.getByRole('searchbox'), 'hey')
|
2022-05-12 09:51:58 +00:00
|
|
|
|
|
|
|
expect(mock).toHaveBeenCalledWith('SEARCH_KEYWORDS_CHANGED', 'hey')
|
|
|
|
})
|
2022-10-24 11:04:25 +00:00
|
|
|
|
|
|
|
it('goes to the search screen if the form is submitted', async () => {
|
2024-01-18 11:13:05 +00:00
|
|
|
const goMock = this.mock(Router, 'go')
|
2022-11-29 10:18:58 +00:00
|
|
|
this.render(SearchForm)
|
2022-10-24 11:04:25 +00:00
|
|
|
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.type(screen.getByRole('searchbox'), 'hey')
|
|
|
|
await this.user.click(screen.getByRole('button', { name: 'Search' }))
|
2022-10-24 11:04:25 +00:00
|
|
|
|
|
|
|
expect(goMock).toHaveBeenCalledWith('search')
|
|
|
|
})
|
2022-05-12 09:51:58 +00:00
|
|
|
}
|
|
|
|
}
|