feat(test): add mediaInfoService tests

This commit is contained in:
Phan An 2022-07-25 15:44:17 +02:00
parent 2fae65bb91
commit 235362ba30
No known key found for this signature in database
GPG key ID: A81E4477F0BB6FDC
3 changed files with 71 additions and 7 deletions

View file

@ -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)

View 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()
})
}
}

View file

@ -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