<commit_before>var $ = require('jquery');
var Backbone = require('backbone');
require('backbone-forms');
Backbone.$ = $;
require('../../../../../../javascripts/cartodb3/components/form-components/editors/base');
var FactoryModals = require('../../../factories/modals');

function dispatchDocumentEvent (type, opts) {
  var e = document.createEvent('HTMLEvents');
  e.initEvent(type, false, true);
  if (opts.which) {
    e.which = opts.which;
  }
  document.dispatchEvent(e, opts);
}

describe('components/form-components/editors/base', function () {
  beforeEach(function () {
    this.view = new Backbone.Form.editors.Base();
    this.view.options = {
      validators: ['required']
    };

    document.body.appendChild(this.view.el);
  });

  afterEach(function () {
    document.body.removeChild(this.view.el);
  });

  it('should take default validation', function () {
    this.view._setOptions({
      schema: {
        min: 0,
        max: 10,
        step: 1,
        showSlider: true
      }
    });
    expect(this.view.options.validators.length).toBe(1);
    expect(this.view.options.validators[0]).toBe('required');
  });

  it('should take opts validation plus the default one', function () {
    this.view._setOptions({
      schema: {
        validators: [{
          type: 'regexp',
          regexp: /^[0-9]*\.?[0-9]*$/,
          message: 'Must be valid'
        }]
      }
    });
    expect(this.view.options.validators.length).toBe(2);
    expect(this.view.options.validators[0].type).toBe('regexp');
    expect(this.view.options.validators[1]).toBe('required');
  });

  it('should add a custom class if it has been passed as parameter', function () {
    var view = new Backbone.Form.editors.Base({
      value: 'Hello!',
      className: 'track-greeting',
      schema: {}
    });

    expect(view.$el.hasClass('track-greeting')).toBeTruthy();
  });

  describe('document click and escape binding', function () {
    beforeEach(function () {
      this.modals = FactoryModals.createModalService();
    });

    it('without modals set', function () {
      var cb = jasmine.createSpy('cb');
      this.view.applyClickOutsideBind(cb);

      dispatchDocumentEvent('click', { target: 'body' });
      expect(cb).toHaveBeenCalled();
    });

    it('with some modal open', function () {
      var clickCB = jasmine.createSpy('clickCB');
      var escCB = jasmine.createSpy('escCB');

      this.view._setOptions({
        modals: this.modals
      });

      this.view.applyClickOutsideBind(clickCB);
      this.view.applyESCBind(escCB);

      this.modals.set('open', true);
      dispatchDocumentEvent('click', { target: 'body' });
      expect(clickCB).not.toHaveBeenCalled();

      dispatchDocumentEvent('keydown', { which: 27 });
      expect(escCB).not.toHaveBeenCalled();
    });

    it('without any modal open', function () {
      var clickCB = jasmine.createSpy('clickCB');
      var escCB = jasmine.createSpy('escCB');

      this.view._setOptions({
        modals: this.modals
      });

      this.view.applyClickOutsideBind(clickCB);
      this.view.applyESCBind(escCB);

      this.modals.set('open', false);
      dispatchDocumentEvent('click', { target: 'body' });
      expect(clickCB).toHaveBeenCalled();

      dispatchDocumentEvent('keydown', { which: 27 });
      expect(escCB).toHaveBeenCalled();
    });
  });
});
<commit_msg>Fixed base specs<commit_after>var Backbone = require('backbone');
var FactoryModals = require('../../../factories/modals');
var EditorHelpers = require('../../../../../../javascripts/cartodb3/components/form-components/editors/editor-helpers-extend');

function dispatchDocumentEvent (type, opts) {
  var e = document.createEvent('HTMLEvents');
  e.initEvent(type, false, true);
  if (opts.which) {
    e.which = opts.which;
  }
  document.dispatchEvent(e, opts);
}

describe('components/form-components/editors/base', function () {
  var view;

  beforeEach(function () {
    view = new Backbone.Form.editors.Base();
    view.options = {
      validators: ['required']
    };

    document.body.appendChild(view.el);
  });

  afterEach(function () {
    document.body.removeChild(view.el);
  });

  it('should take default validation', function () {
    EditorHelpers.setOptions(view, {
      schema: {
        min: 0,
        max: 10,
        step: 1,
        showSlider: true
      }
    });
    expect(view.options.validators.length).toBe(1);
    expect(view.options.validators[0]).toBe('required');
  });

  it('should take opts validation plus the default one', function () {
    EditorHelpers.setOptions(view, {
      schema: {
        validators: [{
          type: 'regexp',
          regexp: /^[0-9]*\.?[0-9]*$/,
          message: 'Must be valid'
        }]
      }
    });
    expect(view.options.validators.length).toBe(2);
    expect(view.options.validators[0].type).toBe('regexp');
    expect(view.options.validators[1]).toBe('required');
  });

  describe('document click and escape binding', function () {
    beforeEach(function () {
      this.modals = FactoryModals.createModalService();
    });

    it('without modals set', function () {
      var cb = jasmine.createSpy('cb');
      view.applyClickOutsideBind(cb);

      dispatchDocumentEvent('click', { target: 'body' });
      expect(cb).toHaveBeenCalled();
    });

    it('with some modal open', function () {
      var clickCB = jasmine.createSpy('clickCB');
      var escCB = jasmine.createSpy('escCB');

      EditorHelpers.setOptions(view, {
        modals: this.modals
      });

      view.applyClickOutsideBind(clickCB);
      view.applyESCBind(escCB);

      this.modals.set('open', true);
      dispatchDocumentEvent('click', { target: 'body' });
      expect(clickCB).not.toHaveBeenCalled();

      dispatchDocumentEvent('keydown', { which: 27 });
      expect(escCB).not.toHaveBeenCalled();
    });

    it('without any modal open', function () {
      var clickCB = jasmine.createSpy('clickCB');
      var escCB = jasmine.createSpy('escCB');

      EditorHelpers.setOptions(view, {
        modals: this.modals
      });

      view.applyClickOutsideBind(clickCB);
      view.applyESCBind(escCB);

      this.modals.set('open', false);
      dispatchDocumentEvent('click', { target: 'body' });
      expect(clickCB).toHaveBeenCalled();

      dispatchDocumentEvent('keydown', { which: 27 });
      expect(escCB).toHaveBeenCalled();
    });
  });
});

{'fraction_non_alphanumeric': 0.16840792140963667, 'fraction_numerical': 0.005613597380321222, 'mean_word_length': 2.9641532756489495, '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': '537335', 'n_tokens_mistral': 2027, 'n_tokens_neox': 1967, 'n_words': 329}