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

180 lines
6.0 KiB
JavaScript
Raw Normal View History

2018-08-06 13:16:10 +08:00
import Vue from 'vue';
2018-08-02 17:29:43 +08:00
import { mount } from '@vue/test-utils';
2018-08-06 09:30:54 +08:00
import { flushPromises } from '../../utils';
2018-08-02 17:29:43 +08:00
import Profile from '@/components/user/Profile';
import toastr from 'toastr';
import { swal } from '@/js/notify';
jest.mock('@/js/notify');
2018-09-09 09:28:05 +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', () => {
2018-09-09 09:28:05 +08:00
window.blessing.extra = { admin: true };
2018-08-02 17:29:43 +08:00
const wrapper = mount(Profile);
expect(wrapper.vm.siteName).toBe('Blessing Skin');
expect(wrapper.vm.isAdmin).toBeTrue();
2018-09-09 09:28:05 +08:00
window.blessing.extra = { admin: false };
2018-08-02 17:29:43 +08:00
expect(mount(Profile).vm.isAdmin).toBeFalse();
});
test('convert linebreak', () => {
const wrapper = mount(Profile);
expect(wrapper.vm.nl2br('a\nb\nc')).toBe('a<br>b<br>c');
});
test('change password', async () => {
jest.spyOn(toastr, 'info');
2018-08-06 13:16:10 +08:00
Vue.prototype.$http.post
.mockResolvedValueOnce({ errno: 1, msg: 'w' })
.mockResolvedValueOnce({ errno: 0, msg: 'o' });
2018-08-02 17:29:43 +08:00
swal.mockResolvedValue();
const wrapper = mount(Profile);
const button = wrapper.find('[data-test=changePassword]');
button.trigger('click');
expect(toastr.info).toBeCalledWith('user.emptyPassword');
2018-08-06 13:16:10 +08:00
expect(Vue.prototype.$http.post).not.toBeCalled();
2018-08-02 17:29:43 +08:00
wrapper.setData({ oldPassword: '1' });
button.trigger('click');
expect(toastr.info).toBeCalledWith('user.emptyNewPassword');
2018-08-06 13:16:10 +08:00
expect(Vue.prototype.$http.post).not.toBeCalled();
2018-08-02 17:29:43 +08:00
wrapper.setData({ newPassword: '1' });
button.trigger('click');
expect(toastr.info).toBeCalledWith('auth.emptyConfirmPwd');
2018-08-06 13:16:10 +08:00
expect(Vue.prototype.$http.post).not.toBeCalled();
2018-08-02 17:29:43 +08:00
wrapper.setData({ confirmPassword: '2' });
button.trigger('click');
expect(toastr.info).toBeCalledWith('auth.invalidConfirmPwd');
2018-08-06 13:16:10 +08:00
expect(Vue.prototype.$http.post).not.toBeCalled();
2018-08-02 17:29:43 +08:00
wrapper.setData({ confirmPassword: '1' });
button.trigger('click');
await wrapper.vm.$nextTick();
2018-08-06 13:16:10 +08:00
expect(Vue.prototype.$http.post).toBeCalledWith(
2018-08-02 17:29:43 +08:00
'/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' });
});
test('change nickname', async () => {
2018-08-06 13:16:10 +08:00
Vue.prototype.$http.post
.mockResolvedValueOnce({ errno: 1, msg: 'w' })
.mockResolvedValue({ errno: 0, msg: 'o' });
2018-08-02 17:29:43 +08:00
swal.mockResolvedValueOnce({})
.mockResolvedValueOnce({ dismiss: 1 })
.mockResolvedValue({});
window.$ = jest.fn(() => ({
each(fn) {
fn();
},
2018-08-14 23:43:56 +08:00
text() {}
2018-08-02 17:29:43 +08:00
}));
const wrapper = mount(Profile);
const button = wrapper.find('[data-test=changeNickName]');
button.trigger('click');
2018-08-06 13:16:10 +08:00
expect(Vue.prototype.$http.post).not.toBeCalled();
2018-08-14 23:43:56 +08:00
expect(swal).toBeCalledWith({ type: 'error', text: 'user.emptyNewNickName' });
2018-08-02 17:29:43 +08:00
wrapper.setData({ nickname: 'nickname' });
button.trigger('click');
2018-08-06 13:16:10 +08:00
expect(Vue.prototype.$http.post).not.toBeCalled();
2018-08-02 17:29:43 +08:00
expect(swal).toBeCalledWith({
text: 'user.changeNickName',
type: 'question',
showCancelButton: true
});
button.trigger('click');
await wrapper.vm.$nextTick();
2018-08-06 13:16:10 +08:00
expect(Vue.prototype.$http.post).toBeCalledWith(
2018-08-02 17:29:43 +08:00
'/user/profile?action=nickname',
{ new_nickname: 'nickname' }
);
await wrapper.vm.$nextTick();
2018-08-14 23:43:56 +08:00
expect(swal).toBeCalledWith({ type: 'warning', text: 'w' });
2018-08-02 17:29:43 +08:00
button.trigger('click');
2018-08-06 09:30:54 +08:00
await flushPromises();
2018-08-14 23:43:56 +08:00
expect(swal).toBeCalledWith({ type: 'success', text: 'o' });
2018-08-02 17:29:43 +08:00
});
test('change email', async () => {
2018-08-06 13:16:10 +08:00
Vue.prototype.$http.post
.mockResolvedValueOnce({ errno: 1, msg: 'w' })
.mockResolvedValue({ errno: 0, msg: 'o' });
2018-08-02 17:29:43 +08:00
swal.mockResolvedValueOnce({})
.mockResolvedValueOnce({})
.mockResolvedValueOnce({ dismiss: 1 })
.mockResolvedValue({});
const wrapper = mount(Profile);
const button = wrapper.find('[data-test=changeEmail]');
button.trigger('click');
2018-08-14 23:43:56 +08:00
expect(swal).toBeCalledWith({ type: 'error', text: 'user.emptyNewEmail' });
2018-08-06 13:16:10 +08:00
expect(Vue.prototype.$http.post).not.toBeCalled();
2018-08-02 17:29:43 +08:00
wrapper.setData({ email: 'e' });
button.trigger('click');
2018-08-14 23:43:56 +08:00
expect(swal).toBeCalledWith({ type: 'warning', text: 'auth.invalidEmail' });
2018-08-06 13:16:10 +08:00
expect(Vue.prototype.$http.post).not.toBeCalled();
2018-08-02 17:29:43 +08:00
wrapper.setData({ email: 'a@b.c', currentPassword: 'abc' });
button.trigger('click');
expect(swal).toBeCalledWith({
text: 'user.changeEmail',
type: 'question',
showCancelButton: true
});
2018-08-06 13:16:10 +08:00
expect(Vue.prototype.$http.post).not.toBeCalled();
2018-08-02 17:29:43 +08:00
button.trigger('click');
await wrapper.vm.$nextTick();
2018-08-06 13:16:10 +08:00
expect(Vue.prototype.$http.post).toBeCalledWith(
2018-08-02 17:29:43 +08:00
'/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');
2018-08-06 09:30:54 +08:00
await flushPromises();
2018-08-02 17:29:43 +08:00
expect(swal).toBeCalledWith({ type: 'success', text: 'o' });
});
test('delete account', async () => {
2018-09-09 09:28:05 +08:00
window.blessing.extra = { admin: true };
2018-08-02 17:29:43 +08:00
swal.mockResolvedValue();
2018-08-06 13:16:10 +08:00
Vue.prototype.$http.post
.mockResolvedValueOnce({ errno: 1, msg: 'w' })
.mockResolvedValue({ errno: 0, msg: 'o' });
2018-08-02 17:29:43 +08:00
const wrapper = mount(Profile);
const button = wrapper.find('[data-test=deleteAccount]');
button.trigger('click');
2018-08-14 23:43:56 +08:00
expect(swal).toBeCalledWith({ type: 'warning', text: 'user.emptyDeletePassword' });
2018-08-06 13:16:10 +08:00
expect(Vue.prototype.$http.post).not.toBeCalled();
2018-08-02 17:29:43 +08:00
wrapper.setData({ deleteConfirm: 'abc' });
button.trigger('click');
2018-08-06 13:16:10 +08:00
expect(Vue.prototype.$http.post).toBeCalledWith(
2018-08-02 17:29:43 +08:00
'/user/profile?action=delete',
{ password: 'abc' }
);
await wrapper.vm.$nextTick();
2018-08-14 23:43:56 +08:00
expect(swal).toBeCalledWith({ type: 'warning', text: 'w' });
2018-08-02 17:29:43 +08:00
button.trigger('click');
await wrapper.vm.$nextTick();
2018-08-14 23:43:56 +08:00
expect(swal).toBeCalledWith({ type: 'success', text: 'o' });
2018-08-02 17:29:43 +08:00
});