mirror of
https://github.com/koel/koel
synced 2024-11-28 06:50:27 +00:00
feat(test): add mediaInfoService tests
This commit is contained in:
parent
2fae65bb91
commit
235362ba30
3 changed files with 71 additions and 7 deletions
|
@ -2,7 +2,7 @@ import { Faker } from '@faker-js/faker'
|
|||
import factory from 'factoria'
|
||||
|
||||
export default (faker: Faker): AlbumInfo => ({
|
||||
image: faker.image.imageUrl(),
|
||||
cover: faker.image.imageUrl(),
|
||||
wiki: {
|
||||
summary: faker.lorem.sentence(),
|
||||
full: faker.lorem.sentences(4)
|
||||
|
|
64
resources/assets/js/services/mediaInfoService.spec.ts
Normal file
64
resources/assets/js/services/mediaInfoService.spec.ts
Normal file
|
@ -0,0 +1,64 @@
|
|||
import { expect, it } from 'vitest'
|
||||
import factory from '@/__tests__/factory'
|
||||
import UnitTestCase from '@/__tests__/UnitTestCase'
|
||||
import { cache, httpService } from '@/services'
|
||||
import { albumStore, artistStore } from '@/stores'
|
||||
import { mediaInfoService } from '@/services/mediaInfoService'
|
||||
|
||||
new class extends UnitTestCase {
|
||||
protected test () {
|
||||
it('fetches the artist info', async () => {
|
||||
const artist = artistStore.syncWithVault(factory<Artist>('artist', { id: 42 }))[0]
|
||||
const artistInfo = factory<ArtistInfo>('artist-info')
|
||||
const getMock = this.mock(httpService, 'get').mockResolvedValue(artistInfo)
|
||||
const hasCacheMock = this.mock(cache, 'has', false)
|
||||
const setCacheMock = this.mock(cache, 'set')
|
||||
|
||||
await mediaInfoService.fetchForArtist(artist)
|
||||
|
||||
expect(getMock).toHaveBeenCalledWith('artists/42/information')
|
||||
expect(hasCacheMock).toHaveBeenCalledWith(['artist.info', 42])
|
||||
expect(setCacheMock).toHaveBeenCalledWith(['artist.info', 42], artistInfo)
|
||||
expect(artist.image).toBe(artistInfo.image)
|
||||
})
|
||||
|
||||
it('gets the artist info from cache', async () => {
|
||||
const artistInfo = factory<ArtistInfo>('artist-info')
|
||||
const hasCacheMock = this.mock(cache, 'has', true)
|
||||
const getCacheMock = this.mock(cache, 'get', artistInfo)
|
||||
const getMock = this.mock(httpService, 'get')
|
||||
|
||||
expect(await mediaInfoService.fetchForArtist(factory<Artist>('artist', { id: 42 }))).toBe(artistInfo)
|
||||
expect(hasCacheMock).toHaveBeenCalledWith(['artist.info', 42])
|
||||
expect(getCacheMock).toHaveBeenCalledWith(['artist.info', 42])
|
||||
expect(getMock).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('fetches the album info', async () => {
|
||||
const album = albumStore.syncWithVault(factory<Album>('album', { id: 42 }))[0]
|
||||
const albumInfo = factory<AlbumInfo>('album-info')
|
||||
const getMock = this.mock(httpService, 'get').mockResolvedValue(albumInfo)
|
||||
const hasCacheMock = this.mock(cache, 'has', false)
|
||||
const setCacheMock = this.mock(cache, 'set')
|
||||
|
||||
await mediaInfoService.fetchForAlbum(album)
|
||||
|
||||
expect(getMock).toHaveBeenCalledWith('albums/42/information')
|
||||
expect(hasCacheMock).toHaveBeenCalledWith(['album.info', 42])
|
||||
expect(setCacheMock).toHaveBeenCalledWith(['album.info', 42], albumInfo)
|
||||
expect(album.cover).toBe(albumInfo.cover)
|
||||
})
|
||||
|
||||
it('gets the album info from cache', async () => {
|
||||
const albumInfo = factory<AlbumInfo>('album-info')
|
||||
const hasCacheMock = this.mock(cache, 'has', true)
|
||||
const getCacheMock = this.mock(cache, 'get', albumInfo)
|
||||
const getMock = this.mock(httpService, 'get')
|
||||
|
||||
expect(await mediaInfoService.fetchForAlbum(factory<Album>('album', { id: 42 }))).toBe(albumInfo)
|
||||
expect(hasCacheMock).toHaveBeenCalledWith(['album.info', 42])
|
||||
expect(getCacheMock).toHaveBeenCalledWith(['album.info', 42])
|
||||
expect(getMock).not.toHaveBeenCalled()
|
||||
})
|
||||
}
|
||||
}
|
|
@ -3,20 +3,20 @@ import { albumStore, artistStore, songStore } from '@/stores'
|
|||
|
||||
export const mediaInfoService = {
|
||||
async fetchForArtist (artist: Artist) {
|
||||
artist = artistStore.syncWithVault(artist)[0]
|
||||
const cacheKey = ['artist.info', artist.id]
|
||||
if (cache.has(cacheKey)) return cache.get<ArtistInfo>(cacheKey)
|
||||
|
||||
const info = await httpService.get<ArtistInfo | null>(`artists/${artist.id}/information`)
|
||||
info && cache.set(cacheKey, info)
|
||||
|
||||
if (info?.image) {
|
||||
artistStore.byId(artist.id)!.image = info.image
|
||||
}
|
||||
info && cache.set(cacheKey, info)
|
||||
info?.image && (artist.image = info.image)
|
||||
|
||||
return info
|
||||
},
|
||||
|
||||
async fetchForAlbum (album: Album) {
|
||||
album = albumStore.syncWithVault(album)[0]
|
||||
const cacheKey = ['album.info', album.id]
|
||||
if (cache.has(cacheKey)) return cache.get<AlbumInfo>(cacheKey)
|
||||
|
||||
|
@ -24,8 +24,8 @@ export const mediaInfoService = {
|
|||
info && cache.set(cacheKey, info)
|
||||
|
||||
if (info?.cover) {
|
||||
albumStore.byId(album.id)!.cover = info.cover
|
||||
songStore.byAlbum(album)!.forEach(song => (song.album_cover = info.cover))
|
||||
album.cover = info.cover
|
||||
songStore.byAlbum(album).forEach(song => (song.album_cover = info.cover!))
|
||||
}
|
||||
|
||||
return info
|
||||
|
|
Loading…
Reference in a new issue