diff --git a/src/plots/cartesian/axes.js b/src/plots/cartesian/axes.js index 5b1d92a886f..f4645b8ee03 100644 --- a/src/plots/cartesian/axes.js +++ b/src/plots/cartesian/axes.js @@ -1262,6 +1262,7 @@ function syncTicks(ax) { } function arrayTicks(ax, major) { + if (major === undefined) throw new Error("arrayTicks must specify ticktype") var rng = Lib.simpleMap(ax.range, ax.r2l); var exRng = expandRange(rng); var tickMin = Math.min(exRng[0], exRng[1]); diff --git a/test/jasmine/tests/axes_test.js b/test/jasmine/tests/axes_test.js index d128072a307..76b4a8e6e74 100644 --- a/test/jasmine/tests/axes_test.js +++ b/test/jasmine/tests/axes_test.js @@ -28,7 +28,6 @@ var supplyDefaults = require('../assets/supply_defaults'); describe('Test axes', function() { 'use strict'; - describe('swap', function() { it('should swap most attributes and fix placeholder titles', function() { var gd = { @@ -8217,3 +8216,65 @@ describe('shift tests', function() { }); }); }); +describe('test tickmode calculator', function() { + beforeEach(function() { + gd = createGraphDiv(); + }); + + afterEach(destroyGraphDiv); + + function generateTickConfig(){ + standardConfig = {tickmode: 'array', ticks: 'inside', ticklen: 1, showticklabels: false}; + + // Number of ticks will be random + var n = Math.floor(Math.random() * 99) + 1; + tickVals = []; + for(let i = 0; i <= n; i++) tickVals.push(i); + standardConfig['tickvals'] = tickVals; + standardConfig['ticktext'] = tickVals; + return standardConfig; + } + var ticksOff = {tickmode:"array", ticks: '', tickvals:[], ticktext:[], ticklen: 0, showticklabels: false}; + // the goal is target the arrayTicks() function, the subject of PR: https://github.com/plotly/plotly.js/pull/6829 + // we test xMajor and xMinor in on/on on/off off/on and off/off + // since we can't unit test functions that are not exported, we shim functions we don't care about instead and + // test the nearest exported functions (calcTicks) + describe('arrayTicks', function() { + for(let i = 0; i < 4; i++) { + (function(i) { + it('should return the specified correct number of major ticks and minor ticks', function() { + const BOTH = 0; + const MAJOR = 1; + const MINOR = 2; + const NEITHER = 3; + var xMajorConfig = ticksOff; + var xMinorConfig = ticksOff; + if(i == BOTH) { + xMajorConfig = generateTickConfig(); + xMinorConfig = generateTickConfig(); + } else if(i == MAJOR) { + xMajorConfig = generateTickConfig(); + } else if(i==MINOR) { + xMinorConfig = generateTickConfig(); + } else if(i == NEITHER) { + // Do nothing, all ticks off + } + xaxis = { + r2l: Lib.cleanNumber, + d2l: Lib.cleanNumber, + _separators: ',', + range: [0, 1000], + ...xMajorConfig, + minor: { + ...xMinorConfig, + }, + }; + Axes.prepMinorTicks = function() { return }; // Not part of this test + Axes.prepTicks = function() { return }; // Not part of this test + ticksOut = Axes.calcTicks(xaxis); + expect(ticksOut.length).toEqual(xMajorConfig.tickvals.length + xMinorConfig.tickvals.length); + }); + })(i); + } + }); +});