fix: prevent real requests in unit tests (#1542)

This commit is contained in:
Phan An 2022-10-22 11:27:03 +02:00 committed by GitHub
parent 98a6d9b4c4
commit 2349995388
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 31 deletions

View file

@ -10,20 +10,26 @@ import AlbumArtOverlay from '@/components/ui/AlbumArtOverlay.vue'
import MainContent from './MainContent.vue'
new class extends UnitTestCase {
private renderComponent (hasCurrentSong = false) {
return this.render(MainContent, {
global: {
provide: {
[CurrentSongKey]: ref(factory<Song>('song'))
},
stubs: {
AlbumArtOverlay,
HomeScreen: this.stub(), // so that home overview requests are not made
Visualizer: this.stub('visualizer'),
},
}
})
}
protected test () {
it('has a translucent overlay per album', async () => {
this.mock(albumStore, 'fetchThumbnail').mockResolvedValue('http://test/foo.jpg')
const { getByTestId } = this.render(MainContent, {
global: {
stubs: {
AlbumArtOverlay
},
provide: {
[CurrentSongKey]: ref(factory<Song>('song'))
}
}
})
const { getByTestId } = this.renderComponent()
await waitFor(() => getByTestId('album-art-overlay'))
})
@ -31,28 +37,13 @@ new class extends UnitTestCase {
it('does not have a translucent over if configured not so', async () => {
preferenceStore.state.showAlbumArtOverlay = false
const { queryByTestId } = this.render(MainContent, {
global: {
stubs: {
AlbumArtOverlay
},
provide: {
[CurrentSongKey]: ref(factory<Song>('song'))
}
}
})
const { queryByTestId } = this.renderComponent()
await waitFor(() => expect(queryByTestId('album-art-overlay')).toBeNull())
})
it('toggles visualizer', async () => {
const { getByTestId, queryByTestId } = this.render(MainContent, {
global: {
stubs: {
Visualizer: this.stub('visualizer')
}
}
})
const { getByTestId, queryByTestId } = this.renderComponent()
eventBus.emit('TOGGLE_VISUALIZER')
await waitFor(() => getByTestId('visualizer'))

View file

@ -15,7 +15,10 @@ new class extends UnitTestCase {
protected test () {
it('renders an empty state if no songs found', async () => {
commonStore.state.song_length = 0
this.mock(overviewStore, 'init')
const { getByTestId } = await this.render(HomeScreen)
getByTestId('screen-empty-state')
})

View file

@ -37,9 +37,10 @@
import { faVolumeOff } from '@fortawesome/free-solid-svg-icons'
import { sample } from 'lodash'
import { computed, ref } from 'vue'
import { eventBus, noop } from '@/utils'
import { eventBus, logger, noop, requireInjection } from '@/utils'
import { commonStore, overviewStore, userStore } from '@/stores'
import { useAuthorization, useInfiniteScroll, useScreen } from '@/composables'
import { DialogBoxKey } from '@/symbols'
import MostPlayedSongs from '@/components/screens/home/MostPlayedSongs.vue'
import RecentlyPlayedSongs from '@/components/screens/home/RecentlyPlayedSongs.vue'
@ -54,6 +55,8 @@ const { ToTopButton, scrolling } = useInfiniteScroll(() => noop())
const { isAdmin } = useAuthorization()
const dialog = requireInjection(DialogBoxKey)
const greetings = [
'Oh hai!',
'Hey, %s!',
@ -77,9 +80,15 @@ eventBus.on(['SONGS_DELETED', 'SONGS_UPDATED'], () => overviewStore.refresh())
useScreen('Home').onScreenActivated(async () => {
if (!initialized) {
loading.value = true
await overviewStore.init()
initialized = true
loading.value = false
try {
await overviewStore.init()
initialized = true
} catch (e) {
dialog.value.error('Failed to load home screen data. Please try again.')
logger.error(e)
} finally {
loading.value = false
}
}
})
</script>