import {
  describe,
  it,
  beforeEach,
  afterEach
} from 'mocha';
import { expect } from 'chai';
import startApp from '../helpers/start-app';
import destroyApp from '../helpers/destroy-app';

import people from '../pages/people';

describe('Acceptance: PeopleForm', function() {
  let application;

  beforeEach(function() {
    application = startApp();
  });

  afterEach(function() {
    destroyApp(application);
  });

  describe('visiting the people form page', function() {
    beforeEach(function() {
      people.visit();
    });

    it('contains the form', function() {
      expect(currentPath()).to.equal('index');
      expect(people.form.isVisible).to.be.true;
    });

    it('contains the list and has the correct length', function() {
      expect(people.list().isVisible).to.be.true;
      expect(people.list().count, 'There should be two people in the list').to.equal(2);
    });

    it('starts with a disabled submit button', function() {
      expect(people.form.submitButton.isVisible).to.be.true;
      expect(people.form.submitButton.isDisabled).to.be.true;
    });

    describe('filling out the form with insufficient data', function() {
      beforeEach(function() {
        people.form.firstName.fillInAndBlur('c');
        people.form.lastName.fillInAndBlur('f');
      });

      it('has a value in first name', function() {
        expect(people.form.firstName.value).to.equal('c');
      });

      it('still has a disabled submit button', function() {
        expect(people.form.submitButton.isDisabled).to.be.true;
      });

      it('has errors', function() {
        expect(people.form.firstName.hasErrors).to.be.true;
        expect(people.form.lastName.hasErrors).to.be.true;
      });

      describe('clicking cancel', function() {
        beforeEach(function() {
          people.form.cancelButton.click();
        });

        it('rolls back to an empty state', function() {
          expect(people.form.firstName.value).to.equal('');
          expect(people.form.lastName.value).to.equal('');
        });
      });
    });

    describe('filling out the form with correct data', function() {
      beforeEach(function() {
        people.form.firstName.fillInAndBlur('john');
        people.form.lastName.fillInAndBlur('smith');
        people.form.favoriteBand.selectAndBlur('Shellac');
      });

      it('has the correct value', function() {
        expect(people.form.firstName.value).to.equal('john');
      });

      it('enables the submit button', function() {
        expect(people.form.submitButton.isDisabled).to.be.false;
      });

      it('has no errors', function() {
        ['firstName', 'lastName', 'favoriteBand'].forEach(field => {
          expect(people.form[field].hasErrors).to.be.false;
        });
      });

      describe('submitting the form', function() {
        beforeEach(function() {
          people.form.submitButton.click();
        });

        it('adds a new person to the list', function() {
          expect(people.list().count).to.equal(3);
        });

        it('creates a new person with the correct information', function() {
          expect(people.list(2).name).to.equal('john smith');
          expect(people.list(2).favoriteBand).to.equal('Favorite Band: Shellac');
        });
      });
    });
  });
});

@@ -1171,32 +1171,435 @@
     
-beforeEach(function() %7B%0A
+describe('without a an explicit validation event (in this case, onblur)', function() %7B%0A        beforeEach(function() %7B%0A          people.form.firstName.fillIn('c');%0A        %7D);%0A%0A        it('does not switch the input to an errored state yet', function() %7B%0A          expect(people.form.firstName.hasErrors).to.be.false;%0A        %7D);%0A      %7D)%0A%0A      describe('when triggering a blur', function() %7B%0A        beforeEach(function() %7B%0A  
     
@@ -1640,24 +1640,26 @@
 dBlur('c');%0A
+  
         peop
@@ -1693,37 +1693,41 @@
 lur('f');%0A      
+ 
+ 
 %7D);%0A%0A
+  
       it('has a 
@@ -1761,32 +1761,34 @@
 ion() %7B%0A        
+ 
+ 
 expect(people.fo
@@ -1818,24 +1818,26 @@
 equal('c');%0A
+  
       %7D);%0A%0A 
@@ -1837,24 +1837,26 @@
  %7D);%0A%0A      
+  
 it('still ha
@@ -1889,32 +1889,34 @@
 ', function() %7B%0A
+  
         expect(p
@@ -1961,32 +1961,34 @@
 .be.true;%0A      
+  
 %7D);%0A%0A      it('h
@@ -1974,32 +1974,34 @@
      %7D);%0A%0A      
+ 
+ 
 it('has errors',
@@ -2006,32 +2006,34 @@
 ', function() %7B%0A
+  
         expect(p
@@ -2076,32 +2076,34 @@
 e.true;%0A        
+ 
+ 
 expect(people.fo
@@ -2133,24 +2133,26 @@
 to.be.true;%0A
+  
       %7D);%0A%0A 
@@ -2148,32 +2148,34 @@
      %7D);%0A%0A      
+  
 describe('clicki
@@ -2191,32 +2191,34 @@
 ', function() %7B%0A
+  
         beforeEa
@@ -2235,32 +2235,34 @@
 n() %7B%0A          
+  
 people.form.canc
@@ -2279,37 +2279,41 @@
 lick();%0A        
+ 
+ 
 %7D);%0A%0A
+  
         it('roll
@@ -2354,32 +2354,34 @@
 n() %7B%0A          
+  
 expect(people.fo
@@ -2406,32 +2406,34 @@
 ).to.equal('');%0A
+  
           expect
@@ -2467,32 +2467,46 @@
 ).to.equal('');%0A
+          %7D);%0A
         %7D);%0A    

Add testing for less eager validation