Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JanAckermann committed Apr 7, 2022
1 parent afc9523 commit f2a06ad
Show file tree
Hide file tree
Showing 10 changed files with 275 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
<div class="oc-flex oc-flex-between oc-flex-middle oc-width-1-1">
<span>{{ unsavedChangesText }}</span>
<div>
<oc-button v-translate :disabled="!unsavedChanges" @click="$emit('revert')"
>Revert</oc-button
>
<oc-button :disabled="!unsavedChanges" @click="$emit('revert')">
<translate>Revert</translate>
</oc-button>
<oc-button
v-translate
appearance="filled"
variation="primary"
:disabled="!unsavedChanges || confirmButtonDisabled"
@click="$emit('confirm')"
>Save</oc-button
>
<translate>Save</translate>
</oc-button>
</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export default {
revertChanges() {
this.editGroup = { ...this.group }
Object.keys(this.formData).forEach((formDataKey) => {
this.formData[formDataKey].invalid = false
this.formData[formDataKey].valid = true
this.formData[formDataKey].errorMessage = ''
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export default {
this.editUser = { ...this.user, ...{ passwordProfile: { password: '' } } }
this.editUserRole = this.roles.find((role) => role.id === this.userRole.id)
Object.keys(this.formData).forEach((formDataKey) => {
this.formData[formDataKey].invalid = false
this.formData[formDataKey].valid = true
this.formData[formDataKey].errorMessage = ''
})
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import Vuex from 'vuex'
import { mount, createLocalVue } from '@vue/test-utils'
import CompareSaveDialog from '../../../src/components/CompareSaveDialog.vue'

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

afterEach(() => jest.clearAllMocks())

describe('CompareSaveDialog', () => {
describe('computed method "unsavedChanges"', () => {
it('should be false if objects are equal', () => {
const wrapper = getWrapper({
propsData: {
originalObject: { id: '1', displayName: 'jan' },
compareObject: { id: '1', displayName: 'jan' }
}
})
expect(wrapper.vm.unsavedChanges).toBeFalsy()
})

it('should be true if objects are not equal', () => {
const wrapper = getWrapper({
propsData: {
originalObject: { id: '1', displayName: 'jan' },
compareObject: { id: '1', displayName: 'janina' }
}
})
expect(wrapper.vm.unsavedChanges).toBeTruthy()
})
})
})

function getWrapper({ propsData = {} } = {}) {
return mount(CompareSaveDialog, {
localVue,
directives: {
translate: jest.fn()
},
mocks: {
$gettext: jest.fn(),
$gettextInterpolate: jest.fn()
},
propsData: {
originalObject: {},
compareObject: {},
...propsData
},
stubs: {
'oc-button': true,
translate: true
}
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import Vuex from 'vuex'
import { mount, createLocalVue } from '@vue/test-utils'
import EditPanel from '../../../../../src/components/Groups/SideBar/EditPanel.vue'

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

afterEach(() => jest.clearAllMocks())

describe('EditPanel', () => {
describe('method "revertChanges"', () => {
it('should revert changes on property editGroup', () => {
const wrapper = getWrapper({
propsData: {
group: { displayName: 'group' }
}
})
wrapper.vm.editGroup = { displayName: 'my group' }
wrapper.vm.revertChanges()
expect(wrapper.vm.editGroup).toEqual({ displayName: 'group' })
})
it('should revert changes on property formData', () => {
const wrapper = getWrapper({
propsData: {
group: { displayName: 'group' }
}
})
wrapper.vm.formData.displayName.valid = false
wrapper.vm.formData.displayName.errorMessage = 'error'
wrapper.vm.revertChanges()
expect(wrapper.vm.formData.displayName.valid).toBeTruthy()
expect(wrapper.vm.formData.displayName.errorMessage).toEqual('')
})
})

describe('method "validateDisplayName"', () => {
it('should return true if displayName is valid', () => {
const wrapper = getWrapper()
wrapper.vm.editGroup.displayName = 'jan'
expect(wrapper.vm.validateDisplayName()).toBeTruthy()
expect(wrapper.vm.formData.displayName.valid).toBeTruthy()
})
it('should return false if displayName is not valid', () => {
const wrapper = getWrapper()
wrapper.vm.editGroup.displayName = ''
expect(wrapper.vm.validateDisplayName()).toBeFalsy()
expect(wrapper.vm.formData.displayName.valid).toBeFalsy()
})
})

describe('computed method "invalidFormData"', () => {
it('should be false if formData is invalid', () => {
const wrapper = getWrapper()
wrapper.vm.formData.displayName.valid = true
expect(wrapper.vm.invalidFormData).toBeFalsy()
})
it('should be true if formData is valid', () => {
const wrapper = getWrapper()
wrapper.vm.formData.displayName.valid = false
expect(wrapper.vm.invalidFormData).toBeTruthy()
})
})
})

function getWrapper({ propsData = {} } = {}) {
return mount(EditPanel, {
localVue,
directives: {
translate: jest.fn()
},
mocks: {
$gettext: jest.fn(),
$gettextInterpolate: jest.fn()
},
propsData: {
group: { displayName: 'group' },
...propsData
},
stubs: {
'oc-text-input': true,
'avatar-image': true,
'oc-button': true,
translate: true
}
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ describe('DetailsPanel', () => {
})
it('should be set if user role is assigned', () => {
const wrapper = getWrapper({
propsData: { users: [{ id: '1', displayName: 'user' }], userRoles: { 1: 'admin' } }
propsData: {
users: [{ id: '1', displayName: 'user' }],
userRoles: { 1: { displayName: 'admin' } }
}
})
expect(wrapper.vm.userRole).toEqual('admin')
})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import Vuex from 'vuex'
import { mount, createLocalVue } from '@vue/test-utils'
import EditPanel from '../../../../../src/components/Users/SideBar/EditPanel.vue'

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

afterEach(() => jest.clearAllMocks())

describe('EditPanel', () => {
describe('method "revertChanges"', () => {
it('should revert changes on property editUser', () => {
const wrapper = getWrapper({
propsData: {
user: { displayName: 'jan', mail: '[email protected]' }
}
})
wrapper.vm.editUser.displayName = 'jana'
wrapper.vm.editUser.mail = '[email protected]'
wrapper.vm.revertChanges()
expect(wrapper.vm.editUser.displayName).toEqual('jan')
expect(wrapper.vm.editUser.mail).toEqual('[email protected]')
})
it('should revert changes on property formData', () => {
const wrapper = getWrapper({
propsData: {
group: { displayName: 'jan' }
}
})
wrapper.vm.formData.displayName.valid = false
wrapper.vm.formData.displayName.errorMessage = 'error'
wrapper.vm.revertChanges()
expect(wrapper.vm.formData.displayName.valid).toBeTruthy()
expect(wrapper.vm.formData.displayName.errorMessage).toEqual('')
})
})

describe('method "validateDisplayName"', () => {
it('should return true if displayName is valid', () => {
const wrapper = getWrapper()
wrapper.vm.editUser.displayName = 'jan'
expect(wrapper.vm.validateDisplayName()).toBeTruthy()
expect(wrapper.vm.formData.displayName.valid).toBeTruthy()
})
it('should return false if displayName is not valid', () => {
const wrapper = getWrapper()
wrapper.vm.editUser.displayName = ''
expect(wrapper.vm.validateDisplayName()).toBeFalsy()
expect(wrapper.vm.formData.displayName.valid).toBeFalsy()
})
})

describe('method "validateEmail"', () => {
it('should return true if email is valid', () => {
const wrapper = getWrapper()
wrapper.vm.editUser.mail = '[email protected]'
expect(wrapper.vm.validateEmail()).toBeTruthy()
expect(wrapper.vm.formData.email.valid).toBeTruthy()
})
it('should return false if email is not valid', () => {
const wrapper = getWrapper()
wrapper.vm.editUser.mail = ''
expect(wrapper.vm.validateEmail()).toBeFalsy()
expect(wrapper.vm.formData.email.valid).toBeFalsy()
})
})

describe('computed method "invalidFormData"', () => {
it('should be false if formData is invalid', () => {
const wrapper = getWrapper()
wrapper.vm.formData.displayName.valid = true
expect(wrapper.vm.invalidFormData).toBeFalsy()
})
it('should be true if formData is valid', () => {
const wrapper = getWrapper()
wrapper.vm.formData.displayName.valid = false
expect(wrapper.vm.invalidFormData).toBeTruthy()
})
})
})

function getWrapper({ propsData = {} } = {}) {
return mount(EditPanel, {
localVue,
directives: {
translate: jest.fn()
},
mocks: {
$gettext: jest.fn(),
$gettextInterpolate: jest.fn()
},
propsData: {
user: { displayName: 'jan', mail: '[email protected]' },
roles: [{ id: '1', displayName: 'admin' }],
userRole: { id: '1', displayName: 'admin' },
...propsData
},
stubs: {
'oc-text-input': true,
'avatar-image': true,
'oc-button': true,
'oc-select': true,
translate: true
}
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('UsersList', () => {
it('should return user role if record is set', () => {
const wrapper = getWrapper({
propsData: {
userRoles: { 1: 'admin' }
userRoles: { 1: { displayName: 'admin' } }
}
})
expect(wrapper.vm.getUserRole({ id: 1 })).toEqual('admin')
Expand Down
29 changes: 16 additions & 13 deletions packages/web-app-user-management/tests/unit/views/Users.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ describe('Users view', () => {
const wrapper = getMountedWrapper({
mocks: {
users: [user],
userAssignments: [
[
userAssignments: {
1: [
{
accountUuid: '1',
roleId: '1'
}
]
],
},
roles: [
{
displayName: 'admin',
Expand All @@ -113,7 +113,10 @@ describe('Users view', () => {
]
}
})
expect(wrapper.vm.userRoles[user.id]).toEqual('admin')
expect(wrapper.vm.userRoles[user.id]).toEqual({
displayName: 'admin',
id: '1'
})
})
it('should not contain user role if userAssignments is empty', () => {
const user = { id: '1' }
Expand All @@ -127,14 +130,14 @@ describe('Users view', () => {
const wrapper = getMountedWrapper({
mocks: {
users: [user],
userAssignments: [
[
userAssignments: {
2: [
{
accountUuid: '2',
roleId: '1'
}
]
],
},
roles: [
{
displayName: 'admin',
Expand All @@ -150,14 +153,14 @@ describe('Users view', () => {
const wrapper = getMountedWrapper({
mocks: {
users: [user],
userAssignments: [
[
userAssignments: {
1: [
{
accountUuid: '1',
roleId: '1'
}
]
],
},
roles: [
{
displayName: 'admin',
Expand All @@ -173,13 +176,13 @@ describe('Users view', () => {
const wrapper = getMountedWrapper({
mocks: {
users: [user],
userAssignments: [
[
userAssignments: {
1: [
{
accountUuid: '1'
}
]
],
},
roles: [
{
displayName: 'admin',
Expand Down
Loading

0 comments on commit f2a06ad

Please sign in to comment.