fix(test): fix playlist-related tests

This commit is contained in:
Phan An 2022-07-25 20:39:52 +02:00
parent be68021723
commit 35e03ec685
No known key found for this signature in database
GPG key ID: A81E4477F0BB6FDC
3 changed files with 31 additions and 34 deletions

View file

@ -1,56 +1,50 @@
import factory from '@/__tests__/factory'
import PlaylistNameEditor from '@/components/playlist/PlaylistNameEditor.vue'
import { expect, it } from 'vitest'
import { fireEvent } from '@testing-library/vue'
import { playlistStore } from '@/stores'
import UnitTestCase from '@/__tests__/UnitTestCase'
import PlaylistNameEditor from './PlaylistNameEditor.vue'
let playlist: Playlist
new class extends UnitTestCase {
private useEditor () {
const updateMock = this.mock(playlistStore, 'update')
const { getByTestId } = this.render(PlaylistNameEditor, {
props: {
playlist: factory<Playlist>('playlist', {
id: 99,
name: 'Foo'
})
}
private renderComponent () {
playlist = factory<Playlist>('playlist', {
id: 99,
name: 'Foo'
})
return {
updateMock,
input: getByTestId<HTMLInputElement>('inline-playlist-name-input')
}
return this.render(PlaylistNameEditor, {
props: {
playlist
}
}).getByRole('textbox')
}
protected test () {
it('updates a playlist name on blur', async () => {
const { updateMock, input } = this.useEditor()
const updateMock = this.mock(playlistStore, 'update')
const input = this.renderComponent()
await fireEvent.update(input, 'Bar')
await fireEvent.blur(input)
expect(updateMock).toHaveBeenCalledWith(expect.objectContaining({
id: 99,
name: 'Bar'
}))
expect(updateMock).toHaveBeenCalledWith(playlist, { name: 'Bar' })
})
it('updates a playlist name on enter', async () => {
const { updateMock, input } = this.useEditor()
const updateMock = this.mock(playlistStore, 'update')
const input = this.renderComponent()
await fireEvent.update(input, 'Bar')
await fireEvent.keyUp(input, { key: 'Enter' })
expect(updateMock).toHaveBeenCalledWith(expect.objectContaining({
id: 99,
name: 'Bar'
}))
expect(updateMock).toHaveBeenCalledWith(playlist, { name: 'Bar' })
})
it('cancels updating on esc', async () => {
const { updateMock, input } = this.useEditor()
const updateMock = this.mock(playlistStore, 'update')
const input = this.renderComponent()
await fireEvent.update(input, 'Bar')
await fireEvent.keyUp(input, { key: 'Esc' })

View file

@ -52,5 +52,8 @@ const update = async () => {
}
}
const cancel = () => emit('cancelled')
const cancel = () => {
name.value = playlist.value.name
emit('cancelled')
}
</script>

View file

@ -2,7 +2,7 @@ import UnitTestCase from '@/__tests__/UnitTestCase'
import factory from '@/__tests__/factory'
import { playlistStore } from '@/stores'
import { expect, it } from 'vitest'
import { Cache, httpService } from '@/services'
import { cache, httpService } from '@/services'
const ruleGroups: SmartPlaylistRuleGroup[] = [
{
@ -114,24 +114,24 @@ new class extends UnitTestCase {
const playlist = factory<Playlist>('playlist', { id: 12 })
const songs = factory<Song[]>('song', 3)
const postMock = this.mock(httpService, 'post').mockResolvedValue(playlist)
const invalidateMock = this.mock(Cache, 'invalidate')
const removeMock = this.mock(cache, 'remove')
await playlistStore.addSongs(playlist, songs)
expect(postMock).toHaveBeenCalledWith('playlists/12/songs', { songs: songs.map(song => song.id) })
expect(invalidateMock).toHaveBeenCalledWith(['playlist.songs', 12])
expect(removeMock).toHaveBeenCalledWith(['playlist.songs', 12])
})
it('removes songs from a playlist', async () => {
const playlist = factory<Playlist>('playlist', { id: 12 })
const songs = factory<Song[]>('song', 3)
const deleteMock = this.mock(httpService, 'delete').mockResolvedValue(playlist)
const invalidateMock = this.mock(Cache, 'invalidate')
const removeMock = this.mock(cache, 'remove')
await playlistStore.removeSongs(playlist, songs)
expect(deleteMock).toHaveBeenCalledWith('playlists/12/songs', { songs: songs.map(song => song.id) })
expect(invalidateMock).toHaveBeenCalledWith(['playlist.songs', 12])
expect(removeMock).toHaveBeenCalledWith(['playlist.songs', 12])
})
it('does not modify a smart playlist content', async () => {
@ -160,13 +160,13 @@ new class extends UnitTestCase {
const rules = factory<SmartPlaylistRuleGroup[]>('smart-playlist-rule-group', 2)
const serializeMock = this.mock(playlistStore, 'serializeSmartPlaylistRulesForStorage', ['Whatever'])
const putMock = this.mock(httpService, 'put').mockResolvedValue(playlist)
const invalidateMock = this.mock(Cache, 'invalidate')
const removeMock = this.mock(cache, 'remove')
await playlistStore.update(playlist, { name: 'Foo', rules })
expect(serializeMock).toHaveBeenCalledWith(rules)
expect(putMock).toHaveBeenCalledWith('playlists/12', { name: 'Foo', rules: ['Whatever'] })
expect(invalidateMock).toHaveBeenCalledWith(['playlist.songs', 12])
expect(removeMock).toHaveBeenCalledWith(['playlist.songs', 12])
})
}
}