2

I'm using this question as a basis for my current code. What I am essentially trying to test is to ensure the correct calls have been made. My current D3 function looks like this:

      public updateDrawnData() {
        d3.selectAll('.linePaths')
          .transition()
          .duration(1000)
          .attr('d',  (d) => {
            return this.line(d.data);
        });
      }

My current test looks like:

import { D3LineClass } from './line-class'

import * as d3 from 'd3';

describe('D3 Line Class', () => {

  const testD3Class = new D3LineClass();

  it('should select and upate the drawn lines', () => {

    let d3SpyObject = jasmine.createSpyObj(d3, ['transition', 'duration', 'attr']);

    spyOn(d3, 'selectAll').and.returnValue(d3SpyObject);

    d3SpyObject.attr.and.callFake((key, value) => {
      return this;
    });
    testD3Class.updateDrawnData();
    expect(d3.selectAll).toHaveBeenCalledWith('.linePaths');
    expect(d3SpyObject.transition).toHaveBeenCalled();
    expect(d3SpyObject.duration).toHaveBeenCalledWith(1000);
    expect(d3SpyObject.attr).toHaveBeenCalled();    
  })

});

But I keep getting the following error:

TypeError: Cannot read property 'duration' of undefined

I'm still rather new to Jasmine and I cannot work out why this error is being raised.

Harpal
  • 12,057
  • 18
  • 61
  • 74

0 Answers0