Skip to content

Replaced findDomNode with ref in order to use react-test-renderer for snapshot testing with storybook #140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions dist/react-chartjs-2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2918,6 +2918,8 @@ var ChartComponent = function (_React$Component) {
getElementAtEvent && getElementAtEvent(instance.getElementAtEvent(event), event);
getElementsAtEvent && getElementsAtEvent(instance.getElementsAtEvent(event), event);
onElementsClick && onElementsClick(instance.getElementsAtEvent(event), event); // Backward compatibility
}, _this.ref = function (element) {
_this.element = element;
}, _temp), _possibleConstructorReturn(_this, _ret);
}

Expand Down Expand Up @@ -2994,7 +2996,7 @@ var ChartComponent = function (_React$Component) {
var data = props.data;

if (typeof data == 'function') {
var node = _reactDom2.default.findDOMNode(this);
var node = this.element;
return data(node);
} else {
return data;
Expand Down Expand Up @@ -3104,7 +3106,7 @@ var ChartComponent = function (_React$Component) {
redraw = _props2.redraw,
plugins = _props2.plugins;

var node = _reactDom2.default.findDOMNode(this);
var node = this.element;
var data = this.memoizeDataProps();

this.chart_instance = new _chart2.default(node, {
Expand All @@ -3124,6 +3126,7 @@ var ChartComponent = function (_React$Component) {


return _react2.default.createElement('canvas', {
ref: this.ref,
height: height,
width: width,
onClick: this.handleOnClick
Expand Down
2 changes: 1 addition & 1 deletion dist/react-chartjs-2.min.js

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ var ChartComponent = function (_React$Component) {
getElementAtEvent && getElementAtEvent(instance.getElementAtEvent(event), event);
getElementsAtEvent && getElementsAtEvent(instance.getElementsAtEvent(event), event);
onElementsClick && onElementsClick(instance.getElementsAtEvent(event), event); // Backward compatibility
}, _this.ref = function (element) {
_this.element = element;
}, _temp), _possibleConstructorReturn(_this, _ret);
}

Expand Down Expand Up @@ -143,7 +145,7 @@ var ChartComponent = function (_React$Component) {
var data = props.data;

if (typeof data == 'function') {
var node = _reactDom2.default.findDOMNode(this);
var node = this.element;
return data(node);
} else {
return data;
Expand Down Expand Up @@ -253,7 +255,7 @@ var ChartComponent = function (_React$Component) {
redraw = _props2.redraw,
plugins = _props2.plugins;

var node = _reactDom2.default.findDOMNode(this);
var node = this.element;
var data = this.memoizeDataProps();

this.chart_instance = new _chart2.default(node, {
Expand All @@ -273,6 +275,7 @@ var ChartComponent = function (_React$Component) {


return _react2.default.createElement('canvas', {
ref: this.ref,
height: height,
width: width,
onClick: this.handleOnClick
Expand Down
12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@
"babel-register": "^6.18.0",
"babelify": "^7.3.0",
"brfs": "^1.4.3",
"canvas": "^1.6.2",
"canvas-prebuilt": "^1.6.5-prerelease.1",
"chai": "^3.5.0",
"chart.js": "^2.3.0",
"cross-env": "^5.0.0",
"debug": "^2.4.1",
"enzyme": "^2.6.0",
"eslint": "^1.6.0",
Expand All @@ -43,6 +44,9 @@
"react-dom": "^0.14 || ^15.0.0-rc || ^15.0",
"sinon": "^1.17.6"
},
"optionalDependencies": {
"canvas": "^1.6.2"
},
"peerDependencies": {
"chart.js": "^2.3",
"react": "^0.14 || ^15.0.0-rc || ^15.0",
Expand All @@ -54,11 +58,11 @@
"chart.js": "global:Chart"
},
"scripts": {
"build": "gulp clean && NODE_ENV=production gulp build",
"build": "gulp clean && cross-env NODE_ENV=production gulp build",
"examples": "gulp dev:server",
"lint": "eslint ./; true",
"publish:site": "NODE_ENV=production gulp publish:examples",
"release": "NODE_ENV=production gulp release",
"publish:site": "cross-env NODE_ENV=production gulp publish:examples",
"release": "cross-env NODE_ENV=production gulp release",
"start": "gulp dev",
"test": "mocha test/config/setup.js test/__tests__/**/*",
"watch": "gulp watch:lib",
Expand Down
9 changes: 7 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class ChartComponent extends React.Component {
transformDataProp(props) {
const { data } = props;
if (typeof(data) == 'function') {
const node = ReactDOM.findDOMNode(this);
const node = this.element;
return data(node);
} else {
return data;
Expand Down Expand Up @@ -194,7 +194,7 @@ class ChartComponent extends React.Component {

renderChart() {
const {options, legend, type, redraw, plugins} = this.props;
const node = ReactDOM.findDOMNode(this);
const node = this.element;
const data = this.memoizeDataProps();

this.chart_instance = new Chart(node, {
Expand All @@ -221,11 +221,16 @@ class ChartComponent extends React.Component {
onElementsClick && onElementsClick(instance.getElementsAtEvent(event), event); // Backward compatibility
}

ref = (element) => {
this.element = element
}

render() {
const {height, width, onElementsClick} = this.props;

return (
<canvas
ref={this.ref}
height={height}
width={width}
onClick={this.handleOnClick}
Expand Down
8 changes: 7 additions & 1 deletion test/config/setup.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
require('babel-register')();

const canvas = require('canvas');
var canvas;
try{
canvas = require('canvas');
}
catch(e){
canvas = require('canvas-prebuilt');
}

const jsdom = require('jsdom');
const document = jsdom.jsdom();
Expand Down
50 changes: 49 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1828,6 +1828,14 @@ caniuse-db@^1.0.30000529, caniuse-db@^1.0.30000634, caniuse-db@^1.0.30000639:
version "1.0.30000665"
resolved "https://registry.yarnpkg.com/caniuse-db/-/caniuse-db-1.0.30000665.tgz#e84f4277935f295f546f8533cb0b410a8415b972"

canvas-prebuilt@^1.6.5-prerelease.1:
version "1.6.5-prerelease.1"
resolved "https://registry.yarnpkg.com/canvas-prebuilt/-/canvas-prebuilt-1.6.5-prerelease.1.tgz#6814b20b9c80835dcc24bfd6199147288630521c"
dependencies:
node-pre-gyp "^0.6.29"
parse-css-font "^2.0.2"
units-css "^0.4.0"

canvas@^1.6.2:
version "1.6.5"
resolved "https://registry.yarnpkg.com/canvas/-/canvas-1.6.5.tgz#557f9988f5d2c95fdc247c61a5ee43de52f6717c"
Expand Down Expand Up @@ -2327,6 +2335,21 @@ create-react-class@^15.5.2:
fbjs "^0.8.9"
object-assign "^4.1.1"

cross-env@^5.0.0:
version "5.0.1"
resolved "https://registry.yarnpkg.com/cross-env/-/cross-env-5.0.1.tgz#ff4e72ea43b47da2486b43a7f2043b2609e44913"
dependencies:
cross-spawn "^5.1.0"
is-windows "^1.0.0"

cross-spawn@^5.1.0:
version "5.1.0"
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-5.1.0.tgz#e8bd0efee58fcff6f8f94510a0a554bbfa235449"
dependencies:
lru-cache "^4.0.1"
shebang-command "^1.2.0"
which "^1.2.9"

cryptiles@2.x.x:
version "2.0.5"
resolved "https://registry.yarnpkg.com/cryptiles/-/cryptiles-2.0.5.tgz#3bdfecdc608147c1c67202fa291e7dca59eaa3b8"
Expand Down Expand Up @@ -4484,6 +4507,10 @@ is-windows@^0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/is-windows/-/is-windows-0.2.0.tgz#de1aa6d63ea29dd248737b69f1ff8b8002d2108c"

is-windows@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/is-windows/-/is-windows-1.0.1.tgz#310db70f742d259a16a369202b51af84233310d9"

isarray@0.0.1, isarray@~0.0.1:
version "0.0.1"
resolved "https://registry.yarnpkg.com/isarray/-/isarray-0.0.1.tgz#8a18acfca9a8f4177e09abfc6038939b05d1eedf"
Expand Down Expand Up @@ -5155,6 +5182,13 @@ lru-cache@2:
version "2.7.3"
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-2.7.3.tgz#6d4524e8b955f95d4f5b58851ce21dd72fb4e952"

lru-cache@^4.0.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-4.1.1.tgz#622e32e82488b49279114a4f9ecf45e7cd6bba55"
dependencies:
pseudomap "^1.0.2"
yallist "^2.1.2"

macaddress@^0.2.8:
version "0.2.8"
resolved "https://registry.yarnpkg.com/macaddress/-/macaddress-0.2.8.tgz#5904dc537c39ec6dbefeae902327135fa8511f12"
Expand Down Expand Up @@ -6225,6 +6259,10 @@ prr@~0.0.0:
version "0.0.0"
resolved "https://registry.yarnpkg.com/prr/-/prr-0.0.0.tgz#1a84b85908325501411853d0081ee3fa86e2926a"

pseudomap@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/pseudomap/-/pseudomap-1.0.2.tgz#f052a28da70e618917ef0a8ac34c1ae5a68286b3"

public-encrypt@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/public-encrypt/-/public-encrypt-4.0.0.tgz#39f699f3a46560dd5ebacbca693caf7c65c18cc6"
Expand Down Expand Up @@ -6944,6 +6982,12 @@ shasum@^1.0.0:
json-stable-stringify "~0.0.0"
sha.js "~2.4.4"

shebang-command@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/shebang-command/-/shebang-command-1.2.0.tgz#44aac65b695b03398968c39f363fee5deafdf1ea"
dependencies:
shebang-regex "^1.0.0"

shebang-regex@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/shebang-regex/-/shebang-regex-1.0.0.tgz#da42f49740c0b42db2ca9728571cb190c98efea3"
Expand Down Expand Up @@ -7926,7 +7970,7 @@ whet.extend@~0.9.9:
version "0.9.9"
resolved "https://registry.yarnpkg.com/whet.extend/-/whet.extend-0.9.9.tgz#f877d5bf648c97e5aa542fadc16d6a259b9c11a1"

which@^1.2.12:
which@^1.2.12, which@^1.2.9:
version "1.2.14"
resolved "https://registry.yarnpkg.com/which/-/which-1.2.14.tgz#9a87c4378f03e827cecaf1acdf56c736c01c14e5"
dependencies:
Expand Down Expand Up @@ -8014,6 +8058,10 @@ y18n@^3.2.0:
version "3.2.1"
resolved "https://registry.yarnpkg.com/y18n/-/y18n-3.2.1.tgz#6d15fba884c08679c0d77e88e7759e811e07fa41"

yallist@^2.1.2:
version "2.1.2"
resolved "https://registry.yarnpkg.com/yallist/-/yallist-2.1.2.tgz#1c11f9218f076089a47dd512f93c6699a6a81d52"

yargs@~3.10.0:
version "3.10.0"
resolved "https://registry.yarnpkg.com/yargs/-/yargs-3.10.0.tgz#f7ee7bd857dd7c1d2d38c0e74efbd681d1431fd1"
Expand Down