Skip to content

Commit

Permalink
Add and adjust unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JammingBen committed Mar 9, 2022
1 parent 773811b commit 6902c52
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 7 deletions.
5 changes: 3 additions & 2 deletions __fixtures__/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ export default {
id: 'alice',
name: 'alice',
displayname: 'Alice Hansen',
additionalInfo: "alice@example.org",
additionalInfo: 'alice@example.org',
home: '/mnt/data/files/alice',
two_factor_auth_enabled: 'false'
two_factor_auth_enabled: 'false',
uuid: 1
}
}
8 changes: 5 additions & 3 deletions packages/web-app-files/src/components/SideBar/SideBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -268,15 +268,17 @@ export default {
if (!this.areMultipleSelected) {
await this.fetchFileInfo()
}
if (this.$route.params.spaceId && !this.highlightedFileIsSpace) {
this.loadSpaceTask.perform(this, this.$route.params.spaceId)
}
this.$nextTick(this.initVisibilityObserver)
},
beforeDestroy() {
visibilityObserver.disconnect()
hiddenObserver.disconnect()
},
mounted() {
if (this.$route.params.spaceId && !this.highlightedFileIsSpace) {
this.loadSpaceTask.perform(this, this.$route.params.spaceId)
}
},
methods: {
...mapActions('Files/sidebar', {
closeSidebar: 'close',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ import Vuex from 'vuex'
import DesignSystem from 'owncloud-design-system'
import Users from '@/__fixtures__/users'
import Collaborators from '@/__fixtures__/collaborators'
import mockAxios from 'jest-mock-axios'
import { buildSpace } from '../../../../../src/helpers/resources'
import { spaceRoleManager } from '../../../../../src/helpers/share'
import VueCompositionAPI from '@vue/composition-api/dist/vue-composition-api'

const localVue = createLocalVue()
localVue.use(DesignSystem)
localVue.use(Vuex)
localVue.use(VueCompositionAPI)
localVue.use(GetTextPlugin, {
translations: 'does-not-matter.json',
silent: true
Expand Down Expand Up @@ -108,6 +113,40 @@ describe('FileShares', () => {
expect(spyOnReloadShares).toHaveBeenCalledTimes(1)
})
})

describe('current space', () => {
afterEach(() => {
mockAxios.reset()
})
it('loads space members if a space is given', async () => {
const spaceMock = {
id: '1',
root: { permissions: [{ roles: ['manager'], grantedTo: [{ user: { id: 1 } }] }] }
}
mockAxios.request.mockImplementationOnce(() => {
return Promise.resolve({
data: { role: spaceRoleManager.name }
})
})

const wrapper = getShallowMountedWrapper({
user,
space: buildSpace(spaceMock)
})

await wrapper.vm.loadSpaceMembersTask.last
expect(wrapper.vm.spaceMembers.length).toBe(1)
expect(wrapper.find('#space-collaborators-list').exists()).toBeTruthy()
})
it('does not load space members if no space is given', async () => {
const wrapper = getShallowMountedWrapper({
user
})

await wrapper.vm.loadSpaceMembersTask.last
expect(wrapper.vm.spaceMembers.length).toBe(0)
})
})
})

function getResource({
Expand Down Expand Up @@ -191,6 +230,10 @@ const storeOptions = (data, isInLoadingState) => {
}
},
getters: {
getToken: jest.fn(() => 'GFwHKXdsMgoFwt'),
configuration: jest.fn(() => ({
server: 'http://example.com/'
})),
isOcis: () => false,
user: () => user,
capabilities: () => {
Expand Down Expand Up @@ -236,6 +279,11 @@ function getShallowMountedWrapper(data, loading = false) {
'oc-button': true,
'oc-icon': true,
'oc-spinner': true
},
provide: {
currentSpace: {
value: data.space
}
}
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ exports[`FileShares if currentUser can not share initially renders no share perm
<p data-testid="files-collaborators-no-reshare-permissions-message">You don't have permission to share this file.</p>
<!---->
<!---->
<!---->
</div>
`;

Expand All @@ -13,6 +14,7 @@ exports[`FileShares if currentUser can share initially renders add people dialog
<invite-collaborator-form-stub class="oc-my-s"></invite-collaborator-form-stub>
<!---->
<!---->
<!---->
</div>
`;

Expand All @@ -33,6 +35,7 @@ exports[`FileShares if there are collaborators present renders sharedWithLabel a
<collaborator-list-item-stub share="[object Object]" modifiable="true"></collaborator-list-item-stub>
</li>
</ul>
<!---->
</div>
`;

Expand All @@ -41,6 +44,7 @@ exports[`FileShares if there are no collaborators does not render avatar wrapper
<invite-collaborator-form-stub class="oc-my-s"></invite-collaborator-form-stub>
<!---->
<!---->
<!---->
</div>
`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { buildResource, renameResource } from '@files/src/helpers/resources'

import SideBar from '@files/src/components/SideBar/SideBar.vue'
import { createLocationSpaces } from '../../../../src/router'
import mockAxios from 'jest-mock-axios'

jest.mock('web-pkg/src/observer')
jest.mock('@files/src/helpers/resources', () => {
Expand Down Expand Up @@ -165,13 +166,58 @@ describe('SideBar', () => {
})
})
})

describe('current space', () => {
beforeEach(() => {
buildResource.mockImplementation((item) => item)
})
afterEach(() => {
jest.clearAllMocks()
mockAxios.reset()
})
it('fetches the current space if a space id is given', async () => {
const spaceId = 1
mockAxios.request.mockImplementationOnce(() => {
return Promise.resolve({
data: { id: spaceId }
})
})

const mockFileInfo = jest.fn()
mockFileInfo.mockReturnValueOnce(Files['/'][1])

const wrapper = createWrapper({
item: simpleOwnFolder,
selectedItems: [simpleOwnFolder],
mocks: { $client: { files: { fileInfo: mockFileInfo } } },
spaceId
})

await wrapper.vm.loadSpaceTask.last
expect(wrapper.vm.currentSpace.id).toBe(spaceId)
})
it('does not fetch the current space if no space id is given', async () => {
const mockFileInfo = jest.fn()
mockFileInfo.mockReturnValueOnce(Files['/'][1])

const wrapper = createWrapper({
item: simpleOwnFolder,
selectedItems: [simpleOwnFolder],
mocks: { $client: { files: { fileInfo: mockFileInfo } } }
})

await wrapper.vm.loadSpaceTask.last
expect(wrapper.vm.currentSpace).toBeNull()
})
})
})

function createWrapper({
item,
selectedItems,
mocks,
currentRouteName = 'files-spaces-personal-home'
currentRouteName = 'files-spaces-personal-home',
spaceId = undefined
}) {
const localVue = createLocalVue()
localVue.use(Vuex)
Expand All @@ -180,6 +226,7 @@ function createWrapper({
translations: 'does-not-matter.json',
silent: true
})

return shallowMount(SideBar, {
store: new Vuex.Store({
getters: {
Expand All @@ -191,7 +238,11 @@ function createWrapper({
api_enabled: true,
public: { enabled: true }
}
})
}),
getToken: jest.fn(() => 'GFwHKXdsMgoFwt'),
configuration: jest.fn(() => ({
server: 'http://example.com/'
}))
},
modules: {
apps: {
Expand Down Expand Up @@ -237,6 +288,11 @@ function createWrapper({
resolve: (r) => {
return { href: r.name }
}
},
$route: {
params: {
spaceId
}
}
},
mocks
Expand Down

0 comments on commit 6902c52

Please sign in to comment.