Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 节点环境新Blob酶的单元测试_Javascript_Ecmascript 6_Blob - Fatal编程技术网

Javascript 节点环境新Blob酶的单元测试

Javascript 节点环境新Blob酶的单元测试,javascript,ecmascript-6,blob,Javascript,Ecmascript 6,Blob,我正在尝试使用一些下载函数进行测试,我的测试在node env中运行。但是我使用的方法实际上是domapi,以及如何在nodeenv中测试它。在我的代码中,我使用DOMAPI像windows.URL.revokeObjectURL,document.createElement('a')window.URL.createObjectURL(新Blob([encodeWorkBook(wbout)],{type:'application/octet stream} 实际上,我们只需要在jsdom中添

我正在尝试使用一些下载函数进行测试,我的测试在node env中运行。但是我使用的方法实际上是domapi,以及如何在nodeenv中测试它。在我的代码中,我使用
DOMAPI
windows.URL.revokeObjectURL
document.createElement('a')
window.URL.createObjectURL(新Blob([encodeWorkBook(wbout)],{type:'application/octet stream}


实际上,我们只需要在jsdom中添加
global.Blob=function Blob(params){return params}
,以及stub
window.URL.createObjectURL=sinon.stub().returns(URL)


您试图在节点环境中执行什么操作?写一个二进制数据文件?显然没有“下载”。@Bergi这里的主要问题是,我不能在node env中使用new Blob(),因为我的测试在node env中运行。我当然不能,但你为什么要这样做?
download
中的任何一行都不能在节点中工作。什么是您的?我的意思是,
window.URL
document.createElement
都是DOM APIwell@BergiI重新表述这些问题,如果测试在node env中运行,如何测试dom API方法?
   export const download = (url, name) => {
    let a = document.createElement('a')
    a.href = url
    a.download = name
    a.click()
    window.URL.revokeObjectURL(url)
}

export const encodeWorkBook = string => {
    const buf = new ArrayBuffer(string.length)
    const unicodeArray = new Uint8Array(buf)

    for (let i=0; i !== string.length; ++i)
        unicodeArray[i] = string.charCodeAt(i) & 0xFF

    return unicodeArray
}

export default (data, sheetName, bookType = OutPutFormats.xlsx) => {
    import('xlsx').then(XLSX => {
        const ws = XLSX.utils.json_to_sheet(data, {cellStyles: true})
        const wb = XLSX.utils.book_new()
        XLSX.utils.book_append_sheet(wb, ws, sheetName)

        const wbout = XLSX.write(wb, {bookType, bookSST:true, type: 'binary'})

        /* creates a DOMString containing a URL */

        let url = window.URL.createObjectURL(new Blob([encodeWorkBook(wbout)], {type:'application/octet-stream'}))

        download(url, `import.${bookType}`)
    })
}
describe('encodeWorkBook', () => {
    it('should encode work book to unicode array', () => {
        encodeWorkBook(workBook).should.be.deep.equal(unicodeArray)
    })
})

describe('exportToExcel', () => {
    beforeEach(() => {
        sinon.stub(XSLX.utils, 'json_to_sheet').withArgs(devices, {cellStyles: true}).returns({})
        sinon.stub(XSLX.utils, 'book_new').returns({})
        sinon.stub(XSLX.utils, 'book_append_sheet').withArgs({},{}, sheetName)
        sinon.stub(XSLX, 'write').returns(workBook)
        window.URL.createObjectURL = sinon.stub().returns(url)
        file.download = sinon.spy()
    })

    afterEach(() => {
        XSLX.utils.json_to_sheet.restore()
        XSLX.utils.book_new.restore()
        XSLX.utils.book_append_sheet.restore()
        XSLX.write.restore()
    })

    it('should call download with correct url and output extension', async () => {
        Object.values(OutputFormats).forEach(async format => {
            await exportToExcel(devices, sheetName, format)

            file.download.args[0][0].should.equal(url)
            file.download.args[0][1].should.equal(`import.${format}`)
            file.download.calledOnce.should.be.true
        })
    })

    it('should call download with default output extension', async () => {
        await exportToExcel(devices, sheetName)

        file.download.args[0][1].should.equal(`import.${OutputFormats.xlsx}`)
    })
})