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

205 lines
5.9 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-25 22:01:57 +08:00
import { MessageBoxData } from 'element-ui/types/message-box'
2019-03-15 11:42:41 +08:00
import { flushPromises } from '../../utils'
2019-03-18 15:00:18 +08:00
import Players from '@/views/user/Players.vue'
2018-08-11 11:59:11 +08:00
2018-09-09 09:28:05 +08:00
window.blessing.extra = {
2019-03-15 11:42:41 +08:00
rule: 'rule',
length: 'length',
}
2018-08-11 11:59:11 +08:00
test('display player name constraints', () => {
2019-04-23 19:14:41 +08:00
Vue.prototype.$http.get.mockResolvedValue({ data: [] })
2019-03-15 11:42:41 +08:00
const wrapper = mount(Players)
const text = wrapper.text()
expect(text).toContain('rule')
expect(text).toContain('length')
})
2019-03-17 10:21:18 +08:00
test('fetch players data before mount', async () => {
Vue.prototype.$http.get
.mockResolvedValueOnce({ data: [] })
.mockResolvedValueOnce({
data: [{
pid: 1, tid_skin: 1, tid_cape: 0,
}],
})
.mockResolvedValueOnce({ data: {} })
2019-03-15 11:42:41 +08:00
mount(Players)
expect(Vue.prototype.$http.get).toBeCalledWith('/user/player/list')
mount(Players)
await flushPromises()
expect(Vue.prototype.$http.get).toBeCalledWith('/skinlib/info/1')
2019-03-15 11:42:41 +08:00
})
2019-03-17 10:21:18 +08:00
2018-08-11 11:59:11 +08:00
test('click to preview player', async () => {
2019-03-15 11:42:41 +08:00
Vue.prototype.$http.get
2019-04-23 19:14:41 +08:00
.mockResolvedValueOnce({
data: [
{
pid: 1, tid_skin: 1, tid_cape: 3,
},
{
pid: 2, tid_skin: 0, tid_cape: 0,
},
{
pid: 3, tid_skin: 2, tid_cape: 0,
},
{
pid: 4, tid_skin: 0, tid_cape: 5,
},
],
})
.mockResolvedValueOnce({ data: { hash: 'a' } })
.mockResolvedValueOnce({ data: { hash: 'b' } })
.mockResolvedValueOnce({ data: { hash: 'c' } })
.mockResolvedValueOnce({ data: { hash: 'd' } })
2019-03-15 11:42:41 +08:00
const wrapper = mount(Players)
await flushPromises()
2019-03-15 11:42:41 +08:00
wrapper.find('tbody > tr:nth-child(1)').trigger('click')
await flushPromises()
expect(Vue.prototype.$http.get).toBeCalledWith('/skinlib/info/1')
expect(Vue.prototype.$http.get).toBeCalledWith('/skinlib/info/3')
expect(wrapper.findAll('.player').at(0)
.classes('player-selected')).toBeTrue()
wrapper.find('tbody > tr:nth-child(2)').trigger('click')
await flushPromises()
wrapper.find('tbody > tr:nth-child(3)').trigger('click')
await flushPromises()
expect(Vue.prototype.$http.get).toBeCalledWith('/skinlib/info/2')
2019-04-10 13:09:40 +08:00
wrapper.find('[data-test="to2d"]').trigger('click')
expect(wrapper.find('img').attributes('src')).toBe('/preview/64/2.png')
wrapper.find('tbody > tr:nth-child(4)').trigger('click')
await flushPromises()
expect(wrapper.find('img').attributes('src')).toBe('/preview/64/5.png')
2019-03-15 11:42:41 +08:00
})
2019-03-17 10:21:18 +08:00
2018-08-11 11:59:11 +08:00
test('change player name', async () => {
2019-03-15 11:42:41 +08:00
Vue.prototype.$http.get
2019-04-23 19:14:41 +08:00
.mockResolvedValue({
data: [
{ pid: 1, name: 'old' },
],
})
2019-03-15 11:42:41 +08:00
Vue.prototype.$http.post
2019-04-23 11:47:45 +08:00
.mockResolvedValueOnce({ code: 1 })
.mockResolvedValue({ code: 0 })
2019-03-25 22:01:57 +08:00
Vue.prototype.$prompt.mockImplementationOnce(() => Promise.reject('cancel'))
.mockImplementation((_, { inputValidator }) => {
2019-03-15 11:42:41 +08:00
if (inputValidator) {
2019-03-18 09:55:24 +08:00
inputValidator('')
2019-03-15 11:42:41 +08:00
inputValidator('new-name')
}
2019-03-25 22:01:57 +08:00
return Promise.resolve({ value: 'new-name' } as MessageBoxData)
2019-03-15 11:42:41 +08:00
})
const wrapper = mount(Players)
await flushPromises()
2019-11-24 14:32:58 +08:00
const button = wrapper.find('.btn-default')
2019-03-15 11:42:41 +08:00
button.trigger('click')
expect(Vue.prototype.$http.post).not.toBeCalled()
button.trigger('click')
await flushPromises()
expect(Vue.prototype.$http.post).toBeCalledWith(
2019-04-24 13:10:03 +08:00
'/user/player/rename/1',
{ name: 'new-name' }
2019-03-15 11:42:41 +08:00
)
button.trigger('click')
await flushPromises()
expect(wrapper.text()).toContain('new-name')
})
2019-03-17 10:21:18 +08:00
2018-08-11 11:59:11 +08:00
test('delete player', async () => {
2019-03-15 11:42:41 +08:00
Vue.prototype.$http.get
2019-04-23 19:14:41 +08:00
.mockResolvedValueOnce({
data: [
{ pid: 1, name: 'to-be-deleted' },
],
})
2019-03-15 11:42:41 +08:00
Vue.prototype.$http.post
2019-04-23 11:47:45 +08:00
.mockResolvedValueOnce({ code: 1 })
.mockResolvedValue({ code: 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(Players)
await flushPromises()
2019-11-24 14:32:58 +08:00
const button = wrapper.findAll('.btn-danger')
2019-03-15 11:42:41 +08:00
button.trigger('click')
expect(Vue.prototype.$http.post).not.toBeCalled()
button.trigger('click')
2019-04-24 13:10:03 +08:00
await flushPromises()
expect(Vue.prototype.$http.post).toBeCalledWith('/user/player/delete/1')
2019-03-15 11:42:41 +08:00
expect(wrapper.text()).toContain('to-be-deleted')
button.trigger('click')
await flushPromises()
expect(wrapper.text()).not.toContain('to-be-deleted')
})
2019-03-17 10:21:18 +08:00
2018-08-11 11:59:11 +08:00
test('toggle preview mode', () => {
2019-04-23 19:14:41 +08:00
Vue.prototype.$http.get.mockResolvedValueOnce({ data: [] })
2019-03-15 11:42:41 +08:00
const wrapper = mount(Players)
wrapper.find('[data-test="to2d"]').trigger('click')
expect(wrapper.text()).toContain('user.player.texture-empty')
})
2019-03-17 10:21:18 +08:00
2018-08-11 11:59:11 +08:00
test('add player', async () => {
2019-03-15 11:42:41 +08:00
window.$ = jest.fn(() => ({ modal() {} }))
2019-04-23 19:14:41 +08:00
Vue.prototype.$http.get.mockResolvedValueOnce({ data: [] })
2019-04-23 11:47:45 +08:00
Vue.prototype.$http.post.mockResolvedValue({ code: 0 })
2019-03-15 11:42:41 +08:00
const wrapper = mount(Players)
2019-03-30 18:36:01 +08:00
const button = wrapper.find('[data-test=addPlayer]')
2019-03-15 11:42:41 +08:00
2019-04-01 21:45:59 +08:00
wrapper.find('input[type="text"]').setValue('the-new')
2019-03-15 11:42:41 +08:00
button.trigger('click')
await flushPromises()
expect(Vue.prototype.$http.get).toBeCalledTimes(2)
})
2019-03-17 10:21:18 +08:00
2018-08-11 11:59:11 +08:00
test('clear texture', async () => {
2019-03-15 11:42:41 +08:00
window.$ = jest.fn(() => ({ modal() {} }))
Vue.prototype.$http.get
.mockResolvedValueOnce({
data: [
{
pid: 1, tid_skin: 1, tid_cape: 0,
},
],
})
.mockResolvedValueOnce({ data: {} })
.mockResolvedValueOnce({ data: {} })
2019-03-15 11:42:41 +08:00
Vue.prototype.$http.post
2019-04-23 11:47:45 +08:00
.mockResolvedValueOnce({ code: 1 })
.mockResolvedValue({ code: 0, message: 'ok' })
2019-03-15 11:42:41 +08:00
const wrapper = mount(Players)
await flushPromises()
2019-03-30 18:36:01 +08:00
const button = wrapper.find('[data-test=clearTexture]')
2019-03-15 11:42:41 +08:00
wrapper.find('.player').trigger('click')
button.trigger('click')
expect(Vue.prototype.$http.post).not.toBeCalled()
wrapper.findAll('input[type="checkbox"]').at(0)
.setChecked()
button.trigger('click')
expect(Vue.prototype.$http.post).toBeCalledWith(
2019-04-24 13:10:03 +08:00
'/user/player/texture/clear/1',
2019-03-15 11:42:41 +08:00
{
2019-04-24 13:10:03 +08:00
skin: true, cape: false,
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('ok')
2019-03-15 11:42:41 +08:00
})