<commit_before>var Plotly = require('@lib/index');
var Lib = require('@src/lib');
var DBLCLICKDELAY = require('@src/plots/cartesian/constants').DBLCLICKDELAY;

var createGraphDiv = require('../assets/create_graph_div');
var destroyGraphDiv = require('../assets/destroy_graph_div');
var mouseEvent = require('../assets/mouse_event');


describe('click interactions', function() {
    var mock = require('@mocks/14.json'),
        mockCopy = Lib.extendDeep({}, mock),
        gd;

    var pointPos = [351, 223],
        blankPos = [70, 363];

    afterEach(destroyGraphDiv);

    // cartesian click events events use the hover data
    // from the mousemove events and then simulate
    // a click event on mouseup
    function click(x, y) {
        mouseEvent('mousemove', x, y);
        mouseEvent('mousedown', x, y);
        mouseEvent('mouseup', x, y);
    }

    function doubleClick(x, y, cb) {
        click(x, y);
        setTimeout(function() {
            click(x, y);
            cb();
        }, DBLCLICKDELAY / 2);
    }

    beforeEach(function(done) {
        gd = createGraphDiv();

        Plotly.plot(gd, mockCopy.data, mockCopy.layout)
            .then(done);
    });

    describe('click events', function() {
        var futureData;

        beforeEach(function() {
            gd.on('plotly_click', function(data) {
                futureData = data;
            });
        });

        it('should not be trigged when not on data points', function() {
            click(blankPos[0], blankPos[1]);
            expect(futureData).toBe(undefined);
        });

        it('should contain the correct fields', function() {
            click(pointPos[0], pointPos[1]);
            expect(futureData.points.length).toEqual(1);

            var pt = futureData.points[0];
            expect(Object.keys(pt)).toEqual([
                'data', 'fullData', 'curveNumber', 'pointNumber',
                'x', 'y', 'xaxis', 'yaxis'
            ]);
            expect(pt.curveNumber).toEqual(0);
            expect(pt.pointNumber).toEqual(11);
            expect(pt.x).toEqual(0.125);
            expect(pt.y).toEqual(2.125);
        });
    });

    describe('double click events', function() {
        var futureData;

        beforeEach(function() {
            gd.on('plotly_doubleclick', function(data) {
                futureData = data;
            });
        });

        it('should return null', function(done) {
            doubleClick(pointPos[0], pointPos[1], function() {
                expect(futureData).toBe(null);
                done();
            });
        });
    });
});<commit_msg>add double click reset/autosize tests<commit_after>var Plotly = require('@lib/index');
var Lib = require('@src/lib');
var DBLCLICKDELAY = require('@src/plots/cartesian/constants').DBLCLICKDELAY;

var createGraphDiv = require('../assets/create_graph_div');
var destroyGraphDiv = require('../assets/destroy_graph_div');
var mouseEvent = require('../assets/mouse_event');
var customMatchers = require('../assets/custom_matchers');


describe('click interactions', function() {
    var mock = require('@mocks/14.json'),
        gd;

    var pointPos = [351, 223],
        blankPos = [70, 363];

    afterEach(destroyGraphDiv);

    // cartesian click events events use the hover data
    // from the mousemove events and then simulate
    // a click event on mouseup
    function click(x, y) {
        mouseEvent('mousemove', x, y);
        mouseEvent('mousedown', x, y);
        mouseEvent('mouseup', x, y);
    }

    function doubleClick(x, y, cb) {
        click(x, y);
        setTimeout(function() {
            click(x, y);
            cb();
        }, DBLCLICKDELAY / 2);
    }

    describe('click events', function() {
        var futureData;

        beforeEach(function(done) {
            gd = createGraphDiv();

            var mockCopy = Lib.extendDeep({}, mock);
            Plotly.plot(gd, mockCopy.data, mockCopy.layout)
                .then(done);

            gd.on('plotly_click', function(data) {
                futureData = data;
            });
        });

        it('should not be trigged when not on data points', function() {
            click(blankPos[0], blankPos[1]);
            expect(futureData).toBe(undefined);
        });

        it('should contain the correct fields', function() {
            click(pointPos[0], pointPos[1]);
            expect(futureData.points.length).toEqual(1);

            var pt = futureData.points[0];
            expect(Object.keys(pt)).toEqual([
                'data', 'fullData', 'curveNumber', 'pointNumber',
                'x', 'y', 'xaxis', 'yaxis'
            ]);
            expect(pt.curveNumber).toEqual(0);
            expect(pt.pointNumber).toEqual(11);
            expect(pt.x).toEqual(0.125);
            expect(pt.y).toEqual(2.125);
        });
    });

    describe('double click events', function() {
        var futureData;

        beforeEach(function(done) {
            gd = createGraphDiv();

            var mockCopy = Lib.extendDeep({}, mock);
            Plotly.plot(gd, mockCopy.data, mockCopy.layout)
                .then(done);

            gd.on('plotly_doubleclick', function(data) {
                futureData = data;
            });

        });

        it('should return null', function(done) {
            doubleClick(pointPos[0], pointPos[1], function() {
                expect(futureData).toBe(null);
                done();
            });
        });
    });

    describe('double click interactions', function() {
        var mockCopy;

        var autoRangeX = [-3.011967491973726, 2.1561305597186564],
            autoRangeY = [-0.9910086301469277, 1.389382716298284];

        var setRangeX = [-3, 1],
            setRangeY = [-0.5, 1];

        var zoomRangeX = [-2, 0],
            zoomRangeY = [0, 0.5];

        var update = {
            'xaxis.range[0]': zoomRangeX[0],
            'xaxis.range[1]': zoomRangeX[1],
            'yaxis.range[0]': zoomRangeY[0],
            'yaxis.range[1]': zoomRangeY[1]
        };

        beforeEach(function() {
            jasmine.addMatchers(customMatchers);

            gd = createGraphDiv();
            mockCopy = Lib.extendDeep({}, mock);
        });

        function setRanges(mockCopy) {
            mockCopy.layout.xaxis.autorange = false;
            mockCopy.layout.xaxis.range = setRangeX.slice();

            mockCopy.layout.yaxis.autorange = false;
            mockCopy.layout.yaxis.range = setRangeY.slice();

            return mockCopy;
        }

        describe('when set to \'reset+autorange\' (the default)', function() {
            it('should work when \'autorange\' is on', function(done) {
                Plotly.plot(gd, mockCopy.data, mockCopy.layout).then(function(){
                    expect(gd.layout.xaxis.range).toBeCloseToArray(autoRangeX);
                    expect(gd.layout.yaxis.range).toBeCloseToArray(autoRangeY);

                    Plotly.relayout(gd, update).then(function() {
                        expect(gd.layout.xaxis.range).toBeCloseToArray(zoomRangeX);
                        expect(gd.layout.yaxis.range).toBeCloseToArray(zoomRangeY);

                        doubleClick(blankPos[0], blankPos[1], function() {
                            expect(gd.layout.xaxis.range).toBeCloseToArray(autoRangeX);
                            expect(gd.layout.yaxis.range).toBeCloseToArray(autoRangeY);

                            done();
                        });
                    });
                });
            });

            it('should reset to set range on double click', function(done) {
                mockCopy = setRanges(mockCopy);

                Plotly.plot(gd, mockCopy.data, mockCopy.layout).then(function(){
                    expect(gd.layout.xaxis.range).toBeCloseToArray(setRangeX);
                    expect(gd.layout.yaxis.range).toBeCloseToArray(setRangeY);

                    Plotly.relayout(gd, update).then(function() {
                        expect(gd.layout.xaxis.range).toBeCloseToArray(zoomRangeX);
                        expect(gd.layout.yaxis.range).toBeCloseToArray(zoomRangeY);

                        doubleClick(blankPos[0], blankPos[1], function() {
                            expect(gd.layout.xaxis.range).toBeCloseToArray(setRangeX);
                            expect(gd.layout.yaxis.range).toBeCloseToArray(setRangeY);

                            done();
                        });
                    });
                });
            });

            it('should autosize on 1st double click and reset on 2nd', function(done) {
                mockCopy = setRanges(mockCopy);

                Plotly.plot(gd, mockCopy.data, mockCopy.layout).then(function(){
                    expect(gd.layout.xaxis.range).toBeCloseToArray(setRangeX);
                    expect(gd.layout.yaxis.range).toBeCloseToArray(setRangeY);

                    doubleClick(blankPos[0], blankPos[1], function() {
                        expect(gd.layout.xaxis.range).toBeCloseToArray(autoRangeX);
                        expect(gd.layout.yaxis.range).toBeCloseToArray(autoRangeY);

                        doubleClick(blankPos[0], blankPos[1], function() {
                            expect(gd.layout.xaxis.range).toBeCloseToArray(setRangeX);
                            expect(gd.layout.yaxis.range).toBeCloseToArray(setRangeY);

                            done();
                        });
                    });
                });
            });

        });

        describe('when set to \'reset\'', function() {
            var config = {
                doubleClick: 'reset'
            };

            it('should work when \'autorange\' is on', function(done) {
                Plotly.plot(gd, mockCopy.data, mockCopy.layout, config).then(function() {
                    expect(gd.layout.xaxis.range).toBeCloseToArray(autoRangeX);
                    expect(gd.layout.yaxis.range).toBeCloseToArray(autoRangeY);

                    Plotly.relayout(gd, update).then(function() {
                        expect(gd.layout.xaxis.range).toBeCloseToArray(zoomRangeX);
                        expect(gd.layout.yaxis.range).toBeCloseToArray(zoomRangeY);

                        doubleClick(blankPos[0], blankPos[1], function() {
                            expect(gd.layout.xaxis.range).toBeCloseToArray(autoRangeX);
                            expect(gd.layout.yaxis.range).toBeCloseToArray(autoRangeY);

                            done();
                        });
                    });
                });
            });

            it('should reset to set range on double click', function(done) {
                mockCopy = setRanges(mockCopy);

                Plotly.plot(gd, mockCopy.data, mockCopy.layout, config).then(function() {
                    expect(gd.layout.xaxis.range).toBeCloseToArray(setRangeX);
                    expect(gd.layout.yaxis.range).toBeCloseToArray(setRangeY);

                    Plotly.relayout(gd, update).then(function() {
                        expect(gd.layout.xaxis.range).toBeCloseToArray(zoomRangeX);
                        expect(gd.layout.yaxis.range).toBeCloseToArray(zoomRangeY);

                        doubleClick(blankPos[0], blankPos[1], function() {
                            expect(gd.layout.xaxis.range).toBeCloseToArray(setRangeX);
                            expect(gd.layout.yaxis.range).toBeCloseToArray(setRangeY);

                            done();
                        });
                    });
                });
            });

            it('should reset on all double clicks', function(done) {
                mockCopy = setRanges(mockCopy);

                Plotly.plot(gd, mockCopy.data, mockCopy.layout, config).then(function() {
                    expect(gd.layout.xaxis.range).toBeCloseToArray(setRangeX);
                    expect(gd.layout.yaxis.range).toBeCloseToArray(setRangeY);

                    doubleClick(blankPos[0], blankPos[1], function() {
                        expect(gd.layout.xaxis.range).toBeCloseToArray(setRangeX);
                        expect(gd.layout.yaxis.range).toBeCloseToArray(setRangeY);

                        done();
                    });
                });
            });

        });

        describe('when set to \'autosize\'', function() {
            var config = {
                doubleClick: 'autosize'
            };

            it('should work when \'autorange\' is on', function(done) {
                Plotly.plot(gd, mockCopy.data, mockCopy.layout, config).then(function() {
                    expect(gd.layout.xaxis.range).toBeCloseToArray(autoRangeX);
                    expect(gd.layout.yaxis.range).toBeCloseToArray(autoRangeY);

                    Plotly.relayout(gd, update).then(function() {
                        expect(gd.layout.xaxis.range).toBeCloseToArray(zoomRangeX);
                        expect(gd.layout.yaxis.range).toBeCloseToArray(zoomRangeY);

                        doubleClick(blankPos[0], blankPos[1], function() {
                            expect(gd.layout.xaxis.range).toBeCloseToArray(autoRangeX);
                            expect(gd.layout.yaxis.range).toBeCloseToArray(autoRangeY);

                            done();
                        });
                    });
                });
            });

            it('should set to autorange on double click', function(done) {
                mockCopy = setRanges(mockCopy);

                Plotly.plot(gd, mockCopy.data, mockCopy.layout, config).then(function() {
                    expect(gd.layout.xaxis.range).toBeCloseToArray(setRangeX);
                    expect(gd.layout.yaxis.range).toBeCloseToArray(setRangeY);

                    Plotly.relayout(gd, update).then(function() {
                        expect(gd.layout.xaxis.range).toBeCloseToArray(zoomRangeX);
                        expect(gd.layout.yaxis.range).toBeCloseToArray(zoomRangeY);

                        doubleClick(blankPos[0], blankPos[1], function() {
                            expect(gd.layout.xaxis.range).toBeCloseToArray(autoRangeX);
                            expect(gd.layout.yaxis.range).toBeCloseToArray(autoRangeY);

                            done();
                        });
                    });
                });
            });

            it('should reset on all double clicks', function(done) {
                mockCopy = setRanges(mockCopy);

                Plotly.plot(gd, mockCopy.data, mockCopy.layout, config).then(function() {
                    expect(gd.layout.xaxis.range).toBeCloseToArray(setRangeX);
                    expect(gd.layout.yaxis.range).toBeCloseToArray(setRangeY);

                    doubleClick(blankPos[0], blankPos[1], function() {
                        expect(gd.layout.xaxis.range).toBeCloseToArray(autoRangeX);
                        expect(gd.layout.yaxis.range).toBeCloseToArray(autoRangeY);

                        done();
                    });
                });
            });

        });

    });
});
{'fraction_non_alphanumeric': 0.13033961262934465, 'fraction_numerical': 0.011408861767046962, 'mean_word_length': 1.6572083186464575, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4501361', 'n_tokens_mistral': 4276, 'n_tokens_neox': 3982, 'n_words': 626}