describe('OfferList cotrollers', function() {
  'use strict';
  beforeEach(module('restaurantAdminViewControllers'));

  describe('RestaurantOfferListCtrl', function() {
    var $scope;

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

    beforeEach(inject(function($rootScope, $controller, $httpBackend) {
      $httpBackend.expectGET('api/v1/restaurants/me').respond(offerUtils.getMockRestaurant());

      $scope = $rootScope.$new();
      $controller('RestaurantAdminViewCtrl', {
        $scope: $scope
      });
    }));

    it('should have restaurant data after we mock-respond to the HTTP request', inject(function($httpBackend) {
      expect($scope.restaurant.name).toBeUndefined();
      $httpBackend.flush();
      expect($scope.restaurant.name).toBe('Bulgarian Chef');
    }));
  });

  describe('RestaurantOfferListCtrl', function() {
    var $scope;
    var restaurantId = 'someId';

    beforeEach(inject(function($rootScope, $controller, $httpBackend) {
      $httpBackend.expectGET('api/v1/restaurants/me/offers').respond(offerUtils.getMockOffers());

      $scope = $rootScope.$new();
      $rootScope.restaurant = {
        _id: restaurantId
      };
      $controller('RestaurantOfferListCtrl', {
        $scope: $scope
      });
    }));

    it('should have model with 3 offers after we mock-respond to the HTTP request', inject(function($httpBackend) {
      expect($scope.offers.length).toBe(0);
      $httpBackend.flush();
      expect($scope.offers.length).toBe(3);
    }));

    describe('with the original offers fetched from the backend', function() {
      beforeEach(inject(function($httpBackend) {
        $httpBackend.flush();
      }));

      it('should prepend a broadcasted offer into the list of offers', function() {
        var mockOffer = 'mock offer';
        var nrOfOffers = $scope.offers.length;

        $scope.$broadcast('offer-posted', mockOffer);
        expect($scope.offers.length).toEqual(nrOfOffers + 1);
        expect($scope.offers[0]).toEqual(mockOffer);
      });
    });
  });

  describe('RestaurantAddOfferCtrl', function(){
    describe('previewImage directive', function() {
      var element, scope, parentScope;
      beforeEach(function() {
        var compiled = utils.compile('<a preview-image />');
        element = compiled.element;
        scope = compiled.parentScope;

        scope.setAsPreview = jasmine.createSpy();
      });

      it('should call setAsPreview method on scope with file data from change event', function(){
        var file = 'a mock file object';
        element.prop('files', [file]);

        element.triggerHandler('change');

        expect(scope.setAsPreview).toHaveBeenCalledWith(file);
      });
    });

    describe('the controller', function(){
      var $scope, mockFileReader;

      beforeEach(inject(function(_$rootScope_, $controller){
        $scope = _$rootScope_.$new();
        mockFileReader = {};
        $controller('RestaurantAddOfferCtrl', {
          $scope: $scope,
          fileReader: mockFileReader
        });
      }));

      describe('setAsPreview', function(){
        var result, file;
        beforeEach(inject(function($q){
          var deferred = $q.defer();
          result = 'result data';
          deferred.resolve(result);
          mockFileReader.readAsDataUrl = jasmine.createSpy().and.returnValue(deferred.promise);
        }));

        describe('with file', function(){
          beforeEach(function(){
            file = 'a mock file';
          });

          it('should set previewImageSrc to the result', function(){
            $scope.setAsPreview(file);

            $scope.$apply();
            expect($scope.previewImageSrc).toBe(result);
          });
        });

        describe('without file', function(){
          beforeEach(function(){
            file = undefined;
          });

          it('should set previewImageSrc to the result', function(){
            $scope.setAsPreview(file);

            $scope.$apply();
            expect($scope.previewImageSrc).toBe('');
          });
        });
      });

      describe('postOffer', function() {
        var postedOffer;

        beforeEach(inject(function($httpBackend) {
          spyOn($parentScope, '$broadcast');
          postedOffer = {
            id: 'mocked response from the backend'
          };
          $httpBackend.whenPOST('api/v1/offers').respond(postedOffer);
        }));

        describe('with the form filled', function() {
          beforeEach(function() {
            $scope.title = 'a title';
            $scope.tags = ['tag1', 'tag2'];
            $scope.price = 2.5;
            $scope.date = new Date(2015, 3, 15);
            $scope.fromTime = new Date(1970, 0, 1, 10, 0, 0);
            $scope.toTime = new Date(1970, 0, 1, 15, 0, 0);
            $scope.image = 'image data';
          });

          it('should broadcast the data returned from the POST', inject(function($httpBackend) {
            $scope.postOffer();

            expect($parentScope.$broadcast).toHaveBeenCalled();
            var args = $parentScope.$broadcast.calls.mostRecent().args;
            var channelName = args[0];
            var data = args[1];
            expect(channelName).toEqual('offer-posted');

            expect(data.id).toBeUndefined();
            $httpBackend.flush();
            expect(data.id).toEqual(postedOffer.id);
          }));

          it('should post the combined offer', inject(function($httpBackend) {
            $httpBackend.expectPOST('api/v1/offers', {
              title: 'a title',
              tags: ['tag1', 'tag2'],
              price: 2.5,
              from_time: new Date(2015, 3, 15, 10, 0, 0),
              to_time: new Date(2015, 3, 15, 15, 0, 0),
              image: 'image data',
            });

            $scope.postOffer();
            $httpBackend.flush();
          }));
        });
      });
    });
  });
});

@@ -2183,24 +2183,25 @@
 , function()
+ 
 %7B%0A    descri
@@ -2596,32 +2596,33 @@
 ent', function()
+ 
 %7B%0A        var fi
@@ -2846,32 +2846,33 @@
 ler', function()
+ 
 %7B%0A      var $sco
@@ -2874,16 +2874,30 @@
  $scope,
+ $parentScope,
  mockFil
@@ -2939,17 +2939,16 @@
 unction(
-_
 $rootSco
@@ -2949,17 +2949,16 @@
 ootScope
-_
 , $contr
@@ -2963,16 +2963,17 @@
 troller)
+ 
 %7B%0A      
@@ -2979,17 +2979,22 @@
    $
-s
+parentS
 cope = 
-_
 $roo
@@ -2999,17 +2999,47 @@
 ootScope
-_
+;%0A        $scope = $parentScope
 .$new();
@@ -3236,32 +3236,33 @@
 iew', function()
+ 
 %7B%0A        var re
@@ -3311,16 +3311,17 @@
 tion($q)
+ 
 %7B%0A      
@@ -3563,32 +3563,33 @@
 ile', function()
+ 
 %7B%0A          befo
@@ -3597,32 +3597,33 @@
 eEach(function()
+ 
 %7B%0A            fi
@@ -3716,32 +3716,33 @@
 ult', function()
+ 
 %7B%0A            $s
@@ -3915,32 +3915,33 @@
 ile', function()
+ 
 %7B%0A          befo
@@ -3949,32 +3949,33 @@
 eEach(function()
+ 
 %7B%0A            fi
@@ -4072,16 +4072,17 @@
 nction()
+ 
 %7B%0A      

fix formatting in a test