blessing-skin-server/resources/assets/tests/views/user/Profile.test.ts

183 lines
6.2 KiB
TypeScript
Raw Normal View History

2019-03-15 11:42:41 +08:00
import Vue from 'vue'
import { mount } from '@vue/test-utils'
import { flushPromises } from '../../utils'
2019-03-18 15:00:18 +08:00
import Profile from '@/views/user/Profile.vue'
2018-08-02 17:29:43 +08:00
2019-03-15 11:42:41 +08:00
window.blessing.extra = { unverified: false }
2018-08-17 12:32:44 +08:00
2018-08-02 17:29:43 +08:00
test('computed values', () => {
2019-03-15 11:42:41 +08:00
window.blessing.extra = { admin: true }
2019-03-18 09:55:24 +08:00
const wrapper = mount<Vue & { siteName: string, isAdmin: boolean }>(Profile)
2019-03-15 11:42:41 +08:00
expect(wrapper.vm.siteName).toBe('Blessing Skin')
expect(wrapper.vm.isAdmin).toBeTrue()
window.blessing.extra = { admin: false }
2019-03-18 09:55:24 +08:00
expect(mount<Vue & { isAdmin: boolean }>(Profile).vm.isAdmin).toBeFalse()
2019-03-15 11:42:41 +08:00
})
2019-03-17 10:21:18 +08:00
2018-08-02 17:29:43 +08:00
test('convert linebreak', () => {
2019-03-18 09:55:24 +08:00
const wrapper = mount<Vue & { nl2br(input: string): string }>(Profile)
2019-03-15 11:42:41 +08:00
expect(wrapper.vm.nl2br('a\nb\nc')).toBe('a<br>b<br>c')
})
2019-03-17 09:46:02 +08:00
test('reset avatar', async () => {
2019-03-25 22:01:57 +08:00
Vue.prototype.$confirm
.mockRejectedValueOnce('close')
.mockResolvedValue('confirm')
2019-03-17 09:46:02 +08:00
Vue.prototype.$http.post.mockResolvedValue({ msg: 'ok' })
const wrapper = mount(Profile)
const button = wrapper.find('[data-test=resetAvatar]')
document.body.innerHTML += '<img alt="User Image" src="a">'
button.trigger('click')
expect(Vue.prototype.$http.post).not.toBeCalled()
button.trigger('click')
await wrapper.vm.$nextTick()
expect(Vue.prototype.$http.post).toBeCalledWith(
'/user/profile/avatar',
{ tid: 0 }
)
await flushPromises()
2019-03-25 22:01:57 +08:00
expect(Vue.prototype.$message.success).toBeCalledWith('ok')
2019-03-18 09:55:24 +08:00
expect(document.querySelector('img')!.src).toMatch(/\d+$/)
2019-03-17 09:46:02 +08:00
})
2018-08-02 17:29:43 +08:00
test('change password', async () => {
2019-03-15 11:42:41 +08:00
Vue.prototype.$http.post
.mockResolvedValueOnce({ errno: 1, msg: 'w' })
.mockResolvedValueOnce({ errno: 0, msg: 'o' })
const wrapper = mount(Profile)
const button = wrapper.find('[data-test=changePassword]')
button.trigger('click')
2019-03-25 22:01:57 +08:00
expect(Vue.prototype.$message.error).toBeCalledWith('user.emptyPassword')
2019-03-15 11:42:41 +08:00
expect(Vue.prototype.$http.post).not.toBeCalled()
wrapper.setData({ oldPassword: '1' })
button.trigger('click')
2019-03-25 22:01:57 +08:00
expect(Vue.prototype.$message.error).toBeCalledWith('user.emptyNewPassword')
2019-03-15 11:42:41 +08:00
expect(Vue.prototype.$http.post).not.toBeCalled()
wrapper.setData({ newPassword: '1' })
button.trigger('click')
2019-03-25 22:01:57 +08:00
expect(Vue.prototype.$message.error).toBeCalledWith('auth.emptyConfirmPwd')
2019-03-15 11:42:41 +08:00
expect(Vue.prototype.$http.post).not.toBeCalled()
wrapper.setData({ confirmPassword: '2' })
button.trigger('click')
2019-03-25 22:01:57 +08:00
expect(Vue.prototype.$message.error).toBeCalledWith('auth.invalidConfirmPwd')
2019-03-15 11:42:41 +08:00
expect(Vue.prototype.$http.post).not.toBeCalled()
wrapper.setData({ confirmPassword: '1' })
button.trigger('click')
await wrapper.vm.$nextTick()
expect(Vue.prototype.$http.post).toBeCalledWith(
'/user/profile?action=password',
{ current_password: '1', new_password: '1' }
)
2019-03-25 22:01:57 +08:00
expect(Vue.prototype.$alert).toBeCalledWith('w', { type: 'warning' })
2019-03-15 11:42:41 +08:00
button.trigger('click')
await wrapper.vm.$nextTick()
2019-03-25 22:01:57 +08:00
expect(Vue.prototype.$alert).toBeCalledWith('o')
2019-03-15 11:42:41 +08:00
})
2019-03-17 10:21:18 +08:00
2018-08-02 17:29:43 +08:00
test('change nickname', async () => {
2019-03-15 11:42:41 +08:00
Vue.prototype.$http.post
.mockResolvedValueOnce({ errno: 1, msg: 'w' })
.mockResolvedValue({ errno: 0, msg: 'o' })
2019-03-25 22:01:57 +08:00
Vue.prototype.$confirm
.mockRejectedValueOnce('close')
.mockResolvedValue('confirm')
2019-03-15 11:42:41 +08:00
const wrapper = mount(Profile)
const button = wrapper.find('[data-test=changeNickName]')
2019-03-17 15:59:52 +08:00
document.body.innerHTML += '<span class="nickname"></span>'
2019-03-15 11:42:41 +08:00
button.trigger('click')
expect(Vue.prototype.$http.post).not.toBeCalled()
2019-03-25 22:01:57 +08:00
expect(Vue.prototype.$alert).toBeCalledWith('user.emptyNewNickName', { type: 'error' })
2019-03-15 11:42:41 +08:00
wrapper.setData({ nickname: 'nickname' })
button.trigger('click')
expect(Vue.prototype.$http.post).not.toBeCalled()
2019-03-25 22:01:57 +08:00
expect(Vue.prototype.$confirm).toBeCalledWith('user.changeNickName')
2019-03-15 11:42:41 +08:00
button.trigger('click')
await wrapper.vm.$nextTick()
expect(Vue.prototype.$http.post).toBeCalledWith(
'/user/profile?action=nickname',
{ new_nickname: 'nickname' }
)
await wrapper.vm.$nextTick()
2019-03-25 22:01:57 +08:00
expect(Vue.prototype.$alert).toBeCalledWith('w', { type: 'warning' })
2019-03-15 11:42:41 +08:00
button.trigger('click')
await flushPromises()
2019-03-25 22:01:57 +08:00
expect(Vue.prototype.$message.success).toBeCalledWith('o')
2019-03-18 09:55:24 +08:00
expect(document.querySelector('.nickname')!.textContent).toBe('nickname')
2019-03-15 11:42:41 +08:00
})
2019-03-17 10:21:18 +08:00
2018-08-02 17:29:43 +08:00
test('change email', async () => {
2019-03-15 11:42:41 +08:00
Vue.prototype.$http.post
.mockResolvedValueOnce({ errno: 1, msg: 'w' })
.mockResolvedValue({ errno: 0, msg: 'o' })
2019-03-25 22:01:57 +08:00
Vue.prototype.$confirm
.mockRejectedValueOnce('close')
.mockResolvedValue('confirm')
2019-03-15 11:42:41 +08:00
const wrapper = mount(Profile)
const button = wrapper.find('[data-test=changeEmail]')
button.trigger('click')
2019-03-25 22:01:57 +08:00
expect(Vue.prototype.$alert).toBeCalledWith('user.emptyNewEmail', { type: 'error' })
2019-03-15 11:42:41 +08:00
expect(Vue.prototype.$http.post).not.toBeCalled()
wrapper.setData({ email: 'e' })
button.trigger('click')
2019-03-25 22:01:57 +08:00
expect(Vue.prototype.$alert).toBeCalledWith('auth.invalidEmail', { type: 'warning' })
2019-03-15 11:42:41 +08:00
expect(Vue.prototype.$http.post).not.toBeCalled()
wrapper.setData({ email: 'a@b.c', currentPassword: 'abc' })
button.trigger('click')
2019-03-25 22:01:57 +08:00
expect(Vue.prototype.$confirm).toBeCalledWith('user.changeEmail')
2019-03-15 11:42:41 +08:00
expect(Vue.prototype.$http.post).not.toBeCalled()
button.trigger('click')
await wrapper.vm.$nextTick()
expect(Vue.prototype.$http.post).toBeCalledWith(
'/user/profile?action=email',
{ new_email: 'a@b.c', password: 'abc' }
)
await wrapper.vm.$nextTick()
2019-03-25 22:01:57 +08:00
expect(Vue.prototype.$alert).toBeCalledWith('w', { type: 'warning' })
2019-03-15 11:42:41 +08:00
button.trigger('click')
await flushPromises()
2019-03-25 22:01:57 +08:00
expect(Vue.prototype.$message.success).toBeCalledWith('o')
2019-03-15 11:42:41 +08:00
})
2019-03-17 10:21:18 +08:00
2018-08-02 17:29:43 +08:00
test('delete account', async () => {
2019-03-15 11:42:41 +08:00
window.blessing.extra = { admin: true }
Vue.prototype.$http.post
.mockResolvedValueOnce({ errno: 1, msg: 'w' })
.mockResolvedValue({ errno: 0, msg: 'o' })
const wrapper = mount(Profile)
const button = wrapper.find('[data-test=deleteAccount]')
button.trigger('click')
2019-03-25 22:01:57 +08:00
expect(Vue.prototype.$alert).toBeCalledWith('user.emptyDeletePassword', { type: 'error' })
2019-03-15 11:42:41 +08:00
expect(Vue.prototype.$http.post).not.toBeCalled()
wrapper.setData({ deleteConfirm: 'abc' })
button.trigger('click')
expect(Vue.prototype.$http.post).toBeCalledWith(
'/user/profile?action=delete',
{ password: 'abc' }
)
await wrapper.vm.$nextTick()
2019-03-25 22:01:57 +08:00
expect(Vue.prototype.$alert).toBeCalledWith('w', { type: 'warning' })
2019-03-15 11:42:41 +08:00
button.trigger('click')
await wrapper.vm.$nextTick()
2019-03-25 22:01:57 +08:00
expect(Vue.prototype.$alert).toBeCalledWith('o', { type: 'success' })
2019-03-15 11:42:41 +08:00
})