koel/resources/assets/js/components/screens/AllSongsScreen.spec.ts

82 lines
2.3 KiB
TypeScript
Raw Normal View History

2024-01-18 11:13:05 +00:00
import Router from '@/router'
2022-07-11 17:03:38 +00:00
import { expect, it } from 'vitest'
import factory from '@/__tests__/factory'
import UnitTestCase from '@/__tests__/UnitTestCase'
import { commonStore, queueStore, songStore } from '@/stores'
import { screen, waitFor } from '@testing-library/vue'
2022-07-11 17:03:38 +00:00
import { playbackService } from '@/services'
2022-07-13 09:49:46 +00:00
import AllSongsScreen from './AllSongsScreen.vue'
2022-07-11 17:03:38 +00:00
new class extends UnitTestCase {
2024-03-18 22:04:01 +00:00
protected beforeEach (cb?: Closure) {
super.beforeEach(cb)
2022-07-11 17:03:38 +00:00
commonStore.state.song_count = 420
commonStore.state.song_length = 123_456
songStore.state.songs = factory<Song>('song', 20)
2024-03-18 22:04:01 +00:00
this.be()
}
private async renderComponent () {
2022-07-24 11:47:18 +00:00
const fetchMock = this.mock(songStore, 'paginate').mockResolvedValue(2)
2022-07-11 17:03:38 +00:00
this.router.$currentRoute.value = {
screen: 'Songs',
path: '/songs'
}
2022-07-11 17:03:38 +00:00
const rendered = this.render(AllSongsScreen, {
global: {
stubs: {
SongList: this.stub('song-list')
}
}
})
await waitFor(() => expect(fetchMock).toHaveBeenCalledWith({
sort: 'title',
order: 'asc',
page: 1,
own_songs_only: false
}))
return [rendered, fetchMock] as const
2022-07-11 17:03:38 +00:00
}
protected test () {
it('renders', async () => {
const [{ html }] = await this.renderComponent()
2022-07-11 17:03:38 +00:00
await waitFor(() => expect(html()).toMatchSnapshot())
})
it('shuffles', async () => {
const queueMock = this.mock(queueStore, 'fetchRandom')
const playMock = this.mock(playbackService, 'playFirstInQueue')
2024-01-18 11:13:05 +00:00
const goMock = this.mock(Router, 'go')
await this.renderComponent()
2022-07-11 17:03:38 +00:00
await this.user.click(screen.getByTitle('Shuffle all. Press Alt/⌥ to change mode.'))
2022-07-11 17:03:38 +00:00
await waitFor(() => {
expect(queueMock).toHaveBeenCalled()
expect(playMock).toHaveBeenCalled()
expect(goMock).toHaveBeenCalledWith('queue')
})
})
it('renders in Plus edition', async () => {
this.enablePlusEdition()
const [{ html }, fetchMock] = await this.renderComponent()
await waitFor(() => expect(html()).toMatchSnapshot())
await this.user.click(screen.getByLabelText('Own songs only'))
await waitFor(() => expect(fetchMock).toHaveBeenCalledWith({
sort: 'title',
order: 'asc',
page: 1,
own_songs_only: true
}))
})
2022-07-11 17:03:38 +00:00
}
}