2017-03-03 31 views
1

我试图测试我使用酶装载方法建立的高图。它试图用浅渲染测试它时工作正常,但我想呈现整个组件。这里是有问题的回购:https://github.com/hyalkaf/react-highCharts-enzyme-issue,运行破损测试做:npm run test-mocha当然分叉并运行npm i。以下是代码片段重现该问题:渲染react-highcharts与安装酶给出一个InvalidCharacterError

App.js

import React, { Component } from 'react'; 
global.HighCharts = require('highcharts'); 
require('highcharts/modules/exporting')(global.HighCharts); 
require('highcharts-offline-exporting')(global.HighCharts); 
const ReactHighCharts = require('react-highcharts'); 

const config = { 
    chart: { 
    plotBackgroundColor: null, 
    plotBorderWidth: null, 
    plotShadow: false, 
    type: 'pie' 
    }, 
    title: { 
     text: 'Browser market shares January, 2015 to May, 2015' 
    }, 
    tooltip: { 
     pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>' 
    }, 
    plotOptions: { 
    pie: { 
     allowPointSelect: true, 
     cursor: 'pointer', 
     dataLabels: { 
     enabled: true, 
     format: '<b>{point.name}</b>: {point.percentage:.1f} %' 
     } 
    } 
    }, 
    series: [{ 
    name: 'Brands', 
    colorByPoint: true, 
    data: [{ 
     name: 'Microsoft Internet Explorer', 
     y: 56.33 
    }, { 
     name: 'Chrome', 
     y: 24.03, 
     sliced: true, 
     selected: true 
    }, { 
     name: 'Firefox', 
     y: 10.38 
    }, { 
     name: 'Safari', 
     y: 4.77 
    }, { 
     name: 'Opera', 
     y: 0.91 
    }, { 
     name: 'Proprietary or Undetectable', 
     y: 0.2 
    }] 
    }] 
}; 

class App extends Component { 
    render() { 
    return (
     <div className="App"> 
     <p className="App-intro"> 
      To get started, edit <code>src/App.js</code> and save to reload. 
     </p> 
     <div> 
      <ReactHighCharts config={config} /> 
     </div> 
     </div> 
    ); 
    } 
} 

export default App; 

App.test.js:

import React from 'react'; 
import ReactDOM from 'react-dom'; 
import App from './App'; 
import { mount, shallow } from 'enzyme'; 

it('fails when trying to mount react highcharts',() => { 
    const wrapper = mount(<App />); 
}); 

setup.js:

var jsdom = require('jsdom').jsdom; 

global.document = jsdom('<!doctype html><html><body></body></html>'); 
global.window = document.defaultView; 
global.navigator = global.window.navigator; 
window.sessionStorage = { 
    getItem(key) { 
    return this[key]; 
    }, 
    setItem(key, value) { 
    this[key] = value; 
    }, 
    removeItem(key) { 
    this[key] = undefined; 
    }, 
}; 
window.localStorage = window.sessionStorage; 

最后这里是堆栈跟踪:

InvalidCharacterError 
    at exports.name (node_modules\jsdom\lib\jsdom\living\helpers\validate-names.js:10:11) 
    at DocumentImpl.createElement (node_modules\jsdom\lib\jsdom\living\nodes\Document-impl.js:685:5) 
    at Document.createElement (node_modules\jsdom\lib\jsdom\living\generated\Document.js:92:59) 
    at a.createElement (node_modules\highcharts\highcharts.js:17:221) 
    at Object.init (node_modules\highcharts\highcharts.js:91:494) 
    at Object.createElement (node_modules\highcharts\highcharts.js:62:286) 
    at Object.createElement (node_modules\highcharts\highcharts.js:107:323) 
    at Object.init (node_modules\highcharts\highcharts.js:100:377) 
    at Object.B (node_modules\highcharts\highcharts.js:109:141) 
    at Object.getContainer (node_modules\highcharts\highcharts.js:249:378) 
    at Object.firstRender (node_modules\highcharts\highcharts.js:263:422) 
    at Object.init (node_modules\highcharts\highcharts.js:240:174) 
    at Object.getArgs (node_modules\highcharts\highcharts.js:239:189) 
    at Object.a.Chart (node_modules\highcharts\highcharts.js:238:501) 
    at renderChart (node_modules\react-highcharts\dist\ReactHighcharts.js:1:1283) 
    at componentDidMount (node_modules\react-highcharts\dist\ReactHighcharts.js:1:1804) 
    at node_modules\react-dom\lib\ReactCompositeComponent.js:265:25 
    at measureLifeCyclePerf (node_modules\react-dom\lib\ReactCompositeComponent.js:75:12) 
    at node_modules\react-dom\lib\ReactCompositeComponent.js:264:11 
    at CallbackQueue.notifyAll (node_modules\react-dom\lib\CallbackQueue.js:76:22) 
    at ReactReconcileTransaction.close (node_modules\react-dom\lib\ReactReconcileTransaction.js:80:26) 
    at ReactReconcileTransaction.closeAll (node_modules\react-dom\lib\Transaction.js:206:25) 
    at ReactReconcileTransaction.perform (node_modules\react-dom\lib\Transaction.js:153:16) 
    at batchedMountComponentIntoNode (node_modules\react-dom\lib\ReactMount.js:126:15) 
    at ReactDefaultBatchingStrategyTransaction.perform (node_modules\react-dom\lib\Transaction.js:140:20) 
    at Object.batchedUpdates (node_modules\react-dom\lib\ReactDefaultBatchingStrategy.js:62:26) 
    at Object.batchedUpdates (node_modules\react-dom\lib\ReactUpdates.js:97:27) 
    at Object._renderNewRootComponent (node_modules\react-dom\lib\ReactMount.js:320:18) 
    at Object._renderSubtreeIntoContainer (node_modules\react-dom\lib\ReactMount.js:401:32) 
    at Object.render (node_modules\react-dom\lib\ReactMount.js:422:23) 
    at Object.renderIntoDocument (node_modules\react-dom\lib\ReactTestUtils.js:79:21) 
    at renderWithOptions (node_modules\enzyme\build\react-compat.js:187:26) 
    at new ReactWrapper (node_modules\enzyme\build\ReactWrapper.js:94:59) 
    at mount (node_modules\enzyme\build\mount.js:19:10) 
    at Context.<anonymous> (C:/Users/phil/Downloads/projects with problems/react-highcharts-enzyme-issue/src/App.test.js:7:19) 

版本库:

NPM:3.10.7
节点:6.9.5
反应:15.4.2
酶:2.7.1
jsdom:9.11.0
摩卡:3.2.0

+0

你有没有解决过这个问题? –

回答

1

看起来像HighCharts模块中的问题。乍一看,它看起来像jsdom不喜欢正在创建的元素。

如果您在node_modules/jsdom/lib/jsdom/living/helpers/validate-names.js像下面放console.log("NAME: " + name);在第10行,然后重新运行测试...

exports.name = function (name) { 
    const result = xnv.name(name); 
    console.log("NAME: " + name); 
    if (!result.success) { 
    throw new DOMException(DOMException.INVALID_CHARACTER_ERR, 
     "\"" + name + "\" did not match the Name production: " + result.error); 
    } 
}; 

这是输出:

... 
NAME: style 
NAME: style 
NAME: style 
NAME: <div filled="f" stroked="f" style="position: absolute;left:0;top:0;width:1px;height:1px;visibility: hidden"/> 

的最后一行(<div>)是它破裂的地方。也许这是一个错误。很难说,但是在你的测试中会用酶的render(而不是mount)完成你的目标吗?

// App.test.js 
import React from 'react'; 
import ReactDOM from 'react-dom'; 
import App from './App'; 
import { render, mount, shallow } from 'enzyme'; 

it('fails when trying to mount react highcharts',() => { 
    const wrapper = render(<App />); 
}); 
+0

我会试一试。谢谢 – Kafo

+0

它与渲染引发同样的错误。 – Kafo

+0

@卡法你曾经解决过这个问题吗? – CDeutsch