Skip to content

Commit

Permalink
test: fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JammingBen committed Dec 11, 2023
1 parent c2caec4 commit a5128ad
Show file tree
Hide file tree
Showing 15 changed files with 74 additions and 78 deletions.
3 changes: 2 additions & 1 deletion packages/web-app-admin-settings/src/views/Users.vue
Original file line number Diff line number Diff line change
Expand Up @@ -1040,7 +1040,8 @@ export default defineComponent({
sideBarPanelContext,
sideBarAvailablePanels,
onToggleCreateUserModal,
onCreateUser
onCreateUser,
onEditUser
}
},
computed: {
Expand Down
53 changes: 27 additions & 26 deletions packages/web-app-admin-settings/tests/unit/views/Groups.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@ jest.mock('@ownclouders/web-pkg', () => ({
}))

describe('Groups view', () => {
describe('method "createGroup"', () => {
describe('method "onCreateGroup"', () => {
it('should hide the modal and show message on success', async () => {
const { wrapper } = getWrapper()
const showMessageStub = jest.spyOn(wrapper.vm, 'showMessage')
const toggleCreateGroupModalStub = jest.spyOn(wrapper.vm, 'toggleCreateGroupModal')
await wrapper.vm.createGroup({ displayName: 'admins' })
const { wrapper, storeOptions } = getWrapper()
expect(wrapper.vm.createGroupModalOpen).toBeFalsy()
await wrapper.vm.onCreateGroup({ displayName: 'admins' })

expect(showMessageStub).toHaveBeenCalled()
expect(toggleCreateGroupModalStub).toHaveBeenCalledTimes(1)
expect(storeOptions.actions.showMessage).toHaveBeenCalled()
expect(wrapper.vm.createGroupModalOpen).toBeTruthy()
})

it('should show message on error', async () => {
Expand All @@ -42,17 +41,14 @@ describe('Groups view', () => {
clientService.graphAuthenticated.groups.createGroup.mockImplementation(() =>
mockAxiosReject()
)
const { wrapper } = getWrapper({ clientService })
const showErrorMessageStub = jest.spyOn(wrapper.vm, 'showErrorMessage')
const toggleCreateGroupModalStub = jest.spyOn(wrapper.vm, 'toggleCreateGroupModal')
await wrapper.vm.createGroup({ displayName: 'admins' })
const { wrapper, storeOptions } = getWrapper({ clientService })
await wrapper.vm.onCreateGroup({ displayName: 'admins' })

expect(showErrorMessageStub).toHaveBeenCalled()
expect(toggleCreateGroupModalStub).toHaveBeenCalledTimes(0)
expect(storeOptions.actions.showErrorMessage).toHaveBeenCalled()
})
})

describe('method "editGroup"', () => {
describe('method "onEditGroup"', () => {
it('should emit event on success', async () => {
const clientService = getClientServiceMock()
clientService.graphAuthenticated.groups.editGroup.mockImplementation(() => mockAxiosResolve())
Expand All @@ -69,7 +65,7 @@ describe('Groups view', () => {
const busStub = jest.spyOn(eventBus, 'publish')
await wrapper.vm.loadResourcesTask.last

const updatedGroup = await wrapper.vm.editGroup(editGroup)
const updatedGroup = await wrapper.vm.onEditGroup(editGroup)

expect(updatedGroup.id).toEqual('1')
expect(updatedGroup.displayName).toEqual('administrators')
Expand All @@ -80,43 +76,47 @@ describe('Groups view', () => {
jest.spyOn(console, 'error').mockImplementation(() => undefined)
const clientService = getClientServiceMock()
clientService.graphAuthenticated.groups.editGroup.mockImplementation(() => mockAxiosReject())
const { wrapper } = getWrapper({ clientService })
const showErrorMessageStub = jest.spyOn(wrapper.vm, 'showErrorMessage')
await wrapper.vm.editGroup({})
const { wrapper, storeOptions } = getWrapper({ clientService })
await wrapper.vm.onEditGroup({})

expect(showErrorMessageStub).toHaveBeenCalled()
expect(storeOptions.actions.showErrorMessage).toHaveBeenCalled()
})
})

describe('computed method "sideBarAvailablePanels"', () => {
describe('EditPanel', () => {
it('should be available when one group is selected', () => {
const { wrapper } = getWrapper()
wrapper.vm.selectedGroups = [{ id: '1' }]
expect(
wrapper.vm.sideBarAvailablePanels.find((panel) => panel.app === 'EditPanel')
wrapper.vm.sideBarAvailablePanels
.find(({ name }) => name === 'EditPanel')
.isVisible({ items: [{ id: '1' }] })
).toBeTruthy()
})
it('should not be available when multiple groups are selected', () => {
const { wrapper } = getWrapper()
wrapper.vm.selectedGroups = [{ id: '1' }, { id: '2' }]
expect(
wrapper.vm.sideBarAvailablePanels.find((panel) => panel.app === 'EditPanel')
wrapper.vm.sideBarAvailablePanels
.find(({ name }) => name === 'EditPanel')
.isVisible({ items: [{ id: '1' }, { id: '2' }] })
).toBeFalsy()
})
it('should not be available when one read-only group is selected', () => {
const { wrapper } = getWrapper()
wrapper.vm.selectedGroups = [{ id: '1', groupTypes: ['ReadOnly'] }]
expect(
wrapper.vm.sideBarAvailablePanels.find((panel) => panel.app === 'EditPanel')
wrapper.vm.sideBarAvailablePanels
.find(({ name }) => name === 'EditPanel')
.isVisible({ items: [{ id: '1', groupTypes: ['ReadOnly'] }] })
).toBeFalsy()
})
})
describe('DetailsPanel', () => {
it('should contain DetailsPanel when no group is selected', () => {
const { wrapper } = getWrapper()
expect(
wrapper.vm.sideBarAvailablePanels.find((panel) => panel.app === 'DetailsPanel')
wrapper.vm.sideBarAvailablePanels
.find(({ name }) => name === 'DetailsPanel')
.isVisible({ items: [] })
).toBeTruthy()
})
})
Expand Down Expand Up @@ -150,6 +150,7 @@ function getWrapper({ clientService = getClientServiceMock() } = {}) {
const storeOptions = { ...defaultStoreMockOptions }
const store = createStore(storeOptions)
return {
storeOptions,
wrapper: mount(Groups, {
global: {
plugins: [...defaultPlugins(), store],
Expand Down
67 changes: 28 additions & 39 deletions packages/web-app-admin-settings/tests/unit/views/Users.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ describe('Users view', () => {
expect(createUserButton.exists()).toBeFalsy()
})
})
describe('method "createUser"', () => {
describe('method "onCreateUser"', () => {
it('should hide the modal and show message on success', async () => {
const clientService = getClientService()
clientService.graphAuthenticated.users.createUser.mockImplementation(() =>
Expand Down Expand Up @@ -185,30 +185,26 @@ describe('Users view', () => {
surname: 'bene'
})
)
const { wrapper } = getMountedWrapper({ clientService })
const showMessageStub = jest.spyOn(wrapper.vm, 'showMessage')
const toggleCreateUserModalStub = jest.spyOn(wrapper.vm, 'toggleCreateUserModal')
await wrapper.vm.createUser({ displayName: 'jan' })
const { wrapper, storeOptions } = getMountedWrapper({ clientService })
expect(wrapper.vm.createUserModalOpen).toBeFalsy()
await wrapper.vm.onCreateUser({ displayName: 'jan' })

expect(showMessageStub).toHaveBeenCalled()
expect(toggleCreateUserModalStub).toHaveBeenCalledTimes(1)
expect(storeOptions.actions.showMessage).toHaveBeenCalled()
expect(wrapper.vm.createUserModalOpen).toBeTruthy()
})

it('should show message on error', async () => {
jest.spyOn(console, 'error').mockImplementation(() => undefined)
const clientService = getClientService()
clientService.graphAuthenticated.users.createUser.mockImplementation(() => mockAxiosReject())
const { wrapper } = getMountedWrapper({ clientService })
const showErrorMessageStub = jest.spyOn(wrapper.vm, 'showErrorMessage')
const toggleCreateUserModalStub = jest.spyOn(wrapper.vm, 'toggleCreateUserModal')
await wrapper.vm.createUser({ displayName: 'jana' })
const { wrapper, storeOptions } = getMountedWrapper({ clientService })
await wrapper.vm.onCreateUser({ displayName: 'jana' })

expect(showErrorMessageStub).toHaveBeenCalled()
expect(toggleCreateUserModalStub).toHaveBeenCalledTimes(0)
expect(storeOptions.actions.showErrorMessage).toHaveBeenCalled()
})
})

describe('method "editUser"', () => {
describe('method "onEditUser"', () => {
it('should emit event on success', async () => {
const editUser = {
appRoleAssignments: [
Expand Down Expand Up @@ -283,23 +279,14 @@ describe('Users view', () => {
})
)

const { wrapper } = getMountedWrapper({
clientService
})
const { wrapper, storeOptions } = getMountedWrapper({ clientService })

const busStub = jest.spyOn(eventBus, 'publish')
const updateSpaceFieldStub = jest.spyOn(wrapper.vm, 'UPDATE_SPACE_FIELD')
const updateUserDriveStub = jest.spyOn(wrapper.vm, 'updateUserDrive')
const updateUserGroupAssignmentsStub = jest.spyOn(wrapper.vm, 'updateUserGroupAssignments')
const updateUserAppRoleAssignmentsStub = jest.spyOn(
wrapper.vm,
'updateUserAppRoleAssignments'
)

await wrapper.vm.loadResourcesTask.last

const userToUpDate = wrapper.vm.users.find((user) => user.id === '1')
const updatedUser = await wrapper.vm.editUser({ user: userToUpDate, editUser })
const updatedUser = await wrapper.vm.onEditUser({ user: userToUpDate, editUser })

expect(updatedUser.id).toEqual('1')
expect(updatedUser.displayName).toEqual('administrator')
Expand All @@ -309,48 +296,49 @@ describe('Users view', () => {
expect(updatedUser.memberOf[0].id).toEqual('1')

expect(busStub).toHaveBeenCalled()
expect(updateUserDriveStub).toHaveBeenCalled()
expect(updateSpaceFieldStub).toHaveBeenCalled()
expect(updateUserGroupAssignmentsStub).toHaveBeenCalled()
expect(updateUserAppRoleAssignmentsStub).toHaveBeenCalled()
expect(
storeOptions.modules.runtime.modules.spaces.mutations.UPDATE_SPACE_FIELD
).toHaveBeenCalled()
})

it('should show message on error', async () => {
jest.spyOn(console, 'error').mockImplementation(() => undefined)
const clientService = getClientService()
clientService.graphAuthenticated.users.editUser.mockImplementation(() => mockAxiosReject())
const { wrapper } = getMountedWrapper({ clientService })
const showErrorMessageStub = jest.spyOn(wrapper.vm, 'showErrorMessage')
const { wrapper, storeOptions } = getMountedWrapper({ clientService })

await wrapper.vm.loadResourcesTask.last
await wrapper.vm.editUser({
await wrapper.vm.onEditUser({
editUser: {}
})

expect(showErrorMessageStub).toHaveBeenCalled()
expect(storeOptions.actions.showErrorMessage).toHaveBeenCalled()
})
})

describe('computed method "sideBarAvailablePanels"', () => {
it('should contain EditPanel when one user is selected', () => {
const { wrapper } = getMountedWrapper()
wrapper.vm.selectedUsers = [{ id: '1' }]
expect(
wrapper.vm.sideBarAvailablePanels.find((panel) => panel.app === 'EditPanel').enabled
wrapper.vm.sideBarAvailablePanels
.find(({ name }) => name === 'EditPanel')
.isVisible({ items: [{ id: '1' }] })
).toBeTruthy()
})
it('should contain DetailsPanel no user is selected', () => {
const { wrapper } = getMountedWrapper()
wrapper.vm.selectedUsers = []
expect(
wrapper.vm.sideBarAvailablePanels.find((panel) => panel.app === 'DetailsPanel').enabled
wrapper.vm.sideBarAvailablePanels
.find(({ name }) => name === 'DetailsPanel')
.isVisible({ items: [] })
).toBeTruthy()
})
it('should not contain EditPanel when multiple users are selected', () => {
const { wrapper } = getMountedWrapper()
wrapper.vm.selectedUsers = [{ id: '1' }, { id: '2' }]
expect(
wrapper.vm.sideBarAvailablePanels.find((panel) => panel.app === 'EditPanel')
wrapper.vm.sideBarAvailablePanels
.find(({ name }) => name === 'EditPanel')
.isVisible({ items: [{ id: '1' }, { id: '2' }] })
).toBeFalsy()
})
})
Expand Down Expand Up @@ -517,6 +505,7 @@ function getMountedWrapper({

return {
mocks,
storeOptions,
wrapper: mountType(Users, {
global: {
plugins: [...defaultPlugins(), store],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

exports[`General view renders component 1`] = `
<div>
<app-template-stub batchactionitems="" batchactions="" breadcrumbs="[object Object],[object Object]" loading="false" showappbar="false" showbatchactions="false" showviewoptions="false" sidebaravailablepanels="" sidebarloading="false" issidebaropen="false"></app-template-stub>
<app-template-stub batchactionitems="" batchactions="" breadcrumbs="[object Object],[object Object]" issidebaropen="false" loading="false" showappbar="false" showbatchactions="false" showviewoptions="false" sidebaravailablepanels="" sidebarloading="false" sidebarpanelcontext="[object Object]"></app-template-stub>
</div>
`;
2 changes: 1 addition & 1 deletion packages/web-app-files/tests/unit/views/Favorites.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('Favorites view', () => {
})
it('sideBar always present', () => {
const { wrapper } = getMountedWrapper()
expect(wrapper.find('side-bar-stub').exists()).toBeTruthy()
expect(wrapper.find('file-side-bar-stub').exists()).toBeTruthy()
})
describe('different files view states', () => {
it('shows the loading spinner during loading', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('SharedViaLink view', () => {
})
it('sideBar always present', () => {
const { wrapper } = getMountedWrapper()
expect(wrapper.find('side-bar-stub').exists()).toBeTruthy()
expect(wrapper.find('file-side-bar-stub').exists()).toBeTruthy()
})
describe('different files view states', () => {
it('shows the loading spinner during loading', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe('SharedWithMe view', () => {
})
it('sideBar always present', () => {
const { wrapper } = getMountedWrapper()
expect(wrapper.find('side-bar-stub').exists()).toBeTruthy()
expect(wrapper.find('file-side-bar-stub').exists()).toBeTruthy()
})
describe('different files view states', () => {
it('shows the loading spinner during loading', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('SharedWithOthers view', () => {
})
it('sideBar always present', () => {
const { wrapper } = getMountedWrapper()
expect(wrapper.find('side-bar-stub').exists()).toBeTruthy()
expect(wrapper.find('file-side-bar-stub').exists()).toBeTruthy()
})
describe('different files view states', () => {
it('shows the loading spinner during loading', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('GenericSpace view', () => {
})
it('sideBar always present', () => {
const { wrapper } = getMountedWrapper()
expect(wrapper.find('side-bar-stub').exists()).toBeTruthy()
expect(wrapper.find('file-side-bar-stub').exists()).toBeTruthy()
})
describe('space header', () => {
it('does not render the space header in the personal space', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('GenericTrash view', () => {
})
it('sideBar always present', () => {
const { wrapper } = getMountedWrapper()
expect(wrapper.find('side-bar-stub').exists()).toBeTruthy()
expect(wrapper.find('file-side-bar-stub').exists()).toBeTruthy()
})
it('shows the personal space breadcrumb', () => {
const { wrapper } = getMountedWrapper()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('Projects view', () => {
})
it('sideBar always present', () => {
const { wrapper } = getMountedWrapper()
expect(wrapper.find('side-bar-stub').exists()).toBeTruthy()
expect(wrapper.find('file-side-bar-stub').exists()).toBeTruthy()
})
describe('different files view states', () => {
it('shows the loading spinner during loading', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ exports[`Projects view different files view states lists all available project s
<div class="oc-flex oc-width-1-1">
<div class="files-view-wrapper oc-width-expand">
<div id="files-view">
<app-bar-stub breadcrumbs="[object Object]" breadcrumbscontextactionsitems="" hasbulkactions="true" hasfileextensions="false" hashiddenfiles="false" haspagination="false" hassidebartoggle="true" hasviewoptions="true" showactionsonselection="true" issidebaropen="false" viewmodedefault="resource-tiles" viewmodes="[object Object],[object Object]"></app-bar-stub>
<app-bar-stub breadcrumbs="[object Object]" breadcrumbscontextactionsitems="" hasbulkactions="true" hasfileextensions="false" hashiddenfiles="false" haspagination="false" hassidebartoggle="true" hasviewoptions="true" issidebaropen="false" showactionsonselection="true" viewmodedefault="resource-tiles" viewmodes="[object Object],[object Object]"></app-bar-stub>
<div class="spaces-list oc-mt-l">
<div class="spaces-list-filters oc-flex oc-flex-right oc-px-m oc-mb-m">
<div class="">
Expand All @@ -23,6 +23,6 @@ exports[`Projects view different files view states lists all available project s
</div>
</div>
<!--v-if-->
<side-bar-stub is-open="false"></side-bar-stub>
<file-side-bar-stub isopen="false"></file-side-bar-stub>
</div>
`;
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ exports[`TrashOverview view states should render spaces list 1`] = `
<div class="oc-flex">
<div class="files-view-wrapper oc-width-expand">
<div id="files-view">
<app-bar-stub breadcrumbs="[object Object]" breadcrumbscontextactionsitems="" hasbulkactions="false" hasfileextensions="false" hashiddenfiles="false" haspagination="false" hassidebartoggle="false" hasviewoptions="false" showactionsonselection="false" issidebaropen="false" viewmodedefault="resource-table" viewmodes=""></app-bar-stub>
<app-bar-stub breadcrumbs="[object Object]" breadcrumbscontextactionsitems="" hasbulkactions="false" hasfileextensions="false" hashiddenfiles="false" haspagination="false" hassidebartoggle="false" hasviewoptions="false" issidebaropen="false" showactionsonselection="false" viewmodedefault="resource-table" viewmodes=""></app-bar-stub>
<div class="trash-bin-filters oc-flex oc-flex-right oc-flex-wrap oc-flex-bottom oc-mx-m oc-mb-m">
<div class="">
<label class="oc-label" for="spaces-filter">Search</label>
Expand Down
Loading

0 comments on commit a5128ad

Please sign in to comment.