帮下面的代码用jest写完整的ut export function splitIntNumberStrToArray(value, separator = ',',…
- User
帮下面的代码用jest写完整的ut export function splitIntNumberStrToArray(value, separator = ',', maxLen, name = '', vueInstance) { const reg = /\D/ let list = [] if (value) { list = value .split(separator) .map((s) => s.trim()) .filter((s) => s) const existNaN = list.some((v) => reg.test(v)) if (existNaN) { return false } if (vueInstance && isNum(maxLen) && maxLen > 0 && list.length > maxLen) { vueInstance.$message({ type: 'warning', message: name + '不能超过' + maxLen + '个' }) return false } } return list }
- Assistant
import { splitIntNumberStrToArray } from './helperFunctions'
describe('splitIntNumberStrToArray', () => { it('should return an empty array if no value is provided', () => { const result = splitIntNumberStrToArray() expect(result).toEqual([]) })
it('should split a number string with comma separator into an array of integers', () => { const result = splitIntNumberStrToArray('1,2,3,4') expect(result).toEqual([1, 2, 3, 4]) })
it('should split a number string with a custom separator into an array of integers', () => { const result = splitIntNumberStrToArray('1|2|3|4', '|') expect(result).toEqual([1, 2, 3, 4]) })
it('should remove any whitespace around the separator', () => { const result = splitIntNumberStrToArray('1 ,2 , 3 , 4', ',') expect(result).toEqual([1, 2, 3, 4]) })
- User
帮下面的代码用jest写完整的ut export function parseUrl(url) { var tmpUrl = url || '' tmpUrl = tmpUrl.toLowerCase() if (tmpUrl.indexOf('http') === 0) { var domainReg = /^http(s)?://(www.)?([a-zA-Z0-9][-a-zA-Z0-9]{0,62}(.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+)(:[0-9]+)?/i var results = url.match(domainReg) var httpHost, domain, dotIndex if (results) { httpHost = results[0] domain = results[3] dotIndex = domain.indexOf('.') return { name: domain.substr(0, dotIndex), domain: domain + (results[5] || ''), // 仅host+端口 host: httpHost, // 包含http部分 pathname: url.substr(httpHost.length) // 包括#?号后面的数据 } } else { return { name: '', domain: '', // 仅host host: '', // 包含http部分 pathname: url // 包括#?号后面的数据 } } } else { return { name: '', domain: '', // 仅host host: '', // 包含http部分 pathname: url // 包括#?号后面的数据 } } }
- Assistant
import { parseUrl } from './helperFunctions'
describe('parseUrl', () => { it('should return an object with empty values when no URL is provided', () => { expect(parseUrl()).toEqual({ name: '', domain: '', host: '', pathname: '' }) })
it('should return an object with domain and host values when a valid http URL is provided', () => { const url = 'http://www.example.com/path/to/page.html' const result = parseUrl(url) expect(result.domain).toBe('example.com') expect(result.host).toBe('http://www.example.com') })
- User
为以下代码用Jest写ut export function downLoadExcel(fileName, res) { fileName = fileName || +new Date() + '.xls' const blob = new Blob([res], { type: 'application/x-xls' }) if (window.navigator.msSaveOrOpenBlob) { navigator.msSaveBlob(blob, fileName) } else { var link = document.createElement('a') link.href = window.URL.createObjectURL(blob) link.download = fileName link.click() window.URL.revokeObjectURL(link.href) } }
- Assistant
import { downLoadExcel } from './helperFunctions'
describe('downLoadExcel', () => { const originalNavigator = window.navigator const originalCreateElement = document.createElement const originalRevokeObjectURL = window.URL.revokeObjectURL
beforeAll(() => { delete window.navigator.msSaveOrOpenBlob // disable this method for testing window.URL.createObjectURL = jest.fn(() => 'download-url') window.URL.revokeObjectURL = jest.fn() document.createElement = jest.fn(() => ({ click: jest.fn() })) })