'use strict';

/* jasmine specs for controllers go here */

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

  describe('PopulationCtrl', function() {
      var $scope, $rootScope, $controller, controller, createController,
          populationIDs, population, individual0, individual1, lineage;
      
      beforeEach(inject(function($injector){
          $rootScope  = $injector.get('$rootScope');
          $controller = $injector.get('$controller');

          $scope      = $rootScope.$new();

          createController = function() {
              return $controller('PopulationCtrl', {
                  '$scope': $scope
              });
          };
          
          population = [individual0, individual1];
          individual0 = {id:0, data:{treeParams:{}, imageUrl:"foo.svg"}};
          individual1 = {id:1, data:{treeParams:{}, imageUrl:"bar.svg"}};          
      }));
      
      it('should have empty lineage', inject(function(Population,CurrentPopulation,Lineage) {
          populationIDs = [0];
          lineage = [];
          
          spyOn(CurrentPopulation, 'query').andReturn(populationIDs);
          spyOn(CurrentPopulation, 'push');
          spyOn(Population, 'queryAll').andReturn(population);
          spyOn(Lineage, 'query').andReturn(lineage);
     
          controller = createController();
          expect($scope.lineage).toEqual(lineage);
          expect($scope.selectedAncestor).not.toBeDefined();
          expect(CurrentPopulation.push).not.toHaveBeenCalled();
      }));

      it('should have one ancestor in lineage', inject(function(Population,CurrentPopulation,Lineage) {
          populationIDs = [0, 1];
          lineage = [{id:0, data:1, individual:individual1}];
          
          spyOn(CurrentPopulation, 'query').andReturn(populationIDs);
          spyOn(CurrentPopulation, 'push');
          spyOn(Population, 'queryAll').andReturn(population);
          spyOn(Lineage, 'query').andReturn(lineage);
     
          controller = createController();
          expect($scope.lineage).toEqual(lineage);
          expect($scope.selectedAncestor).not.toBeDefined();
          expect(CurrentPopulation.push).not.toHaveBeenCalled();
          
          expect($scope.selectAncestor).toBeDefined();
          $scope.selectAncestor(1);
          expect($scope.selectedAncestor).toEqual(1);
          expect($rootScope.selectedAncestor).toEqual(1);
      }));

      it('should have increased population size by 7',
       inject(function(Population,CurrentPopulation,Lineage,Image) {
          var nextChildID, populationIDs, population;
          
          // For this test, we are faking individuals as simple integers.
          lineage = [];
          populationIDs = [0,1];
          population = [0,1];
          spyOn(Lineage, 'insertInto').andCallFake(function(id){lineage.push(id);});
          spyOn(CurrentPopulation, 'init').andCallFake(function(){populationIDs = [];});
          spyOn(CurrentPopulation, 'push').andCallFake(function(id){populationIDs.push(id);});
          spyOn(CurrentPopulation, 'query').andCallFake(function(){return populationIDs;});
          spyOn(Population, 'queryIDs').andCallFake(function(){return populationIDs;});
          spyOn(Lineage, 'query').andReturn(lineage);

          nextChildID = 2;
          spyOn(Population, 'reproduce').andCallFake(function(id){ return nextChildID++; });
          spyOn(Image, 'getByID');

          var controller = createController();
          $scope.propagate(1);

          expect(Lineage.insertInto).toHaveBeenCalledWith(1);
          expect(CurrentPopulation.init).toHaveBeenCalled();
          expect(populationIDs).toEqual([1,2,3,4,5,6,7,8]);
          expect(Image.getByID).toHaveBeenCalled();

          expect($scope.population).toEqual([1,2,3,4,5,6,7,8]);
          expect($scope.lineage).toEqual([1]);
          expect($scope.selectedAncestor).not.toBeDefined();
      }));
  });
});

@@ -2623,16 +2623,28 @@
      var
+ controller,
  nextChi
@@ -2761,16 +2761,17 @@
 tegers.%0A
+%0A
         
@@ -2790,71 +2790,8 @@
 %5B%5D;%0A
-          populationIDs = %5B0,1%5D;%0A          population = %5B0,1%5D;%0A
     
@@ -2863,32 +2863,120 @@
 ge.push(id);%7D);%0A
+          spyOn(Lineage, 'query').andReturn(lineage);%0A%0A          populationIDs = %5B0,1%5D;%0A
           spyOn(
@@ -3226,32 +3226,90 @@
 opulationIDs;%7D);
+%0A%0A          population = %5B0,1%5D;%0A          nextChildID = 2;
 %0A          spyOn
@@ -3385,90 +3385,8 @@
 %7D);%0A
-          spyOn(Lineage, 'query').andReturn(lineage);%0A%0A          nextChildID = 2;%0A
     
@@ -3470,24 +3470,25 @@
 ldID++; %7D);%0A
+%0A
           sp
@@ -3513,36 +3513,32 @@
 D');%0A%0A          
-var 
 controller = cre
@@ -3730,51 +3730,44 @@
 ect(
-population
+Image.getBy
 ID
-s
 ).to
-Equal(%5B1,2,3,4,5,6,7,8%5D
+HaveBeenCalled(
 );%0A
+%0A
     
@@ -3783,44 +3783,51 @@
 ect(
-Image.getBy
+population
 ID
+s
 ).to
-HaveBeenCalled(
+Equal(%5B1,2,3,4,5,6,7,8%5D
 );%0A
-%0A
     

Refactor for clarity