Skip to content

Commit

Permalink
add emptyTrashBin action tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JanAckermann committed Mar 15, 2022
1 parent 5321a9a commit 73a0d04
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 4 deletions.
7 changes: 3 additions & 4 deletions packages/web-app-files/src/mixins/actions/emptyTrashBin.js
Original file line number Diff line number Diff line change
@@ -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 [
{
Expand Down Expand Up @@ -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()
Expand Down
120 changes: 120 additions & 0 deletions packages/web-app-files/tests/unit/mixins/actions/emptyTrashBin.spec.js
Original file line number Diff line number Diff line change
@@ -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()
}
}
}
})
})
}

0 comments on commit 73a0d04

Please sign in to comment.