From 3f93d68f9546dbf6afbd6377788e15cf342ee19e Mon Sep 17 00:00:00 2001 From: Phan An Date: Sun, 8 May 2022 20:18:27 +0200 Subject: [PATCH] feat(test): add PlaylistNameEditor component tests --- .../components/playlist/name-editor.spec.ts | 29 --------- .../playlist/CreateNewPlaylistContextMenu.vue | 6 +- .../playlist/PlaylistContextMenu.vue | 6 +- .../playlist/PlaylistNameEditor.spec.ts | 63 +++++++++++++++++++ .../playlist/PlaylistNameEditor.vue | 5 +- .../playlist/PlaylistSidebarItem.vue | 10 +-- .../playlist/PlaylistSidebarList.vue | 22 +++---- .../SmartPlaylistCreateForm.vue | 12 ++-- .../smart-playlist/SmartPlaylistEditForm.vue | 10 +-- .../smart-playlist/SmartPlaylistRule.vue | 1 - .../smart-playlist/SmartPlaylistRuleInput.vue | 2 +- .../js/components/ui/ContextMenuBase.vue | 8 +-- 12 files changed, 103 insertions(+), 71 deletions(-) delete mode 100644 resources/assets/js/__tests__/components/playlist/name-editor.spec.ts create mode 100644 resources/assets/js/components/playlist/PlaylistNameEditor.spec.ts diff --git a/resources/assets/js/__tests__/components/playlist/name-editor.spec.ts b/resources/assets/js/__tests__/components/playlist/name-editor.spec.ts deleted file mode 100644 index 163c9ef0..00000000 --- a/resources/assets/js/__tests__/components/playlist/name-editor.spec.ts +++ /dev/null @@ -1,29 +0,0 @@ -import Component from '@/components/playlist/PlaylistNameEditor.vue' -import factory from '@/__tests__/factory' -import { playlistStore } from '@/stores' -import { mock } from '@/__tests__/__helpers__' -import { shallow } from '@/__tests__/adapter' - -describe('components/playlist/PlaylistNameEditor', () => { - let playlist: Playlist - beforeEach(() => { - playlist = factory('playlist', { - id: 99, - name: 'Foo' - }) - }) - - afterEach(() => { - jest.resetModules() - jest.clearAllMocks() - }) - - it('updates a playlist', () => { - const updateStub = mock(playlistStore, 'update') - const wrapper = shallow(Component, { - propsData: { playlist } - }) - wrapper.find('[type=text]').setValue('Bar').input().blur() - expect(updateStub).toHaveBeenCalledWith(expect.objectContaining({ id: 99, name: 'Bar' })) - }) -}) diff --git a/resources/assets/js/components/playlist/CreateNewPlaylistContextMenu.vue b/resources/assets/js/components/playlist/CreateNewPlaylistContextMenu.vue index 37b936a0..66c18e8c 100644 --- a/resources/assets/js/components/playlist/CreateNewPlaylistContextMenu.vue +++ b/resources/assets/js/components/playlist/CreateNewPlaylistContextMenu.vue @@ -1,7 +1,7 @@ diff --git a/resources/assets/js/components/playlist/PlaylistContextMenu.vue b/resources/assets/js/components/playlist/PlaylistContextMenu.vue index fa7968d1..822756eb 100644 --- a/resources/assets/js/components/playlist/PlaylistContextMenu.vue +++ b/resources/assets/js/components/playlist/PlaylistContextMenu.vue @@ -1,7 +1,7 @@ diff --git a/resources/assets/js/components/playlist/PlaylistNameEditor.spec.ts b/resources/assets/js/components/playlist/PlaylistNameEditor.spec.ts new file mode 100644 index 00000000..6774306d --- /dev/null +++ b/resources/assets/js/components/playlist/PlaylistNameEditor.spec.ts @@ -0,0 +1,63 @@ +import factory from '@/__tests__/factory' +import PlaylistNameEditor from '@/components/playlist/PlaylistNameEditor.vue' +import { beforeEach, expect, it } from 'vitest' +import { mockHelper, render } from '@/__tests__/__helpers__' +import { cleanup, fireEvent } from '@testing-library/vue' +import { playlistStore } from '@/stores' + +beforeEach(() => { + mockHelper.restoreAllMocks() + cleanup() +}) + +const setup = () => { + const updateMock = mockHelper.mock(playlistStore, 'update') + + const { getByTestId } = render(PlaylistNameEditor, { + props: { + playlist: factory('playlist', { + id: 99, + name: 'Foo' + }) + } + }) + + return { + updateMock, + input: getByTestId('inline-playlist-name-input') + } +} + +it('updates a playlist name on blur', async () => { + const { updateMock, input } = setup() + + await fireEvent.update(input, 'Bar') + await fireEvent.blur(input) + + expect(updateMock).toHaveBeenCalledWith(expect.objectContaining({ + id: 99, + name: 'Bar' + })) +}) + +it('updates a playlist name on enter', async () => { + const { updateMock, input } = setup() + + await fireEvent.update(input, 'Bar') + await fireEvent.keyUp(input, { key: 'Enter' }) + + expect(updateMock).toHaveBeenCalledWith(expect.objectContaining({ + id: 99, + name: 'Bar' + })) +}) + +it('cancels updating on esc', async () => { + const { updateMock, input } = setup() + + await fireEvent.update(input, 'Bar') + await fireEvent.keyUp(input, { key: 'Esc' }) + + expect(input.value).toBe('Foo') + expect(updateMock).not.toHaveBeenCalled() +}) diff --git a/resources/assets/js/components/playlist/PlaylistNameEditor.vue b/resources/assets/js/components/playlist/PlaylistNameEditor.vue index eb7cbcde..c67ca99a 100644 --- a/resources/assets/js/components/playlist/PlaylistNameEditor.vue +++ b/resources/assets/js/components/playlist/PlaylistNameEditor.vue @@ -51,5 +51,8 @@ const update = async () => { emit('updated', mutatedPlaylist) } -const cancel = () => emit('cancelled') +const cancel = () => { + mutatedPlaylist.name = playlist.value.name + emit('cancelled') +} diff --git a/resources/assets/js/components/playlist/PlaylistSidebarItem.vue b/resources/assets/js/components/playlist/PlaylistSidebarItem.vue index 1c41a9b2..b587cc72 100644 --- a/resources/assets/js/components/playlist/PlaylistSidebarItem.vue +++ b/resources/assets/js/components/playlist/PlaylistSidebarItem.vue @@ -1,26 +1,26 @@