2019-03-15 11:42:41 +08:00
|
|
|
import Vue from 'vue'
|
|
|
|
import { mount } from '@vue/test-utils'
|
|
|
|
import SkinLibItem from '@/components/skinlib/SkinLibItem'
|
|
|
|
import toastr from 'toastr'
|
|
|
|
import { flushPromises } from '../../utils'
|
|
|
|
import { swal } from '@/js/notify'
|
2018-08-14 23:27:36 +08:00
|
|
|
|
2019-03-15 11:42:41 +08:00
|
|
|
jest.mock('@/js/notify')
|
|
|
|
jest.mock('toastr')
|
2018-08-14 23:27:36 +08:00
|
|
|
|
|
|
|
test('urls', () => {
|
2019-03-15 11:42:41 +08:00
|
|
|
const wrapper = mount(SkinLibItem, {
|
|
|
|
propsData: { tid: 1 },
|
|
|
|
})
|
|
|
|
expect(wrapper.find('a').attributes('href')).toBe('/skinlib/show/1')
|
|
|
|
expect(wrapper.find('img').attributes('src')).toBe('/preview/1.png')
|
|
|
|
})
|
2019-03-17 10:21:18 +08:00
|
|
|
|
2018-08-14 23:27:36 +08:00
|
|
|
test('render basic information', () => {
|
2019-03-15 11:42:41 +08:00
|
|
|
const wrapper = mount(SkinLibItem, {
|
|
|
|
propsData: {
|
|
|
|
tid: 1,
|
|
|
|
name: 'test',
|
|
|
|
type: 'steve',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
expect(wrapper.text()).toContain('test')
|
|
|
|
expect(wrapper.text()).toContain('skinlib.filter.steve')
|
|
|
|
})
|
2019-03-17 10:21:18 +08:00
|
|
|
|
2018-08-14 23:27:36 +08:00
|
|
|
test('anonymous user', () => {
|
2019-03-15 11:42:41 +08:00
|
|
|
const wrapper = mount(SkinLibItem, {
|
|
|
|
propsData: { anonymous: true },
|
|
|
|
})
|
|
|
|
const button = wrapper.find('.more')
|
|
|
|
expect(button.attributes('title')).toBe('skinlib.anonymous')
|
|
|
|
button.trigger('click')
|
|
|
|
expect(Vue.prototype.$http.post).not.toBeCalled()
|
|
|
|
})
|
2019-03-17 10:21:18 +08:00
|
|
|
|
2018-08-14 23:27:36 +08:00
|
|
|
test('private texture', () => {
|
2019-03-15 11:42:41 +08:00
|
|
|
const wrapper = mount(SkinLibItem, {
|
|
|
|
propsData: { isPublic: false },
|
|
|
|
})
|
|
|
|
expect(wrapper.text()).toContain('skinlib.private')
|
|
|
|
|
|
|
|
wrapper.setProps({ isPublic: true })
|
|
|
|
expect(wrapper.text()).not.toContain('skinlib.private')
|
|
|
|
})
|
2019-03-17 10:21:18 +08:00
|
|
|
|
2018-08-14 23:27:36 +08:00
|
|
|
test('liked state', () => {
|
2019-03-15 11:42:41 +08:00
|
|
|
const wrapper = mount(SkinLibItem, {
|
|
|
|
propsData: { liked: true, anonymous: false },
|
|
|
|
})
|
|
|
|
const button = wrapper.find('.like')
|
|
|
|
|
|
|
|
expect(button.attributes('title')).toBe('skinlib.removeFromCloset')
|
|
|
|
expect(button.classes('liked')).toBeTrue()
|
|
|
|
|
|
|
|
wrapper.setProps({ liked: false })
|
|
|
|
expect(button.attributes('title')).toBe('skinlib.addToCloset')
|
|
|
|
expect(button.classes('liked')).toBeFalse()
|
|
|
|
})
|
2019-03-17 10:21:18 +08:00
|
|
|
|
2018-08-14 23:27:36 +08:00
|
|
|
test('remove from closet', async () => {
|
2019-03-15 11:42:41 +08:00
|
|
|
Vue.prototype.$http.post
|
|
|
|
.mockResolvedValueOnce({ errno: 1, msg: '1' })
|
|
|
|
.mockResolvedValue({ errno: 0 })
|
|
|
|
swal.mockResolvedValueOnce({ dismiss: 1 })
|
|
|
|
.mockResolvedValue({})
|
|
|
|
jest.spyOn(toastr, 'warning')
|
|
|
|
const wrapper = mount(SkinLibItem, {
|
|
|
|
propsData: {
|
|
|
|
tid: 1, liked: true, anonymous: false,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
const button = wrapper.find('.like')
|
|
|
|
|
|
|
|
button.trigger('click')
|
|
|
|
expect(Vue.prototype.$http.post).not.toBeCalled()
|
|
|
|
|
|
|
|
button.trigger('click')
|
|
|
|
await flushPromises()
|
|
|
|
expect(Vue.prototype.$http.post).toBeCalledWith(
|
|
|
|
'/user/closet/remove',
|
|
|
|
{ tid: 1 }
|
|
|
|
)
|
|
|
|
expect(toastr.warning).toBeCalledWith('1')
|
|
|
|
|
|
|
|
button.trigger('click')
|
|
|
|
await flushPromises()
|
|
|
|
expect(wrapper.emitted('like-toggled')[0]).toEqual([false])
|
|
|
|
})
|
2019-03-17 10:21:18 +08:00
|
|
|
|
2018-08-14 23:27:36 +08:00
|
|
|
test('add to closet', async () => {
|
2019-03-15 11:42:41 +08:00
|
|
|
Vue.prototype.$http.post
|
|
|
|
.mockResolvedValueOnce({ errno: 1, msg: '1' })
|
|
|
|
.mockResolvedValue({ errno: 0 })
|
|
|
|
swal.mockImplementationOnce(() => ({ dismiss: 1 }))
|
|
|
|
.mockImplementation(({ inputValidator }) => {
|
|
|
|
if (inputValidator) {
|
|
|
|
inputValidator()
|
|
|
|
inputValidator('name')
|
|
|
|
}
|
|
|
|
return { value: 'name' }
|
|
|
|
})
|
|
|
|
jest.spyOn(toastr, 'warning')
|
|
|
|
const wrapper = mount(SkinLibItem, {
|
|
|
|
propsData: {
|
|
|
|
tid: 1, liked: false, anonymous: false,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
const button = wrapper.find('.like')
|
|
|
|
|
|
|
|
button.trigger('click')
|
|
|
|
expect(Vue.prototype.$http.post).not.toBeCalled()
|
|
|
|
|
|
|
|
button.trigger('click')
|
|
|
|
await flushPromises()
|
|
|
|
expect(Vue.prototype.$http.post).toBeCalledWith(
|
|
|
|
'/user/closet/add',
|
|
|
|
{ tid: 1, name: 'name' }
|
|
|
|
)
|
|
|
|
expect(toastr.warning).toBeCalledWith('1')
|
|
|
|
|
|
|
|
button.trigger('click')
|
|
|
|
await flushPromises()
|
|
|
|
expect(wrapper.emitted('like-toggled')[0]).toEqual([true])
|
|
|
|
})
|