mirror of
https://github.com/koel/koel
synced 2024-12-21 01:53:11 +00:00
81 lines
2.7 KiB
TypeScript
81 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(`favorites`)
|
||
|
expect(favoriteStore.state.songs).toEqual(songs)
|
||
|
})
|
||
|
}
|
||
|
}
|