2022-07-10 17:33:53 +00:00
|
|
|
import { fireEvent, waitFor } from '@testing-library/vue'
|
2022-07-10 17:15:56 +00:00
|
|
|
import { expect, it } from 'vitest'
|
|
|
|
import factory from '@/__tests__/factory'
|
|
|
|
import UnitTestCase from '@/__tests__/UnitTestCase'
|
2022-07-10 17:33:53 +00:00
|
|
|
import { albumStore, commonStore, songStore } from '@/stores'
|
|
|
|
import { downloadService } from '@/services'
|
|
|
|
import { eventBus } from '@/utils'
|
2022-07-11 17:35:58 +00:00
|
|
|
import CloseModalBtn from '@/components/ui/BtnCloseModal.vue'
|
|
|
|
import AlbumScreen from './AlbumScreen.vue'
|
2022-07-10 17:33:53 +00:00
|
|
|
|
|
|
|
let album: Album
|
2022-07-10 17:15:56 +00:00
|
|
|
|
|
|
|
new class extends UnitTestCase {
|
|
|
|
protected async renderComponent () {
|
|
|
|
commonStore.state.use_last_fm = true
|
|
|
|
|
2022-07-10 17:33:53 +00:00
|
|
|
album = factory<Album>('album', {
|
|
|
|
id: 42,
|
2022-07-10 17:15:56 +00:00
|
|
|
name: 'Led Zeppelin IV',
|
2022-07-10 17:33:53 +00:00
|
|
|
artist_id: 123,
|
2022-07-10 17:15:56 +00:00
|
|
|
artist_name: 'Led Zeppelin',
|
|
|
|
song_count: 10,
|
|
|
|
length: 1_603
|
|
|
|
})
|
|
|
|
|
2022-08-01 09:38:32 +00:00
|
|
|
const resolveAlbumMock = this.mock(albumStore, 'resolve').mockResolvedValue(album)
|
|
|
|
|
2022-09-12 15:33:41 +00:00
|
|
|
const songs = factory<Song>('song', 13)
|
2022-07-10 17:15:56 +00:00
|
|
|
const fetchSongsMock = this.mock(songStore, 'fetchForAlbum').mockResolvedValue(songs)
|
|
|
|
|
2022-10-08 10:54:25 +00:00
|
|
|
await this.router.activateRoute({
|
|
|
|
path: 'albums/42',
|
|
|
|
screen: 'Album'
|
|
|
|
}, { id: '42' })
|
|
|
|
|
2022-07-10 17:15:56 +00:00
|
|
|
const rendered = this.render(AlbumScreen, {
|
2022-07-10 17:33:53 +00:00
|
|
|
global: {
|
|
|
|
stubs: {
|
|
|
|
CloseModalBtn,
|
|
|
|
AlbumInfo: this.stub('album-info'),
|
|
|
|
SongList: this.stub('song-list')
|
|
|
|
}
|
2022-07-10 17:15:56 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-08-01 09:38:32 +00:00
|
|
|
await waitFor(() => {
|
|
|
|
expect(resolveAlbumMock).toHaveBeenCalledWith(album.id)
|
|
|
|
expect(fetchSongsMock).toHaveBeenCalledWith(album.id)
|
|
|
|
})
|
|
|
|
|
|
|
|
await this.tick(2)
|
2022-07-10 17:15:56 +00:00
|
|
|
|
|
|
|
return rendered
|
|
|
|
}
|
|
|
|
|
|
|
|
protected test () {
|
2022-07-10 17:33:53 +00:00
|
|
|
it('shows and hides info', async () => {
|
2022-08-01 09:38:32 +00:00
|
|
|
const { getByTitle, getByTestId, queryByTestId, html } = await this.renderComponent()
|
2022-07-10 17:33:53 +00:00
|
|
|
expect(queryByTestId('album-info')).toBeNull()
|
|
|
|
|
|
|
|
await fireEvent.click(getByTitle('View album information'))
|
|
|
|
expect(queryByTestId('album-info')).not.toBeNull()
|
|
|
|
|
|
|
|
await fireEvent.click(getByTestId('close-modal-btn'))
|
|
|
|
expect(queryByTestId('album-info')).toBeNull()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('downloads', async () => {
|
|
|
|
const downloadMock = this.mock(downloadService, 'fromAlbum')
|
|
|
|
const { getByText } = await this.renderComponent()
|
|
|
|
|
|
|
|
await fireEvent.click(getByText('Download All'))
|
|
|
|
|
|
|
|
expect(downloadMock).toHaveBeenCalledWith(album)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('goes back to list if album is deleted', async () => {
|
2022-10-08 10:54:25 +00:00
|
|
|
const goMock = this.mock(this.router, 'go')
|
2022-07-10 17:33:53 +00:00
|
|
|
const byIdMock = this.mock(albumStore, 'byId', null)
|
|
|
|
await this.renderComponent()
|
|
|
|
|
|
|
|
eventBus.emit('SONGS_UPDATED')
|
|
|
|
|
|
|
|
await waitFor(() => {
|
|
|
|
expect(byIdMock).toHaveBeenCalledWith(album.id)
|
|
|
|
expect(goMock).toHaveBeenCalledWith('albums')
|
|
|
|
})
|
2022-07-10 17:15:56 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|