<commit_before>// var Backbone = require('backbone');
// var _ = require('underscore');
// var AnalysesQuotaFactory = require('../../../../../../../javascripts/cartodb3/editor/layers/layer-content-views/analyses/analyses-quota-factory');
// var QUOTA_FIELDS = ['totalQuota', 'usedQuota', 'blockSize', 'blockPrice', 'hardLimit'];

describe('editor/layers/layer-content-view/analyses/analyses-service', function () {
  // beforeEach(function () {
  //   this.userModel = new Backbone.Model({
  //     isolines_provider: 'heremaps',
  //     geocoder_provider: 'heremaps'
  //   });
  //   this.userModel._organizationModel = new Backbone.Model({
  //     isolines_provider: 'heremaps',
  //     geocoder_provider: 'heremaps'
  //   });

  //   this.userModel.isInsideOrg = function () {
  //     return false;
  //   };
  //   this.userModel.getOrganization = function () {
  //     return this.userModel._organizationModel;
  //   }.bind(this);
  // });

  // describe('analyses without quota', function () {
  //   it('should not return any quota info', function () {
  //     expect(AnalysesQuotaFactory.requiresQuota('carto-analysis', this.userModel)).toBeFalsy();
  //   });
  // });

  // describe('analysis with quota', function () {
  //   describe('trade-area', function () {
  //     it('should return quota', function () {
  //       expect(AnalysesQuotaFactory.requiresQuota('trade-area', this.userModel)).toBeTruthy();
  //       var analysisQuota = AnalysesQuotaFactory.getQuotaInfo('trade-area', this.userModel);
  //       _.each(QUOTA_FIELDS, function (item) {
  //         expect(analysisQuota[item]).toBeDefined();
  //       });
  //     });

  //     it('should not require quota when provider is different than heremaps', function () {
  //       expect(AnalysesQuotaFactory.requiresQuota('trade-area', this.userModel)).toBeTruthy();
  //       this.userModel.set('isolines_provider', null);
  //       expect(AnalysesQuotaFactory.requiresQuota('trade-area', this.userModel)).toBeFalsy();
  //       this.userModel.set('isolines_provider', 'mapzen');
  //       expect(AnalysesQuotaFactory.requiresQuota('trade-area', this.userModel)).toBeFalsy();
  //     });

  //     it('should take organization provider attributes when user belongs to one', function () {
  //       this.userModel.isInsideOrg = function () { return true; };
  //       expect(AnalysesQuotaFactory.requiresQuota('trade-area', this.userModel)).toBeTruthy();
  //       this.userModel.set('isolines_provider', null);
  //       expect(AnalysesQuotaFactory.requiresQuota('trade-area', this.userModel)).toBeTruthy();
  //     });
  //   });

  //   describe('georeference-street-address', function () {
  //     it('should return quota', function () {
  //       expect(AnalysesQuotaFactory.requiresQuota('georeference-street-address', this.userModel)).toBeTruthy();
  //       var analysisQuota = AnalysesQuotaFactory.getQuotaInfo('georeference-street-address', this.userModel);
  //       _.each(QUOTA_FIELDS, function (item) {
  //         expect(analysisQuota[item]).toBeDefined();
  //       });
  //     });

  //     it('should not require quota when provider is not set or is different than heremaps', function () {
  //       expect(AnalysesQuotaFactory.requiresQuota('georeference-street-address', this.userModel)).toBeTruthy();
  //       this.userModel.set('geocoder_provider', null);
  //       expect(AnalysesQuotaFactory.requiresQuota('georeference-street-address', this.userModel)).toBeFalsy();
  //       this.userModel.set('geocoder_provider', 'mapzen');
  //       expect(AnalysesQuotaFactory.requiresQuota('georeference-street-address', this.userModel)).toBeFalsy();
  //       this.userModel.set('geocoder_provider', 'google');
  //       expect(AnalysesQuotaFactory.requiresQuota('georeference-street-address', this.userModel)).toBeFalsy();
  //     });

  //     it('should take organization provider attributes when user belongs to one', function () {
  //       this.userModel.isInsideOrg = function () { return true; };
  //       expect(AnalysesQuotaFactory.requiresQuota('georeference-street-address', this.userModel)).toBeTruthy();
  //       this.userModel.set('geocoder_provider', null);
  //       expect(AnalysesQuotaFactory.requiresQuota('georeference-street-address', this.userModel)).toBeTruthy();
  //     });
  //   });
  // });
});<commit_msg>fix tests<commit_after>var Backbone = require('backbone');
var CoreView = require('backbone/core-view');
var LayerDefinitionModel = require('../../../../../../../javascripts/cartodb3/data/layer-definition-model');
var LayerDefinitionsCollection = require('../../../../../../../javascripts/cartodb3/data/layer-definitions-collection');
var ConfigModel = require('../../../../../../../javascripts/cartodb3/data/config-model');
var UserModel = require('../../../../../../../javascripts/cartodb3/data/user-model');
var AnalysesService = require('../../../../../../../javascripts/cartodb3/editor/layers/layer-content-views/analyses/analyses-service');
var StackLayoutView = require('../../../../../../../javascripts/cartodb3/components/stack-layout/stack-layout-view');
var ModalsService = require('../../../../../../../javascripts/cartodb3/components/modals/modals-service-model');
var AddAnalysisView = require('../../../../../../../javascripts/cartodb3/components/modals/add-analysis/add-analysis-view');

describe('editor/layers/layer-content-view/analyses/analyses-service', function () {
  beforeEach(function () {
    this.configModel = new ConfigModel({
      base_url: '/u/pepe'
    });

    this.userModel = new UserModel({}, {
      configModel: this.configModel
    });

    this.layerDefinitionModel = new LayerDefinitionModel({
      id: 'l-1',
      options: {
        type: 'CartoDB',
        table_name: 'foo'
      }
    }, {
      parse: true,
      configModel: this.configModel
    });

    var onboardings = new Backbone.Model();
    onboardings.create = function () {};
    onboardings.destroy = function () {};

    this.layerDefinitionsCollection = new LayerDefinitionsCollection(null, {
      configModel: this.configModel,
      userModel: this.userModel,
      analysisDefinitionNodesCollection: new Backbone.Collection(),
      mapId: 'map-123',
      stateDefinitionModel: {}
    });

    this.layerDefinitionsCollection.add(this.layerDefinitionModel);

    var modals = new ModalsService();

    AnalysesService.init({
      onboardings: onboardings,
      layerDefinitionsCollection: this.layerDefinitionsCollection,
      modals: modals,
      userModel: this.userModel,
      configModel: this.configModel
    });

    var model1 = new Backbone.Model({
      createStackView: function (stackLayoutModel) {
        return new CoreView();
      }
    });
    var model2 = new Backbone.Model({
      createStackView: function (stackLayoutModel) {
        return new CoreView();
      }
    });
    var collection = new Backbone.Collection([model1, model2]);
    this._stackLayoutView = new StackLayoutView({
      collection: collection
    });
  });

  describe('.setLayerId', function () {
    it('should set LayerId', function () {
      AnalysesService.setLayerId(this.layerDefinitionModel.get('id'));
      expect(AnalysesService._layerId).toBe('l-1');
    });
  });

  describe('.setStackLayoutView', function () {
    it('should set StackLayoutView', function () {
      AnalysesService.setStackLayoutView(this._stackLayoutView);
      expect(AnalysesService._stackLayoutView).toBe(this._stackLayoutView);
    });
  });

  describe('.addAnalysis', function () {
    beforeEach(function () {
      var mockView = new CoreView();
      spyOn(AddAnalysisView.prototype, 'initialize');
      spyOn(AddAnalysisView.prototype, 'render').and.returnValue(mockView);

      spyOn(AnalysesService._modals, 'create').and.callThrough();
      spyOn(AnalysesService._modals, 'onDestroyOnce').and.callThrough();

      AnalysesService.setLayerId(this.layerDefinitionModel.get('id'));
      AnalysesService.setStackLayoutView(this._stackLayoutView);
      AnalysesService.addAnalysis();
    });

    afterEach(function () {
      AnalysesService._modals.destroy();
    });

    it('should open a modal add-analysis-view', function () {
      expect(AnalysesService._modals.create).toHaveBeenCalled();
    });

    describe('when modal is closed', function () {
      var modalView;

      beforeEach(function () {
        modalView = AnalysesService._modals._modalView;
        AnalysesService.setStackLayoutView(this._stackLayoutView);
        spyOn(AnalysesService._stackLayoutView.model, 'goToStep');

        this.analysisFormAttrs = {id: 'a1', type: 'buffer'};
        var destroyOnceArgs = AnalysesService._modals.onDestroyOnce.calls.argsFor(0);
        destroyOnceArgs[0].call(destroyOnceArgs[1], this.analysisFormAttrs);
      });

      it('should redirect stack to layer-content', function () {
        expect(AnalysesService._stackLayoutView.model.goToStep).toHaveBeenCalledWith(1, this.layerDefinitionModel, 'layer-content', this.analysisFormAttrs);
      });

      afterEach(function () {
        modalView.clean();
      })
    });
  });
});
{'fraction_non_alphanumeric': 0.16557305336832895, 'fraction_numerical': 0.002515310586176728, 'mean_word_length': 3.185354691075515, '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': '6277897', 'n_tokens_mistral': 2786, 'n_tokens_neox': 2617, 'n_words': 386}