fix: actual HTTP requests in tests (#1522)

This commit is contained in:
Phan An 2022-10-09 12:53:10 +02:00 committed by GitHub
parent 63a66bc511
commit e500329465
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 47 additions and 26 deletions

View file

@ -5,6 +5,7 @@ import { afterEach, beforeEach, vi } from 'vitest'
import { clickaway, focus } from '@/directives'
import { defineComponent, nextTick } from 'vue'
import { commonStore, userStore } from '@/stores'
import { http } from '@/services'
import factory from '@/__tests__/factory'
import { DialogBoxKey, MessageToasterKey, RouterKey } from '@/symbols'
import { DialogBoxStub, MessageToasterStub } from '@/__tests__/stubs'
@ -30,6 +31,7 @@ export default abstract class UnitTestCase {
public constructor () {
this.router = new Router(routes)
this.mock(http, 'request') // prevent actual HTTP requests from being made
this.beforeEach()
this.afterEach()
this.test()

View file

@ -1,5 +1,12 @@
declare global {
interface Window {
BASE_URL: string;
}
}
import vueSnapshotSerializer from 'jest-serializer-vue'
import { expect, vi } from 'vitest'
import Axios from 'axios'
expect.addSnapshotSerializer(vueSnapshotSerializer)
@ -14,4 +21,6 @@ window.HTMLMediaElement.prototype.load = vi.fn()
window.HTMLMediaElement.prototype.play = vi.fn()
window.HTMLMediaElement.prototype.pause = vi.fn()
window.BASE_URL = 'http://localhost/'
window.BASE_URL = 'http://test/'
Axios.defaults.adapter = vi.fn()

View file

@ -10,7 +10,7 @@ import MainContent from './MainContent.vue'
new class extends UnitTestCase {
protected test () {
it('has a translucent overlay per album', async () => {
this.mock(albumStore, 'fetchThumbnail').mockResolvedValue('http://localhost/foo.jpg')
this.mock(albumStore, 'fetchThumbnail').mockResolvedValue('http://test/foo.jpg')
const { getByTestId } = this.render(MainContent, {
global: {

View file

@ -21,9 +21,12 @@ new class extends UnitTestCase {
it('renders overview components if applicable', async () => {
commonStore.state.song_length = 100
const initMock = this.mock(overviewStore, 'init')
const { getByTestId, queryByTestId } = await this.renderComponent()
expect(initMock).toHaveBeenCalled()
;[
'most-played-songs',
'recently-played-songs',
@ -38,11 +41,13 @@ new class extends UnitTestCase {
it.each<[EventName]>([['SONGS_UPDATED'], ['SONGS_DELETED']])
('refreshes the overviews on %s event', async (eventName) => {
const initMock = this.mock(overviewStore, 'init')
const refreshMock = this.mock(overviewStore, 'refresh')
await this.renderComponent()
eventBus.emit(eventName)
expect(initMock).toHaveBeenCalled()
expect(refreshMock).toHaveBeenCalled()
})
}

View file

@ -75,6 +75,10 @@ let initialized = false
eventBus.on(['SONGS_DELETED', 'SONGS_UPDATED'], () => overviewStore.refresh())
useScreen('Home').onScreenActivated(async () => {
if (libraryEmpty.value) {
return
}
if (!initialized) {
loading.value = true
await overviewStore.init()

View file

@ -39,7 +39,7 @@ new class extends UnitTestCase {
title: 'Rocket to Heaven',
artist_name: 'Led Zeppelin',
album_name: 'IV',
album_cover: 'http://localhost/album.jpg',
album_cover: 'http://test/album.jpg',
genre: 'Rock'
}))

View file

@ -3,7 +3,7 @@
exports[`edits a single song 1`] = `
<div class="edit-song" data-testid="edit-song-form" tabindex="0" data-v-210b4214="">
<form data-v-210b4214="">
<header data-v-210b4214=""><span class="cover" style="background-image: url(http://localhost/album.jpg);" data-v-210b4214=""></span>
<header data-v-210b4214=""><span class="cover" style="background-image: url(http://test/album.jpg);" data-v-210b4214=""></span>
<div class="meta" data-v-210b4214="">
<h1 class="" data-v-210b4214="">Rocket to Heaven</h1>
<h2 data-testid="displayed-artist-name" class="" data-v-210b4214="">Led Zeppelin</h2>

View file

@ -23,7 +23,7 @@ new class extends UnitTestCase {
protected test () {
it('fetches and displays the album thumbnail', async () => {
const fetchMock = this.mock(albumStore, 'fetchThumbnail').mockResolvedValue('http://localhost/thumb.jpg')
const fetchMock = this.mock(albumStore, 'fetchThumbnail').mockResolvedValue('http://test/thumb.jpg')
const { html } = await this.renderComponent()

View file

@ -14,7 +14,7 @@ new class extends UnitTestCase {
private renderForAlbum () {
album = factory<Album>('album', {
name: 'IV',
cover: 'https://localhost/album.jpg'
cover: 'https://test/album.jpg'
})
return this.render(Thumbnail, {
@ -27,7 +27,7 @@ new class extends UnitTestCase {
private renderForArtist () {
artist = factory<Artist>('artist', {
name: 'Led Zeppelin',
image: 'https://localhost/blimp.jpg'
image: 'https://test/blimp.jpg'
})
return this.render(Thumbnail, {

View file

@ -2,6 +2,6 @@
exports[`displays nothing if fetching fails 1`] = `<div style="background-image: none;" data-testid="album-art-overlay" data-v-75d06710=""></div>`;
exports[`fetches and displays the album thumbnail 1`] = `<div style="background-image: url(https://localhost/thumb.jpg);" data-testid="album-art-overlay" data-v-75d06710=""></div>`;
exports[`fetches and displays the album thumbnail 1`] = `<div style="background-image: url(https://test/thumb.jpg);" data-testid="album-art-overlay" data-v-75d06710=""></div>`;
exports[`fetches and displays the album thumbnail 2`] = `<div style="background-image: url(http://localhost/thumb.jpg);" data-testid="album-art-overlay" data-v-75d06710=""></div>`;
exports[`fetches and displays the album thumbnail 2`] = `<div style="background-image: url(http://test/thumb.jpg);" data-testid="album-art-overlay" data-v-75d06710=""></div>`;

View file

@ -134,6 +134,8 @@ new class extends UnitTestCase {
])(
'when next song preloaded is %s, isTranscoding is %s, current media time is %d, media duration is %d, then preload() should be called %d times',
(preloaded, isTranscoding, currentTime, duration, numberOfCalls) => {
this.mock(playbackService, 'registerPlay')
this.setReadOnlyProperty(queueStore, 'next', factory<Song>('song', { preloaded }))
this.setReadOnlyProperty(playbackService, 'isTranscoding', isTranscoding)
playbackService.init()
@ -149,7 +151,6 @@ new class extends UnitTestCase {
expect(preloadMock).toHaveBeenCalledTimes(numberOfCalls)
}
)
it('registers play', () => {
const recentlyPlayedStoreAddMock = this.mock(recentlyPlayedStore, 'add')
const registerPlayMock = this.mock(songStore, 'registerPlay')

View file

@ -57,25 +57,25 @@ new class extends UnitTestCase {
const album = factory<Album>('album')
albumStore.syncWithVault(album)
const songsInAlbum = factory<Song>('song', 3, { album_id: album.id })
const putMock = this.mock(http, 'put').mockResolvedValue({ coverUrl: 'http://localhost/cover.jpg' })
const putMock = this.mock(http, 'put').mockResolvedValue({ coverUrl: 'http://test/cover.jpg' })
this.mock(songStore, 'byAlbum', songsInAlbum)
await albumStore.uploadCover(album, 'data://cover')
expect(album.cover).toBe('http://localhost/cover.jpg')
expect(album.cover).toBe('http://test/cover.jpg')
expect(putMock).toHaveBeenCalledWith(`album/${album.id}/cover`, { cover: 'data://cover' })
expect(albumStore.byId(album.id)?.cover).toBe('http://localhost/cover.jpg')
songsInAlbum.forEach(song => expect(song.album_cover).toBe('http://localhost/cover.jpg'))
expect(albumStore.byId(album.id)?.cover).toBe('http://test/cover.jpg')
songsInAlbum.forEach(song => expect(song.album_cover).toBe('http://test/cover.jpg'))
})
it('fetches an album thumbnail', async () => {
const getMock = this.mock(http, 'get').mockResolvedValue({ thumbnailUrl: 'http://localhost/thumbnail.jpg' })
const getMock = this.mock(http, 'get').mockResolvedValue({ thumbnailUrl: 'http://test/thumbnail.jpg' })
const album = factory<Album>('album')
const url = await albumStore.fetchThumbnail(album.id)
expect(getMock).toHaveBeenCalledWith(`album/${album.id}/thumbnail`)
expect(url).toBe('http://localhost/thumbnail.jpg')
expect(url).toBe('http://test/thumbnail.jpg')
})
it('resolves an album', async () => {

View file

@ -70,13 +70,13 @@ new class extends UnitTestCase {
it('uploads an image for an artist', async () => {
const artist = factory<Artist>('artist')
artistStore.syncWithVault(artist)
const putMock = this.mock(http, 'put').mockResolvedValue({ imageUrl: 'http://localhost/img.jpg' })
const putMock = this.mock(http, 'put').mockResolvedValue({ imageUrl: 'http://test/img.jpg' })
await artistStore.uploadImage(artist, 'data://image')
expect(artist.image).toBe('http://localhost/img.jpg')
expect(artist.image).toBe('http://test/img.jpg')
expect(putMock).toHaveBeenCalledWith(`artist/${artist.id}/image`, { image: 'data://image' })
expect(artistStore.byId(artist.id)?.image).toBe('http://localhost/img.jpg')
expect(artistStore.byId(artist.id)?.image).toBe('http://test/img.jpg')
})
it('resolves an artist', async () => {

View file

@ -105,13 +105,13 @@ new class extends UnitTestCase {
id: 10,
artist_id: 3,
name: 'Removed Album',
cover: 'http://localhost/removed-album.jpg',
cover: 'http://test/removed-album.jpg',
created_at: '2020-01-01'
}],
artists: [{
id: 42,
name: 'Removed Artist',
image: 'http://localhost/removed-artist.jpg',
image: 'http://test/removed-artist.jpg',
created_at: '2020-01-01'
}]
}
@ -145,20 +145,20 @@ new class extends UnitTestCase {
})
it('gets source URL', () => {
commonStore.state.cdn_url = 'http://localhost/'
commonStore.state.cdn_url = 'http://test/'
const song = factory<Song>('song', { id: 'foo' })
this.mock(authService, 'getToken', 'hadouken')
expect(songStore.getSourceUrl(song)).toBe('http://localhost/play/foo?api_token=hadouken')
expect(songStore.getSourceUrl(song)).toBe('http://test/play/foo?api_token=hadouken')
isMobile.any = true
preferenceStore.transcodeOnMobile = true
expect(songStore.getSourceUrl(song)).toBe('http://localhost/play/foo/1/128?api_token=hadouken')
expect(songStore.getSourceUrl(song)).toBe('http://test/play/foo/1/128?api_token=hadouken')
})
it('gets shareable URL', () => {
const song = factory<Song>('song', { id: 'foo' })
expect(songStore.getShareableUrl(song)).toBe('http://localhost/#/song/foo')
expect(songStore.getShareableUrl(song)).toBe('http://test/#/song/foo')
})
it('syncs with the vault', () => {
@ -248,7 +248,7 @@ new class extends UnitTestCase {
const getMock = this.mock(http, 'get').mockResolvedValueOnce({
data: songs,
links: {
next: 'http://localhost/api/v1/songs?page=3'
next: 'http://test/api/v1/songs?page=3'
},
meta: {
current_page: 2