'use strict';

var _LoginPage = require('./pageObjects/LoginPage');

var _LoginPage2 = _interopRequireDefault(_LoginPage);

var _ProtectedPage = require('./pageObjects/ProtectedPage');

var _ProtectedPage2 = _interopRequireDefault(_ProtectedPage);

var _SearchPage = require('./pageObjects/SearchPage');

var _SearchPage2 = _interopRequireDefault(_SearchPage);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

describe('login', function suite() {
  describe('from the login page', () => {
    const loginPage = new _LoginPage2.default();
    const protectedPage = new _ProtectedPage2.default();
    const searchPage = new _SearchPage2.default();

    beforeEach(() => {
      if (!loginPage.isVisible()) {
        loginPage.open();
      }
    });

    afterEach(() => {
      if (protectedPage.isVisible()) {
        protectedPage.logout();

        loginPage.becomesVisible();
      }
    });

    context('when valid credentials are entered', function context() {
      beforeEach(() => {
        loginPage.setUsername(testParams.adminUser.username).setPassword(testParams.adminUser.password);
      });

      it('should open the search page when the submit button is clicked', () => {
        loginPage.clickSubmitButton();

        searchPage.becomesVisible().should.equal(true);
        searchPage.isLoggedInAs(testParams.adminUser.screenName).should.equal(true);
      });

      it('should open the search page when enter is pressed on the username input', () => {
        loginPage.enterUsernameInput();

        searchPage.becomesVisible().should.equal(true);
        searchPage.isLoggedInAs(testParams.adminUser.screenName).should.equal(true);
      });

      it('should open the search page when enter is pressed on the password input', () => {
        loginPage.enterPasswordInput();

        searchPage.becomesVisible().should.equal(true);
        searchPage.isLoggedInAs(testParams.adminUser.screenName).should.equal(true);
      });
    });

    context('when invalid credentials are entered', () => {
      beforeEach(() => {
        loginPage.setUsername(testParams.adminUser.username).setPassword(`not ${testParams.adminUser.password}`);
      });

      it('should stay on the login page and show an error message when the credentials are submitted', () => {
        loginPage.submit();

        searchPage.becomesVisible().should.equal(false);

        loginPage.getNotificationText().should.contain('Incorrect username/password');
      });
    });
  });
});