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

131 lines
3.7 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 ClosetItem from '@/components/user/ClosetItem'
import { swal } from '@/js/notify'
2018-07-27 16:17:22 +08:00
2019-03-15 11:42:41 +08:00
jest.mock('@/js/notify')
2018-07-27 16:17:22 +08:00
function factory(opt = {}) {
2019-03-15 11:42:41 +08:00
return {
tid: 1,
name: 'texture',
type: 'steve',
...opt,
}
2018-07-27 16:17:22 +08:00
}
test('computed values', () => {
2019-03-15 11:42:41 +08:00
const wrapper = mount(ClosetItem, { propsData: factory() })
expect(wrapper.find('img').attributes('src')).toBe('/preview/1.png')
expect(wrapper.find('a.more').attributes('href')).toBe('/skinlib/show/1')
})
2018-07-27 16:17:22 +08:00
test('selected item', () => {
2019-03-15 11:42:41 +08:00
const wrapper = mount(ClosetItem, { propsData: factory({ selected: true }) })
expect(wrapper.find('.item').classes('item-selected')).toBeTrue()
})
2018-07-27 16:17:22 +08:00
test('click item body', () => {
2019-03-15 11:42:41 +08:00
const wrapper = mount(ClosetItem, { propsData: factory() })
2018-07-27 16:17:22 +08:00
2019-03-15 11:42:41 +08:00
wrapper.find('.item').trigger('click')
expect(wrapper.emitted().select).toBeUndefined()
2018-07-27 16:17:22 +08:00
2019-03-15 11:42:41 +08:00
wrapper.find('.item-body').trigger('click')
expect(wrapper.emitted().select).toBeTruthy()
})
2018-07-27 16:17:22 +08:00
test('rename texture', async () => {
2019-03-15 11:42:41 +08:00
Vue.prototype.$http.post
.mockResolvedValueOnce({ errno: 0 })
.mockResolvedValueOnce({ errno: 1 })
swal.mockImplementationOnce(() => ({ dismiss: 'cancel' }))
.mockImplementation(options => {
options.inputValidator('name')
options.inputValidator()
return { value: 'new-name' }
})
const wrapper = mount(ClosetItem, { propsData: factory() })
const button = wrapper.findAll('.dropdown-menu > li').at(0)
.find('a')
button.trigger('click')
await wrapper.vm.$nextTick()
expect(Vue.prototype.$http.post).not.toBeCalled()
button.trigger('click')
await wrapper.vm.$nextTick()
button.trigger('click')
await flushPromises()
expect(wrapper.find('.texture-name > span').text()).toBe('new-name (steve)')
expect(Vue.prototype.$http.post).toBeCalledWith(
'/user/closet/rename',
{ tid: 1, new_name: 'new-name' }
)
})
2018-07-27 16:17:22 +08:00
test('remove texture', async () => {
2019-03-15 11:42:41 +08:00
Vue.prototype.$http.post
.mockResolvedValueOnce({ errno: 0 })
.mockResolvedValueOnce({ errno: 1 })
swal
.mockResolvedValueOnce({ dismiss: 'cancel' })
.mockResolvedValue({})
const wrapper = mount(ClosetItem, { propsData: factory() })
const button = wrapper.findAll('.dropdown-menu > li').at(1)
.find('a')
button.trigger('click')
await wrapper.vm.$nextTick()
expect(Vue.prototype.$http.post).not.toBeCalled()
button.trigger('click')
await wrapper.vm.$nextTick()
button.trigger('click')
await flushPromises()
expect(wrapper.emitted()['item-removed']).toBeTruthy()
expect(Vue.prototype.$http.post).toBeCalledWith('/user/closet/remove', { tid: 1 })
})
2019-03-16 23:47:56 +08:00
2018-07-27 16:17:22 +08:00
test('set as avatar', async () => {
2019-03-15 11:42:41 +08:00
Vue.prototype.$http.post
.mockResolvedValueOnce({ errno: 0 })
.mockResolvedValueOnce({ errno: 1 })
swal
.mockResolvedValueOnce({ dismiss: 'cancel' })
.mockResolvedValue({})
window.$ = jest.fn(() => ({
each(fn) {
fn()
},
prop() {},
attr() {
return ''
},
}))
const wrapper = mount(ClosetItem, { propsData: factory() })
const button = wrapper.findAll('.dropdown-menu > li').at(2)
.find('a')
button.trigger('click')
await wrapper.vm.$nextTick()
expect(Vue.prototype.$http.post).not.toBeCalled()
button.trigger('click')
await wrapper.vm.$nextTick()
button.trigger('click')
await flushPromises()
await wrapper.vm.$nextTick()
expect(Vue.prototype.$http.post).toBeCalledWith('/user/profile/avatar', { tid: 1 })
expect(window.$).toBeCalledWith('[alt="User Image"]')
})
2019-03-16 23:47:56 +08:00
test('no avatar option if texture is cape', () => {
const wrapper = mount(ClosetItem, { propsData: factory({ type: 'cape' }) })
const button = wrapper.findAll('.dropdown-menu > li').at(2)
expect(button.isEmpty()).toBeTrue()
})