<commit_before>import { moduleForComponent, test } from 'ember-qunit';
import hbs from 'htmlbars-inline-precompile';
import Ember from 'ember';
// import EmberValidations from 'ember-validations';

moduleForComponent('bs-form', 'Integration | Component | bs-form', {
  integration: true
});

test('form has correct CSS class', function(assert) {
  this.render(hbs`{{#bs-form formLayout=formLayout}}Test{{/bs-form}}`);

  let classSpec = {
    vertical: 'form',
    horizontal: 'form-horizontal',
    inline: 'form-inline'
  };

  for (let layout in classSpec) {
    this.set('formLayout', layout);
    assert.equal(this.$('form').hasClass(classSpec[layout]), true, 'form has expected class.');
  }
});

test('Submitting the form calls default action', function(assert) {
  this.on('testAction', () => {
    assert.ok(true, 'Default action has been called.');
  });
  this.render(hbs`{{#bs-form action=(action "testAction")}}Test{{/bs-form}}`);

  assert.expect(1);
  this.$('form').submit();
});

test('Submitting the form calls before submit action', function(assert) {
  this.on('beforeAction', () => {
    assert.ok(true, 'Before action has been called.');
  });
  this.on('submitAction', () => {
    assert.ok(true, 'Default action has been called.');
  });
  this.render(hbs`{{#bs-form before=(action "beforeAction") action=(action "submitAction")}}Test{{/bs-form}}`);

  assert.expect(2);
  this.$('form').submit();
});

/*

 Cannot test validations as expected in unit tests.
 See https://github.com/dockyard/ember-validations/issues/247

 test('Submitting the form with successful validation calls default action', function(assert) {
 var testController = Ember.Controller.extend({
 actions: {
 success: function() {
 ok(true, 'Default action has been called.');
 },
 error: function() {
 ok(false, 'Invalid action must not be called.');
 }
 }
 }).create(),
 model = Ember.Object.extend(EmberValidations,{
 test: 'someValue',
 validations: {
 test: {
 presence: true
 }
 }
 }).create(),
 component = this.subject({
 targetObject: testController,
 action: 'success',
 invalid: 'error',
 model: model
 });

 expect(1);

 this.$().submit();
 });




 test('Submitting the form with invalid validation calls invalid action', function(assert) {
 var testController = Ember.Controller.extend({
 actions: {
 success: function() {
 ok(false, 'Default action must not be called.');
 },
 error: function() {
 ok(true, 'Invalid action has been called called.');
 }
 }
 }).create(),
 model = Ember.Object.extend(EmberValidations,{
 test: null,
 validations: {
 test: {
 presence: true
 }
 }
 }).create(),
 component = this.subject({
 targetObject: testController,
 action: 'success',
 invalid: 'error',
 model: model
 });

 expect(1);

 this.$().submit();
 });

 */

test('Pressing enter on a form with submitOnEnter submits the form', function(assert) {
  this.on('testAction', () => {
    assert.ok(true, 'Default action has been called.');
  });
  this.render(hbs`{{#bs-form action=(action "testAction") submitOnEnter=true}}Test{{/bs-form}}`);
  let e = Ember.$.Event('keypress');
  e.which = e.keyCode = 13;
  assert.expect(1);

  this.$('form').trigger(e);
});

test('supports novalidate attribute', function(assert) {
  assert.expect(2);
  this.render(hbs`{{bs-form}}`);
  assert.ok(
    this.$('form').attr('novalidate') === 'false',
    'defaults to false'
  );
  this.set('novalidate', true);
  this.render(hbs`{{bs-form novalidate=novalidate}}`);
  assert.ok(
    this.$('form').attr('novalidate') === 'true'
  );
});

// test('shows validation errors on submit', function(assert) {
//   assert.expect(3);
//   this.set('model',
//     Ember.Object.extend(EmberValidations, {
//       name: null,
//       validations: {<commit_msg>test(Form): fixed novalidate test broken for ember 2.7<commit_after>import { moduleForComponent, test } from 'ember-qunit';
import hbs from 'htmlbars-inline-precompile';
import Ember from 'ember';
// import EmberValidations from 'ember-validations';

moduleForComponent('bs-form', 'Integration | Component | bs-form', {
  integration: true
});

test('form has correct CSS class', function(assert) {
  this.render(hbs`{{#bs-form formLayout=formLayout}}Test{{/bs-form}}`);

  let classSpec = {
    vertical: 'form',
    horizontal: 'form-horizontal',
    inline: 'form-inline'
  };

  for (let layout in classSpec) {
    this.set('formLayout', layout);
    assert.equal(this.$('form').hasClass(classSpec[layout]), true, 'form has expected class.');
  }
});

test('Submitting the form calls default action', function(assert) {
  this.on('testAction', () => {
    assert.ok(true, 'Default action has been called.');
  });
  this.render(hbs`{{#bs-form action=(action "testAction")}}Test{{/bs-form}}`);

  assert.expect(1);
  this.$('form').submit();
});

test('Submitting the form calls before submit action', function(assert) {
  this.on('beforeAction', () => {
    assert.ok(true, 'Before action has been called.');
  });
  this.on('submitAction', () => {
    assert.ok(true, 'Default action has been called.');
  });
  this.render(hbs`{{#bs-form before=(action "beforeAction") action=(action "submitAction")}}Test{{/bs-form}}`);

  assert.expect(2);
  this.$('form').submit();
});

/*

 Cannot test validations as expected in unit tests.
 See https://github.com/dockyard/ember-validations/issues/247

 test('Submitting the form with successful validation calls default action', function(assert) {
 var testController = Ember.Controller.extend({
 actions: {
 success: function() {
 ok(true, 'Default action has been called.');
 },
 error: function() {
 ok(false, 'Invalid action must not be called.');
 }
 }
 }).create(),
 model = Ember.Object.extend(EmberValidations,{
 test: 'someValue',
 validations: {
 test: {
 presence: true
 }
 }
 }).create(),
 component = this.subject({
 targetObject: testController,
 action: 'success',
 invalid: 'error',
 model: model
 });

 expect(1);

 this.$().submit();
 });




 test('Submitting the form with invalid validation calls invalid action', function(assert) {
 var testController = Ember.Controller.extend({
 actions: {
 success: function() {
 ok(false, 'Default action must not be called.');
 },
 error: function() {
 ok(true, 'Invalid action has been called called.');
 }
 }
 }).create(),
 model = Ember.Object.extend(EmberValidations,{
 test: null,
 validations: {
 test: {
 presence: true
 }
 }
 }).create(),
 component = this.subject({
 targetObject: testController,
 action: 'success',
 invalid: 'error',
 model: model
 });

 expect(1);

 this.$().submit();
 });

 */

test('Pressing enter on a form with submitOnEnter submits the form', function(assert) {
  this.on('testAction', () => {
    assert.ok(true, 'Default action has been called.');
  });
  this.render(hbs`{{#bs-form action=(action "testAction") submitOnEnter=true}}Test{{/bs-form}}`);
  let e = Ember.$.Event('keypress');
  e.which = e.keyCode = 13;
  assert.expect(1);

  this.$('form').trigger(e);
});

test('supports novalidate attribute', function(assert) {
  assert.expect(2);
  this.render(hbs`{{bs-form}}`);
  assert.ok(
    this.$('form').attr('novalidate') === undefined,
    'defaults to false'
  );
  this.render(hbs`{{bs-form novalidate=true}}`);
  assert.ok(
    this.$('form').attr('novalidate') !== undefined
  );
});

// test('shows validation errors on submit', function(assert) {
//   assert.expect(3);
//   this.set('model',
//     Ember.Object.extend(EmberValidations, {
//       name: null,
//       validations: {
{'fraction_non_alphanumeric': 0.18613041154462853, 'fraction_numerical': 0.00347407803313736, 'mean_word_length': 4.338801711840229, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 2, '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': '2357794', 'n_tokens_mistral': 2387, 'n_tokens_neox': 2351, 'n_words': 563}