A
A
Artem Anashev2016-10-27 19:50:57
React
Artem Anashev, 2016-10-27 19:50:57

Why does the enzyme test on component mount fail with an error when initializing Chart.js?

Created a react component:

import React            from 'react';
import Chart            from 'chart.js';

class RadarChart extends React.Component {
  componentDidMount() {
    const ctx = document.getElementById('radar-chart');
    this.chart = this._createChart(ctx);
  }

  _createChart(ctx) {
    return new Chart(cxt, {
      type: 'radar',
        data: {
        labels: ['Strength', 'Skill', 'Health', 'Speed', 'Luck'],
        datasets: [
          {
            label: 'Points',
            data: [25, 50, 55, 30, 40],
            backgroundColor: 'rgba(255, 99, 132, 0.2)',
            borderColor: 'rgba(255,99,132,1)',
            borderWidth: 1
          },
          {
            label: 'Points',
            data: [42, 19, 43, 56, 33],
            backgroundColor: 'rgba(255, 159, 64, 0.2)',
            borderColor: 'rgba(255, 159, 64, 1)',
            borderWidth: 1
          }
        ]
      }
    });
  },
  render() {
    return (
      <canvas id="radar-chart"></canvas>
      );
  }

}

Everything works perfectly. I create a spec file, I try to test:
import React            from 'react';
import { mount }        from 'enzyme';
import sinon            from 'sinon';

import RadarChart       from './radar-chart.react';

describe('<RadarChart />', () => {
  it('компонент успешно замаунтился', () => {
    sinon.spy(RadarChart.prototype, 'componentDidMount');
    mount(<RadarChart />);
    expect(RadarChart.prototype.componentDidMount.calledOnce).to.equal(true);
  });
});

The code crashes with an error:
null is not an object (evaluating 'context.length')
[email protected]:///~/chart.js/src/core/core.js?5de4:16:0 <- src/js/engine/modules/chart/radar-chart/radar-chart.spec.js:

If you do not call the `_createChart` method, then everything is fine.
Builder: Webpack
Test Runner: Karma
Code: https://github.com/MadMed677/8ForLife/tree/feature...
Maybe it's the very work of the library with Karma?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question