blessing-skin-server/resources/assets/tests/components/user/Profile.test.js

204 lines
6.3 KiB
JavaScript
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'
import Profile from '@/components/user/Profile'
import toastr from 'toastr'
import { swal } from '@/js/notify'
2018-08-02 17:29:43 +08:00
2019-03-15 11:42:41 +08:00
jest.mock('@/js/notify')
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 }
const wrapper = mount(Profile)
expect(wrapper.vm.siteName).toBe('Blessing Skin')
expect(wrapper.vm.isAdmin).toBeTrue()
window.blessing.extra = { admin: false }
expect(mount(Profile).vm.isAdmin).toBeFalse()
})
2019-03-17 10:21:18 +08:00
2018-08-02 17:29:43 +08:00
test('convert linebreak', () => {
2019-03-15 11:42:41 +08:00
const wrapper = mount(Profile)
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 () => {
jest.spyOn(toastr, 'success')
swal.mockResolvedValueOnce({})
.mockResolvedValueOnce({ dismiss: 1 })
.mockResolvedValue({})
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()
expect(toastr.success).toBeCalledWith('ok')
expect(document.querySelector('img').src).toMatch(/\d+$/)
})
2018-08-02 17:29:43 +08:00
test('change password', async () => {
2019-03-15 11:42:41 +08:00
jest.spyOn(toastr, 'info')
Vue.prototype.$http.post
.mockResolvedValueOnce({ errno: 1, msg: 'w' })
.mockResolvedValueOnce({ errno: 0, msg: 'o' })
swal.mockResolvedValue()
const wrapper = mount(Profile)
const button = wrapper.find('[data-test=changePassword]')
button.trigger('click')
expect(toastr.info).toBeCalledWith('user.emptyPassword')
expect(Vue.prototype.$http.post).not.toBeCalled()
wrapper.setData({ oldPassword: '1' })
button.trigger('click')
expect(toastr.info).toBeCalledWith('user.emptyNewPassword')
expect(Vue.prototype.$http.post).not.toBeCalled()
wrapper.setData({ newPassword: '1' })
button.trigger('click')
expect(toastr.info).toBeCalledWith('auth.emptyConfirmPwd')
expect(Vue.prototype.$http.post).not.toBeCalled()
wrapper.setData({ confirmPassword: '2' })
button.trigger('click')
expect(toastr.info).toBeCalledWith('auth.invalidConfirmPwd')
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' }
)
expect(swal).toBeCalledWith({ type: 'warning', text: 'w' })
button.trigger('click')
await wrapper.vm.$nextTick()
expect(swal).toBeCalledWith({ type: 'success', text: 'o' })
})
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' })
swal.mockResolvedValueOnce({})
.mockResolvedValueOnce({ dismiss: 1 })
.mockResolvedValue({})
window.$ = jest.fn(() => ({
each(fn) {
fn()
},
text() {},
}))
const wrapper = mount(Profile)
const button = wrapper.find('[data-test=changeNickName]')
button.trigger('click')
expect(Vue.prototype.$http.post).not.toBeCalled()
expect(swal).toBeCalledWith({ type: 'error', text: 'user.emptyNewNickName' })
wrapper.setData({ nickname: 'nickname' })
button.trigger('click')
expect(Vue.prototype.$http.post).not.toBeCalled()
expect(swal).toBeCalledWith({
text: 'user.changeNickName',
type: 'question',
showCancelButton: true,
})
button.trigger('click')
await wrapper.vm.$nextTick()
expect(Vue.prototype.$http.post).toBeCalledWith(
'/user/profile?action=nickname',
{ new_nickname: 'nickname' }
)
await wrapper.vm.$nextTick()
expect(swal).toBeCalledWith({ type: 'warning', text: 'w' })
button.trigger('click')
await flushPromises()
expect(swal).toBeCalledWith({ type: 'success', text: 'o' })
})
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' })
swal.mockResolvedValueOnce({})
.mockResolvedValueOnce({})
.mockResolvedValueOnce({ dismiss: 1 })
.mockResolvedValue({})
const wrapper = mount(Profile)
const button = wrapper.find('[data-test=changeEmail]')
button.trigger('click')
expect(swal).toBeCalledWith({ type: 'error', text: 'user.emptyNewEmail' })
expect(Vue.prototype.$http.post).not.toBeCalled()
wrapper.setData({ email: 'e' })
button.trigger('click')
expect(swal).toBeCalledWith({ type: 'warning', text: 'auth.invalidEmail' })
expect(Vue.prototype.$http.post).not.toBeCalled()
wrapper.setData({ email: 'a@b.c', currentPassword: 'abc' })
button.trigger('click')
expect(swal).toBeCalledWith({
text: 'user.changeEmail',
type: 'question',
showCancelButton: true,
})
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()
expect(swal).toBeCalledWith({ type: 'warning', text: 'w' })
button.trigger('click')
await flushPromises()
expect(swal).toBeCalledWith({ type: 'success', text: 'o' })
})
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 }
swal.mockResolvedValue()
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')
expect(swal).toBeCalledWith({ type: 'warning', text: 'user.emptyDeletePassword' })
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()
expect(swal).toBeCalledWith({ type: 'warning', text: 'w' })
button.trigger('click')
await wrapper.vm.$nextTick()
expect(swal).toBeCalledWith({ type: 'success', text: 'o' })
})