blessing-skin-server/resources/assets/tests/views/skinlib/List.test.ts

260 lines
6.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'
2019-03-26 17:42:27 +08:00
import { Button } from 'element-ui'
2019-03-18 15:00:18 +08:00
import List from '@/views/skinlib/List.vue'
2018-08-14 23:27:36 +08:00
2019-03-26 17:42:27 +08:00
jest.mock('element-ui/lib/select', () => ({
install(vue: typeof Vue) {
vue.component('ElSelect', {
render(h) {
return h('select', {
on: {
change: (event: Event) => this.$emit(
'change',
(event.target as HTMLSelectElement).value
),
},
attrs: {
value: this.value,
},
}, this.$slots.default)
},
props: {
value: String,
},
model: {
prop: 'value',
event: 'change',
},
})
},
}))
jest.mock('element-ui/lib/option', () => ({
install(vue: typeof Vue) {
vue.component('ElOption', {
render(h) {
return h('option', { attrs: { value: this.value } }, this.label)
},
props: {
label: String,
value: String,
},
})
},
}))
2018-08-14 23:27:36 +08:00
test('fetch data before mounting', () => {
2019-03-15 11:42:41 +08:00
Vue.prototype.$http.get.mockResolvedValue({
2019-04-23 19:14:41 +08:00
data: {
items: [], total_pages: 0, current_uid: 0,
},
2019-03-15 11:42:41 +08:00
})
mount(List)
expect(Vue.prototype.$http.get).toBeCalledWith(
'/skinlib/data',
{
filter: 'skin', uploader: 0, sort: 'time', keyword: '', page: 1,
}
)
})
2018-08-14 23:27:36 +08:00
test('empty skin library', () => {
2019-03-15 11:42:41 +08:00
Vue.prototype.$http.get.mockResolvedValue({
2019-04-23 19:14:41 +08:00
data: {
items: [], total_pages: 0, current_uid: 0,
},
2019-03-15 11:42:41 +08:00
})
const wrapper = mount(List)
expect(wrapper.text()).toContain('general.noResult')
})
2019-03-17 10:21:18 +08:00
2018-08-14 23:27:36 +08:00
test('toggle texture type', () => {
2019-03-15 11:42:41 +08:00
Vue.prototype.$http.get.mockResolvedValue({
2019-04-23 19:14:41 +08:00
data: {
items: [], total_pages: 0, current_uid: 0,
},
2019-03-15 11:42:41 +08:00
})
const wrapper = mount(List)
2019-03-26 17:42:27 +08:00
const select = wrapper.find({ name: 'ElSelect' })
2019-03-15 11:42:41 +08:00
const breadcrumb = wrapper.find('.breadcrumb')
expect(breadcrumb.text()).toContain('skinlib.filter.skin')
2019-03-26 17:42:27 +08:00
select.setValue('steve')
select.trigger('change')
2019-03-15 11:42:41 +08:00
expect(breadcrumb.text()).toContain('skinlib.filter.steve')
expect(Vue.prototype.$http.get).toBeCalledWith(
'/skinlib/data',
{
filter: 'steve', uploader: 0, sort: 'time', keyword: '', page: 1,
}
)
2019-03-26 17:42:27 +08:00
select.setValue('alex')
select.trigger('change')
2019-03-15 11:42:41 +08:00
expect(breadcrumb.text()).toContain('skinlib.filter.alex')
expect(Vue.prototype.$http.get).toBeCalledWith(
'/skinlib/data',
{
filter: 'alex', uploader: 0, sort: 'time', keyword: '', page: 1,
}
)
2019-03-26 17:42:27 +08:00
select.setValue('cape')
select.trigger('change')
2019-03-15 11:42:41 +08:00
expect(breadcrumb.text()).toContain('general.cape')
expect(Vue.prototype.$http.get).toBeCalledWith(
'/skinlib/data',
{
filter: 'cape', uploader: 0, sort: 'time', keyword: '', page: 1,
}
)
})
2018-08-14 23:27:36 +08:00
test('check specified uploader', async () => {
2019-03-15 11:42:41 +08:00
Vue.prototype.$http.get.mockResolvedValue({
2019-04-23 19:14:41 +08:00
data: {
items: [], total_pages: 0, current_uid: 1,
},
2019-03-15 11:42:41 +08:00
})
const wrapper = mount(List)
await wrapper.vm.$nextTick()
const breadcrumb = wrapper.find('.breadcrumb')
2019-03-26 17:42:27 +08:00
const button = wrapper
.find('.advanced-filter')
.findAll(Button)
.at(2)
2019-03-15 11:42:41 +08:00
expect(breadcrumb.text()).toContain('skinlib.filter.allUsers')
button.trigger('click')
expect(breadcrumb.text()).toContain('skinlib.filter.uploader')
expect(Vue.prototype.$http.get).toBeCalledWith(
'/skinlib/data',
{
filter: 'skin', uploader: 1, sort: 'time', keyword: '', page: 1,
}
)
})
2018-08-14 23:27:36 +08:00
test('sort items', () => {
2019-03-15 11:42:41 +08:00
Vue.prototype.$http.get.mockResolvedValue({
2019-04-23 19:14:41 +08:00
data: {
items: [], total_pages: 0, current_uid: 0,
},
2019-03-15 11:42:41 +08:00
})
const wrapper = mount(List)
2019-03-26 17:42:27 +08:00
const buttons = wrapper.find('.advanced-filter').findAll(Button)
const sortByLikes = buttons.at(0)
const sortByTime = buttons.at(1)
2019-03-15 11:42:41 +08:00
sortByLikes.trigger('click')
expect(Vue.prototype.$http.get).toBeCalledWith(
'/skinlib/data',
{
filter: 'skin', uploader: 0, sort: 'likes', keyword: '', page: 1,
}
)
expect(wrapper.text()).toContain('skinlib.sort.likes')
sortByTime.trigger('click')
expect(Vue.prototype.$http.get).toBeCalledWith(
'/skinlib/data',
{
filter: 'skin', uploader: 0, sort: 'time', keyword: '', page: 1,
}
)
expect(wrapper.text()).toContain('skinlib.sort.time')
})
2019-03-17 10:21:18 +08:00
2018-08-14 23:27:36 +08:00
test('search by keyword', () => {
2019-03-15 11:42:41 +08:00
Vue.prototype.$http.get.mockResolvedValue({
2019-04-23 19:14:41 +08:00
data: {
items: [], total_pages: 0, current_uid: 0,
},
2019-03-15 11:42:41 +08:00
})
const wrapper = mount(List)
2019-03-26 17:42:27 +08:00
wrapper.setData({ keyword: 'a' })
2019-03-15 11:42:41 +08:00
wrapper.find('form').trigger('submit')
expect(Vue.prototype.$http.get).toBeCalledWith(
'/skinlib/data',
{
filter: 'skin', uploader: 0, sort: 'time', keyword: 'a', page: 1,
}
)
2019-03-26 17:42:27 +08:00
wrapper.setData({ keyword: 'b' })
wrapper.find('[data-test="btn-search"]').trigger('click')
2019-03-15 11:42:41 +08:00
expect(Vue.prototype.$http.get).toBeCalledWith(
'/skinlib/data',
{
filter: 'skin', uploader: 0, sort: 'time', keyword: 'b', page: 1,
}
)
})
test('reset all filters', () => {
Vue.prototype.$http.get.mockResolvedValue({
2019-04-23 19:14:41 +08:00
data: {
items: [], total_pages: 0, current_uid: 0,
},
2019-03-15 11:42:41 +08:00
})
const wrapper = mount(List)
2019-03-26 17:42:27 +08:00
wrapper.findAll('option').at(3)
.setSelected()
wrapper.setData({ keyword: 'abc' })
const buttons = wrapper.find('.advanced-filter').findAll(Button)
buttons.at(1).trigger('click')
2019-03-15 11:42:41 +08:00
Vue.prototype.$http.get.mockClear()
2019-03-26 17:42:27 +08:00
buttons.at(3).trigger('click')
2019-03-15 11:42:41 +08:00
expect(Vue.prototype.$http.get).toBeCalledTimes(1)
})
2019-03-17 10:21:18 +08:00
2018-08-14 23:27:36 +08:00
test('is anonymous', () => {
2019-03-15 11:42:41 +08:00
Vue.prototype.$http.get.mockResolvedValue({
2019-04-23 19:14:41 +08:00
data: {
items: [], total_pages: 0, current_uid: 0,
},
2019-03-15 11:42:41 +08:00
})
2019-03-18 09:55:24 +08:00
const wrapper = mount<Vue & { anonymous: boolean }>(List)
2019-03-15 11:42:41 +08:00
expect(wrapper.vm.anonymous).toBeTrue()
})
2019-03-17 10:21:18 +08:00
2018-08-14 23:27:36 +08:00
test('on page changed', () => {
2019-03-15 11:42:41 +08:00
Vue.prototype.$http.get.mockResolvedValue({
2019-04-23 19:14:41 +08:00
data: {
items: [], total_pages: 0, current_uid: 0,
},
2019-03-15 11:42:41 +08:00
})
2019-03-18 09:55:24 +08:00
const wrapper = mount<Vue & { pageChanged(page: number): void }>(List)
2019-03-15 11:42:41 +08:00
wrapper.vm.pageChanged(2)
expect(Vue.prototype.$http.get).toBeCalledWith(
'/skinlib/data',
{
filter: 'skin', uploader: 0, sort: 'time', keyword: '', page: 2,
}
)
})
2018-08-14 23:27:36 +08:00
test('on like toggled', async () => {
2019-03-15 11:42:41 +08:00
Vue.prototype.$http.get.mockResolvedValue({
2019-04-23 19:14:41 +08:00
data: {
items: [{
tid: 1, liked: false, likes: 0,
}],
total_pages: 1,
current_uid: 0,
},
2019-03-15 11:42:41 +08:00
})
2019-03-18 09:55:24 +08:00
const wrapper = mount<Vue & {
onLikeToggled(tid: number, like: boolean): void,
items: Array<{ liked: boolean, likes: number }>
}>(List)
2019-03-15 11:42:41 +08:00
await wrapper.vm.$nextTick()
wrapper.vm.onLikeToggled(0, true)
expect(wrapper.vm.items[0].liked).toBeTrue()
expect(wrapper.vm.items[0].likes).toBe(1)
wrapper.vm.onLikeToggled(0, false)
expect(wrapper.vm.items[0].liked).toBeFalse()
expect(wrapper.vm.items[0].likes).toBe(0)
})