mirror of
https://github.com/koel/koel
synced 2024-11-24 13:13:05 +00:00
fix(test): favorite store tests
This commit is contained in:
parent
eea9be5c58
commit
e3aec84842
2 changed files with 82 additions and 8 deletions
80
resources/assets/js/stores/favoriteStore.spec.ts
Normal file
80
resources/assets/js/stores/favoriteStore.spec.ts
Normal file
|
@ -0,0 +1,80 @@
|
|||
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)
|
||||
})
|
||||
}
|
||||
}
|
|
@ -1,14 +1,12 @@
|
|||
import { reactive } from 'vue'
|
||||
import { differenceBy, union, unionBy } from 'lodash'
|
||||
import { differenceBy, unionBy } from 'lodash'
|
||||
import { httpService } from '@/services'
|
||||
import { arrayify } from '@/utils'
|
||||
import { songStore } from '@/stores'
|
||||
|
||||
export const favoriteStore = {
|
||||
state: reactive({
|
||||
songs: [] as Song[],
|
||||
length: 0,
|
||||
fmtLength: ''
|
||||
songs: [] as Song[]
|
||||
}),
|
||||
|
||||
async toggleOne (song: Song) {
|
||||
|
@ -28,10 +26,6 @@ export const favoriteStore = {
|
|||
this.state.songs = differenceBy(this.state.songs, arrayify(songs), 'id')
|
||||
},
|
||||
|
||||
clear () {
|
||||
this.state.songs = []
|
||||
},
|
||||
|
||||
async like (songs: Song[]) {
|
||||
// Don't wait for the HTTP response to update the status, just set them to Liked right away.
|
||||
// This may cause a minor problem if the request fails somehow, but do we care?
|
||||
|
|
Loading…
Reference in a new issue