'use strict';

/* jasmine specs for controllers go here */

describe('controllers', function () {
  beforeEach(module('myApp.controllers'));


  describe('PageDetailsCtrl', function () {
    var $scope, createController, $q;
    var mockPageManager, mockAuthentication;
    var pageBeingViewed, currentUser;
    var ratingForSession;


    mockAuthentication = {
      getCurrentUser: function () {
        return currentUser;
      },
      registerUserChangeCallback: function (callback) {
      }
    };

    mockPageManager = {
      getPageBeingViewed: function () {
        var deferred = $q.defer();
        deferred.resolve(pageBeingViewed);
        return deferred.promise;
      },
      registerPageBeingViewedObserverCallback: function (callback) {
      },
      getSessionRatingForPage: function (page, sessionToken) {
        var deferred = $q.defer();
        deferred.resolve({data: {rating: ratingForSession}, status: 200});
        return deferred.promise;
      },
      ratePage: function (page, rating, sessionToken) {
        var deferred = $q.defer();
        page.rating = rating;
        deferred.resolve({data: {page: page}, status: 200});
        return deferred.promise;
      },
      setPageBeingEdited: function (page) {

      }
    };

    beforeEach
    (inject(function ($rootScope, $controller, _$q_) {
      $scope = $rootScope.$new();
      $q = _$q_;

      createController = function () {
        return $controller('PageDetailsCtrl', {
          $scope: $scope,
          PageManager: mockPageManager,
          Authentication: mockAuthentication
        });
      };
      createController();
    }));

    it('should watch for changes to the page being viewed', function () {
      spyOn(mockPageManager, 'registerPageBeingViewedObserverCallback');
      createController();
      expect(mockPageManager.registerPageBeingViewedObserverCallback).toHaveBeenCalled();
    });

    it('should watch for changes to the user logged in', function () {
      spyOn(mockAuthentication, 'registerUserChangeCallback');
      createController();
      expect(mockAuthentication.registerUserChangeCallback).toHaveBeenCalled();
    });

    it('should NOT get the current user\'s page rating when the page changes if no user is logged in', function () {
      spyOn(mockPageManager, 'getSessionRatingForPage').andCallThrough();
      currentUser = null;
      $scope.$apply();
      expect(mockPageManager.getSessionRatingForPage).not.toHaveBeenCalled();
    });

    it('should get the current user\'s page rating when the page changes', function () {
      spyOn(mockPageManager, 'getSessionRatingForPage').andCallThrough();
      currentUser = {session: ''};
      ratingForSession = 3;
      $scope.page = {};
      $scope.$apply();
      expect(mockPageManager.getSessionRatingForPage).toHaveBeenCalled();
    });

    it('should attempt to update the user rating when the page changes', function () {
      spyOn($scope, 'getCurrentUserRatingForPage');
      $scope.page = {};
      $scope.$apply();
      expect($scope.getCurrentUserRatingForPage).toHaveBeenCalled();
    });

    it('should attempt update the user rating notified that the logged in user has changed', function () {
      spyOn($scope, 'getCurrentUserRatingForPage');
      $scope.user = {};
      $scope.$apply();
      expect($scope.getCurrentUserRatingForPage).toHaveBeenCalled();
    });

    it('should try to rate the page when the rating changes to a different value than the previous rating', function () {
      $scope.previousRating = 1;
      spyOn(mockPageManager, 'ratePage').andCallThrough();
      $scope.rating = 2;
      $scope.$apply();
      expect(mockPageManager.ratePage).toHaveBeenCalled();
    });

    it('should NOT rate the page if the current rating is the same as the previous', function () {
      $scope.previousRating = 1;
      spyOn(mockPageManager, 'ratePage').andCallThrough();
      $scope.rating = 1;
      $scope.$apply();
      expect(mockPageManager.ratePage).not.toHaveBeenCalled();
    });

    it('should set the page being edited to the current page when user tries to edit a page', function(){
      spyOn(mockPageManager, 'setPageBeingEdited');
      $scope.page = 'someval';
      $scope.editPage();
      expect(mockPageManager.setPageBeingEdited).toHaveBeenCalledWith($scope.page);
    });

  })
});

@@ -329,16 +329,47 @@
 sion;%0A%0A%0A
+    beforeEach(function () %7B%0A  
     mock
@@ -393,16 +393,18 @@
 %7B%0A      
+  
 getCurre
@@ -429,24 +429,26 @@
 ) %7B%0A        
+  
 return curre
@@ -451,24 +451,26 @@
 urrentUser;%0A
+  
       %7D,%0A   
@@ -464,32 +464,34 @@
        %7D,%0A      
+  
 registerUserChan
@@ -526,34 +526,40 @@
 ck) %7B%0A      
-%7D%0A
+  %7D%0A  
     %7D;%0A
-%0A
+   
     mockPage
@@ -566,24 +566,26 @@
 Manager = %7B%0A
+  
       getPag
@@ -612,32 +612,34 @@
 on () %7B%0A        
+  
 var deferred = $
@@ -641,32 +641,34 @@
 d = $q.defer();%0A
+  
         deferred
@@ -694,16 +694,18 @@
 iewed);%0A
+  
         
@@ -731,27 +731,31 @@
 mise;%0A      
+  
 %7D,%0A
+  
       regist
@@ -807,24 +807,26 @@
 callback) %7B%0A
+  
       %7D,%0A   
@@ -828,16 +828,18 @@
 ,%0A      
+  
 getSessi
@@ -887,32 +887,34 @@
 oken) %7B%0A        
+  
 var deferred = $
@@ -916,32 +916,34 @@
 d = $q.defer();%0A
+  
         deferred
@@ -1001,32 +1001,34 @@
  200%7D);%0A        
+  
 return deferred.
@@ -1038,27 +1038,31 @@
 mise;%0A      
+  
 %7D,%0A
+  
       ratePa
@@ -1105,32 +1105,34 @@
 oken) %7B%0A        
+  
 var deferred = $
@@ -1150,16 +1150,18 @@
         
+  
 page.rat
@@ -1174,16 +1174,18 @@
 rating;%0A
+  
         
@@ -1237,32 +1237,34 @@
  200%7D);%0A        
+  
 return deferred.
@@ -1274,27 +1274,31 @@
 mise;%0A      
+  
 %7D,%0A
+  
       setPag
@@ -1340,15 +1340,27 @@
     
+  
 %7D%0A    
+ 
-%7D
+ %7D;%0A    %7D)
 ;%0A%0A 
@@ -4230,18 +4230,20 @@
 function
+ 
 ()
+ 
 %7B%0A      

put beforeEach in the mock setups so that the mocks get reset before each test