d3 addGraph not getting called..

272 views
Skip to first unread message

G Bhargava

unread,
Mar 31, 2015, 6:12:47 AM3/31/15
to jasmi...@googlegroups.com
I do not have much of experience with jasmine. Am trying to test the nv.d3 addGraph function but the function doesn't seem to get called. I am not sure what am I missing. Any help will be greatly appreciated!! Thanks in advance.

The factory is:
angular.module('test.addGraph', []).factory('displayCharts', function() {
           nv.addGraph(function() {
            var chart = nv.models.lineChart().margin({
                left: 30, right: 30
            }).useInteractiveGuideline(false).interactive(false).transitionDuration(350).showYAxis(true).showXAxis(true);
            var maxTicks = 3;
           ..........
           });
});

The test is:
describe('Unit Test: chart service', function(){
 var mockAddGraph, scope, data, id, chartType, d3Spy;

    beforeEach(module(''test.addGraph'));

    beforeEach(function() {
        data = [{"data1": "1424178000000","data2": "100"}];
        id = "utilChart";
        chartType = "performance";
    });

    beforeEach(inject(function($injector, $rootScope, $q) {
        mockAddGraph= $injector.get('displayCharts');
        scope = $rootScope.$new();
        //mock get element by id
        var mockHTML = document.createElement('svg');
        document.getElementById = jasmine.createSpy("HTML Element").and.returnValue(mockHTML);
    }));

   it('unit test for renderUtilChart', function()   {
       expect(mockAddGraph).toBeDefined();
       var d3Spy = jasmine.createSpyObj('nv', ['addGraph']);
       spyOn(nv, 'addGraph').and.returnValue(d3Spy);
   });
});


Gregg Van Hove

unread,
Mar 31, 2015, 9:27:06 PM3/31/15
to jasmi...@googlegroups.com
From what I'm seeing in your spec, there is only setup, but nothing to actually do any work or `expect` anything afterwards. Can you provide more detail about what you're trying to test?

-Gregg

--
You received this message because you are subscribed to the Google Groups "Jasmine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jasmine-js+...@googlegroups.com.
To post to this group, send email to jasmi...@googlegroups.com.
Visit this group at http://groups.google.com/group/jasmine-js.
For more options, visit https://groups.google.com/d/optout.

G Bhargava

unread,
Apr 2, 2015, 1:49:41 AM4/2/15
to jasmi...@googlegroups.com
Hello Gregg,

I am trying to call the nv.addGraph() function. I have not added any expect yet as the function itself is not getting called. I have added 

 var d3Spy = jasmine.createSpyObj('nv', ['addGraph']);
 spyOn(nv, 'addGraph').and.returnValue(d3Spy);

to call the addGraph() function but it does not work.

I have included nv.d3.in.js in karma-conf.js. 

Thanks.

Gregg Van Hove

unread,
Apr 2, 2015, 3:55:07 PM4/2/15
to jasmi...@googlegroups.com
I think the problem may be that in your `beforeEach` you've already executed the factory when you `$injector.get('displayCharts')` so when you spy on the `nv.addGraph` function later it has already been called.

-Gregg
Reply all
Reply to author
Forward
0 new messages