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

81 lines
2.7 KiB
TypeScript
Raw Normal View History

2022-07-22 22:19:42 +00:00
import { expect, it } from 'vitest'
import factory from '@/__tests__/factory'
import UnitTestCase from '@/__tests__/UnitTestCase'
import { favoriteStore } from '@/stores'
import { httpService } from '@/services'
new class extends UnitTestCase {
protected beforeEach () {
super.beforeEach(() => (favoriteStore.state.songs = []))
}
protected test () {
it('toggles one song', async () => {
const addMock = this.mock(favoriteStore, 'add')
const removeMock = this.mock(favoriteStore, 'remove')
const postMock = this.mock(httpService, 'post')
const song = factory<Song>('song', { liked: false })
await favoriteStore.toggleOne(song)
expect(postMock).toHaveBeenNthCalledWith(1, `interaction/like`, { song: song.id })
expect(addMock).toHaveBeenCalledWith(song)
expect(song.liked).toBe(true)
await favoriteStore.toggleOne(song)
expect(postMock).toHaveBeenNthCalledWith(2, `interaction/like`, { song: song.id })
expect(removeMock).toHaveBeenCalledWith(song)
expect(song.liked).toBe(false)
})
it('adds songs', () => {
const songs = factory<Song>('song', 3)
2022-07-22 22:19:42 +00:00
favoriteStore.add(songs)
expect(favoriteStore.state.songs).toEqual(songs)
// doesn't duplicate songs
favoriteStore.add(songs[0])
expect(favoriteStore.state.songs).toEqual(songs)
})
it('removes songs', () => {
const songs = factory<Song>('song', 3)
2022-07-22 22:19:42 +00:00
favoriteStore.state.songs = songs
favoriteStore.remove(songs)
expect(favoriteStore.state.songs).toEqual([])
})
it('likes several songs', async () => {
const songs = factory<Song>('song', 3)
2022-07-22 22:19:42 +00:00
const addMock = this.mock(favoriteStore, 'add')
const postMock = this.mock(httpService, 'post')
await favoriteStore.like(songs)
expect(postMock).toHaveBeenCalledWith(`interaction/batch/like`, { songs: songs.map(song => song.id) })
expect(addMock).toHaveBeenCalledWith(songs)
})
it('unlikes several songs', async () => {
const songs = factory<Song>('song', 3)
2022-07-22 22:19:42 +00:00
const removeMock = this.mock(favoriteStore, 'remove')
const postMock = this.mock(httpService, 'post')
await favoriteStore.unlike(songs)
expect(postMock).toHaveBeenCalledWith(`interaction/batch/unlike`, { songs: songs.map(song => song.id) })
expect(removeMock).toHaveBeenCalledWith(songs)
})
it('fetches favorites', async () => {
const songs = factory<Song>('song', 3)
2022-07-22 22:19:42 +00:00
const getMock = this.mock(httpService, 'get').mockResolvedValue(songs)
await favoriteStore.fetch()
2022-07-27 09:30:04 +00:00
expect(getMock).toHaveBeenCalledWith('songs/favorite')
2022-07-22 22:19:42 +00:00
expect(favoriteStore.state.songs).toEqual(songs)
})
}
}