'use strict';

describe('SessionCtrl', function() {

  // load the controller's module
  // We need to override the async translateProvider
  // http://angular-translate.github.io/docs/#/guide/22_unit-testing-with-angular-translate
  beforeEach(module('Teem', function ($provide, $translateProvider) {
    $provide.factory('customLoader', function ($q) {
      return function () {
        var deferred = $q.defer();
        deferred.resolve({});
        return deferred.promise;
      };
    });
    $translateProvider.useLoader('customLoader');
  }));

  var SessionCtrl,
      SessionSvc,
      $controller,
      $route,
      $rootScope,
      scope,
      $timeout,
      swellRT,
      nick = 'testingnick',
      password = 'testingpassword',
      email = 'testing@local.net';
 
 
  // Initialize the controller and a mock scope
  beforeEach(inject(function (_$controller_, _$route_, _$rootScope_, _$timeout_, _swellRT_, _SessionSvc_) {
    $controller = _$controller_;
    $route = _$route_;
    $rootScope = _$rootScope_;
    $timeout = _$timeout_;
    swellRT = _swellRT_;
    SessionSvc = _SessionSvc_;
  }));

  describe('when loggin in', function() {
    var calledNick, calledPassword;

    beforeEach(function() {
      $route.current = {
        params: {
          form: 'login'
        }
      };

      spyOn(SwellRT, 'startSession').
        and.callFake(function(domain, nick, password, success) {
          calledNick = nick;
          calledPassword = password;

          __session.address = nick + '@' + __session.domain;

          success();
      });
    });

    it('should call SwellRT', function() {
      scope = $rootScope.$new();

      SessionCtrl = $controller('SessionCtrl', {
        $route: $route,
        $scope: scope
      });

      scope.form.login.values = {
        nick: nick,
        password: password
      };

      scope.form.login.submit();

      $timeout.flush();

      expect(SwellRT.startSession).
        toHaveBeenCalled();

      expect(calledNick).toBe(nick);
      expect(calledPassword).toBe(password);
      expect(SessionSvc.users.current()).toBe(nick + '@' + __session.domain);

    });
  });

  describe('when registering', function() {
    beforeEach(function() {
      $route.current = {
        params: {
          form: 'register'
        }
      };
    });
  });

  describe('when forgot password', function() {
    beforeEach(function() {
      $route.current = {
        params: {
          form: 'forgotten_password'
        }
      };
    });
  });

  describe('when recovering password', function() {
    beforeEach(function() {
      $route.current = {
        params: {
          form: 'recover_password'
        }
      };
    });
  });
});

@@ -779,19 +779,17 @@
 l.net';%0A
- %0A 
+%0A
 %0A  // In
@@ -1161,45 +1161,8 @@
 ) %7B%0A
-    var calledNick, calledPassword;%0A%0A
     
@@ -1267,25 +1267,162 @@
  %7D%0A      %7D;%0A
-%0A
+    %7D);%0A%0A    describe('and SwellRT will send the ok', function() %7B%0A      var calledNick, calledPassword;%0A%0A      beforeEach(function() %7B%0A  
       spyOn(
@@ -1664,28 +1664,32 @@
 ss();%0A      
+  
 %7D);%0A
+  
     %7D);%0A%0A   
@@ -1689,16 +1689,18 @@
 );%0A%0A    
+  
 it('shou
@@ -1706,20 +1706,13 @@
 uld 
-call SwellRT
+login
 ', f
@@ -1719,24 +1719,26 @@
 unction() %7B%0A
+  
       scope 
@@ -1755,24 +1755,26 @@
 pe.$new();%0A%0A
+  
       Sessio
@@ -1814,24 +1814,26 @@
 , %7B%0A        
+  
 $route: $rou
@@ -1844,16 +1844,18 @@
         
+  
 $scope: 
@@ -1860,16 +1860,18 @@
 : scope%0A
+  
       %7D)
@@ -1871,32 +1871,34 @@
      %7D);%0A%0A      
+  
 scope.form.login
@@ -1913,24 +1913,26 @@
 = %7B%0A        
+  
 nick: nick,%0A
@@ -1931,16 +1931,18 @@
 : nick,%0A
+  
         
@@ -1970,12 +1970,16 @@
     
+  
 %7D;%0A%0A
+  
     
@@ -2004,24 +2004,26 @@
 .submit();%0A%0A
+  
       $timeo
@@ -2037,24 +2037,26 @@
 h();%0A%0A      
+  
 expect(Swell
@@ -2104,24 +2104,26 @@
 d();%0A%0A      
+  
 expect(calle
@@ -2141,16 +2141,18 @@
 (nick);%0A
+  
       ex
@@ -2190,24 +2190,26 @@
 ord);%0A      
+  
 expect(Sessi
@@ -2268,16 +2268,351 @@
 omain);%0A
+      %7D);%0A    %7D);%0A%0A    describe('and SwellRT will send an error', function() %7B%0A      beforeEach(function() %7B%0A        spyOn(SwellRT, 'startSession').%0A        and.callFake(function(domain, nick, password, success, error) %7B%0A          error();%0A        %7D);%0A      %7D);%0A%0A      it('should show the error', function() %7B%0A        // TODO%0A      %7D);
 %0A    %7D);

Add login error scaffold