koel/resources/assets/js/stores/albumStore.spec.ts

112 lines
3.7 KiB
TypeScript
Raw Normal View History

2022-07-22 21:56:13 +00:00
import { expect, it } from 'vitest'
2022-05-15 14:57:28 +00:00
import UnitTestCase from '@/__tests__/UnitTestCase'
2022-07-22 21:56:13 +00:00
import factory from '@/__tests__/factory'
import { http } from '@/services'
2022-09-14 16:45:08 +00:00
import { albumStore, songStore } from '.'
2022-05-15 14:57:28 +00:00
new class extends UnitTestCase {
2022-07-22 21:56:13 +00:00
protected beforeEach () {
super.beforeEach(() => {
albumStore.vault.clear()
2022-05-15 14:57:28 +00:00
albumStore.state.albums = []
})
}
protected test () {
2022-07-22 21:56:13 +00:00
it('gets an album by ID', () => {
const album = factory('album')
2022-07-22 21:56:13 +00:00
albumStore.vault.set(album.id, album)
expect(albumStore.byId(album.id)).toEqual(album)
})
it('removes albums by IDs', () => {
const albums = factory('album', 3)
2022-07-22 21:56:13 +00:00
albums.forEach(album => albumStore.vault.set(album.id, album))
albumStore.state.albums = albums
albumStore.removeByIds([albums[0].id, albums[1].id])
expect(albumStore.vault.size).toBe(1)
expect(albumStore.vault.has(albums[0].id)).toBe(false)
expect(albumStore.vault.has(albums[1].id)).toBe(false)
expect(albumStore.state.albums).toEqual([albums[2]])
})
it('identifies an unknown album', () => {
const album = factory.states('unknown')('album')
2022-07-22 21:56:13 +00:00
expect(albumStore.isUnknown(album)).toBe(true)
expect(albumStore.isUnknown(album.id)).toBe(true)
expect(albumStore.isUnknown(factory('album'))).toBe(false)
2022-07-22 21:56:13 +00:00
})
it('syncs albums with the vault', () => {
const album = factory('album', { name: 'IV' })
2022-07-22 21:56:13 +00:00
albumStore.syncWithVault(album)
expect(albumStore.vault.get(album.id)).toEqual(album)
album.name = 'V'
albumStore.syncWithVault(album)
expect(albumStore.vault.size).toBe(1)
expect(albumStore.vault.get(album.id)?.name).toBe('V')
2022-07-22 21:56:13 +00:00
})
it('uploads a cover for an album', async () => {
const album = factory('album')
2022-07-22 21:56:13 +00:00
albumStore.syncWithVault(album)
const songsInAlbum = factory('song', 3, { album_id: album.id })
const putMock = this.mock(http, 'put').mockResolvedValue({ cover_url: 'http://test/cover.jpg' })
2022-07-22 21:56:13 +00:00
this.mock(songStore, 'byAlbum', songsInAlbum)
await albumStore.uploadCover(album, 'data://cover')
expect(album.cover).toBe('http://test/cover.jpg')
2024-02-24 15:37:01 +00:00
expect(putMock).toHaveBeenCalledWith(`albums/${album.id}/cover`, { cover: 'data://cover' })
expect(albumStore.byId(album.id)?.cover).toBe('http://test/cover.jpg')
songsInAlbum.forEach(song => expect(song.album_cover).toBe('http://test/cover.jpg'))
2022-07-22 21:56:13 +00:00
})
it('fetches an album thumbnail', async () => {
const getMock = this.mock(http, 'get').mockResolvedValue({ thumbnailUrl: 'http://test/thumbnail.jpg' })
const album = factory('album')
2022-07-22 21:56:13 +00:00
const url = await albumStore.fetchThumbnail(album.id)
2024-02-24 15:37:01 +00:00
expect(getMock).toHaveBeenCalledWith(`albums/${album.id}/thumbnail`)
expect(url).toBe('http://test/thumbnail.jpg')
2022-07-22 21:56:13 +00:00
})
it('resolves an album', async () => {
const album = factory('album')
const getMock = this.mock(http, 'get').mockResolvedValueOnce(album)
2022-07-22 21:56:13 +00:00
expect(await albumStore.resolve(album.id)).toEqual(album)
expect(getMock).toHaveBeenCalledWith(`albums/${album.id}`)
// next call shouldn't make another request
expect(await albumStore.resolve(album.id)).toEqual(album)
expect(getMock).toHaveBeenCalledOnce()
})
it('paginates', async () => {
const albums = factory('album', 3)
2022-07-22 21:56:13 +00:00
this.mock(http, 'get').mockResolvedValueOnce({
2022-07-22 21:56:13 +00:00
data: albums,
links: {
next: '/albums?page=2',
2022-07-22 21:56:13 +00:00
},
meta: {
current_page: 1,
},
2022-07-22 21:56:13 +00:00
})
expect(await albumStore.paginate(1)).toEqual(2)
expect(albumStore.state.albums).toEqual(albums)
expect(albumStore.vault.size).toBe(3)
})
2022-05-15 14:57:28 +00:00
}
}