describe('RegisterFormCtrl', function() {
  'use strict';
  beforeEach(module('registerForm', function($provide) {
    $provide.provider('page', {
      $get: function() {
        return offerUtils.getMockPages()[0];
      }
    });
  }));

  describe('with the controller initialized', function() {
    var vm, redirect, $httpBackend;
    beforeEach(inject(function($location, $rootScope, $controller, _$httpBackend_) {
      $httpBackend = _$httpBackend_;
      $location.path = jasmine.createSpy('$location.path');
      redirect = $location.path;

      var $scope = $rootScope.$new();
      $controller('RegisterFormCtrl as vm', {
        $scope: $scope,
      });
      vm = $scope.vm;
    }));

    describe('initialization', function() {
      it('should preload the restaurant data', function() {
        expect(vm.restaurant.id).toBeUndefined();
        expect(vm.restaurant.name).toEqual('La Dolce Vita');
        expect(vm.restaurant.address).toEqual('Kompanii 10, Tartu');
        expect(vm.restaurant.phone).toEqual('+372 567 8910');
        expect(vm.restaurant.webpage).toEqual('ladolcevita.ee');
      });
    });

    describe('submit', function() {
      var respond;
      beforeEach(function() {
        vm.restaurant = {
          name: 'test name',
        };
        vm.registerSpecifier({
          getLocation: jasmine.createSpy('getLocation').and.returnValue({
            lat: 'lat',
            lng: 'lng',
          }),
        });
        respond = $httpBackend.expectPOST('/api/v1/restaurants', {
          name: 'test name',
          location: {
            type: 'Point',
            coordinates: ['lng', 'lat'],
          },
        }).respond;
      });

      describe('with submit succeeding', function() {
        beforeEach(function() {
          respond(201);
        });

        it('should redirect to admin view', function() {
          vm.submit();
          expect(redirect).not.toHaveBeenCalled();
          $httpBackend.flush();
          expect(redirect).toHaveBeenCalledWith('/admin');
        });
      });

      describe('with submit failing', function() {
        beforeEach(function() {
          respond(500, 'an error message');
        });

        it('should display the error message', function() {
          vm.submit();
          $httpBackend.flush();
          expect(vm.errorMessage).toEqual('an error message');
        });
      });

      afterEach(function() {
        $httpBackend.verifyNoOutstandingExpectation();
        $httpBackend.verifyNoOutstandingRequest();
      });
    });

    describe('isReadyForError', function() {
      it('should return true when dirty, touched and invalid', function() {
        var input = {
          $dirty: true,
          $touched: true,
          $invalid: true,
        };
        expect(vm.isReadyForError(input)).toBe(true);
      });

      it('should return false when not dirty, but touched and invalid', function() {
        var input = {
          $dirty: false,
          $touched: true,
          $invalid: true,
        };
        expect(vm.isReadyForError(input)).toBe(false);
      });

      it('should return true when when first input is false, second true', function() {
        var input1 = {
          $dirty: false,
          $touched: true,
          $invalid: true,
        };
        var input2 = {
          $dirty: true,
          $touched: true,
          $invalid: true,
        };
        expect(vm.isReadyForError(input1, input2)).toBe(true);
      });
    });
  });
});

@@ -168,61 +168,10 @@
 () %7B
-%0A        return offerUtils.getMockPages()%5B0%5D;%0A      
 %7D
+,
 %0A   
@@ -226,16 +226,31 @@
 tialized
+ with page data
 ', funct
@@ -308,16 +308,217 @@
 oreEach(
+function() %7B%0A      module(function($provide) %7B%0A        $provide.provider('page', %7B%0A          $get: function() %7B%0A            return offerUtils.getMockPages()%5B0%5D;%0A          %7D%0A        %7D);%0A      %7D);%0A      
 inject(f
@@ -581,24 +581,26 @@
 d_) %7B%0A      
+  
 $httpBackend
@@ -618,16 +618,18 @@
 ckend_;%0A
+  
       $l
@@ -676,24 +676,26 @@
 ion.path');%0A
+  
       redire
@@ -718,24 +718,26 @@
 ath;%0A%0A      
+  
 var $scope =
@@ -752,24 +752,26 @@
 ope.$new();%0A
+  
       $contr
@@ -800,24 +800,26 @@
 l as vm', %7B%0A
+  
         $sco
@@ -832,28 +832,32 @@
 cope,%0A      
+  
 %7D);%0A
+  
       vm = $
@@ -870,18 +870,27 @@
 vm;%0A    
+ 
+ 
 %7D)
+;%0A    %7D
 );%0A%0A    
@@ -1305,32 +1305,499 @@
      %7D);%0A    %7D);
+%0A  %7D);%0A%0A  describe('with the controller initialized', function() %7B%0A    var vm, redirect, $httpBackend;%0A    beforeEach(inject(function($location, $rootScope, $controller, _$httpBackend_) %7B%0A      $httpBackend = _$httpBackend_;%0A      $location.path = jasmine.createSpy('$location.path');%0A      redirect = $location.path;%0A%0A      var $scope = $rootScope.$new();%0A      $controller('RegisterFormCtrl as vm', %7B%0A        $scope: $scope,%0A      %7D);%0A      vm = $scope.vm;%0A    %7D));
 %0A%0A    describe('

update form tests to cover prefilled data separately