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 {
|
2024-05-19 05:49:42 +00:00
|
|
|
SelectedPlayablesKey,
|
|
|
|
PlayableListConfigKey,
|
|
|
|
PlayableListContextKey,
|
|
|
|
PlayableListSortFieldKey,
|
2024-03-25 22:59:38 +00:00
|
|
|
SongListSortOrderKey,
|
2024-05-19 05:49:42 +00:00
|
|
|
PlayablesKey
|
2024-03-25 22:59:38 +00:00
|
|
|
} 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'
|
|
|
|
|
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 () => {
|
2024-06-01 18:02:27 +00:00
|
|
|
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']
|
|
|
|
])('sorts by %s upon %s clicked', async (field, testId) => {
|
2024-06-01 18:02:27 +00:00
|
|
|
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 () => {
|
2024-06-01 18:02:27 +00:00
|
|
|
const { emitted } = await this.renderComponent(factory('song', 5), {
|
2024-04-23 21:01:27 +00:00
|
|
|
sortable: false,
|
|
|
|
reorderable: true
|
|
|
|
})
|
|
|
|
|
|
|
|
await this.user.click(screen.getByTestId('header-track-number'))
|
|
|
|
expect(emitted().sort).toBeUndefined()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-11-12 21:38:31 +00:00
|
|
|
private async renderComponent (
|
2024-05-19 05:49:42 +00:00
|
|
|
_songs: MaybeArray<Playable>,
|
|
|
|
config: Partial<PlayableListConfig> = {
|
2022-11-12 21:38:31 +00:00
|
|
|
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',
|
2022-07-21 07:54:36 +00:00
|
|
|
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: {
|
2024-05-19 05:49:42 +00:00
|
|
|
[<symbol>PlayablesKey]: [ref(songs)],
|
2024-06-02 17:15:31 +00:00
|
|
|
[<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)],
|
2024-03-25 22:59:38 +00:00
|
|
|
[<symbol>SongListSortOrderKey]: [sortOrderRef, (value: SortOrder) => (sortOrderRef.value = value)]
|
2022-07-21 07:54:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|