var _ = require('underscore');
var HistogramDataModel = require('../../../../src/dataviews/histogram-dataview/histogram-data-model');

describe('dataviews/histogram-data-model', function () {
  var apiKey = 'ac3560ef-78f8-45d8-b043-5544f8a76753';
  var url = 'https://carto.geo';

  function buildUrl (params) {
    var urlParams = _.map(_.keys(params), function (key) {
      return key + '=' + params[key];
    });
    return url + '?' + urlParams.join('&');
  }

  beforeEach(function () {
    this.model = new HistogramDataModel({
      apiKey: apiKey,
      url: url
    });
  });

  describe('.url', function () {
    it('should return no bins param if type is number and bins is undefined', function () {
      this.model.set({ column_type: 'number' });

      var url = this.model.url();

      expect(url).toEqual(buildUrl({ api_key: apiKey }));
    });

    it('should return bins param if type is number and bins is defined', function () {
      this.model.set({
        column_type: 'number',
        bins: 48
      });

      var url = this.model.url();

      expect(url).toEqual(buildUrl({
        bins: 48,
        api_key: apiKey
      }));
    });

    it('should return aggregation auto if type is date and aggregation is undefined', function () {
      this.model.set({
        column_type: 'date',
        aggregation: undefined
      });

      var url = this.model.url();

      expect(url).toEqual(buildUrl({
        aggregation: 'auto',
        api_key: apiKey
      }));
    });

    it('should return aggregation if type is date and aggregation is defined', function () {
      this.model.set({
        column_type: 'date',
        aggregation: 'minute'
      });

      var url = this.model.url();

      expect(url).toEqual(buildUrl({
        aggregation: 'minute',
        api_key: apiKey
      }));
    });
  });
});

@@ -272,16 +272,40 @@
 to.geo';
+%0A  var defaultBins = 45;
 %0A%0A  func
@@ -604,16 +604,1936 @@
 %0A  %7D);%0A%0A
+  describe('._initBinds', function () %7B%0A    beforeEach(function () %7B%0A      spyOn(this.model, 'fetch');%0A    %7D);%0A%0A    afterEach(function () %7B%0A      this.model.set(%7B%0A        url: url,%0A        aggregation: undefined,%0A        bins: defaultBins%0A      %7D, %7B silent: true %7D);%0A    %7D);%0A%0A    it('should call to fetch when the url changes', function () %7B%0A      this.model.set('url', 'https://carto.geo/aa45');%0A%0A      expect(this.model.fetch).toHaveBeenCalled();%0A    %7D);%0A%0A    it('should call to fetch when the aggregation changes to a defined value in a date column', function () %7B%0A      this.model.set('column_type', 'date', %7B silent: true %7D);%0A%0A      this.model.set('aggregation', 'month');%0A%0A      expect(this.model.fetch).toHaveBeenCalled();%0A    %7D);%0A%0A    it('should not call to fetch when the aggregation changes to an undefined value in a date column', function () %7B%0A      this.model.set('column_type', 'date', %7B silent: true %7D);%0A%0A      this.model.set('aggregation', undefined);%0A%0A      expect(this.model.fetch).not.toHaveBeenCalled();%0A    %7D);%0A%0A    it('should call to fetch when the bins changes to a defined value with no aggregation in a number column', function () %7B%0A      this.model.set('column_type', 'number', %7B silent: true %7D);%0A      this.model.set('aggregation', undefined, %7B silent: true %7D);%0A%0A      this.model.set('bins', defaultBins + 1);%0A%0A      expect(this.model.fetch).toHaveBeenCalled();%0A    %7D);%0A%0A    it('should not call to fetch when the bins changes to a defined value with aggregation in a number column', function () %7B%0A      // This happens when switching from date column to bins column.%0A      // This prevents requesting data before having the map well instantiated%0A      this.model.set('column_type', 'number', %7B silent: true %7D);%0A      this.model.set('aggregation', 'week', %7B silent: true %7D);%0A%0A      this.model.set('bins', defaultBins + 1);%0A%0A      expect(this.model.fetch).not.toHaveBeenCalled();%0A    %7D);%0A  %7D);%0A%0A
   descri

Add _initBind specs to histogram-data-model