2022-07-21 07:54:36 +00:00
|
|
|
import { ref } from 'vue'
|
2022-09-08 05:06:49 +00:00
|
|
|
import { expect, it } from 'vitest'
|
|
|
|
import { fireEvent } from '@testing-library/vue'
|
2022-07-21 07:54:36 +00:00
|
|
|
import factory from '@/__tests__/factory'
|
|
|
|
import UnitTestCase from '@/__tests__/UnitTestCase'
|
|
|
|
import { arrayify } from '@/utils'
|
|
|
|
import {
|
2022-10-09 06:31:46 +00:00
|
|
|
ScreenNameKey,
|
2022-07-21 07:54:36 +00:00
|
|
|
SelectedSongsKey,
|
|
|
|
SongListConfigKey,
|
|
|
|
SongListSortFieldKey,
|
|
|
|
SongListSortOrderKey,
|
|
|
|
SongsKey
|
|
|
|
} from '@/symbols'
|
|
|
|
import SongList from './SongList.vue'
|
|
|
|
|
|
|
|
let songs: Song[]
|
|
|
|
|
|
|
|
new class extends UnitTestCase {
|
|
|
|
private renderComponent (
|
|
|
|
_songs: Song | Song[],
|
2022-10-09 06:31:46 +00:00
|
|
|
screen: ScreenName = 'Songs',
|
2022-07-21 07:54:36 +00:00
|
|
|
config: Partial<SongListConfig> = {},
|
|
|
|
selectedSongs: Song[] = [],
|
|
|
|
sortField: SongListSortField = 'title',
|
|
|
|
sortOrder: SortOrder = 'asc'
|
|
|
|
) {
|
|
|
|
songs = arrayify(_songs)
|
|
|
|
|
2022-09-08 05:06:49 +00:00
|
|
|
const sortFieldRef = ref(sortField)
|
|
|
|
const sortOrderRef = ref(sortOrder)
|
|
|
|
|
2022-07-21 07:54:36 +00:00
|
|
|
return this.render(SongList, {
|
|
|
|
global: {
|
|
|
|
stubs: {
|
|
|
|
VirtualScroller: this.stub('virtual-scroller')
|
|
|
|
},
|
|
|
|
provide: {
|
2022-09-08 05:06:49 +00:00
|
|
|
[<symbol>SongsKey]: [ref(songs)],
|
|
|
|
[<symbol>SelectedSongsKey]: [ref(selectedSongs), value => (selectedSongs = value)],
|
2022-10-09 06:31:46 +00:00
|
|
|
[<symbol>ScreenNameKey]: [ref(screen)],
|
2022-09-08 05:06:49 +00:00
|
|
|
[<symbol>SongListConfigKey]: [config],
|
|
|
|
[<symbol>SongListSortFieldKey]: [sortFieldRef, value => (sortFieldRef.value = value)],
|
|
|
|
[<symbol>SongListSortOrderKey]: [sortOrderRef, value => (sortOrderRef.value = value)]
|
2022-07-21 07:54:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
protected test () {
|
|
|
|
it('renders', async () => {
|
2022-09-08 05:06:49 +00:00
|
|
|
const { html } = this.renderComponent(factory<Song>('song', 5))
|
2022-07-21 07:54:36 +00:00
|
|
|
expect(html()).toMatchSnapshot()
|
|
|
|
})
|
|
|
|
|
2022-09-08 05:06:49 +00:00
|
|
|
it.each<[SongListSortField, string]>([
|
2022-07-21 07:54:36 +00:00
|
|
|
['track', 'header-track-number'],
|
|
|
|
['title', 'header-title'],
|
|
|
|
['album_name', 'header-album'],
|
|
|
|
['length', 'header-length'],
|
|
|
|
['artist_name', 'header-artist']
|
2022-09-08 05:06:49 +00:00
|
|
|
])('sorts by %s upon %s clicked', async (field, testId) => {
|
|
|
|
const { getByTestId, emitted } = this.renderComponent(factory<Song>('song', 5))
|
2022-07-21 07:54:36 +00:00
|
|
|
|
|
|
|
await fireEvent.click(getByTestId(testId))
|
2022-09-08 05:06:49 +00:00
|
|
|
expect(emitted().sort[0]).toEqual([field, 'desc'])
|
2022-07-21 07:54:36 +00:00
|
|
|
|
|
|
|
await fireEvent.click(getByTestId(testId))
|
2022-09-08 05:06:49 +00:00
|
|
|
expect(emitted().sort[1]).toEqual([field, 'asc'])
|
2022-07-21 07:54:36 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|