2022-07-20 21:20:43 +00:00
|
|
|
import { expect, it } from 'vitest'
|
|
|
|
import factory from '@/__tests__/factory'
|
|
|
|
import UnitTestCase from '@/__tests__/UnitTestCase'
|
2022-09-15 09:07:25 +00:00
|
|
|
import { arrayify, eventBus } from '@/utils'
|
2022-07-20 21:20:43 +00:00
|
|
|
import { EditSongFormInitialTabKey, SongsKey } from '@/symbols'
|
|
|
|
import { ref } from 'vue'
|
|
|
|
import { fireEvent } from '@testing-library/vue'
|
|
|
|
import { songStore } from '@/stores'
|
2022-08-10 14:56:01 +00:00
|
|
|
import EditSongForm from './EditSongForm.vue'
|
2022-07-20 21:20:43 +00:00
|
|
|
|
|
|
|
let songs: Song[]
|
|
|
|
|
|
|
|
new class extends UnitTestCase {
|
|
|
|
private async renderComponent (_songs: Song | Song[], initialTab: EditSongFormTabName = 'details') {
|
|
|
|
songs = arrayify(_songs)
|
|
|
|
|
2022-08-10 14:56:01 +00:00
|
|
|
const rendered = this.render(EditSongForm, {
|
2022-07-20 21:20:43 +00:00
|
|
|
global: {
|
|
|
|
provide: {
|
2022-09-08 05:06:49 +00:00
|
|
|
[<symbol>SongsKey]: [ref(songs)],
|
|
|
|
[<symbol>EditSongFormInitialTabKey]: [ref(initialTab)]
|
2022-07-20 21:20:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
await this.tick()
|
|
|
|
|
|
|
|
return rendered
|
|
|
|
}
|
|
|
|
|
|
|
|
protected test () {
|
|
|
|
it('edits a single song', async () => {
|
|
|
|
const updateMock = this.mock(songStore, 'update')
|
2022-09-15 09:07:25 +00:00
|
|
|
const emitMock = this.mock(eventBus, 'emit')
|
2022-07-20 21:20:43 +00:00
|
|
|
|
|
|
|
const { html, getByTestId, getByRole } = await this.renderComponent(factory<Song>('song', {
|
|
|
|
title: 'Rocket to Heaven',
|
|
|
|
artist_name: 'Led Zeppelin',
|
|
|
|
album_name: 'IV',
|
2022-10-09 10:53:10 +00:00
|
|
|
album_cover: 'http://test/album.jpg',
|
2022-09-23 06:21:29 +00:00
|
|
|
genre: 'Rock'
|
2022-07-20 21:20:43 +00:00
|
|
|
}))
|
|
|
|
|
|
|
|
expect(html()).toMatchSnapshot()
|
|
|
|
|
|
|
|
await fireEvent.update(getByTestId('title-input'), 'Highway to Hell')
|
|
|
|
await fireEvent.update(getByTestId('artist-input'), 'AC/DC')
|
|
|
|
await fireEvent.update(getByTestId('albumArtist-input'), 'AC/DC')
|
|
|
|
await fireEvent.update(getByTestId('album-input'), 'Back in Black')
|
|
|
|
await fireEvent.update(getByTestId('disc-input'), '1')
|
|
|
|
await fireEvent.update(getByTestId('track-input'), '10')
|
2022-09-23 06:21:29 +00:00
|
|
|
await fireEvent.update(getByTestId('genre-input'), 'Rock & Roll')
|
|
|
|
await fireEvent.update(getByTestId('year-input'), '1971')
|
2022-07-20 21:20:43 +00:00
|
|
|
await fireEvent.update(getByTestId('lyrics-input'), 'I\'m gonna make him an offer he can\'t refuse')
|
|
|
|
|
|
|
|
await fireEvent.click(getByRole('button', { name: 'Update' }))
|
|
|
|
|
|
|
|
expect(updateMock).toHaveBeenCalledWith(songs, {
|
|
|
|
title: 'Highway to Hell',
|
|
|
|
album_name: 'Back in Black',
|
|
|
|
artist_name: 'AC/DC',
|
|
|
|
album_artist_name: 'AC/DC',
|
|
|
|
lyrics: 'I\'m gonna make him an offer he can\'t refuse',
|
2022-07-21 08:08:17 +00:00
|
|
|
track: 10,
|
2022-09-23 06:21:29 +00:00
|
|
|
disc: 1,
|
|
|
|
genre: 'Rock & Roll',
|
|
|
|
year: 1971
|
2022-07-20 21:20:43 +00:00
|
|
|
})
|
|
|
|
|
2022-09-15 09:07:25 +00:00
|
|
|
expect(emitMock).toHaveBeenCalledWith('SONGS_UPDATED')
|
2022-07-20 21:20:43 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('edits multiple songs', async () => {
|
|
|
|
const updateMock = this.mock(songStore, 'update')
|
2022-09-15 09:07:25 +00:00
|
|
|
const emitMock = this.mock(eventBus, 'emit')
|
2022-07-20 21:20:43 +00:00
|
|
|
|
2022-09-08 05:06:49 +00:00
|
|
|
const { html, getByTestId, getByRole, queryByTestId } = await this.renderComponent(factory<Song>('song', 3))
|
2022-07-20 21:20:43 +00:00
|
|
|
|
|
|
|
expect(html()).toMatchSnapshot()
|
|
|
|
expect(queryByTestId('title-input')).toBeNull()
|
|
|
|
expect(queryByTestId('lyrics-input')).toBeNull()
|
|
|
|
|
|
|
|
await fireEvent.update(getByTestId('artist-input'), 'AC/DC')
|
|
|
|
await fireEvent.update(getByTestId('albumArtist-input'), 'AC/DC')
|
|
|
|
await fireEvent.update(getByTestId('album-input'), 'Back in Black')
|
|
|
|
await fireEvent.update(getByTestId('disc-input'), '1')
|
|
|
|
await fireEvent.update(getByTestId('track-input'), '10')
|
2022-09-23 06:21:29 +00:00
|
|
|
await fireEvent.update(getByTestId('year-input'), '1990')
|
|
|
|
await fireEvent.update(getByTestId('genre-input'), 'Pop')
|
2022-07-20 21:20:43 +00:00
|
|
|
|
|
|
|
await fireEvent.click(getByRole('button', { name: 'Update' }))
|
|
|
|
|
|
|
|
expect(updateMock).toHaveBeenCalledWith(songs, {
|
|
|
|
album_name: 'Back in Black',
|
|
|
|
artist_name: 'AC/DC',
|
|
|
|
album_artist_name: 'AC/DC',
|
2022-07-21 08:08:17 +00:00
|
|
|
track: 10,
|
2022-09-23 06:21:29 +00:00
|
|
|
disc: 1,
|
|
|
|
genre: 'Pop',
|
|
|
|
year: 1990
|
2022-07-20 21:20:43 +00:00
|
|
|
})
|
|
|
|
|
2022-09-15 09:07:25 +00:00
|
|
|
expect(emitMock).toHaveBeenCalledWith('SONGS_UPDATED')
|
2022-07-20 21:20:43 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('displays artist name if all songs have the same artist', async () => {
|
2022-09-08 05:06:49 +00:00
|
|
|
const { getByTestId } = await this.renderComponent(factory<Song>('song', 4, {
|
2022-07-20 21:20:43 +00:00
|
|
|
artist_id: 1000,
|
|
|
|
artist_name: 'Led Zeppelin',
|
|
|
|
album_id: 1001,
|
|
|
|
album_name: 'IV'
|
2022-09-08 05:06:49 +00:00
|
|
|
}))
|
2022-07-20 21:20:43 +00:00
|
|
|
|
|
|
|
expect(getByTestId('displayed-artist-name').textContent).toBe('Led Zeppelin')
|
|
|
|
expect(getByTestId('displayed-album-name').textContent).toBe('IV')
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|