第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

【九月打卡】第10天 jest 環(huán)境之謎、在編輯器中添加上傳組件-上傳校驗(yàn)

標(biāo)簽:
Vue.js

课程名称web前端架构师

课程章节:第11周 第五章

主讲老师:张轩

课程内容:jest浏览器环境之谜,完成upload组件测试;在编辑器中添加上传组件-上传校验

完成upload组件图片本地预览测试

编写测试代码

it.only('test upload img and show img', async () => {
  const wrapper = mount(UploadFile, {
    props: {
      listType: 'picture'
    }
  })
  window.URL.createObjectURL = vi.fn(() => { return 'test.url' })
  const fileInput = wrapper.get('input').element
  setInputVal(fileInput)
  request.mockResolvedValueOnce({ status: 'success' })
  await wrapper.get('input').trigger('change')
  expect(request).toHaveBeenCalledTimes(0)
  expect(wrapper.get('li').classes()).toContain('upload-img')
  const firstImg = wrapper.find('li:first-child img')
  expect(firstImg.exists()).toBeTruthy()
  expect(firstImg.attributes('src')).toEqual('test.url')
})

视频中的 如果不mock window.URL.createObjectURL 会报错,而我本地的代码没有出现报错,但是无法判断图片是否可以正常预览,所以还是需要使用mock

jest 就是通过 jsdom 来模拟浏览器,但是有一部分还是不存在的,比如 window.URL.createObjectURL ,不存在的特性模拟实现一下就可以了

在编辑器中添加上传中组件

首先需要给组件加个一个 shouLIst 参数,表示是否需要展示列表,因为有时候我们需要只展示一个按钮

通用代码封装 - 上传校验

上传文件我们可能需要校验文件大小和文件格式,格式正确才能上传

我们可能会这样写

function uploadCheck (file: File, formatArr: string[], size: number): boolean {
  if (!formatArr.includes(file.type)) {
    return false
  } else if (file.size > size) {
    return false
  }
  return true
}

如果我们需要错误提示信息,可能会这样做

function uploadCheck (file: File, formatArr: string[], size: number): boolean {
  if (!formatArr.includes(file.type)) {
    toast(xxx)
    return false
  } else if (file.size > size) {
    toast(xxx)
    return false
  }
  return true
}

如果我们需要根据指定提示信息,那么可能会将提示信息当作参数传递进来,这样做这个函数就会变得越来约臃肿

function uploadCheck (file: File, formatArr: string[], size: number, msg1: string, msg2: string): boolean {
  if (!formatArr.includes(file.type)) {
    toast(msg1)
    return st
  } else if (file.size > size) {
    toast(msg2)
    return false
  }
  return true
}

我们可以将通用的内容给拆分出来

  • 我们可以将需要校验你的参数传递给函数
  • 函数内部会针对所有需要校验的参数进行处理

下面编写代码

interface CheckCondition{
  format: string[]
  size: number
}

function uploadCheck (file: File, condition: CheckCondition): {passed: boolean, err: string} {
  const { format, size } = condition
  const isValidFormat = format ? format.includes(file.type) : true
  const isValidSize = size ? (file.size > size) : true
  let err: string = ''
  if (isValidFormat) {
    err = 'format'
  }
  if (isValidSize) {
    err = 'size'
  }
  return {
    passed: isValidFormat && isValidSize,
    err
  }
}

其实有时候我们可以将参数校验的函数和信息通过参数传递进来,类似 async-validator

const descriptor = {
  name: {
    type: 'string',
    required: true,
    validator: (rule, value) => value === 'muji',
  },
  age: {
    type: 'number',
    asyncValidator: (rule, value) => {
      return new Promise((resolve, reject) => {
        if (value < 18) {
          reject('too young');  // reject with error message
        } else {
          resolve();
        }
      });
    },
  },
};
const validator = new Schema(descriptor);
validator.validate({ name: 'muji' }, (errors, fields) => {
  if (errors) {
    // validation failed, errors is an array of all errors
    // fields is an object keyed by field name with an array of
    // errors per field
    return handleErrors(errors, fields);
  }
  // validation passed
});
點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫(xiě)下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶(hù)
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專(zhuān)欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消