Refactor tests

This commit is contained in:
Phan An 2017-12-19 23:45:11 +01:00
parent c48e09f4c3
commit 21b57ba1ef
17 changed files with 82 additions and 113 deletions

View file

@ -9,8 +9,7 @@ import { songInfo } from '@/services'
describe('components/main-wrapper/extra/index', () => {
it('shows by default', () => {
const wrapper = shallow(ExtraSidebar)
wrapper.findAll('#extra.showing').should.have.lengthOf(1)
shallow(ExtraSidebar).findAll('#extra.showing').should.have.lengthOf(1)
})
it('has a YouTube tab if using YouTube', () => {
@ -33,10 +32,11 @@ describe('components/main-wrapper/extra/index', () => {
})
it('has proper child components', () => {
const wrapper = shallow(ExtraSidebar)
wrapper.setData({
song: factory('song'),
sharedState: { useYouTube: true }
const wrapper = shallow(ExtraSidebar, {
data: {
song: factory('song'),
sharedState: { useYouTube: true }
}
})
;[ArtistInfo, AlbumInfo, Lyrics, YouTube].forEach(component => {
wrapper.contains(component).should.be.true
@ -44,7 +44,7 @@ describe('components/main-wrapper/extra/index', () => {
})
it('fetch song info when a new song is played', () => {
const wrapper = shallow(ExtraSidebar)
shallow(ExtraSidebar)
const song = factory('song')
const fetchSongInfoStub = sinon.stub(songInfo, 'fetch').callsFake(() => song)
event.emit('song:played', song)

View file

@ -4,18 +4,16 @@ import factory from '@/tests/factory'
describe('components/main-wrapper/extra/lyrics', () => {
it('displays lyrics if the song has lyrics', () => {
const song = factory('song')
const wrapper = shallow(Lyrics, {
shallow(Lyrics, {
propsData: { song }
})
wrapper.html().should.contain(song.lyrics)
}).html().should.contain(song.lyrics)
})
it('displays a fallback message if the song has no lyrics', () => {
const wrapper = shallow(Lyrics, {
shallow(Lyrics, {
propsData: {
song: factory('song', { lyrics: '' })
}
})
wrapper.html().should.contain('No lyrics found. Are you not listening to Bach?')
}).html().should.contain('No lyrics found. Are you not listening to Bach?')
})
})

View file

@ -8,10 +8,10 @@ describe('components/main-wrapper/extra/youtube', () => {
beforeEach(() => {
song = factory('song')
wrapper = shallow(YouTube, {
propsData: { song }
})
wrapper.setData({
videos: factory('video', 5)
propsData: { song },
data: {
videos: factory('video', 5)
}
})
})

View file

@ -20,11 +20,12 @@ describe('components/main-wrapper/main-content/album', () => {
})
it('loads info from Last.fm', () => {
const wrapper = shallow(Component)
const album = factory('album', { info: null })
wrapper.setData({
album,
sharedState: { useLastfm: true }
const wrapper = shallow(Component, {
data: {
album,
sharedState: { useLastfm: true }
}
})
const stub = sinon.stub(albumInfoService, 'fetch')
wrapper.find('a.info').trigger('click')
@ -33,11 +34,12 @@ describe('components/main-wrapper/main-content/album', () => {
})
it('allows downloading', () => {
const wrapper = shallow(Component)
const album = factory('album')
wrapper.setData({
album,
sharedState: { allowDownload: true }
const wrapper = shallow(Component, {
data: {
album,
sharedState: { allowDownload: true }
}
})
const downloadStub = sinon.stub(download, 'fromAlbum')
wrapper.find('a.download').trigger('click')

View file

@ -4,10 +4,8 @@ import factory from '@/tests/factory'
describe('components/main-wrapper/main-content/albums', () => {
it('displays a list of albums', () => {
const wrapper = shallow(Albums)
wrapper.setData({
shallow(Albums, { data: {
albums: factory('album', 5)
})
wrapper.findAll(AlbumItem).should.have.lengthOf(5)
}}).findAll(AlbumItem).should.have.lengthOf(5)
})
})

View file

@ -36,12 +36,11 @@ describe('components/main-wrapper/main-content/artist', () => {
})
it('loads info from Last.fm', () => {
const wrapper = shallow(Component)
artist.info = null
wrapper.setData({
const wrapper = shallow(Component, { data: {
artist,
sharedState: { useLastfm: true }
})
}})
const stub = sinon.stub(artistInfoService, 'fetch')
wrapper.find('a.info').trigger('click')
stub.calledWith(artist).should.be.true
@ -49,11 +48,10 @@ describe('components/main-wrapper/main-content/artist', () => {
})
it('allows downloading', () => {
const wrapper = shallow(Component)
wrapper.setData({
const wrapper = shallow(Component, { data: {
artist,
sharedState: { allowDownload: true }
})
}})
const downloadStub = sinon.stub(download, 'fromArtist')
wrapper.find('a.download').trigger('click')
downloadStub.calledWith(artist).should.be.true

View file

@ -4,10 +4,8 @@ import factory from '@/tests/factory'
describe('components/main-wrapper/main-content/artists', () => {
it('displays a list of artists', () => {
const wrapper = shallow(Artists)
wrapper.setData({
shallow(Artists, { data: {
artists: factory('artist', 5)
})
wrapper.findAll(ArtistItem).should.have.lengthOf(5)
}}).findAll(ArtistItem).should.have.lengthOf(5)
})
})

View file

@ -6,37 +6,32 @@ import factory from '@/tests/factory'
describe('components/main-wrapper/main-content/favorites', () => {
it('displays the song list if there are favorites', () => {
const wrapper = shallow(Component)
wrapper.setData({
const wrapper = shallow(Component, { data: {
state: {
songs: factory('song', 5)
}
})
}})
wrapper.contains(SongList).should.be.true
wrapper.contains(SongListControls).should.be.true
wrapper.findAll('div.none').should.have.lengthOf(0)
})
it('displays a fallback message if there are no favorites', () => {
const wrapper = shallow(Component)
wrapper.setData({
shallow(Component, { data: {
state: {
songs: []
}
})
wrapper.findAll('div.none').should.have.lengthOf(1)
}}).findAll('div.none').should.have.lengthOf(1)
})
it('allows downloading', () => {
const wrapper = shallow(Component)
wrapper.setData({
const downloadStub = sinon.stub(download, 'fromFavorites')
shallow(Component, { data: {
state: {
songs: factory('song', 5)
},
sharedState: { allowDownload: true }
})
const downloadStub = sinon.stub(download, 'fromFavorites')
wrapper.find('a.download').trigger('click')
}}).find('a.download').trigger('click')
downloadStub.called.should.be.true
downloadStub.restore()
})

View file

@ -28,8 +28,7 @@ describe('components/main-wrapper/main-content/home', () => {
})
it('displays all sections', () => {
const wrapper = mount(Home)
wrapper.setData(data)
const wrapper = mount(Home, { data })
wrapper.find('h1.heading span').text().should.not.be.empty
wrapper.find('.top-song-list').findAll(HomeSongItem).should.have.lengthOf(4)

View file

@ -7,15 +7,14 @@ import { playlistStore } from '@/stores'
describe('components/main-wrapper/main-content/playlist', () => {
it('renders properly', () => {
const playlist = factory('playlist', { populated: true })
const wrapper = shallow(Component)
wrapper.setData({ playlist })
const wrapper = shallow(Component, { data: { playlist }})
wrapper.find('h1.heading').html().should.contain(playlist.name)
wrapper.contains(SongList).should.be.true
})
it('fetch and populate playlist content on demand', () => {
const playlist = factory('playlist', { songs: [] })
const wrapper = shallow(Component)
shallow(Component)
const fetchSongsStub = sinon.stub(playlistStore, 'fetchSongs')
event.emit('main-content-view:load', 'playlist', playlist)
fetchSongsStub.calledWith(playlist).should.be.true
@ -23,13 +22,12 @@ describe('components/main-wrapper/main-content/playlist', () => {
})
it('displays a fallback message if the playlist is empty', () => {
const wrapper = shallow(Component)
const playlist = factory('playlist', {
populated: true,
songs: []
})
wrapper.setData({ playlist })
wrapper.contains('div.none').should.be.true
shallow(Component, { data: {
playlist: factory('playlist', {
populated: true,
songs: []
})
}}).contains('div.none').should.be.true
})
it('confirms deleting if the playlist is not empty', () => {

View file

@ -8,16 +8,14 @@ describe('components/main-wrapper/main-content/user', () => {
})
it('displays a form to update profile', () => {
const wrapper = shallow(Profile)
wrapper.contains('form').should.be.true
shallow(Profile).contains('form').should.be.true
})
it('validates password confirmation', () => {
const wrapper = shallow(Profile)
wrapper.setData({
const wrapper = shallow(Profile, { data: {
pwd: 'foo',
confirmPwd: 'bar'
})
}})
const updateProfileStub = sinon.stub(userStore, 'updateProfile')
wrapper.find('form').trigger('submit')
updateProfileStub.called.should.be.false

View file

@ -6,52 +6,47 @@ import { playback } from '@/services'
describe('components/main-wrapper/main-content/queue', () => {
it('renders properly', () => {
const wrapper = shallow(Component)
wrapper.setData({
const wrapper = shallow(Component, { data: {
state: { songs: factory('song', 10) }
})
}})
wrapper.find('h1.heading').text().should.contain('Current Queue')
wrapper.contains(SongList).should.be.true
})
it('prompts to shuffle all songs if there are songs and current queue is empty', () => {
songStore.state.songs = factory('song', 10)
const wrapper = shallow(Component)
wrapper.setData({
shallow(Component, { data: {
state: { songs: [] }
})
wrapper.find('a.start').text().should.contain('shuffling all songs')
}}).find('a.start').text().should.contain('shuffling all songs')
})
it("doesn't prompt to shuffle all songs if there is no song", () => {
songStore.state.songs = []
const wrapper = shallow(Component)
wrapper.setData({
state: { songs: [] }
})
wrapper.findAll('a.start').should.have.lengthOf(0)
shallow(Component, { data: {
state: {
songs: []
}
}}).contains('a.start').should.be.false
})
it('shuffles all songs in the queue if any', () => {
const stub = sinon.stub(playback, 'queueAndPlay')
const wrapper = mount(Component)
const songs = factory('song', 10)
wrapper.setData({
mount(Component, { data: {
state: { songs }
})
wrapper.find('button.btn-shuffle-all').trigger('click')
}}).find('button.btn-shuffle-all').trigger('click')
stub.calledWith(songs).should.be.true
stub.restore()
})
it('shuffles all available songs if there are no songs queued', () => {
const stub = sinon.stub(playback, 'queueAndPlay')
const wrapper = mount(Component)
songStore.state.songs = factory('song', 10)
wrapper.setData({
state: { songs: [] }
})
wrapper.find('button.btn-shuffle-all').trigger('click')
mount(Component, { data: {
state: {
songs: []
}
}}).find('button.btn-shuffle-all').trigger('click')
stub.calledWith(songStore.all).should.be.true
stub.restore()
})

View file

@ -12,25 +12,22 @@ describe('components/main-wrapper/main-content/settings', () => {
})
it('renders a settings form', () => {
const wrapper = shallow(Component)
wrapper.findAll('form').should.have.lengthOf(1)
shallow(Component).findAll('form').should.have.lengthOf(1)
})
it('warns if changing a non-empty media path', () => {
sharedStore.state.originalMediaPath = '/bar'
const wrapper = shallow(Component)
const stub = sinon.stub(alerts, 'confirm')
wrapper.find('form').trigger('submit')
shallow(Component).find('form').trigger('submit')
stub.called.should.be.true
stub.restore()
})
it("doesn't warn if changing an empty media path", () => {
sharedStore.state.originalMediaPath = ''
const wrapper = shallow(Component)
const confirmStub = sinon.stub(alerts, 'confirm')
const updateStub = sinon.stub(settingStore, 'update')
wrapper.find('form').trigger('submit')
shallow(Component).find('form').trigger('submit')
confirmStub.called.should.be.false
updateStub.called.should.be.true
confirmStub.restore()

View file

@ -8,8 +8,7 @@ import { userStore } from '@/stores'
describe('components/main-wrapper/main-content/users', () => {
it('displays the users', () => {
userStore.all = factory('user', 10)
const wrapper = mount(Component)
wrapper.findAll(UserItem).should.have.lengthOf(10)
mount(Component).findAll(UserItem).should.have.lengthOf(10)
})
it('adds new user', () => {

View file

@ -3,7 +3,6 @@ import { event } from '@/utils'
describe('components/main-wrapper/main-content/youtube-player', () => {
it('renders properly', () => {
const wrapper = mount(Component)
wrapper.find('h1.heading').text().should.contain('YouTube Video')
mount(Component).find('h1.heading').text().should.contain('YouTube Video')
})
})

View file

@ -14,17 +14,15 @@ describe('compoponents/main-wrapper/sidebar/index', () => {
it('displays YouTube menu item if using YouTube', () => {
sharedStore.state.useYouTube = true
const wrapper = shallow(Component)
wrapper.contains('a.youtube').should.be.true
shallow(Component).contains('a.youtube').should.be.true
})
it('displays management menu items for admin', () => {
const wrapper = shallow(Component)
wrapper.setData({
const wrapper = shallow(Component, { data: {
userState: {
current: factory('user', { is_admin: true })
}
})
}})
;['settings', 'users'].forEach(item => {
wrapper.contains(`.menu a.${item}`).should.be.true
})
@ -33,12 +31,11 @@ describe('compoponents/main-wrapper/sidebar/index', () => {
it('displays new version info', () => {
sharedStore.state.currentVersion = 'v0.0.0'
sharedStore.state.latestVersion = 'v0.0.1'
const wrapper = shallow(Component)
wrapper.setData({
const wrapper = shallow(Component, { data: {
userState: {
current: factory('user', { is_admin: true })
}
})
}})
wrapper.contains('a.new-ver').should.be.true
wrapper.find('a.new-ver').text().should.contain('Koel version v0.0.1 is available!')
})

View file

@ -12,22 +12,20 @@ describe('component/main-wrapper/sidebar/playlist-item', () => {
})
it('renders a playlist menu item', () => {
const wrapper = shallow(Component, {
shallow(Component, {
propsData: { playlist }
})
wrapper.find('a[href="#!/playlist/99"]').text().should.equal('Foo')
}).find('a[href="#!/playlist/99"]').text().should.equal('Foo')
})
it('renders the Favorites menu item', () => {
const wrapper = shallow(Component, {
shallow(Component, {
propsData: {
playlist: {
name: 'Favorites'
},
type: 'favorites'
}
})
wrapper.find('a[href="#!/favorites"]').text().should.equal('Favorites')
}).find('a[href="#!/favorites"]').text().should.equal('Favorites')
})
it('edits a playlist', () => {