d3.chart [unit testing]: Spying on Chart Methods

46 views
Skip to first unread message

E Dana

unread,
Mar 3, 2015, 4:33:08 PM3/3/15
to d3...@googlegroups.com
What is the best way to spy on a d3.chart method that is called during `initialize()`?

Context:
  • d3.chart library
  • Unit testing with mocha and sinon

The below code requires instantiating the grid twice:
Once to create the Grid instance on which the spy is created
Then a second time for the spied method to be recorded

The below code works, but I wonder if there is a better and cleaner way. 
*Code is based on the unit tests for d3.chart itself.


test("Calls `util.parse.options` with `options`", function() {
var gridSpy = this.gridSpy =
d3.select("#test").chart("Grid");

sinon.spy(gridSpy.deep.nested, "method");

this.grid = d3.select('#test')
.append('svg')
.chart(
'Grid', 
{ options: this.options }
);

assert.deepEqual(gridSpy.deep.nested.method.args[0][0], this.options);
});

Thanks,

-Evan
Reply all
Reply to author
Forward
0 new messages