From 73a0d043756f08484d75d3e432df38ec643b9485 Mon Sep 17 00:00:00 2001 From: JanAckermann Date: Tue, 15 Mar 2022 13:30:45 +0100 Subject: [PATCH] add emptyTrashBin action tests --- .../src/mixins/actions/emptyTrashBin.js | 7 +- .../unit/mixins/actions/emptyTrashBin.spec.js | 120 ++++++++++++++++++ 2 files changed, 123 insertions(+), 4 deletions(-) create mode 100644 packages/web-app-files/tests/unit/mixins/actions/emptyTrashBin.spec.js diff --git a/packages/web-app-files/src/mixins/actions/emptyTrashBin.js b/packages/web-app-files/src/mixins/actions/emptyTrashBin.js index 4bc53bea176..7e0e47b61bd 100644 --- a/packages/web-app-files/src/mixins/actions/emptyTrashBin.js +++ b/packages/web-app-files/src/mixins/actions/emptyTrashBin.js @@ -1,11 +1,11 @@ -import { mapActions, mapGetters } from 'vuex' +import { mapActions, mapGetters, mapState } from 'vuex' import { isLocationTrashActive } from '../../router' import { buildWebDavFilesTrashPath, buildWebDavSpacesTrashPath } from '../../helpers/resources' export default { computed: { ...mapGetters('Files', ['activeFiles']), - ...mapGetters(['user']), + ...mapState(['user']), $_emptyTrashBin_items() { return [ { @@ -57,8 +57,7 @@ export default { ? buildWebDavSpacesTrashPath(this.$route.params.spaceId) : buildWebDavFilesTrashPath(this.user.id) - console.log(path) - this.$client.fileTrash + return this.$client.fileTrash .clearTrashBin(path) .then(() => { this.hideModal() diff --git a/packages/web-app-files/tests/unit/mixins/actions/emptyTrashBin.spec.js b/packages/web-app-files/tests/unit/mixins/actions/emptyTrashBin.spec.js new file mode 100644 index 00000000000..b87ab20d06c --- /dev/null +++ b/packages/web-app-files/tests/unit/mixins/actions/emptyTrashBin.spec.js @@ -0,0 +1,120 @@ +import Vuex from 'vuex' +import { createStore } from 'vuex-extensions' +import { mount, createLocalVue } from '@vue/test-utils' +import EmptyTashBin from '@files/src/mixins/actions/emptyTrashBin.js' +import { createLocationTrash } from '../../../../src/router' +// eslint-disable-next-line jest/no-mocks-import +import sdkMock from '@/__mocks__/sdk' + +const localVue = createLocalVue() +localVue.use(Vuex) + +const Component = { + render() {}, + mixins: [EmptyTashBin] +} + +describe('emptyTrashBin', () => { + afterEach(() => jest.clearAllMocks()) + + describe('isEnabled property', () => { + it('should be false when resource is given', () => { + const wrapper = getWrapper() + expect(wrapper.vm.$_emptyTrashBin_items[0].isEnabled({ resources: [{}] })).toBe(false) + }) + it('should be true when no resource is given', () => { + const wrapper = getWrapper() + expect(wrapper.vm.$_emptyTrashBin_items[0].isEnabled({ resources: [] })).toBe(true) + }) + }) + + describe('method "$_emptyTrashBin_trigger"', () => { + it('should trigger the empty trash bin modal window', async () => { + const wrapper = getWrapper() + const spyCreateModalStub = jest.spyOn(wrapper.vm, 'createModal') + await wrapper.vm.$_emptyTrashBin_trigger() + + expect(spyCreateModalStub).toHaveBeenCalledTimes(1) + }) + }) + + describe('method "$_emptyTrashBin_emptyTrashBin"', () => { + it('should hide the modal and show message on success', async () => { + const wrapper = getWrapper() + const hideModalStub = jest.spyOn(wrapper.vm, 'hideModal') + const showMessageStub = jest.spyOn(wrapper.vm, 'showMessage') + await wrapper.vm.$_emptyTrashBin_emptyTrashBin() + + expect(hideModalStub).toHaveBeenCalledTimes(1) + expect(showMessageStub).toHaveBeenCalledTimes(1) + }) + + it('should show message on error', async () => { + jest.spyOn(console, 'error').mockImplementation(() => {}) + + const wrapper = getWrapper(false) + const showMessageStub = jest.spyOn(wrapper.vm, 'showMessage') + await wrapper.vm.$_emptyTrashBin_emptyTrashBin() + + expect(showMessageStub).toHaveBeenCalledTimes(1) + }) + }) +}) + +function getWrapper(resolveClearTrashBin = true) { + return mount(Component, { + localVue, + mocks: { + $router: { + currentRoute: createLocationTrash('files-trash-personal'), + resolve: (r) => { + return { href: r.name } + } + }, + $gettext: jest.fn(), + $pgettext: jest.fn(), + $client: { + ...sdkMock, + fileTrash: { + ...sdkMock.files, + clearTrashBin: jest.fn().mockImplementation(() => { + if (resolveClearTrashBin) { + return Promise.resolve({}) + } + return Promise.reject(new Error('')) + }) + } + } + }, + store: createStore(Vuex.Store, { + actions: { + createModal: jest.fn(), + hideModal: jest.fn(), + showMessage: jest.fn() + }, + getters: { + configuration: () => ({ + server: 'https://example.com' + }), + getToken: () => 'token' + }, + modules: { + user: { + state: { + id: 'alice', + uuid: 1 + } + }, + Files: { + namespaced: true, + mutations: { + REMOVE_FILE: jest.fn() + }, + actions: { + clearTrashBin: jest.fn() + } + } + } + }) + }) +}