From 235362ba3083878eee16f79e05af334f8b182e09 Mon Sep 17 00:00:00 2001 From: Phan An Date: Mon, 25 Jul 2022 15:44:17 +0200 Subject: [PATCH] feat(test): add mediaInfoService tests --- .../js/__tests__/factory/albumInfoFactory.ts | 2 +- .../js/services/mediaInfoService.spec.ts | 64 +++++++++++++++++++ .../assets/js/services/mediaInfoService.ts | 12 ++-- 3 files changed, 71 insertions(+), 7 deletions(-) create mode 100644 resources/assets/js/services/mediaInfoService.spec.ts diff --git a/resources/assets/js/__tests__/factory/albumInfoFactory.ts b/resources/assets/js/__tests__/factory/albumInfoFactory.ts index f0f37c4c..c356177e 100644 --- a/resources/assets/js/__tests__/factory/albumInfoFactory.ts +++ b/resources/assets/js/__tests__/factory/albumInfoFactory.ts @@ -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) diff --git a/resources/assets/js/services/mediaInfoService.spec.ts b/resources/assets/js/services/mediaInfoService.spec.ts new file mode 100644 index 00000000..b2d4c1ed --- /dev/null +++ b/resources/assets/js/services/mediaInfoService.spec.ts @@ -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', { id: 42 }))[0] + const artistInfo = factory('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('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', { 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', { id: 42 }))[0] + const albumInfo = factory('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('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', { id: 42 }))).toBe(albumInfo) + expect(hasCacheMock).toHaveBeenCalledWith(['album.info', 42]) + expect(getCacheMock).toHaveBeenCalledWith(['album.info', 42]) + expect(getMock).not.toHaveBeenCalled() + }) + } +} diff --git a/resources/assets/js/services/mediaInfoService.ts b/resources/assets/js/services/mediaInfoService.ts index 3ddb08b6..4b8b6a60 100644 --- a/resources/assets/js/services/mediaInfoService.ts +++ b/resources/assets/js/services/mediaInfoService.ts @@ -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(cacheKey) const info = await httpService.get(`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(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