2018-08-06 13:16:10 +08:00
|
|
|
import Vue from 'vue';
|
2018-08-02 09:45:33 +08:00
|
|
|
import { mount } from '@vue/test-utils';
|
|
|
|
import Closet from '@/components/user/Closet';
|
|
|
|
import ClosetItem from '@/components/user/ClosetItem';
|
|
|
|
import Previewer from '@/components/common/Previewer';
|
|
|
|
import toastr from 'toastr';
|
|
|
|
import { swal } from '@/js/notify';
|
|
|
|
|
|
|
|
jest.mock('@/js/notify');
|
|
|
|
|
2018-09-09 09:28:05 +08:00
|
|
|
window.blessing.extra = { unverified: false };
|
2018-08-17 12:32:44 +08:00
|
|
|
|
2018-08-02 09:45:33 +08:00
|
|
|
test('fetch closet data before mount', () => {
|
2018-08-06 13:16:10 +08:00
|
|
|
Vue.prototype.$http.get.mockResolvedValue({});
|
2018-08-02 09:45:33 +08:00
|
|
|
mount(Closet);
|
|
|
|
jest.runAllTicks();
|
2018-08-06 13:16:10 +08:00
|
|
|
expect(Vue.prototype.$http.get).toBeCalledWith(
|
|
|
|
'/user/closet-data',
|
|
|
|
{
|
2018-08-02 09:45:33 +08:00
|
|
|
category: 'skin',
|
|
|
|
q: '',
|
|
|
|
page: 1,
|
|
|
|
}
|
2018-08-06 13:16:10 +08:00
|
|
|
);
|
2018-08-02 09:45:33 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('switch tabs', () => {
|
2018-08-06 13:16:10 +08:00
|
|
|
Vue.prototype.$http.get.mockResolvedValue({
|
|
|
|
items: [],
|
|
|
|
category: 'skin',
|
|
|
|
total_pages: 1
|
2018-08-02 09:45:33 +08:00
|
|
|
}).mockResolvedValueOnce({
|
2018-08-06 13:16:10 +08:00
|
|
|
items: [],
|
|
|
|
category: 'cape',
|
|
|
|
total_pages: 1
|
2018-08-02 09:45:33 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
const wrapper = mount(Closet);
|
|
|
|
|
|
|
|
const tabSkin = wrapper.findAll('.nav-tabs > li').at(0);
|
|
|
|
tabSkin.find('a').trigger('click');
|
|
|
|
jest.runAllTicks();
|
2018-08-06 13:16:10 +08:00
|
|
|
expect(Vue.prototype.$http.get).toBeCalledWith(
|
|
|
|
'/user/closet-data',
|
|
|
|
{
|
2018-08-02 09:45:33 +08:00
|
|
|
category: 'skin',
|
|
|
|
q: '',
|
|
|
|
page: 1,
|
|
|
|
}
|
2018-08-06 13:16:10 +08:00
|
|
|
);
|
2018-08-02 09:45:33 +08:00
|
|
|
|
|
|
|
const tabCape = wrapper.findAll('.nav-tabs > li').at(1);
|
|
|
|
tabCape.find('a').trigger('click');
|
|
|
|
jest.runAllTicks();
|
2018-08-06 13:16:10 +08:00
|
|
|
expect(Vue.prototype.$http.get).toBeCalledWith(
|
|
|
|
'/user/closet-data',
|
|
|
|
{
|
2018-08-02 09:45:33 +08:00
|
|
|
category: 'cape',
|
|
|
|
q: '',
|
|
|
|
page: 1,
|
|
|
|
}
|
2018-08-06 13:16:10 +08:00
|
|
|
);
|
2018-08-02 09:45:33 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('different categories', () => {
|
2018-08-06 13:16:10 +08:00
|
|
|
Vue.prototype.$http.get.mockResolvedValue({});
|
2018-08-02 09:45:33 +08:00
|
|
|
|
|
|
|
const wrapper = mount(Closet);
|
2018-09-08 13:25:14 +08:00
|
|
|
expect(wrapper.findAll('.nav-tabs > li').at(0).classes('active')).toBeTrue();
|
|
|
|
expect(wrapper.find('#skin-category').classes('active')).toBeTrue();
|
2018-08-02 09:45:33 +08:00
|
|
|
|
|
|
|
wrapper.setData({ category: 'cape' });
|
2018-09-08 13:25:14 +08:00
|
|
|
expect(wrapper.findAll('.nav-tabs > li').at(1).classes('active')).toBeTrue();
|
|
|
|
expect(wrapper.find('#cape-category').classes('active')).toBeTrue();
|
2018-08-02 09:45:33 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('search textures', () => {
|
2018-08-06 13:16:10 +08:00
|
|
|
Vue.prototype.$http.get.mockResolvedValue({});
|
2018-08-02 09:45:33 +08:00
|
|
|
|
|
|
|
const wrapper = mount(Closet);
|
|
|
|
const input = wrapper.find('input');
|
|
|
|
input.element.value = 'q';
|
|
|
|
input.trigger('input');
|
|
|
|
jest.runAllTimers();
|
|
|
|
jest.runAllTicks();
|
2018-08-06 13:16:10 +08:00
|
|
|
expect(Vue.prototype.$http.get).toBeCalledWith(
|
|
|
|
'/user/closet-data',
|
|
|
|
{
|
2018-08-02 09:45:33 +08:00
|
|
|
category: 'skin',
|
|
|
|
q: 'q',
|
|
|
|
page: 1,
|
|
|
|
}
|
2018-08-06 13:16:10 +08:00
|
|
|
);
|
2018-08-02 09:45:33 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('empty closet', () => {
|
2018-08-06 13:16:10 +08:00
|
|
|
Vue.prototype.$http.get.mockResolvedValue({});
|
2018-08-02 09:45:33 +08:00
|
|
|
const wrapper = mount(Closet);
|
|
|
|
expect(wrapper.find('#skin-category').text()).toContain('user.emptyClosetMsg');
|
|
|
|
wrapper.setData({ category: 'cape' });
|
|
|
|
expect(wrapper.find('#cape-category').text()).toContain('user.emptyClosetMsg');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('no matched search result', () => {
|
2018-08-06 13:16:10 +08:00
|
|
|
Vue.prototype.$http.get.mockResolvedValue({});
|
2018-08-02 09:45:33 +08:00
|
|
|
const wrapper = mount(Closet);
|
|
|
|
wrapper.setData({ query: 'q' });
|
|
|
|
expect(wrapper.find('#skin-category').text()).toContain('general.noResult');
|
|
|
|
wrapper.setData({ category: 'cape' });
|
|
|
|
expect(wrapper.find('#cape-category').text()).toContain('general.noResult');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('render items', async () => {
|
2018-08-06 13:16:10 +08:00
|
|
|
Vue.prototype.$http.get.mockResolvedValue({
|
2018-08-02 09:45:33 +08:00
|
|
|
items: [
|
|
|
|
{ tid: 1 },
|
|
|
|
{ tid: 2 }
|
|
|
|
],
|
|
|
|
category: 'skin',
|
|
|
|
total_pages: 1
|
2018-08-06 13:16:10 +08:00
|
|
|
});
|
2018-08-02 09:45:33 +08:00
|
|
|
const wrapper = mount(Closet);
|
|
|
|
await wrapper.vm.$nextTick();
|
|
|
|
expect(wrapper.findAll(ClosetItem)).toHaveLength(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('reload closet when page changed', () => {
|
2018-08-06 13:16:10 +08:00
|
|
|
Vue.prototype.$http.get.mockResolvedValue({});
|
2018-08-02 09:45:33 +08:00
|
|
|
const wrapper = mount(Closet);
|
|
|
|
wrapper.vm.pageChanged();
|
|
|
|
jest.runAllTicks();
|
2018-09-08 11:56:00 +08:00
|
|
|
expect(Vue.prototype.$http.get).toBeCalledTimes(2);
|
2018-08-02 09:45:33 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('remove skin item', () => {
|
2018-08-06 13:16:10 +08:00
|
|
|
Vue.prototype.$http.get.mockResolvedValue({});
|
2018-08-02 09:45:33 +08:00
|
|
|
const wrapper = mount(Closet);
|
2018-08-20 22:19:05 +08:00
|
|
|
wrapper.setData({ skinItems: [{ tid: 1 }] });
|
|
|
|
wrapper.vm.removeSkinItem(0);
|
|
|
|
expect(wrapper.find('#skin-category').text()).toContain('user.emptyClosetMsg');
|
2018-08-02 09:45:33 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('remove cape item', () => {
|
2018-08-06 13:16:10 +08:00
|
|
|
Vue.prototype.$http.get.mockResolvedValue({});
|
2018-08-02 09:45:33 +08:00
|
|
|
const wrapper = mount(Closet);
|
2018-08-20 22:19:05 +08:00
|
|
|
wrapper.setData({ capeItems: [{ tid: 1 }], category: 'cape' });
|
|
|
|
wrapper.vm.removeCapeItem(0);
|
|
|
|
expect(wrapper.find('#cape-category').text()).toContain('user.emptyClosetMsg');
|
2018-08-02 09:45:33 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('compute avatar URL', () => {
|
2018-08-06 13:16:10 +08:00
|
|
|
Vue.prototype.$http.get.mockResolvedValue({});
|
2018-08-02 09:45:33 +08:00
|
|
|
const wrapper = mount(Closet);
|
|
|
|
const { avatarUrl } = wrapper.vm;
|
2019-03-02 21:13:17 +08:00
|
|
|
expect(avatarUrl({ tid_skin: 1 })).toBe('/avatar/35/1');
|
2018-08-02 09:45:33 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('select texture', async () => {
|
2018-08-17 21:38:44 +08:00
|
|
|
Vue.prototype.$http.get
|
|
|
|
.mockResolvedValueOnce({})
|
2018-08-06 13:16:10 +08:00
|
|
|
.mockResolvedValueOnce({ type: 'steve', hash: 'a' })
|
|
|
|
.mockResolvedValueOnce({ type: 'cape', hash: 'b' });
|
2018-08-02 09:45:33 +08:00
|
|
|
|
|
|
|
const wrapper = mount(Closet);
|
|
|
|
wrapper.setData({ skinItems: [{ tid: 1 }] });
|
|
|
|
wrapper.find(ClosetItem).vm.$emit('select');
|
|
|
|
await wrapper.vm.$nextTick();
|
2018-08-17 21:38:44 +08:00
|
|
|
expect(Vue.prototype.$http.get).toBeCalledWith('/skinlib/info/1');
|
2018-08-02 09:45:33 +08:00
|
|
|
expect(wrapper.vm.skinUrl).toBe('/textures/a');
|
|
|
|
|
|
|
|
wrapper.setData({ skinItems: [], capeItems: [{ tid: 2 }], category: 'cape' });
|
|
|
|
wrapper.find(ClosetItem).vm.$emit('select');
|
|
|
|
await wrapper.vm.$nextTick();
|
2018-08-17 21:38:44 +08:00
|
|
|
expect(Vue.prototype.$http.get).toBeCalledWith('/skinlib/info/2');
|
2018-08-02 09:45:33 +08:00
|
|
|
expect(wrapper.vm.capeUrl).toBe('/textures/b');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('apply texture', async () => {
|
|
|
|
window.$ = jest.fn(() => ({
|
|
|
|
iCheck: () => ({
|
|
|
|
on(evt, cb) {
|
|
|
|
cb();
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
0: {
|
|
|
|
dispatchEvent: () => {}
|
|
|
|
}
|
|
|
|
}));
|
2018-08-06 13:16:10 +08:00
|
|
|
Vue.prototype.$http.get
|
|
|
|
.mockResolvedValueOnce({})
|
|
|
|
.mockResolvedValueOnce([])
|
|
|
|
.mockResolvedValueOnce([
|
2019-03-13 13:16:51 +08:00
|
|
|
{ pid: 1, name: 'name', tid_skin: 10 }
|
2018-08-06 13:16:10 +08:00
|
|
|
]);
|
2018-08-02 09:45:33 +08:00
|
|
|
|
|
|
|
const wrapper = mount(Closet);
|
|
|
|
const button = wrapper.find(Previewer).findAll('button').at(0);
|
|
|
|
button.trigger('click');
|
|
|
|
jest.runAllTicks();
|
|
|
|
expect(wrapper.find('.modal-body').text()).toContain('user.closet.use-as.empty');
|
|
|
|
|
|
|
|
button.trigger('click');
|
|
|
|
await wrapper.vm.$nextTick();
|
2018-09-08 13:25:14 +08:00
|
|
|
expect(wrapper.find('input[type="radio"]').attributes('value')).toBe('1');
|
|
|
|
expect(wrapper.find('.model-label > img').attributes('src')).toBe('/avatar/35/10');
|
2018-08-02 09:45:33 +08:00
|
|
|
expect(wrapper.find('.modal-body').text()).toContain('name');
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('submit applying texture', async () => {
|
|
|
|
window.$ = jest.fn(() => ({ modal() {} }));
|
|
|
|
jest.spyOn(toastr, 'info');
|
2018-08-06 13:16:10 +08:00
|
|
|
Vue.prototype.$http.get.mockResolvedValue({});
|
|
|
|
Vue.prototype.$http.post.mockResolvedValueOnce({ errno: 1 })
|
|
|
|
.mockResolvedValue({ errno: 0, msg: 'ok' });
|
2018-08-02 09:45:33 +08:00
|
|
|
const wrapper = mount(Closet);
|
|
|
|
const button = wrapper.find('.modal-footer > a:nth-child(2)');
|
|
|
|
|
|
|
|
button.trigger('click');
|
|
|
|
expect(toastr.info).toBeCalledWith('user.emptySelectedPlayer');
|
|
|
|
|
|
|
|
wrapper.setData({ selectedPlayer: 1 });
|
|
|
|
button.trigger('click');
|
|
|
|
expect(toastr.info).toBeCalledWith('user.emptySelectedTexture');
|
|
|
|
|
|
|
|
wrapper.setData({ selectedSkin: 1 });
|
|
|
|
button.trigger('click');
|
2018-08-06 13:16:10 +08:00
|
|
|
expect(Vue.prototype.$http.post).toBeCalledWith(
|
2018-08-02 09:45:33 +08:00
|
|
|
'/user/player/set',
|
|
|
|
{
|
|
|
|
pid: 1,
|
|
|
|
tid: {
|
|
|
|
skin: 1,
|
|
|
|
cape: undefined
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
wrapper.setData({ selectedSkin: 0, selectedCape: 1 });
|
|
|
|
button.trigger('click');
|
2018-08-06 13:16:10 +08:00
|
|
|
expect(Vue.prototype.$http.post).toBeCalledWith(
|
2018-08-02 09:45:33 +08:00
|
|
|
'/user/player/set',
|
|
|
|
{
|
|
|
|
pid: 1,
|
|
|
|
tid: {
|
|
|
|
skin: undefined,
|
|
|
|
cape: 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
await wrapper.vm.$nextTick();
|
2018-08-14 23:43:56 +08:00
|
|
|
expect(swal).toBeCalledWith({ type: 'success', text: 'ok' });
|
2018-08-02 09:45:33 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('reset selected texture', () => {
|
2018-08-06 13:16:10 +08:00
|
|
|
Vue.prototype.$http.get.mockResolvedValue({});
|
2018-08-02 09:45:33 +08:00
|
|
|
const wrapper = mount(Closet);
|
|
|
|
wrapper.setData({
|
|
|
|
selectedSkin: 1,
|
|
|
|
selectedCape: 2,
|
|
|
|
skinUrl: 'a',
|
|
|
|
capeUrl: 'b'
|
|
|
|
});
|
|
|
|
wrapper.find(Previewer).findAll('button').at(1).trigger('click');
|
|
|
|
expect(wrapper.vm).toEqual(expect.objectContaining({
|
|
|
|
selectedSkin: 0,
|
|
|
|
selectedCape: 0,
|
|
|
|
skinUrl: '',
|
|
|
|
capeUrl: ''
|
|
|
|
}));
|
|
|
|
});
|
2018-08-17 21:38:44 +08:00
|
|
|
|
|
|
|
test('select specified texture initially', async () => {
|
2018-09-08 11:56:00 +08:00
|
|
|
window.history.pushState({}, 'title', `${location.href}?tid=1`);
|
2018-08-17 21:38:44 +08:00
|
|
|
window.$ = jest.fn(() => ({
|
|
|
|
modal() {},
|
|
|
|
iCheck: () => ({
|
|
|
|
on(evt, cb) {
|
|
|
|
cb();
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
0: {
|
|
|
|
dispatchEvent: () => {}
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
Vue.prototype.$http.get
|
|
|
|
.mockResolvedValueOnce({
|
|
|
|
items: [],
|
|
|
|
category: 'skin',
|
|
|
|
total_pages: 1
|
|
|
|
})
|
|
|
|
.mockResolvedValueOnce({ type: 'cape', hash: '' })
|
|
|
|
.mockResolvedValueOnce([]);
|
|
|
|
const wrapper = mount(Closet);
|
|
|
|
jest.runAllTimers();
|
|
|
|
await wrapper.vm.$nextTick();
|
|
|
|
jest.unmock('@/js/utils');
|
|
|
|
});
|