define('dummy/tests/acceptance/form-validation-test', ['ember', 'dummy/tests/helpers/start-app'], function (Ember, startApp) {

  'use strict';

  var application;

  var SAVE_BUTTON = "button.save";
  var NAME_ERROR_FIELD = ".name-parent-div span";
  var ZIPCODE_ERROR_FIELD = ".zipcode-parent-div span";
  var EMAIL_ERROR_FIELD = ".email-parent-div span";

  var NAME_INPUT = ".name-parent-div input";
  var ZIPCODE_INPUT = ".zipcode-parent-div input";
  var EMAIL_INPUT = ".email-parent-div input";

  var VALID_NAME = "x";
  var VALID_ZIPCODE = "52601";
  var VALID_EMAIL = "hi@hi.com";

  module("Acceptance: Form Validation", {
    setup: function () {
      application = startApp['default']();
    },
    teardown: function () {
      Ember['default'].run(application, "destroy");
    }
  });

  test("clicking save will transition to success when each field is valid", function () {
    visit("/");
    fillIn(NAME_INPUT, VALID_NAME);
    fillIn(ZIPCODE_INPUT, VALID_ZIPCODE);
    fillIn(EMAIL_INPUT, VALID_EMAIL);
    click(SAVE_BUTTON);
    andThen(function () {
      equal(currentURL(), "/success");
    });
  });

  test("clicking save will not transition to success when only the last field is valid", function () {
    visit("/");
    fillIn(EMAIL_INPUT, VALID_EMAIL);
    click(SAVE_BUTTON);
    andThen(function () {
      equal(currentURL(), "/");
    });
    fillIn(NAME_INPUT, VALID_NAME);
    fillIn(ZIPCODE_INPUT, VALID_ZIPCODE);
    click(SAVE_BUTTON);
    andThen(function () {
      equal(currentURL(), "/success");
    });
  });

  test("inputs with default validation will not accept empty string as valid", function () {
    visit("/");
    fillIn(NAME_INPUT, " ");
    fillIn(ZIPCODE_INPUT, VALID_ZIPCODE);
    fillIn(EMAIL_INPUT, VALID_EMAIL);
    click(SAVE_BUTTON);
    andThen(function () {
      equal(currentURL(), "/");
      equal(find(NAME_ERROR_FIELD).hasClass("hidden"), false);
      equal(find(ZIPCODE_ERROR_FIELD).hasClass("hidden"), true);
      equal(find(EMAIL_ERROR_FIELD).hasClass("hidden"), true);
    });
  });

  test("clicking save will fire each validation", function () {
    visit("/");
    andThen(function () {
      equal(currentURL(), "/");
      equal(find(NAME_ERROR_FIELD).hasClass("hidden"), true);
      equal(find(ZIPCODE_ERROR_FIELD).hasClass("hidden"), true);
      equal(find(EMAIL_ERROR_FIELD).hasClass("hidden"), true);
    });
    click(SAVE_BUTTON);
    andThen(function () {
      equal(currentURL(), "/");
      equal(find(NAME_ERROR_FIELD).hasClass("hidden"), false);
      equal(find(ZIPCODE_ERROR_FIELD).hasClass("hidden"), false);
      equal(find(EMAIL_ERROR_FIELD).hasClass("hidden"), false);
    });
  });

  test("component will render with the correct text and css class", function () {
    visit("/");
    click(SAVE_BUTTON);
    andThen(function () {
      equal(currentURL(), "/");
      equal(find(NAME_ERROR_FIELD).hasClass("red"), false);
      //equal(find(ZIPCODE_ERROR_FIELD).hasClass('hidden'), false);
      equal(find(EMAIL_ERROR_FIELD).hasClass("blue"), false);
      equal(find(NAME_ERROR_FIELD).text(), "invalid name");
      equal(find(ZIPCODE_ERROR_FIELD).text(), "invalid zipcode");
      equal(find(EMAIL_ERROR_FIELD).text(), "invalid email");
    });
  });

  test("when inputs are showing errors they turn hidden when field is correctly entered", function () {
    visit("/");
    andThen(function () {
      equal(find(NAME_ERROR_FIELD).hasClass("hidden"), true);
      equal(find(ZIPCODE_ERROR_FIELD).hasClass("hidden"), true);
      equal(find(EMAIL_ERROR_FIELD).hasClass("hidden"), true);
    });
    click(SAVE_BUTTON);
    fillIn(NAME_INPUT, VALID_NAME);
    andThen(function () {
      equal(find(NAME_ERROR_FIELD).hasClass("hidden"), true);
      equal(find(ZIPCODE_ERROR_FIELD).hasClass("hidden"), false);
      equal(find(EMAIL_ERROR_FIELD).hasClass("hidden"), false);
    });
    fillIn(ZIPCODE_INPUT, VALID_ZIPCODE);
    andThen(function () {
      equal(find(NAME_ERROR_FIELD).hasClass("hidden"), true);
      equal(find(ZIPCODE_ERROR_FIELD).hasClass("hidden"), true);
      equal(find(EMAIL_ERROR_FIELD).hasClass("hidden"), false);
    });
    fillIn(EMAIL_INPUT, VALID_EMAIL);
    andThen(function () {
      equal(find(NAME_ERROR_FIELD).hasClass("hidden"), true);
      equal(find(ZIPCODE_ERROR_FIELD).hasClass("hidden"), true);
      equal(find(EMAIL_ERROR_FIELD).hasClass("hidden"), true);
    });
  });

  test("inputs with default validation start working without submit firing", function () {
    visit("/");
    andThen(function () {
      equal(find(NAME_ERROR_FIELD).hasClass("hidden"), true);
    });
    fillIn(NAME_INPUT, VALID_NAME);
    andThen(function () {
      equal(find(NAME_ERROR_FIELD).hasClass("hidden"), true);
    });
    fillIn(NAME_INPUT, "");
    andThen(function () {
      equal(find(NAME_ERROR_FIELD).hasClass("hidden"), false);
    });
    fillIn(NAME_INPUT, VALID_NAME);
    andThen(function () {
      equal(find(NAME_ERROR_FIELD).hasClass("hidden"), true);
    });
  });

  test("inputs with regex validation start working without submit firing", function () {
    visit("/");
    andThen(function () {
      equal(find(ZIPCODE_ERROR_FIELD).hasClass("hidden"), true);
    });
    fillIn(ZIPCODE_INPUT, VALID_ZIPCODE);
    andThen(function () {
      equal(find(ZIPCODE_ERROR_FIELD).hasClass("hidden"), true);
    });
    fillIn(ZIPCODE_INPUT, "");
    andThen(function () {
      equal(find(ZIPCODE_ERROR_FIELD).hasClass("hidden"), false);
    });
    fillIn(ZIPCODE_INPUT, VALID_ZIPCODE);
    andThen(function () {
      equal(find(ZIPCODE_ERROR_FIELD).hasClass("hidden"), true);
    });
  });

  test("inputs with regex validation will not work without submit when delay option set", function () {
    visit("/");
    andThen(function () {
      equal(find(EMAIL_ERROR_FIELD).hasClass("hidden"), true);
    });
    fillIn(EMAIL_INPUT, VALID_EMAIL);
    andThen(function () {
      equal(find(EMAIL_ERROR_FIELD).hasClass("hidden"), true);
    });
    fillIn(EMAIL_INPUT, "");
    andThen(function () {
      equal(find(EMAIL_ERROR_FIELD).hasClass("hidden"), true);
    });
    click(SAVE_BUTTON);
    andThen(function () {
      equal(find(EMAIL_ERROR_FIELD).hasClass("hidden"), false);
    });
    fillIn(EMAIL_INPUT, VALID_EMAIL);
    andThen(function () {
      equal(find(EMAIL_ERROR_FIELD).hasClass("hidden"), true);
    });
    fillIn(EMAIL_INPUT, "hi");
    andThen(function () {
      equal(find(EMAIL_ERROR_FIELD).hasClass("hidden"), false);
    });
    fillIn(EMAIL_INPUT, VALID_EMAIL);
    andThen(function () {
      equal(find(EMAIL_ERROR_FIELD).hasClass("hidden"), true);
    });
  });

  test("inputs with default validation will not trip validation when they are tabbed into", function () {
    visit("/");
    andThen(function () {
      equal(find(ZIPCODE_ERROR_FIELD).hasClass("hidden"), true);
    });
    triggerEvent(ZIPCODE_INPUT, "dblclick");
    triggerEvent(ZIPCODE_INPUT, "blur");
    andThen(function () {
      equal(find(ZIPCODE_ERROR_FIELD).hasClass("hidden"), true);
    });
    fillIn(ZIPCODE_INPUT, "5003");
    triggerEvent(ZIPCODE_INPUT, "blur");
    andThen(function () {
      equal(find(ZIPCODE_ERROR_FIELD).hasClass("hidden"), false);
    });
    fillIn(ZIPCODE_INPUT, "12345");
    triggerEvent(ZIPCODE_INPUT, "blur");
    andThen(function () {
      equal(find(ZIPCODE_ERROR_FIELD).hasClass("hidden"), true);
    });
  });

});