What are unit testing strategies for D3JS?

前端 未结 4 1331
后悔当初
后悔当初 2020-12-23 14:46

I am completely new to D3JS and would like to understand the testing strategies for D3 JS.

To elaborate little more on question - consider I have a simple page that

4条回答
  •  生来不讨喜
    2020-12-23 15:00

    I think you should consider this: http://busypeoples.github.io/post/testing-d3-with-jasmine/

    And it really seems to make sense. I have read the others' answers but I am little bit disagree with them. I think we not only check if right function is called or not but we can check much more than that. Checking only some function call are good at unit testing level but not enough. Such test cases written by developer will be based on developer's understanding like these functions are called or not. But whether these methods should be called or not, this thing can be only checked by going at another level because unlike other work, here are code is making something not returning something that can be just checked and make sure everything is correct.

    We obviously don't need to check whether D3 is doing its work correctly or not. So we can use D3 inside our testing code. But D3 renders SVG and we can check things like if svg have elements where expected. Again it is not going to test whether SVG is showing and rendering properly or not. We are going to check if SVG have elements which are expected and they are set as expected.

    For example: If this is bar chart, we can check the number of bars. As in example in above link here it is check that.

        // extend beforeEach to load the correct data...
    beforeEach(function() {
        var testData =  [{ date: '2014-01', value: 100}, { date: '2014-02', value: 140}, {date: '2014-03', value: 215}];
        c = barChart();
        c.setData(testData);
        c.render();
    });
    
    describe('create bars' ,function() {
        it('should render the correct number of bars', function() {
            expect(getBars().length).toBe(3);
        });
    
        it('should render the bars with correct height', function() {
            expect(d3.select(getBars()[0]).attr('height')).toBeCloseTo(420);
        });
    
        it('should render the bars with correct x', function() {
            expect(d3.select(getBars()[0]).attr('x')).toBeCloseTo(9);
        });
    
        it('should render the bars with correct y', function() {
            expect(d3.select(getBars()[0]).attr('y')).toBeCloseTo(0);
        });
    });
    
    // added a simple helper method for finding the bars..
    function getBars() {
        return d3.selectAll('rect.bar')[0];
    }
    

    Some people probably gonna say that we are going to use D3 inside testing code? Again we should remember that purpose of test writing here is not to test D3 but our logic and SVG code that is compiled in response to our code.

    This is just a way and jasmine is something that is helping us in writing test, you can also go into more detail and in different scenarios. You can make domain and check datapoints width height to cross check if they result into data which were given to render.

    I think I am clear if not then check this link : http://busypeoples.github.io/post/testing-d3-with-jasmine/

    Here write of this article have explained things in detail with how you can use jasmine. Also I think I am still gone into detail. If only unit testing is required at different js functions level then there are a lot more things which can be tested without going into elements detail.

提交回复
热议问题