2022-10-21 20:06:43 +00:00
|
|
|
import { expect, it } from 'vitest'
|
2022-11-29 10:18:58 +00:00
|
|
|
import { screen, waitFor } from '@testing-library/vue'
|
2022-10-21 20:06:43 +00:00
|
|
|
import factory from '@/__tests__/factory'
|
|
|
|
import UnitTestCase from '@/__tests__/UnitTestCase'
|
|
|
|
import { genreStore, songStore } from '@/stores'
|
|
|
|
import { playbackService } from '@/services'
|
|
|
|
import GenreScreen from './GenreScreen.vue'
|
|
|
|
|
|
|
|
new class extends UnitTestCase {
|
2024-04-23 21:01:27 +00:00
|
|
|
protected test () {
|
|
|
|
it('renders the song list', async () => {
|
|
|
|
await this.renderComponent()
|
|
|
|
expect(screen.getByTestId('song-list')).toBeTruthy()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('shuffles all songs without fetching if genre has <= 500 songs', async () => {
|
2024-06-01 18:02:27 +00:00
|
|
|
const genre = factory('genre', { song_count: 10 })
|
|
|
|
const songs = factory('song', 10)
|
2024-04-23 21:01:27 +00:00
|
|
|
const playbackMock = this.mock(playbackService, 'queueAndPlay')
|
|
|
|
|
|
|
|
await this.renderComponent(genre, songs)
|
|
|
|
|
|
|
|
await this.user.click(screen.getByTitle('Shuffle all. Press Alt/⌥ to change mode.'))
|
|
|
|
|
|
|
|
expect(playbackMock).toHaveBeenCalledWith(songs, true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('fetches and shuffles all songs if genre has > 500 songs', async () => {
|
2024-06-01 18:02:27 +00:00
|
|
|
const genre = factory('genre', { song_count: 501 })
|
|
|
|
const songs = factory('song', 10) // we don't really need to generate 501 songs
|
2024-04-23 21:01:27 +00:00
|
|
|
const playbackMock = this.mock(playbackService, 'queueAndPlay')
|
|
|
|
const fetchMock = this.mock(songStore, 'fetchRandomForGenre').mockResolvedValue(songs)
|
|
|
|
|
|
|
|
await this.renderComponent(genre, songs)
|
|
|
|
|
|
|
|
await this.user.click(screen.getByTitle('Shuffle all. Press Alt/⌥ to change mode.'))
|
|
|
|
|
|
|
|
await waitFor(() => {
|
|
|
|
expect(fetchMock).toHaveBeenCalledWith(genre, 500)
|
|
|
|
expect(playbackMock).toHaveBeenCalledWith(songs)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-21 20:06:43 +00:00
|
|
|
private async renderComponent (genre?: Genre, songs?: Song[]) {
|
2024-06-01 18:02:27 +00:00
|
|
|
genre = genre || factory('genre')
|
2022-10-21 20:06:43 +00:00
|
|
|
|
|
|
|
const fetchGenreMock = this.mock(genreStore, 'fetchOne').mockResolvedValue(genre)
|
|
|
|
const paginateMock = this.mock(songStore, 'paginateForGenre').mockResolvedValue({
|
|
|
|
nextPage: 2,
|
2024-10-13 17:37:01 +00:00
|
|
|
songs: songs || factory('song', 13),
|
2022-10-21 20:06:43 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
await this.router.activateRoute({
|
|
|
|
path: `genres/${genre.name}`,
|
2024-10-13 17:37:01 +00:00
|
|
|
screen: 'Genre',
|
2022-10-21 20:06:43 +00:00
|
|
|
}, { name: genre.name })
|
|
|
|
|
|
|
|
const rendered = this.render(GenreScreen, {
|
|
|
|
global: {
|
|
|
|
stubs: {
|
2024-10-13 17:37:01 +00:00
|
|
|
SongList: this.stub('song-list'),
|
|
|
|
},
|
|
|
|
},
|
2022-10-21 20:06:43 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
await waitFor(() => {
|
2022-10-27 13:33:32 +00:00
|
|
|
expect(fetchGenreMock).toHaveBeenCalledWith(genre!.name)
|
2024-01-15 22:26:50 +00:00
|
|
|
expect(paginateMock).toHaveBeenCalledWith(genre!.name, {
|
|
|
|
sort: 'title',
|
|
|
|
order: 'asc',
|
2024-10-13 17:37:01 +00:00
|
|
|
page: 1,
|
2024-01-15 22:26:50 +00:00
|
|
|
})
|
2022-10-21 20:06:43 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
await this.tick(2)
|
|
|
|
|
|
|
|
return rendered
|
|
|
|
}
|
|
|
|
}
|