koel/resources/assets/js/components/song/SongList.spec.ts

93 lines
2.8 KiB
TypeScript
Raw Normal View History

2022-07-21 07:54:36 +00:00
import { ref } from 'vue'
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 {
2024-05-19 05:49:42 +00:00
PlayableListConfigKey,
PlayableListContextKey,
PlayableListSortFieldKey,
2024-06-03 07:16:29 +00:00
PlayablesKey,
SelectedPlayablesKey,
SongListSortOrderKey,
2024-03-25 22:59:38 +00:00
} from '@/symbols'
import { screen } from '@testing-library/vue'
2022-07-21 07:54:36 +00:00
import SongList from './SongList.vue'
2024-05-19 05:49:42 +00:00
let songs: Playable[]
2022-07-21 07:54:36 +00:00
new class extends UnitTestCase {
2024-04-23 21:01:27 +00:00
protected test () {
it('renders', async () => {
const { html } = await this.renderComponent(factory('song', 5))
2024-04-23 21:01:27 +00:00
expect(html()).toMatchSnapshot()
})
2024-05-19 05:49:42 +00:00
it.each<[PlayableListSortField, string]>([
2024-04-23 21:01:27 +00:00
['track', 'header-track-number'],
['title', 'header-title'],
['album_name', 'header-album'],
['length', 'header-length'],
2024-04-23 21:01:27 +00:00
])('sorts by %s upon %s clicked', async (field, testId) => {
const { emitted } = await this.renderComponent(factory('song', 5))
2024-04-23 21:01:27 +00:00
await this.user.click(screen.getByTestId(testId))
expect(emitted().sort[0]).toEqual([field, 'desc'])
await this.user.click(screen.getByTestId(testId))
expect(emitted().sort[1]).toEqual([field, 'asc'])
})
it('cannot be sorted if configured so', async () => {
const { emitted } = await this.renderComponent(factory('song', 5), {
2024-04-23 21:01:27 +00:00
sortable: false,
reorderable: true,
2024-04-23 21:01:27 +00:00
})
await this.user.click(screen.getByTestId('header-track-number'))
expect(emitted().sort).toBeUndefined()
})
}
private async renderComponent (
2024-05-19 05:49:42 +00:00
_songs: MaybeArray<Playable>,
config: Partial<PlayableListConfig> = {
sortable: true,
reorderable: true,
},
2024-05-19 05:49:42 +00:00
context: PlayableListContext = {
2024-03-25 22:59:38 +00:00
type: 'Album',
},
2024-05-19 05:49:42 +00:00
selectedPlayables: Playable[] = [],
sortField: PlayableListSortField = 'title',
sortOrder: SortOrder = 'asc',
2022-07-21 07:54:36 +00:00
) {
songs = arrayify(_songs)
const sortFieldRef = ref(sortField)
const sortOrderRef = ref(sortOrder)
await this.router.activateRoute({
screen: 'Songs',
path: '/songs',
})
2022-07-21 07:54:36 +00:00
return this.render(SongList, {
global: {
stubs: {
VirtualScroller: this.stub('virtual-scroller'),
SongListSorter: this.stub('song-list-sorter'),
2022-07-21 07:54:36 +00:00
},
provide: {
2024-05-19 05:49:42 +00:00
[<symbol>PlayablesKey]: [ref(songs)],
[<symbol>SelectedPlayablesKey]: [ref(selectedPlayables), (value: Playable[]) => (selectedPlayables = value)],
2024-05-19 05:49:42 +00:00
[<symbol>PlayableListConfigKey]: [config],
[<symbol>PlayableListContextKey]: [context],
[<symbol>PlayableListSortFieldKey]: [sortFieldRef, (value: PlayableListSortField) => (sortFieldRef.value = value)],
[<symbol>SongListSortOrderKey]: [sortOrderRef, (value: SortOrder) => (sortOrderRef.value = value)],
},
},
2022-07-21 07:54:36 +00:00
})
}
}