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

262 lines
7.5 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'
import { queryString } from '@/scripts/utils'
2019-03-18 15:00:18 +08:00
import List from '@/views/skinlib/List.vue'
2018-08-14 23:27:36 +08:00
beforeEach(() => {
window.history.pushState(null, '', 'skinlib')
})
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,
2019-11-25 23:30:51 +08:00
},
2019-03-15 11:42:41 +08:00
)
})
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-11-24 14:32:58 +08:00
const options = wrapper.findAll('.dropdown-item')
const btnSkin = options.at(0)
const btnSteve = options.at(1)
const btnAlex = options.at(2)
const btnCape = options.at(3)
const dropdownToggle = wrapper.find('.dropdown-toggle')
2019-03-15 11:42:41 +08:00
const breadcrumb = wrapper.find('.breadcrumb')
2019-11-24 14:32:58 +08:00
expect(btnSkin.classes()).toContain('active')
expect(btnSteve.classes()).not.toContain('active')
expect(btnAlex.classes()).not.toContain('active')
expect(btnCape.classes()).not.toContain('active')
expect(dropdownToggle.text()).toContain('general.skin')
2019-03-15 11:42:41 +08:00
expect(breadcrumb.text()).toContain('skinlib.filter.skin')
2019-11-24 14:32:58 +08:00
btnSteve.trigger('click')
expect(btnSkin.classes()).not.toContain('active')
expect(btnSteve.classes()).toContain('active')
expect(btnAlex.classes()).not.toContain('active')
expect(btnCape.classes()).not.toContain('active')
expect(dropdownToggle.text()).toContain('Steve')
2019-03-15 11:42:41 +08:00
expect(breadcrumb.text()).toContain('skinlib.filter.steve')
2019-11-24 14:32:58 +08:00
expect(queryString('filter')).toBe('steve')
2019-03-15 11:42:41 +08:00
expect(Vue.prototype.$http.get).toBeCalledWith(
'/skinlib/data',
{
filter: 'steve', uploader: 0, sort: 'time', keyword: '', page: 1,
2019-11-25 23:30:51 +08:00
},
2019-03-15 11:42:41 +08:00
)
2019-11-24 14:32:58 +08:00
btnAlex.trigger('click')
expect(btnSkin.classes()).not.toContain('active')
expect(btnSteve.classes()).not.toContain('active')
expect(btnAlex.classes()).toContain('active')
expect(btnCape.classes()).not.toContain('active')
expect(dropdownToggle.text()).toContain('Alex')
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-11-25 23:30:51 +08:00
},
2019-03-15 11:42:41 +08:00
)
expect(queryString('filter')).toBe('alex')
2019-11-24 14:32:58 +08:00
btnCape.trigger('click')
expect(btnSkin.classes()).not.toContain('active')
expect(btnSteve.classes()).not.toContain('active')
expect(btnAlex.classes()).not.toContain('active')
expect(btnCape.classes()).toContain('active')
expect(dropdownToggle.text()).toContain('general.cape')
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,
2019-11-25 23:30:51 +08:00
},
2019-03-15 11:42:41 +08:00
)
expect(queryString('filter')).toBe('cape')
2019-03-15 11:42:41 +08:00
})
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 flushPromises()
2019-03-15 11:42:41 +08:00
const breadcrumb = wrapper.find('.breadcrumb')
2019-11-24 14:32:58 +08:00
const button = wrapper.findAll('.bg-olive').at(2)
2019-03-15 11:42:41 +08:00
expect(breadcrumb.text()).toContain('skinlib.filter.allUsers')
button.trigger('click')
2019-11-24 14:32:58 +08:00
expect(button.classes()).toContain('active')
2019-03-15 11:42:41 +08:00
expect(breadcrumb.text()).toContain('skinlib.filter.uploader')
expect(Vue.prototype.$http.get).toBeCalledWith(
'/skinlib/data',
{
filter: 'skin', uploader: 1, sort: 'time', keyword: '', page: 1,
2019-11-25 23:30:51 +08:00
},
2019-03-15 11:42:41 +08:00
)
expect(queryString('uploader')).toBe('1')
2019-03-15 11:42:41 +08:00
})
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-11-24 14:32:58 +08:00
const buttons = wrapper.findAll('.bg-olive')
2019-03-26 17:42:27 +08:00
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,
2019-11-25 23:30:51 +08:00
},
2019-03-15 11:42:41 +08:00
)
expect(wrapper.text()).toContain('skinlib.sort.likes')
2019-11-24 14:32:58 +08:00
expect(sortByLikes.classes()).toContain('active')
expect(queryString('sort')).toBe('likes')
2019-03-15 11:42:41 +08:00
sortByTime.trigger('click')
expect(Vue.prototype.$http.get).toBeCalledWith(
'/skinlib/data',
{
filter: 'skin', uploader: 0, sort: 'time', keyword: '', page: 1,
2019-11-25 23:30:51 +08:00
},
2019-03-15 11:42:41 +08:00
)
expect(wrapper.text()).toContain('skinlib.sort.time')
2019-11-24 14:32:58 +08:00
expect(sortByTime.classes()).toContain('active')
expect(queryString('sort')).toBe('time')
2019-03-15 11:42:41 +08:00
})
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-11-24 14:32:58 +08:00
const input = wrapper.find('[data-test="keyword"]')
2019-03-15 11:42:41 +08:00
2019-11-24 14:32:58 +08:00
input.setValue('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-11-25 23:30:51 +08:00
},
2019-03-15 11:42:41 +08:00
)
expect(queryString('keyword')).toBe('a')
2019-03-15 11:42:41 +08:00
2019-11-24 14:32:58 +08:00
input.setValue('b')
2019-03-26 17:42:27 +08:00
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,
2019-11-25 23:30:51 +08:00
},
2019-03-15 11:42:41 +08:00
)
expect(queryString('keyword')).toBe('b')
2019-03-15 11:42:41 +08:00
})
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-11-24 14:32:58 +08:00
wrapper
.findAll('.dropdown-item')
.at(3)
.trigger('click')
2019-03-26 17:42:27 +08:00
wrapper.setData({ keyword: 'abc' })
2019-11-24 14:32:58 +08:00
const buttons = wrapper.findAll('.bg-olive')
2019-03-26 17:42:27 +08:00
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,
2019-11-25 23:30:51 +08:00
},
2019-03-15 11:42:41 +08:00
)
expect(queryString('page')).toBe('2')
2019-03-15 11:42:41 +08:00
})
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 & {
2019-11-25 23:30:51 +08:00
onLikeToggled(tid: number, like: boolean): void
2019-03-18 09:55:24 +08:00
items: Array<{ liked: boolean, likes: number }>
}>(List)
await flushPromises()
2019-03-15 11:42:41 +08:00
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)
})