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

206 lines
5.6 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 09:55:24 +08:00
import Players from '@/components/user/Players.vue'
2019-03-15 11:42:41 +08:00
import { swal } from '@/js/notify'
2018-08-11 11:59:11 +08:00
2019-03-15 11:42:41 +08:00
jest.mock('toastr')
jest.mock('@/js/notify')
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-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
2018-08-11 11:59:11 +08:00
test('fetch players data before mount', () => {
2019-03-15 11:42:41 +08:00
Vue.prototype.$http.get.mockResolvedValue([])
mount(Players)
expect(Vue.prototype.$http.get).toBeCalledWith('/user/player/list')
})
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
.mockResolvedValueOnce([
{
pid: 1, tid_skin: 1, tid_cape: 3,
},
{
pid: 2, tid_skin: 0, tid_cape: 0,
},
{
pid: 3, tid_skin: 2, tid_cape: 0,
},
])
.mockResolvedValueOnce({ hash: 'a' })
.mockResolvedValueOnce({ hash: 'b' })
.mockResolvedValueOnce({ hash: 'c' })
const wrapper = mount(Players)
await wrapper.vm.$nextTick()
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-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
.mockResolvedValueOnce([
{ pid: 1, name: 'old' },
])
Vue.prototype.$http.post
.mockResolvedValueOnce({ errno: 1 })
.mockResolvedValue({ errno: 0 })
2019-03-18 09:55:24 +08:00
swal.mockImplementationOnce(() => Promise.resolve({ dismiss: 1 }))
2019-03-15 11:42:41 +08:00
.mockImplementation(({ inputValidator }) => {
if (inputValidator) {
2019-03-18 09:55:24 +08:00
inputValidator('')
2019-03-15 11:42:41 +08:00
inputValidator('new-name')
}
2019-03-18 09:55:24 +08:00
return Promise.resolve({ value: 'new-name' })
2019-03-15 11:42:41 +08:00
})
const wrapper = mount(Players)
await wrapper.vm.$nextTick()
const button = wrapper.find('.btn-default')
button.trigger('click')
expect(Vue.prototype.$http.post).not.toBeCalled()
button.trigger('click')
await flushPromises()
expect(Vue.prototype.$http.post).toBeCalledWith(
'/user/player/rename',
{ pid: 1, new_player_name: 'new-name' }
)
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('load iCheck', async () => {
2019-03-15 11:42:41 +08:00
Vue.prototype.$http.get
.mockResolvedValueOnce([
{ pid: 1 },
])
window.$ = jest.fn(() => ({
iCheck: () => ({
2019-03-18 09:55:24 +08:00
on(_: Event, cb: CallableFunction) {
2019-03-15 11:42:41 +08:00
cb()
},
}),
0: {
dispatchEvent: () => {},
},
}))
const wrapper = mount(Players)
await wrapper.vm.$nextTick()
wrapper.find('.btn-warning').trigger('click')
expect(window.$).toBeCalled()
})
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
.mockResolvedValueOnce([
{ pid: 1, name: 'to-be-deleted' },
])
Vue.prototype.$http.post
.mockResolvedValueOnce({ errno: 1 })
.mockResolvedValue({ errno: 0 })
swal.mockResolvedValueOnce({ dismiss: 1 })
.mockResolvedValue({})
const wrapper = mount(Players)
await wrapper.vm.$nextTick()
const button = wrapper.find('.btn-danger')
button.trigger('click')
expect(Vue.prototype.$http.post).not.toBeCalled()
button.trigger('click')
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-03-15 11:42:41 +08:00
Vue.prototype.$http.get.mockResolvedValueOnce([])
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() {} }))
Vue.prototype.$http.get.mockResolvedValueOnce([])
Vue.prototype.$http.post
.mockResolvedValueOnce({ errno: 1 })
.mockResolvedValue({ errno: 0 })
const wrapper = mount(Players)
const button = wrapper.findAll('.modal-footer').at(0)
.find('a')
wrapper.find('input[type="text"]').setValue('the-new')
button.trigger('click')
expect(Vue.prototype.$http.post).toBeCalledWith(
'/user/player/add',
{ player_name: 'the-new' }
)
await flushPromises()
await wrapper.vm.$nextTick()
expect(wrapper.text()).not.toContain('the-new')
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([
{
pid: 1, tid_skin: 1, tid_cape: 0,
},
])
Vue.prototype.$http.post
.mockResolvedValueOnce({ errno: 1 })
.mockResolvedValue({ errno: 0, msg: 'ok' })
const wrapper = mount(Players)
await wrapper.vm.$nextTick()
const button = wrapper.findAll('.modal-footer').at(1)
.find('a')
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(
'/user/player/texture/clear',
{
pid: 1, skin: true, cape: false,
}
)
button.trigger('click')
await flushPromises()
expect(swal).toBeCalledWith({ type: 'success', text: 'ok' })
})