From 21b57ba1efb4815d2fc1bf21002ca494b2925481 Mon Sep 17 00:00:00 2001 From: Phan An Date: Tue, 19 Dec 2017 23:45:11 +0100 Subject: [PATCH] Refactor tests --- .../main-wrapper/extra/index.spec.js | 14 +++---- .../main-wrapper/extra/lyrics.spec.js | 10 ++--- .../main-wrapper/extra/youtube.spec.js | 8 ++-- .../main-wrapper/main-content/album.spec.js | 18 +++++---- .../main-wrapper/main-content/albums.spec.js | 6 +-- .../main-wrapper/main-content/artist.spec.js | 10 ++--- .../main-wrapper/main-content/artists.spec.js | 6 +-- .../main-content/favorites.spec.js | 19 ++++------ .../main-wrapper/main-content/home.spec.js | 3 +- .../main-content/playlist.spec.js | 18 ++++----- .../main-wrapper/main-content/profile.spec.js | 8 ++-- .../main-wrapper/main-content/queue.spec.js | 37 ++++++++----------- .../main-content/settings.spec.js | 9 ++--- .../main-wrapper/main-content/users.spec.js | 3 +- .../main-content/youtube-player.spec.js | 3 +- .../main-wrapper/sidebar/index.spec.js | 13 +++---- .../sidebar/playlist-item.spec.js | 10 ++--- 17 files changed, 82 insertions(+), 113 deletions(-) diff --git a/resources/assets/js/tests/components/main-wrapper/extra/index.spec.js b/resources/assets/js/tests/components/main-wrapper/extra/index.spec.js index 4a09b8a5..d9e08963 100644 --- a/resources/assets/js/tests/components/main-wrapper/extra/index.spec.js +++ b/resources/assets/js/tests/components/main-wrapper/extra/index.spec.js @@ -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) diff --git a/resources/assets/js/tests/components/main-wrapper/extra/lyrics.spec.js b/resources/assets/js/tests/components/main-wrapper/extra/lyrics.spec.js index 799068ae..457251b8 100644 --- a/resources/assets/js/tests/components/main-wrapper/extra/lyrics.spec.js +++ b/resources/assets/js/tests/components/main-wrapper/extra/lyrics.spec.js @@ -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?') }) }) diff --git a/resources/assets/js/tests/components/main-wrapper/extra/youtube.spec.js b/resources/assets/js/tests/components/main-wrapper/extra/youtube.spec.js index df288544..0a3e24a1 100644 --- a/resources/assets/js/tests/components/main-wrapper/extra/youtube.spec.js +++ b/resources/assets/js/tests/components/main-wrapper/extra/youtube.spec.js @@ -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) + } }) }) diff --git a/resources/assets/js/tests/components/main-wrapper/main-content/album.spec.js b/resources/assets/js/tests/components/main-wrapper/main-content/album.spec.js index 2202c436..4f054d0c 100644 --- a/resources/assets/js/tests/components/main-wrapper/main-content/album.spec.js +++ b/resources/assets/js/tests/components/main-wrapper/main-content/album.spec.js @@ -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') diff --git a/resources/assets/js/tests/components/main-wrapper/main-content/albums.spec.js b/resources/assets/js/tests/components/main-wrapper/main-content/albums.spec.js index d496850f..53213545 100644 --- a/resources/assets/js/tests/components/main-wrapper/main-content/albums.spec.js +++ b/resources/assets/js/tests/components/main-wrapper/main-content/albums.spec.js @@ -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) }) }) diff --git a/resources/assets/js/tests/components/main-wrapper/main-content/artist.spec.js b/resources/assets/js/tests/components/main-wrapper/main-content/artist.spec.js index ca67a352..273ba089 100644 --- a/resources/assets/js/tests/components/main-wrapper/main-content/artist.spec.js +++ b/resources/assets/js/tests/components/main-wrapper/main-content/artist.spec.js @@ -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 diff --git a/resources/assets/js/tests/components/main-wrapper/main-content/artists.spec.js b/resources/assets/js/tests/components/main-wrapper/main-content/artists.spec.js index 7d108e36..085a3d91 100644 --- a/resources/assets/js/tests/components/main-wrapper/main-content/artists.spec.js +++ b/resources/assets/js/tests/components/main-wrapper/main-content/artists.spec.js @@ -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) }) }) diff --git a/resources/assets/js/tests/components/main-wrapper/main-content/favorites.spec.js b/resources/assets/js/tests/components/main-wrapper/main-content/favorites.spec.js index 0d090934..bb2a7de3 100644 --- a/resources/assets/js/tests/components/main-wrapper/main-content/favorites.spec.js +++ b/resources/assets/js/tests/components/main-wrapper/main-content/favorites.spec.js @@ -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() }) diff --git a/resources/assets/js/tests/components/main-wrapper/main-content/home.spec.js b/resources/assets/js/tests/components/main-wrapper/main-content/home.spec.js index b628b6d3..596144b9 100644 --- a/resources/assets/js/tests/components/main-wrapper/main-content/home.spec.js +++ b/resources/assets/js/tests/components/main-wrapper/main-content/home.spec.js @@ -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) diff --git a/resources/assets/js/tests/components/main-wrapper/main-content/playlist.spec.js b/resources/assets/js/tests/components/main-wrapper/main-content/playlist.spec.js index 7f3fecad..ab44e011 100644 --- a/resources/assets/js/tests/components/main-wrapper/main-content/playlist.spec.js +++ b/resources/assets/js/tests/components/main-wrapper/main-content/playlist.spec.js @@ -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', () => { diff --git a/resources/assets/js/tests/components/main-wrapper/main-content/profile.spec.js b/resources/assets/js/tests/components/main-wrapper/main-content/profile.spec.js index 52b71f47..80499c67 100644 --- a/resources/assets/js/tests/components/main-wrapper/main-content/profile.spec.js +++ b/resources/assets/js/tests/components/main-wrapper/main-content/profile.spec.js @@ -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 diff --git a/resources/assets/js/tests/components/main-wrapper/main-content/queue.spec.js b/resources/assets/js/tests/components/main-wrapper/main-content/queue.spec.js index c363797d..41d4f69b 100644 --- a/resources/assets/js/tests/components/main-wrapper/main-content/queue.spec.js +++ b/resources/assets/js/tests/components/main-wrapper/main-content/queue.spec.js @@ -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() }) diff --git a/resources/assets/js/tests/components/main-wrapper/main-content/settings.spec.js b/resources/assets/js/tests/components/main-wrapper/main-content/settings.spec.js index f4ca2ccd..48f6c801 100644 --- a/resources/assets/js/tests/components/main-wrapper/main-content/settings.spec.js +++ b/resources/assets/js/tests/components/main-wrapper/main-content/settings.spec.js @@ -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() diff --git a/resources/assets/js/tests/components/main-wrapper/main-content/users.spec.js b/resources/assets/js/tests/components/main-wrapper/main-content/users.spec.js index 71d493da..89c73e12 100644 --- a/resources/assets/js/tests/components/main-wrapper/main-content/users.spec.js +++ b/resources/assets/js/tests/components/main-wrapper/main-content/users.spec.js @@ -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', () => { diff --git a/resources/assets/js/tests/components/main-wrapper/main-content/youtube-player.spec.js b/resources/assets/js/tests/components/main-wrapper/main-content/youtube-player.spec.js index 610da9e3..0f3ae558 100644 --- a/resources/assets/js/tests/components/main-wrapper/main-content/youtube-player.spec.js +++ b/resources/assets/js/tests/components/main-wrapper/main-content/youtube-player.spec.js @@ -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') }) }) diff --git a/resources/assets/js/tests/components/main-wrapper/sidebar/index.spec.js b/resources/assets/js/tests/components/main-wrapper/sidebar/index.spec.js index bf5bd4e3..c6dbe36a 100644 --- a/resources/assets/js/tests/components/main-wrapper/sidebar/index.spec.js +++ b/resources/assets/js/tests/components/main-wrapper/sidebar/index.spec.js @@ -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!') }) diff --git a/resources/assets/js/tests/components/main-wrapper/sidebar/playlist-item.spec.js b/resources/assets/js/tests/components/main-wrapper/sidebar/playlist-item.spec.js index 06ca00d0..bc38b9d8 100644 --- a/resources/assets/js/tests/components/main-wrapper/sidebar/playlist-item.spec.js +++ b/resources/assets/js/tests/components/main-wrapper/sidebar/playlist-item.spec.js @@ -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', () => {