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'
|
2022-07-21 07:54:36 +00:00
|
|
|
import factory from '@/__tests__/factory'
|
|
|
|
import UnitTestCase from '@/__tests__/UnitTestCase'
|
|
|
|
import { arrayify } from '@/utils'
|
2024-03-25 22:59:38 +00:00
|
|
|
import {
|
|
|
|
SelectedSongsKey,
|
|
|
|
SongListConfigKey,
|
|
|
|
SongListContextKey,
|
|
|
|
SongListSortFieldKey,
|
|
|
|
SongListSortOrderKey,
|
|
|
|
SongsKey
|
|
|
|
} from '@/symbols'
|
2022-11-29 10:18:58 +00:00
|
|
|
import { screen } from '@testing-library/vue'
|
2022-07-21 07:54:36 +00:00
|
|
|
import SongList from './SongList.vue'
|
|
|
|
|
|
|
|
let songs: Song[]
|
|
|
|
|
|
|
|
new class extends UnitTestCase {
|
2022-11-12 21:38:31 +00:00
|
|
|
private async renderComponent (
|
2022-07-21 07:54:36 +00:00
|
|
|
_songs: Song | Song[],
|
2022-11-12 21:38:31 +00:00
|
|
|
config: Partial<SongListConfig> = {
|
|
|
|
sortable: true,
|
|
|
|
reorderable: true
|
|
|
|
},
|
2024-03-25 22:59:38 +00:00
|
|
|
context: SongListContext = {
|
|
|
|
type: 'Album',
|
|
|
|
},
|
2022-07-21 07:54:36 +00:00
|
|
|
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-11-12 21:38:31 +00:00
|
|
|
await this.router.activateRoute({
|
|
|
|
screen: 'Songs',
|
|
|
|
path: '/songs'
|
|
|
|
})
|
|
|
|
|
2022-07-21 07:54:36 +00:00
|
|
|
return this.render(SongList, {
|
|
|
|
global: {
|
|
|
|
stubs: {
|
2022-11-12 21:38:31 +00:00
|
|
|
VirtualScroller: this.stub('virtual-scroller'),
|
|
|
|
SongListSorter: this.stub('song-list-sorter')
|
2022-07-21 07:54:36 +00:00
|
|
|
},
|
|
|
|
provide: {
|
2022-09-08 05:06:49 +00:00
|
|
|
[<symbol>SongsKey]: [ref(songs)],
|
2024-03-25 22:59:38 +00:00
|
|
|
[<symbol>SelectedSongsKey]: [ref(selectedSongs), (value: Song[]) => (selectedSongs = value)],
|
2022-09-08 05:06:49 +00:00
|
|
|
[<symbol>SongListConfigKey]: [config],
|
2024-03-25 22:59:38 +00:00
|
|
|
[<symbol>SongListContextKey]: [context],
|
|
|
|
[<symbol>SongListSortFieldKey]: [sortFieldRef, (value: SongListSortField) => (sortFieldRef.value = value)],
|
|
|
|
[<symbol>SongListSortOrderKey]: [sortOrderRef, (value: SortOrder) => (sortOrderRef.value = value)]
|
2022-07-21 07:54:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
protected test () {
|
|
|
|
it('renders', async () => {
|
2022-11-12 21:38:31 +00:00
|
|
|
const { html } = await 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'],
|
2022-11-12 21:38:31 +00:00
|
|
|
['length', 'header-length']
|
2022-09-08 05:06:49 +00:00
|
|
|
])('sorts by %s upon %s clicked', async (field, testId) => {
|
2022-11-29 10:18:58 +00:00
|
|
|
const { emitted } = await this.renderComponent(factory<Song>('song', 5))
|
2022-07-21 07:54:36 +00:00
|
|
|
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.user.click(screen.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
|
|
|
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.user.click(screen.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
|
|
|
})
|
2022-11-12 21:38:31 +00:00
|
|
|
|
|
|
|
it('cannot be sorted if configured so', async () => {
|
2022-11-29 10:18:58 +00:00
|
|
|
const { emitted } = await this.renderComponent(factory<Song>('song', 5), {
|
2022-11-12 21:38:31 +00:00
|
|
|
sortable: false,
|
|
|
|
reorderable: true
|
|
|
|
})
|
|
|
|
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.user.click(screen.getByTestId('header-track-number'))
|
2022-11-12 21:38:31 +00:00
|
|
|
expect(emitted().sort).toBeUndefined()
|
|
|
|
})
|
2022-07-21 07:54:36 +00:00
|
|
|
}
|
|
|
|
}
|