fixing issue with defaultYExtents not working, added tests to ensure there are no regressions

This commit is contained in:
Shelby Sturgis 2015-04-16 10:38:47 -04:00
parent 621511c795
commit 40dd93cacb
2 changed files with 50 additions and 3 deletions

View file

@ -74,6 +74,10 @@ define(function (require) {
return [Math.max(1, yMin), yMax];
};
YAxis.prototype._setDefaultYExtents = function () {
return this._attr.defaultYExtents;
};
/**
* Returns the domain, i.e. the extent of the y axis
*
@ -83,6 +87,7 @@ define(function (require) {
* @returns {*[]}
*/
YAxis.prototype.getDomain = function (scale, yMin, yMax) {
if (this._setDefaultYExtents()) return [yMin, yMax];
if (scale === 'log') return this.returnLogDomain(yMin, yMax); // Negative values cannot be displayed with a log scale.
if (yMin === 0 && yMax === 0) return this.throwNoResultsError(); // yMin and yMax can never both be equal to zero
@ -97,10 +102,15 @@ define(function (require) {
* @returns {D3.Scale.QuantitiveScale|*} D3 yScale function
*/
YAxis.prototype.getYScale = function (height) {
return this.yScale = this.getScaleType(this._attr.scale)
this.yScale = this.getScaleType(this._attr.scale)
.domain(this.getDomain(this._attr.scale, this.yMin, this.yMax))
.range([height, 0])
.nice();
.range([height, 0]);
// Nicing the scale, rounds values down or up to make the scale look better
// When defaultYExtents are selected, the extents (i.e. min and max) should
// be shown without any rounding.
if (!this._attr.defaultYExtents) return this.yScale.nice();
return this.yScale;
};
/**

View file

@ -204,6 +204,23 @@ define(function (require) {
checkRange();
});
});
describe('should not return a nice scale when defaultYExtents is true', function () {
beforeEach(function () {
createData(defaultGraphData);
yAxis._attr.defaultYExtents = true;
yAxis.getYAxis(height);
yAxis.render();
});
it('not return a nice scale', function () {
var min = _.min(_.flatten(defaultGraphData));
var max = _.max(_.flatten(defaultGraphData));
var domain = yAxis.yAxis.scale().domain();
expect(domain[0]).to.be(min);
expect(domain[1]).to.be(max);
});
});
});
describe('formatAxisLabel method', function () {
@ -260,6 +277,13 @@ define(function (require) {
});
describe('getDomain method', function () {
beforeEach(function () {
// Need to set this to false before each test since its
// status changes in one of the tests below. Having this set to
// true causes other tests to fail that need this attr to be set to false.
yAxis._attr.defaultYExtents = false;
});
it('should return a log domain', function () {
var scale = 'log';
var yMin = 0;
@ -270,6 +294,19 @@ define(function (require) {
expect(domain[0]).to.be(1);
});
it('should return the default y axis extents (i.e. the min and max values)', function () {
var scale = 'linear';
var yMin = 25;
var yMax = 150;
var domain;
yAxis._attr.defaultYExtents = true;
domain = yAxis.getDomain(scale, yMin, yMax);
expect(domain[0]).to.be(yMin);
expect(domain[1]).to.be(yMax);
});
it('should throw a no results error if yMin and yMax values are both 0', function () {
expect(function () {
yAxis.getDomain('linear', 0, 0);