mirror of
https://github.com/koel/koel
synced 2024-12-29 22:13:06 +00:00
80 lines
2.7 KiB
TypeScript
80 lines
2.7 KiB
TypeScript
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)
|
|
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)
|
|
favoriteStore.state.songs = songs
|
|
favoriteStore.remove(songs)
|
|
expect(favoriteStore.state.songs).toEqual([])
|
|
})
|
|
|
|
it('likes several songs', async () => {
|
|
const songs = factory<Song>('song', 3)
|
|
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)
|
|
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)
|
|
const getMock = this.mock(httpService, 'get').mockResolvedValue(songs)
|
|
|
|
await favoriteStore.fetch()
|
|
|
|
expect(getMock).toHaveBeenCalledWith('songs/favorite')
|
|
expect(favoriteStore.state.songs).toEqual(songs)
|
|
})
|
|
}
|
|
}
|