blessing-skin-server/resources/assets/tests/views/admin/Users.test.ts

501 lines
14 KiB
TypeScript
Raw Normal View History

2019-03-15 11:42:41 +08:00
import Vue from 'vue'
import { mount } from '@vue/test-utils'
2019-03-18 15:00:18 +08:00
import Users from '@/views/admin/Users.vue'
2019-03-28 16:37:01 +08:00
import '@/scripts/i18n'
2019-03-30 18:36:01 +08:00
import { Button } from 'element-ui'
2019-03-25 22:01:57 +08:00
import { MessageBoxData } from 'element-ui/types/message-box'
2019-03-18 09:55:24 +08:00
import { flushPromises } from '../../utils'
2019-03-15 11:42:41 +08:00
2019-03-28 16:37:01 +08:00
jest.mock('@/scripts/i18n', () => ({
2019-03-18 09:55:24 +08:00
trans: (key: string) => key,
2019-03-15 11:42:41 +08:00
}))
2018-08-06 12:14:20 +08:00
test('fetch data after initializing', () => {
2019-03-15 11:42:41 +08:00
Vue.prototype.$http.get.mockResolvedValue({ data: [] })
mount(Users)
expect(Vue.prototype.$http.get).toBeCalledWith(
'/admin/user-data',
{
page: 1, perPage: 10, search: '', sortField: 'uid', sortType: 'asc',
}
)
})
2018-08-13 11:08:14 +08:00
2018-08-06 12:14:20 +08:00
test('humanize permission', async () => {
2019-03-15 11:42:41 +08:00
Vue.prototype.$http.get.mockResolvedValue({
data: [
{ uid: 1, permission: -1 },
{ uid: 2, permission: 0 },
{ uid: 3, permission: 1 },
{ uid: 4, permission: 2 },
],
})
const wrapper = mount(Users)
await wrapper.vm.$nextTick()
const text = wrapper.find('.vgt-table').text()
expect(text).toContain('admin.banned')
expect(text).toContain('admin.normal')
expect(text).toContain('admin.admin')
expect(text).toContain('admin.superAdmin')
})
2019-03-17 10:21:18 +08:00
2018-08-06 12:14:20 +08:00
test('generate players page link', async () => {
2019-03-15 11:42:41 +08:00
Vue.prototype.$http.get.mockResolvedValue({
data: [
{ uid: 1, permission: 0 },
],
})
const wrapper = mount(Users)
await wrapper.vm.$nextTick()
expect(wrapper.find('[data-toggle="tooltip"]').attributes('href')).toBe('/admin/players?uid=1')
})
2019-03-17 10:21:18 +08:00
2019-03-18 13:24:03 +08:00
test('permission option should not be displayed for super admins', async () => {
2019-03-15 11:42:41 +08:00
Vue.prototype.$http.get.mockResolvedValue({
data: [
{ uid: 1, permission: 2 },
],
})
const wrapper = mount(Users)
await wrapper.vm.$nextTick()
2019-03-18 13:24:03 +08:00
expect(wrapper.find('[data-test=permission]').exists()).toBeFalse()
2019-03-15 11:42:41 +08:00
})
2019-03-17 10:21:18 +08:00
2019-03-18 13:24:03 +08:00
test('permission option should be displayed for admin as super admin', async () => {
2019-03-15 11:42:41 +08:00
Vue.prototype.$http.get.mockResolvedValue({
data: [
{
uid: 1, permission: 1, operations: 2,
},
],
})
const wrapper = mount(Users)
await wrapper.vm.$nextTick()
2019-03-18 13:24:03 +08:00
expect(wrapper.find('[data-test=permission]').exists()).toBeTrue()
2019-03-15 11:42:41 +08:00
})
2019-03-17 10:21:18 +08:00
2019-03-18 13:24:03 +08:00
test('permission option should be displayed for normal users as super admin', async () => {
2019-03-15 11:42:41 +08:00
Vue.prototype.$http.get.mockResolvedValue({
data: [
{
uid: 1, permission: 0, operations: 2,
},
],
})
const wrapper = mount(Users)
await wrapper.vm.$nextTick()
2019-03-18 13:24:03 +08:00
expect(wrapper.find('[data-test=permission]').exists()).toBeTrue()
2019-03-15 11:42:41 +08:00
})
2019-03-17 10:21:18 +08:00
2019-03-18 13:24:03 +08:00
test('permission option should be displayed for banned users as super admin', async () => {
2019-03-15 11:42:41 +08:00
Vue.prototype.$http.get.mockResolvedValue({
data: [
{
uid: 1, permission: -1, operations: 2,
},
],
})
const wrapper = mount(Users)
await wrapper.vm.$nextTick()
2019-03-18 13:24:03 +08:00
expect(wrapper.find('[data-test=permission]').exists()).toBeTrue()
2019-03-15 11:42:41 +08:00
})
2019-03-17 10:21:18 +08:00
2019-03-18 13:24:03 +08:00
test('permission option should not be displayed for other admins as admin', async () => {
2019-03-15 11:42:41 +08:00
Vue.prototype.$http.get.mockResolvedValue({
data: [
{
uid: 1, permission: 1, operations: 1,
},
],
})
const wrapper = mount(Users)
await wrapper.vm.$nextTick()
2019-03-18 13:24:03 +08:00
expect(wrapper.find('[data-test=permission]').exists()).toBeFalse()
2019-03-15 11:42:41 +08:00
})
2019-03-17 10:21:18 +08:00
2019-03-18 13:24:03 +08:00
test('permission option should be displayed for normal users as admin', async () => {
2019-03-15 11:42:41 +08:00
Vue.prototype.$http.get.mockResolvedValue({
data: [
{
uid: 1, permission: 0, operations: 1,
},
],
})
const wrapper = mount(Users)
await wrapper.vm.$nextTick()
2019-03-18 13:24:03 +08:00
expect(wrapper.find('[data-test=permission]').exists()).toBeTrue()
2019-03-15 11:42:41 +08:00
})
2019-03-17 10:21:18 +08:00
2019-03-18 13:24:03 +08:00
test('permission option should be displayed for banned users as admin', async () => {
2019-03-15 11:42:41 +08:00
Vue.prototype.$http.get.mockResolvedValue({
data: [
{
uid: 1, permission: -1, operations: 1,
},
],
})
const wrapper = mount(Users)
await wrapper.vm.$nextTick()
2019-03-18 13:24:03 +08:00
expect(wrapper.find('[data-test=permission]').exists()).toBeTrue()
2019-03-15 11:42:41 +08:00
})
2019-03-17 10:21:18 +08:00
2018-08-06 12:14:20 +08:00
test('deletion button should not be displayed for super admins', async () => {
2019-03-15 11:42:41 +08:00
Vue.prototype.$http.get.mockResolvedValue({
data: [
{ uid: 1, permission: 2 },
],
})
const wrapper = mount(Users)
await wrapper.vm.$nextTick()
2019-03-30 18:36:01 +08:00
expect(wrapper.find('[data-test="deleteUser"]').attributes('disabled')).toBe('disabled')
2019-03-15 11:42:41 +08:00
})
2019-03-17 10:21:18 +08:00
2018-08-06 12:14:20 +08:00
test('deletion button should be displayed for admins as super admin', async () => {
2019-03-15 11:42:41 +08:00
Vue.prototype.$http.get.mockResolvedValue({
data: [
{
uid: 1, permission: 1, operations: 2,
},
],
})
const wrapper = mount(Users)
await wrapper.vm.$nextTick()
2019-03-30 18:36:01 +08:00
expect(wrapper.find('[data-test="deleteUser"]').attributes('disabled')).toBeNil()
2019-03-15 11:42:41 +08:00
})
2019-03-17 10:21:18 +08:00
2018-08-06 12:14:20 +08:00
test('deletion button should be displayed for normal users as super admin', async () => {
2019-03-15 11:42:41 +08:00
Vue.prototype.$http.get.mockResolvedValue({
data: [
{
uid: 1, permission: 0, operations: 2,
},
],
})
const wrapper = mount(Users)
await wrapper.vm.$nextTick()
2019-03-30 18:36:01 +08:00
expect(wrapper.find('[data-test="deleteUser"]').attributes('disabled')).toBeNil()
2019-03-15 11:42:41 +08:00
})
2019-03-17 10:21:18 +08:00
2018-08-06 12:14:20 +08:00
test('deletion button should be displayed for banned users as super admin', async () => {
2019-03-15 11:42:41 +08:00
Vue.prototype.$http.get.mockResolvedValue({
data: [
{
uid: 1, permission: -1, operations: 2,
},
],
})
const wrapper = mount(Users)
await wrapper.vm.$nextTick()
2019-03-30 18:36:01 +08:00
expect(wrapper.find('[data-test="deleteUser"]').attributes('disabled')).toBeNil()
2019-03-15 11:42:41 +08:00
})
2019-03-17 10:21:18 +08:00
2018-08-06 12:14:20 +08:00
test('deletion button should not be displayed for other admins as admin', async () => {
2019-03-15 11:42:41 +08:00
Vue.prototype.$http.get.mockResolvedValue({
data: [
{
uid: 1, permission: 1, operations: 1,
},
],
})
const wrapper = mount(Users)
await wrapper.vm.$nextTick()
2019-03-30 18:36:01 +08:00
expect(wrapper.find('[data-test="deleteUser"]').attributes('disabled')).toBe('disabled')
2019-03-15 11:42:41 +08:00
})
2019-03-17 10:21:18 +08:00
2018-08-06 12:14:20 +08:00
test('deletion button should be displayed for normal users as admin', async () => {
2019-03-15 11:42:41 +08:00
Vue.prototype.$http.get.mockResolvedValue({
data: [
{
uid: 1, permission: 0, operations: 1,
},
],
})
const wrapper = mount(Users)
await wrapper.vm.$nextTick()
2019-03-30 18:36:01 +08:00
expect(wrapper.find('[data-test="deleteUser"]').attributes('disabled')).toBeNil()
2019-03-15 11:42:41 +08:00
})
2019-03-17 10:21:18 +08:00
2018-08-06 12:14:20 +08:00
test('deletion button should be displayed for banned users as admin', async () => {
2019-03-15 11:42:41 +08:00
Vue.prototype.$http.get.mockResolvedValue({
data: [
{
uid: 1, permission: -1, operations: 1,
},
],
})
const wrapper = mount(Users)
await wrapper.vm.$nextTick()
2019-03-30 18:36:01 +08:00
expect(wrapper.find('[data-test="deleteUser"]').attributes('disabled')).toBeNil()
2019-03-15 11:42:41 +08:00
})
2019-03-17 10:21:18 +08:00
2018-08-06 12:14:20 +08:00
test('change email', async () => {
2019-03-15 11:42:41 +08:00
Vue.prototype.$http.get.mockResolvedValue({
data: [
{ uid: 1, email: 'a@b.c' },
],
})
Vue.prototype.$http.post
2019-04-23 11:47:45 +08:00
.mockResolvedValueOnce({ code: 1, message: '1' })
.mockResolvedValueOnce({ code: 0, message: '0' })
2019-03-25 22:01:57 +08:00
Vue.prototype.$prompt
.mockImplementationOnce(() => Promise.reject())
.mockImplementation((_, options) => {
2019-03-18 09:55:24 +08:00
if (options.inputValidator) {
options.inputValidator('')
options.inputValidator('value')
}
2019-03-25 22:01:57 +08:00
return Promise.resolve({ value: 'd@e.f' } as MessageBoxData)
2019-03-15 11:42:41 +08:00
})
const wrapper = mount(Users)
await wrapper.vm.$nextTick()
const button = wrapper.find('[data-test="email"]')
button.trigger('click')
expect(Vue.prototype.$http.post).not.toBeCalled()
button.trigger('click')
await wrapper.vm.$nextTick()
expect(Vue.prototype.$http.post).toBeCalledWith(
'/admin/users?action=email',
{ uid: 1, email: 'd@e.f' }
)
expect(wrapper.text()).toContain('a@b.c')
button.trigger('click')
await flushPromises()
expect(wrapper.text()).toContain('d@e.f')
})
2019-03-17 10:21:18 +08:00
2018-08-17 12:32:44 +08:00
test('toggle verification', async () => {
2019-03-15 11:42:41 +08:00
Vue.prototype.$http.get.mockResolvedValue({
data: [
{ uid: 1, verified: false },
],
})
Vue.prototype.$http.post
2019-04-23 11:47:45 +08:00
.mockResolvedValueOnce({ code: 1, message: '1' })
.mockResolvedValueOnce({ code: 0, message: '0' })
2019-03-15 11:42:41 +08:00
const wrapper = mount(Users)
await wrapper.vm.$nextTick()
const button = wrapper.find('[data-test="verification"')
button.trigger('click')
await wrapper.vm.$nextTick()
expect(Vue.prototype.$http.post).toBeCalledWith(
'/admin/users?action=verification',
{ uid: 1 }
)
button.trigger('click')
await flushPromises()
expect(wrapper.text()).toContain('admin.verified')
})
2019-03-17 10:21:18 +08:00
2018-08-06 12:14:20 +08:00
test('change nickname', async () => {
2019-03-15 11:42:41 +08:00
Vue.prototype.$http.get.mockResolvedValue({
data: [
{ uid: 1, nickname: 'old' },
],
})
Vue.prototype.$http.post
2019-04-23 11:47:45 +08:00
.mockResolvedValueOnce({ code: 1, message: '1' })
.mockResolvedValueOnce({ code: 0, message: '0' })
2019-03-25 22:01:57 +08:00
Vue.prototype.$prompt
.mockImplementationOnce(() => Promise.reject())
.mockImplementation((_, options) => {
2019-03-18 09:55:24 +08:00
if (options.inputValidator) {
options.inputValidator('')
options.inputValidator('value')
}
2019-03-25 22:01:57 +08:00
return Promise.resolve({ value: 'new' } as MessageBoxData)
2019-03-15 11:42:41 +08:00
})
const wrapper = mount(Users)
await wrapper.vm.$nextTick()
const button = wrapper.find('[data-test="nickname"]')
button.trigger('click')
expect(Vue.prototype.$http.post).not.toBeCalled()
button.trigger('click')
await wrapper.vm.$nextTick()
expect(Vue.prototype.$http.post).toBeCalledWith(
'/admin/users?action=nickname',
{ uid: 1, nickname: 'new' }
)
expect(wrapper.text()).toContain('old')
button.trigger('click')
await flushPromises()
expect(wrapper.text()).toContain('new')
})
2019-03-17 10:21:18 +08:00
2018-08-06 12:14:20 +08:00
test('change password', async () => {
2019-03-15 11:42:41 +08:00
Vue.prototype.$http.get.mockResolvedValue({
data: [
{ uid: 1 },
],
})
Vue.prototype.$http.post
2019-04-23 11:47:45 +08:00
.mockResolvedValueOnce({ code: 0, message: '0' })
.mockResolvedValueOnce({ code: 1, message: '1' })
2019-03-25 22:01:57 +08:00
Vue.prototype.$prompt
.mockRejectedValueOnce('')
.mockResolvedValue({ value: 'password' }as MessageBoxData)
2019-03-15 11:42:41 +08:00
const wrapper = mount(Users)
await wrapper.vm.$nextTick()
2019-03-30 18:36:01 +08:00
const button = wrapper.findAll(Button).at(0)
2019-03-15 11:42:41 +08:00
button.trigger('click')
expect(Vue.prototype.$http.post).not.toBeCalled()
button.trigger('click')
await wrapper.vm.$nextTick()
expect(Vue.prototype.$http.post).toBeCalledWith(
'/admin/users?action=password',
{ uid: 1, password: 'password' }
)
await flushPromises()
2019-03-25 22:01:57 +08:00
expect(Vue.prototype.$message.success).toBeCalledWith('0')
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.warning).toBeCalledWith('1')
2019-03-15 11:42:41 +08:00
})
2019-03-17 10:21:18 +08:00
2018-08-06 12:14:20 +08:00
test('change score', async () => {
2019-03-15 11:42:41 +08:00
Vue.prototype.$http.get.mockResolvedValue({
data: [
{ uid: 1, score: 23 },
],
})
Vue.prototype.$http.post
2019-04-23 11:47:45 +08:00
.mockResolvedValueOnce({ code: 1, message: '1' })
.mockResolvedValueOnce({ code: 0, message: '0' })
2019-03-25 22:01:57 +08:00
Vue.prototype.$prompt
.mockRejectedValueOnce('')
.mockResolvedValue({ value: '45' }as MessageBoxData)
2019-03-15 11:42:41 +08:00
const wrapper = mount(Users)
await wrapper.vm.$nextTick()
const button = wrapper.find('[data-test="score"]')
button.trigger('click')
expect(Vue.prototype.$http.post).not.toBeCalled()
button.trigger('click')
await wrapper.vm.$nextTick()
expect(Vue.prototype.$http.post).toBeCalledWith(
'/admin/users?action=score',
{ uid: 1, score: 45 }
)
expect(wrapper.text()).toContain('23')
button.trigger('click')
await flushPromises()
expect(wrapper.text()).toContain('45')
})
2019-03-17 10:21:18 +08:00
2019-03-18 13:24:03 +08:00
test('change permission', async () => {
Vue.prototype.$http.get
.mockResolvedValueOnce({
data: [
{
uid: 1, permission: 0, operations: 2,
},
],
})
.mockResolvedValueOnce({
data: [
{
uid: 1, permission: 0, operations: 1,
},
],
})
2019-03-15 11:42:41 +08:00
Vue.prototype.$http.post
2019-04-23 11:47:45 +08:00
.mockResolvedValueOnce({ code: 1, message: '1' })
.mockResolvedValue({ code: 0, message: '0' })
2019-03-25 22:01:57 +08:00
Vue.prototype.$msgbox
.mockImplementationOnce(() => Promise.reject())
.mockImplementationOnce(options => {
if (options.message) {
const vnode = options.message as Vue.VNode
const elm = document.createElement('select')
elm.appendChild(document.createElement('option'))
elm.appendChild(document.createElement('option'))
elm.appendChild(document.createElement('option'))
elm.selectedIndex = 2
;(vnode.children as Vue.VNode[])[1].elm = elm
}
return Promise.resolve({} as MessageBoxData)
})
.mockImplementation(options => {
if (options.message) {
const vnode = options.message as Vue.VNode
const elm = document.createElement('select')
elm.appendChild(document.createElement('option'))
elm.appendChild(document.createElement('option'))
elm.selectedIndex = 0
;(vnode.children as Vue.VNode[])[1].elm = elm
}
return Promise.resolve({} as MessageBoxData)
})
2019-03-15 11:42:41 +08:00
2019-03-18 13:24:03 +08:00
let wrapper = mount(Users)
2019-03-15 11:42:41 +08:00
await wrapper.vm.$nextTick()
2019-03-18 13:24:03 +08:00
let button = wrapper.find('[data-test=permission]')
2019-03-15 11:42:41 +08:00
button.trigger('click')
2019-03-18 13:24:03 +08:00
expect(Vue.prototype.$http.post).not.toBeCalled()
2019-03-15 11:42:41 +08:00
button.trigger('click')
await wrapper.vm.$nextTick()
expect(Vue.prototype.$http.post).toBeCalledWith(
2019-03-18 13:24:03 +08:00
'/admin/users?action=permission',
{ uid: 1, permission: 1 }
2019-03-15 11:42:41 +08:00
)
2019-03-18 13:24:03 +08:00
expect(wrapper.text()).toContain('admin.normal')
2019-03-15 11:42:41 +08:00
2019-03-18 13:24:03 +08:00
wrapper = mount(Users)
await wrapper.vm.$nextTick()
button = wrapper.find('[data-test=permission]')
2019-03-15 11:42:41 +08:00
button.trigger('click')
await flushPromises()
2019-03-18 13:24:03 +08:00
expect(wrapper.text()).toContain('admin.banned')
2019-03-15 11:42:41 +08:00
})
2019-03-17 10:21:18 +08:00
2018-08-06 12:14:20 +08:00
test('delete user', async () => {
2019-03-15 11:42:41 +08:00
Vue.prototype.$http.get.mockResolvedValue({
data: [
{ uid: 1, nickname: 'to-be-deleted' },
],
})
Vue.prototype.$http.post
2019-04-23 11:47:45 +08:00
.mockResolvedValueOnce({ code: 1, message: '1' })
.mockResolvedValue({ code: 0, message: '0' })
2019-03-25 22:01:57 +08:00
Vue.prototype.$confirm
.mockRejectedValueOnce('')
.mockResolvedValue('confirm')
2019-03-15 11:42:41 +08:00
const wrapper = mount(Users)
await wrapper.vm.$nextTick()
2019-03-30 18:36:01 +08:00
const button = wrapper.findAll(Button).at(1)
2019-03-15 11:42:41 +08:00
button.trigger('click')
expect(Vue.prototype.$http.post).not.toBeCalled()
button.trigger('click')
await wrapper.vm.$nextTick()
expect(Vue.prototype.$http.post).toBeCalledWith(
'/admin/users?action=delete',
{ uid: 1 }
)
expect(wrapper.text()).toContain('to-be-deleted')
button.trigger('click')
await flushPromises()
expect(wrapper.text()).toContain('No data')
})