adapt change user formular unit tests to changes

This commit is contained in:
mahula 2023-03-31 09:30:15 +02:00
parent 941663af36
commit f238a3ddb1

View File

@ -2,6 +2,7 @@ import { mount } from '@vue/test-utils'
import ChangeUserRoleFormular from './ChangeUserRoleFormular' import ChangeUserRoleFormular from './ChangeUserRoleFormular'
import { setUserRole } from '../graphql/setUserRole' import { setUserRole } from '../graphql/setUserRole'
import { toastSuccessSpy, toastErrorSpy } from '../../test/testSetup' import { toastSuccessSpy, toastErrorSpy } from '../../test/testSetup'
import { BIconFileEarmarkSpreadsheet } from 'bootstrap-vue'
const localVue = global.localVue const localVue = global.localVue
@ -28,6 +29,7 @@ const mocks = {
let propsData let propsData
let wrapper let wrapper
let spy
describe('ChangeUserRoleFormular', () => { describe('ChangeUserRoleFormular', () => {
const Wrapper = () => { const Wrapper = () => {
@ -70,12 +72,16 @@ describe('ChangeUserRoleFormular', () => {
expect(wrapper.text()).toContain('userRole.notChangeYourSelf') expect(wrapper.text()).toContain('userRole.notChangeYourSelf')
}) })
it('has role select disabled', () => { it('has no role select', () => {
expect(wrapper.find('select[disabled="disabled"]').exists()).toBe(true) expect(wrapper.find('select.role-select').exists()).toBe(false)
})
it('has no button', () => {
expect(wrapper.find('button.btn.btn-dange').exists()).toBe(false)
}) })
}) })
describe('change others role', () => { describe("change other user's role", () => {
let rolesToSelect let rolesToSelect
describe('general', () => { describe('general', () => {
@ -106,19 +112,12 @@ describe('ChangeUserRoleFormular', () => {
expect(wrapper.find('select.role-select[disabled="disabled"]').exists()).toBe(false) expect(wrapper.find('select.role-select[disabled="disabled"]').exists()).toBe(false)
}) })
describe('on API error', () => { it('has "change_user_role" button', () => {
beforeEach(() => { expect(wrapper.find('button.btn.btn-danger').text()).toBe('change_user_role')
apolloMutateMock.mockRejectedValue({ message: 'Oh no!' })
rolesToSelect.at(1).setSelected()
})
it('toasts an error message', () => {
expect(toastErrorSpy).toBeCalledWith('Oh no!')
})
}) })
}) })
describe('user is usual user', () => { describe('user has role "usual user"', () => {
beforeEach(() => { beforeEach(() => {
apolloMutateMock.mockResolvedValue({ apolloMutateMock.mockResolvedValue({
data: { data: {
@ -141,6 +140,10 @@ describe('ChangeUserRoleFormular', () => {
describe('change select to', () => { describe('change select to', () => {
describe('same role', () => { describe('same role', () => {
it('has "change_user_role" button disabled', () => {
expect(wrapper.find('button.btn.btn-danger[disabled="disabled"]').exists()).toBe(true)
})
it('does not call the API', () => { it('does not call the API', () => {
rolesToSelect.at(0).setSelected() rolesToSelect.at(0).setSelected()
expect(apolloMutateMock).not.toHaveBeenCalled() expect(apolloMutateMock).not.toHaveBeenCalled()
@ -152,6 +155,25 @@ describe('ChangeUserRoleFormular', () => {
rolesToSelect.at(1).setSelected() rolesToSelect.at(1).setSelected()
}) })
it('has "change_user_role" button enabled', () => {
expect(wrapper.find('button.btn.btn-danger').exists()).toBe(true)
expect(wrapper.find('button.btn.btn-danger[disabled="disabled"]').exists()).toBe(false)
})
describe('clicking the "change_user_role" button', () => {
beforeEach(async () => {
spy = jest.spyOn(wrapper.vm.$bvModal, 'msgBoxConfirm')
spy.mockImplementation(() => Promise.resolve(true))
await wrapper.find('button').trigger('click')
await wrapper.vm.$nextTick()
})
it('calls the modal', () => {
expect(wrapper.emitted('showModal'))
expect(spy).toHaveBeenCalled()
})
describe('confirm role change with success', () => {
it('calls the API', () => { it('calls the API', () => {
expect(apolloMutateMock).toBeCalledWith( expect(apolloMutateMock).toBeCalledWith(
expect.objectContaining({ expect.objectContaining({
@ -181,10 +203,25 @@ describe('ChangeUserRoleFormular', () => {
expect(toastSuccessSpy).toBeCalledWith('userRole.successfullyChangedTo') expect(toastSuccessSpy).toBeCalledWith('userRole.successfullyChangedTo')
}) })
}) })
describe('confirm role change with error', () => {
beforeEach(async () => {
spy = jest.spyOn(wrapper.vm.$bvModal, 'msgBoxConfirm')
apolloMutateMock.mockRejectedValue({ message: 'Oh no!' })
await wrapper.find('button').trigger('click')
await wrapper.vm.$nextTick()
})
it('toasts an error message', () => {
expect(toastErrorSpy).toBeCalledWith('Oh no!')
})
})
})
})
}) })
}) })
describe('user is admin', () => { describe('user has role "admin"', () => {
beforeEach(() => { beforeEach(() => {
apolloMutateMock.mockResolvedValue({ apolloMutateMock.mockResolvedValue({
data: { data: {
@ -207,6 +244,10 @@ describe('ChangeUserRoleFormular', () => {
describe('change select to', () => { describe('change select to', () => {
describe('same role', () => { describe('same role', () => {
it('has "change_user_role" button disabled', () => {
expect(wrapper.find('button.btn.btn-danger[disabled="disabled"]').exists()).toBe(true)
})
it('does not call the API', () => { it('does not call the API', () => {
rolesToSelect.at(1).setSelected() rolesToSelect.at(1).setSelected()
expect(apolloMutateMock).not.toHaveBeenCalled() expect(apolloMutateMock).not.toHaveBeenCalled()
@ -218,6 +259,25 @@ describe('ChangeUserRoleFormular', () => {
rolesToSelect.at(0).setSelected() rolesToSelect.at(0).setSelected()
}) })
it('has "change_user_role" button enabled', () => {
expect(wrapper.find('button.btn.btn-danger').exists()).toBe(true)
expect(wrapper.find('button.btn.btn-danger[disabled="disabled"]').exists()).toBe(false)
})
describe('clicking the "change_user_role" button', () => {
beforeEach(async () => {
spy = jest.spyOn(wrapper.vm.$bvModal, 'msgBoxConfirm')
spy.mockImplementation(() => Promise.resolve(true))
await wrapper.find('button').trigger('click')
await wrapper.vm.$nextTick()
})
it('calls the modal', () => {
expect(wrapper.emitted('showModal'))
expect(spy).toHaveBeenCalled()
})
describe('confirm role change with success', () => {
it('calls the API', () => { it('calls the API', () => {
expect(apolloMutateMock).toBeCalledWith( expect(apolloMutateMock).toBeCalledWith(
expect.objectContaining({ expect.objectContaining({
@ -247,6 +307,21 @@ describe('ChangeUserRoleFormular', () => {
expect(toastSuccessSpy).toBeCalledWith('userRole.successfullyChangedTo') expect(toastSuccessSpy).toBeCalledWith('userRole.successfullyChangedTo')
}) })
}) })
describe('confirm role change with error', () => {
beforeEach(async () => {
spy = jest.spyOn(wrapper.vm.$bvModal, 'msgBoxConfirm')
apolloMutateMock.mockRejectedValue({ message: 'Oh no!' })
await wrapper.find('button').trigger('click')
await wrapper.vm.$nextTick()
})
it('toasts an error message', () => {
expect(toastErrorSpy).toBeCalledWith('Oh no!')
})
})
})
})
}) })
}) })
}) })