feat(test): add UserEditForm tests

This commit is contained in:
Phan An 2022-07-22 18:43:32 +02:00
parent 439882e761
commit 8a201b178f
No known key found for this signature in database
GPG key ID: A81E4477F0BB6FDC
2 changed files with 61 additions and 12 deletions

View file

@ -0,0 +1,43 @@
import { ref } from 'vue'
import { expect, it } from 'vitest'
import factory from '@/__tests__/factory'
import UnitTestCase from '@/__tests__/UnitTestCase'
import { UserKey } from '@/symbols'
import { fireEvent, waitFor } from '@testing-library/vue'
import { userStore } from '@/stores'
import { alerts } from '@/utils'
import UserEditForm from './UserEditForm.vue'
new class extends UnitTestCase {
protected test () {
it('edits a user', async () => {
const updateMock = this.mock(userStore, 'update')
const alertMock = this.mock(alerts, 'success')
const user = ref(factory<User>('user', { name: 'John Doe' }))
const { getByLabelText, getByRole } = this.render(UserEditForm, {
global: {
provide: {
[UserKey]: [user]
}
}
})
await fireEvent.update(getByLabelText('Name'), 'Jane Doe')
await fireEvent.update(getByLabelText('Password'), 'new-password-duck')
await fireEvent.click(getByRole('button', { name: 'Update' }))
await waitFor(() => {
expect(updateMock).toHaveBeenCalledWith(user.value, {
name: 'Jane Doe',
email: user.value.email,
is_admin: user.value.is_admin,
password: 'new-password-duck'
})
expect(alertMock).toHaveBeenCalledWith('User profile updated.')
})
})
}
}

View file

@ -8,22 +8,28 @@
<main>
<div class="form-row">
<label>Name</label>
<input v-model="updateData.name" v-koel-focus name="name" required title="Name" type="text">
<label>
Name
<input v-model="updateData.name" v-koel-focus name="name" required title="Name" type="text">
</label>
</div>
<div class="form-row">
<label>Email</label>
<input v-model="updateData.email" name="email" required title="Email" type="email">
<label>
Email
<input v-model="updateData.email" name="email" required title="Email" type="email">
</label>
</div>
<div class="form-row">
<label>Password</label>
<input
v-model="updateData.password"
autocomplete="new-password"
name="password"
placeholder="Leave blank for no changes"
type="password"
>
<label>
Password
<input
v-model="updateData.password"
autocomplete="new-password"
name="password"
placeholder="Leave blank for no changes"
type="password"
>
</label>
<p class="help">Min. 10 characters. Should be a mix of characters, numbers, and symbols.</p>
</div>
<div class="form-row">