test user card form username

This commit is contained in:
Moriz Wahl 2021-06-14 22:56:57 +02:00
parent f39b2e2d39
commit 2ed08841a1
2 changed files with 126 additions and 2 deletions

View File

@ -0,0 +1,121 @@
import { mount } from '@vue/test-utils'
import { extend } from 'vee-validate'
import UserCardFormUsername from './UserCard_FormUsername'
import loginAPI from '../../../apis/loginAPI'
import flushPromises from 'flush-promises'
jest.mock('../../../apis/loginAPI')
extend('gddUsernameRgex', {
validate(value) {
return true
},
})
extend('gddUsernameUnique', {
validate(value) {
return true
},
})
const localVue = global.localVue
const mockAPIcall = jest.fn((args) => {
return { success: true }
})
loginAPI.changeUsernameProfile = mockAPIcall
describe('UserCard_FormUsername', () => {
let wrapper
const mocks = {
$t: jest.fn((t) => t),
$store: {
state: {
sessionId: 1,
email: 'user@example.org',
username: '',
},
commit: jest.fn(),
},
$toast: {
success: jest.fn(),
},
}
const Wrapper = () => {
return mount(UserCardFormUsername, { localVue, mocks })
}
describe('mount', () => {
beforeEach(() => {
wrapper = Wrapper()
})
it('renders the component', () => {
expect(wrapper.find('div#formusername').exists()).toBeTruthy()
})
describe('username in store is empty', () => {
it('renders an empty username', () => {
expect(wrapper.find('div.display-username').text()).toEqual('@')
})
it('has an edit icon', () => {
expect(wrapper.find('svg.bi-pencil').exists()).toBeTruthy()
})
describe('edit username', () => {
beforeEach(async () => {
await wrapper.find('svg.bi-pencil').trigger('click')
})
it('shows an input field for the username', () => {
expect(wrapper.find('input[placeholder="Username"]').exists()).toBeTruthy()
})
it('shows an cancel icon', () => {
expect(wrapper.find('svg.bi-x-circle').exists()).toBeTruthy()
})
it('closes the input when cancel icon is clicked', async () => {
wrapper.find('svg.bi-x-circle').trigger('click')
await wrapper.vm.$nextTick()
expect(wrapper.find('input[placeholder="Username"]').exists()).toBeFalsy()
})
it('does not change the username when cancel is clicked', async () => {
wrapper.find('input[placeholder="Username"]').setValue('username')
wrapper.find('svg.bi-x-circle').trigger('click')
await wrapper.vm.$nextTick()
expect(wrapper.find('div.display-username').text()).toEqual('@')
})
it('has a submit button', () => {
expect(wrapper.find('button[type="submit"]').exists()).toBeTruthy()
})
describe('successfull submit', () => {
beforeEach(async () => {
await wrapper.find('input[placeholder="Username"]').setValue('username')
await wrapper.find('form').trigger('submit')
await flushPromises()
})
it('calls the loginAPI', () => {
expect(mockAPIcall).toHaveBeenCalledWith(1, 'user@example.org', 'username')
})
it('displays the new username', () => {
expect(wrapper.find('div.display-username').text()).toEqual('@username')
})
it('has no edit button anymore', () => {
expect(wrapper.find('svg.bi-pencil').exists()).toBeFalsy()
})
})
})
})
})
})

View File

@ -26,7 +26,7 @@
<b-col class="col-lg-3 col-md-10 col-sm-10 text-md-left text-lg-right">
<small>{{ $t('form.username') }}</small>
</b-col>
<b-col class="col-md-9 col-sm-10">@{{ form.username }}</b-col>
<b-col class="col-md-9 col-sm-10 display-username">@{{ username }}</b-col>
</b-row>
</b-container>
<b-container v-else>
@ -97,7 +97,10 @@ export default {
this.showUsername = true
this.$toast.success(this.$t('site.profil.user-data.change-success'))
} else {
alert(result.result.message)
this.$toast.error(result.result.message)
this.showUsername = true
this.username = this.$store.state.username
this.form.username = this.$store.state.username
}
},
},