koel/resources/assets/js/components/ui/YouTubeVideoList.spec.ts

49 lines
1.4 KiB
TypeScript
Raw Normal View History

import { expect, it } from 'vitest'
import factory from '@/__tests__/factory'
2022-05-13 17:58:38 +00:00
import UnitTestCase from '@/__tests__/UnitTestCase'
2022-07-22 15:03:45 +00:00
import { youTubeService } from '@/services'
import { fireEvent, waitFor } from '@testing-library/vue'
import Btn from '@/components/ui/Btn.vue'
import YouTubeVideo from '@/components/ui/YouTubeVideoItem.vue'
2022-07-22 15:03:45 +00:00
import YouTubeVideoList from './YouTubeVideoList.vue'
2022-05-13 17:58:38 +00:00
new class extends UnitTestCase {
protected test () {
2022-07-22 15:03:45 +00:00
it('functions', async () => {
const song = factory<Song>('song')
const searchMock = this.mock(youTubeService, 'searchVideosBySong').mockResolvedValueOnce({
nextPageToken: 'foo',
items: factory<YouTubeVideo>('video', 5)
2022-07-22 15:03:45 +00:00
}).mockResolvedValueOnce({
nextPageToken: 'bar',
items: factory<YouTubeVideo>('video', 3)
})
2022-07-22 15:03:45 +00:00
const { getAllByTestId, getByRole } = this.render(YouTubeVideoList, {
props: {
song
},
global: {
stubs: {
Btn,
YouTubeVideo
}
}
})
2022-07-22 15:03:45 +00:00
await waitFor(() => {
expect(searchMock).toHaveBeenNthCalledWith(1, song, '')
expect(getAllByTestId('youtube-video')).toHaveLength(5)
})
2022-07-22 15:03:45 +00:00
await fireEvent.click(getByRole('button', { name: 'Load More' }))
2022-07-22 15:03:45 +00:00
await waitFor(() => {
expect(searchMock).toHaveBeenNthCalledWith(2, song, 'foo')
expect(getAllByTestId('youtube-video')).toHaveLength(8)
})
})
}
}