Skip to content

Commit

Permalink
Add tests for navigate.js action
Browse files Browse the repository at this point in the history
  • Loading branch information
JanAckermann committed Mar 18, 2022
1 parent 9f2b377 commit b2513c1
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/web-app-files/src/mixins/spaces/actions/navigate.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ export default {
}
},
methods: {
$_navigate_space_trigger({ resources }) {
$_navigate_space_trigger() {
this.$router.push(
createLocationSpaces('files-spaces-project', {
params: {
storageId: this.$route.params.storageId
storageId: this.$router.currentRoute.params.storageId
}
})
)
Expand Down
86 changes: 86 additions & 0 deletions packages/web-app-files/tests/unit/mixins/spaces/navigate.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import Vuex from 'vuex'
import { createStore } from 'vuex-extensions'
import { mount, createLocalVue } from '@vue/test-utils'
import Navigate from '@files/src/mixins/spaces/actions/navigate.js'
import { createLocationSpaces, createLocationTrash } from '../../../../src/router'

const localVue = createLocalVue()
localVue.use(Vuex)

const Component = {
render() {},
mixins: [Navigate]
}

describe('navigate', () => {
afterEach(() => jest.clearAllMocks())

describe('isEnabled property', () => {
it('should be true when no resource given', () => {
const wrapper = getWrapper()
expect(wrapper.vm.$_navigate_space_items[0].isEnabled({ resources: [] })).toBe(true)
})
it('should be false when resource is given', () => {
const wrapper = getWrapper()
expect(wrapper.vm.$_navigate_space_items[0].isEnabled({ resources: [{}] })).toBe(false)
})
it('should be false when location is invalid', () => {
const wrapper = getWrapper({ invalidLocation: true })
expect(wrapper.vm.$_navigate_space_items[0].isEnabled({ resources: [] })).toBe(false)
})
})

describe('method "$_navigate_space_trigger"', () => {
it('should trigger route change', async () => {
const wrapper = getWrapper()
await wrapper.vm.$_navigate_space_trigger()

expect(wrapper.vm.$router.push).toHaveBeenCalledWith(
createLocationSpaces('files-spaces-project', {
params: {
storageId: wrapper.vm.$router.currentRoute.params.storageId
}
})
)
})
})
})

function getWrapper({ invalidLocation = false } = {}) {
return mount(Component, {
localVue,
mocks: {
$router: {
currentRoute: invalidLocation
? createLocationTrash('files-trash-personal')
: createLocationTrash('files-trash-spaces-project', { params: { storageId: '1' } }),
resolve: (r) => {
return { href: r.name }
},
push: jest.fn()
},
$gettext: jest.fn()
},
store: createStore(Vuex.Store, {
actions: {
createModal: jest.fn(),
hideModal: jest.fn(),
showMessage: jest.fn()
},
getters: {
configuration: () => ({
server: 'https://example.com'
}),
getToken: () => 'token'
},
modules: {
Files: {
namespaced: true,
mutations: {
REMOVE_FILE: jest.fn()
}
}
}
})
})
}

0 comments on commit b2513c1

Please sign in to comment.