koel/resources/assets/js/components/layout/AppHeader.spec.ts

58 lines
1.7 KiB
TypeScript
Raw Normal View History

2022-05-11 15:24:28 +00:00
import isMobile from 'ismobilejs'
import { expect, it } from 'vitest'
2022-07-22 17:27:24 +00:00
import { fireEvent, waitFor } from '@testing-library/vue'
import { eventBus } from '@/utils'
import compareVersions from 'compare-versions'
2022-05-13 17:58:38 +00:00
import UnitTestCase from '@/__tests__/UnitTestCase'
import AppHeader from './AppHeader.vue'
import SearchForm from '@/components/ui/SearchForm.vue'
2022-05-13 17:58:38 +00:00
new class extends UnitTestCase {
protected test () {
it('toggles sidebar (mobile only)', async () => {
isMobile.any = true
const { getByTitle } = this.render(AppHeader)
const mock = this.mock(eventBus, 'emit')
await fireEvent.click(getByTitle('Show or hide the sidebar'))
expect(mock).toHaveBeenCalledWith('TOGGLE_SIDEBAR')
})
it('toggles search form (mobile only)', async () => {
isMobile.any = true
const { getByTitle, getByRole, queryByRole } = this.render(AppHeader, {
global: {
stubs: {
SearchForm
}
}
})
expect(await queryByRole('search')).toBeNull()
await fireEvent.click(getByTitle('Show or hide the search form'))
2022-07-10 15:17:48 +00:00
await waitFor(() => getByRole('search'))
})
it.each([[true, true, true], [false, true, false], [true, false, false], [false, false, false]])(
2022-07-22 17:27:24 +00:00
'announces a new version has new version: %s, is admin: %s, should announce: %s',
async (hasNewVersion, isAdmin, announcing) => {
this.mock(compareVersions, 'compare', hasNewVersion)
2022-07-22 17:27:24 +00:00
if (isAdmin) {
this.actingAsAdmin()
} else {
this.actingAs()
}
const { queryAllByTestId } = this.render(AppHeader)
2022-07-10 15:17:48 +00:00
expect(queryAllByTestId('new-version')).toHaveLength(announcing ? 1 : 0)
}
)
}
}