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

184 lines
5.4 KiB
TypeScript
Raw Normal View History

2019-03-15 11:42:41 +08:00
/* eslint-disable accessor-pairs */
import Vue from 'vue'
import { mount } from '@vue/test-utils'
import { Radio } from 'element-ui'
import * as skinview3d from 'skinview3d'
2019-03-18 15:00:18 +08:00
import Upload from '@/views/skinlib/Upload.vue'
2019-03-15 11:42:41 +08:00
import { flushPromises } from '../../utils'
2018-08-15 16:44:21 +08:00
2018-09-09 09:28:05 +08:00
window.blessing.extra = {
2019-03-15 11:42:41 +08:00
textureNameRule: 'rule',
privacyNotice: 'privacyNotice',
scorePrivate: 10,
scorePublic: 1,
2019-03-20 23:28:04 +08:00
award: 0,
contentPolicy: 'the policy',
2019-03-15 11:42:41 +08:00
}
2018-08-15 16:44:21 +08:00
2019-03-15 11:42:41 +08:00
const csrf = document.createElement('meta')
csrf.name = 'csrf-token'
csrf.content = 'token'
document.head.appendChild(csrf)
2018-08-20 23:33:31 +08:00
2018-08-15 16:44:21 +08:00
test('display drap and drop notice', () => {
2019-03-15 11:42:41 +08:00
const wrapper = mount(Upload, {
stubs: ['file-upload'],
})
expect(wrapper.text()).toContain('skinlib.upload.dropZone')
wrapper.setData({ files: [{}] })
expect(wrapper.contains('img')).toBeTrue()
})
2019-03-17 10:21:18 +08:00
2018-08-15 16:44:21 +08:00
test('button for removing texture', () => {
2019-03-18 09:55:24 +08:00
const wrapper = mount<Vue & { texture: string }>(Upload, {
2019-03-15 11:42:41 +08:00
stubs: ['file-upload'],
})
2019-03-18 09:55:24 +08:00
Object.defineProperty(wrapper.vm.$refs.upload, 'clear', {
get: () => jest.fn(),
})
2019-03-26 22:04:27 +08:00
const button = wrapper.find('[data-test=remove]')
2019-03-15 11:42:41 +08:00
expect(button.isVisible()).toBeFalse()
wrapper.setData({ files: [{}] })
expect(button.isVisible()).toBeTrue()
button.trigger('click')
expect(wrapper.vm.texture).toBe('')
})
2019-03-17 10:21:18 +08:00
2018-08-15 16:44:21 +08:00
test('notice should be display if texture is private', () => {
2019-03-15 11:42:41 +08:00
const wrapper = mount(Upload, {
stubs: ['file-upload'],
})
2019-05-01 20:15:41 +08:00
expect(wrapper.contains('.callout-info')).toBeFalse()
2019-03-15 11:42:41 +08:00
wrapper.find('[type=checkbox]').setChecked()
2019-05-01 20:15:41 +08:00
expect(wrapper.find('.callout-info').text()).toBe('privacyNotice')
2019-03-15 11:42:41 +08:00
})
2019-03-17 10:21:18 +08:00
2018-08-15 16:44:21 +08:00
test('display score cost', () => {
2019-03-15 11:42:41 +08:00
const origin = Vue.prototype.$t
Vue.prototype.$t = (key, args) => `${key}${JSON.stringify(args)}`
const wrapper = mount(Upload, {
stubs: ['file-upload'],
})
wrapper.find('[type=checkbox]').setChecked()
wrapper.setData({ files: [{ size: 1024 }] })
expect(wrapper.text()).toContain(JSON.stringify({ score: 10 }))
Vue.prototype.$t = origin
})
2019-03-17 10:21:18 +08:00
2018-08-15 16:44:21 +08:00
test('process input file', () => {
2019-03-15 11:42:41 +08:00
window.URL.createObjectURL = jest.fn().mockReturnValue('file-url')
2019-03-18 09:55:24 +08:00
;(window as Window & { Image: jest.Mock }).Image = jest.fn()
.mockImplementationOnce(function (this: HTMLImageElement) {
2019-03-15 11:42:41 +08:00
this.src = ''
this.onload = null
Object.defineProperty(this, 'onload', {
set: fn => fn(),
})
})
2019-03-18 09:55:24 +08:00
.mockImplementationOnce(function (this: HTMLImageElement) {
2019-03-15 11:42:41 +08:00
this.src = ''
this.width = 500
this.onload = null
Object.defineProperty(this, 'onload', {
set: fn => fn(),
})
})
jest.spyOn(skinview3d, 'isSlimSkin')
.mockReturnValueOnce(true)
.mockReturnValue(false)
2019-03-15 11:42:41 +08:00
const blob = new Blob()
2019-03-18 09:55:24 +08:00
type Component = Vue & {
name: string
texture: string
inputFile(attrs?: { file: Blob, name: string }): void
}
const wrapper = mount<Component>(Upload, {
2019-03-15 11:42:41 +08:00
stubs: ['file-upload'],
})
const radioes = wrapper.findAll(Radio)
2019-03-15 11:42:41 +08:00
wrapper.vm.inputFile()
expect(wrapper.vm.name).toBe('')
wrapper.vm.inputFile({ file: blob, name: '123.png' })
expect(wrapper.vm.name).toBe('123')
expect(radioes.at(1).classes()).toContain('is-checked')
2019-03-15 11:42:41 +08:00
wrapper.vm.inputFile({ file: blob, name: '456.png' })
expect(wrapper.vm.name).toBe('123')
expect(radioes.at(0).classes()).toContain('is-checked')
2019-03-15 11:42:41 +08:00
wrapper.setData({ name: '' })
wrapper.vm.inputFile({ file: blob, name: '789' })
expect(wrapper.vm.name).toBe('789')
expect(wrapper.vm.texture).toBe('file-url')
2019-03-18 09:55:24 +08:00
;(window as Window & { Image: jest.Mock }).Image.mockRestore()
2019-03-15 11:42:41 +08:00
})
2019-03-17 10:21:18 +08:00
2018-08-15 16:44:21 +08:00
test('upload file', async () => {
2019-03-18 09:55:24 +08:00
(window as Window & { Request: jest.Mock }).Request = jest.fn()
2019-03-15 11:42:41 +08:00
Vue.prototype.$http.post
2019-04-23 11:47:45 +08:00
.mockResolvedValueOnce({ code: 1, message: '1' })
2019-03-15 11:42:41 +08:00
.mockResolvedValueOnce({
2019-04-23 19:14:41 +08:00
code: 0, message: '0', data: { tid: 1 },
2019-03-15 11:42:41 +08:00
})
const wrapper = mount(Upload, {
stubs: ['file-upload'],
})
const button = wrapper.find('.box-footer > button')
button.trigger('click')
expect(Vue.prototype.$http.post).not.toBeCalled()
2019-03-25 22:01:57 +08:00
expect(Vue.prototype.$message.error).toBeCalledWith('skinlib.emptyUploadFile')
2019-03-15 11:42:41 +08:00
wrapper.setData({
files: [{
file: {}, name: 't', type: 'image/jpeg',
}],
})
button.trigger('click')
expect(Vue.prototype.$http.post).not.toBeCalled()
2019-03-25 22:01:57 +08:00
expect(Vue.prototype.$message.error).toBeCalledWith('skinlib.emptyTextureName')
2019-03-15 11:42:41 +08:00
wrapper.find('[type=text]').setValue('t')
button.trigger('click')
expect(Vue.prototype.$http.post).not.toBeCalled()
2019-03-25 22:01:57 +08:00
expect(Vue.prototype.$message.error).toBeCalledWith('skinlib.fileExtError')
2019-03-15 11:42:41 +08:00
wrapper.setData({
files: [{
file: new Blob(), name: 't.png', type: 'image/png',
}],
})
button.trigger('click')
await flushPromises()
expect(Vue.prototype.$http.post).toBeCalledWith('/skinlib/upload', expect.any(FormData))
2019-03-25 22:01:57 +08:00
expect(Vue.prototype.$message.error).toBeCalledWith('1')
2019-03-15 11:42:41 +08:00
button.trigger('click')
await flushPromises()
jest.runAllTimers()
2019-03-25 22:01:57 +08:00
expect(Vue.prototype.$message.success).toBeCalledWith('0')
2019-03-15 11:42:41 +08:00
})
2019-03-20 23:28:04 +08:00
test('show notice about awarding', () => {
const wrapper = mount(Upload)
expect(wrapper.find('.callout-success').exists()).toBeFalse()
wrapper.setData({ award: 5 })
expect(wrapper.find('.callout-success').exists()).toBeTrue()
wrapper.find('[type=checkbox]').setChecked()
expect(wrapper.find('.callout-success').exists()).toBeFalse()
})
test('show content policy', () => {
const wrapper = mount(Upload)
2019-05-01 20:15:41 +08:00
expect(wrapper.find('.callout-warning').exists()).toBeTrue()
})