Last active
          August 29, 2015 14:10 
        
      - 
      
- 
        Save saibotsivad/77a632381f4be2ffbaf7 to your computer and use it in GitHub Desktop. 
    Testing in Angular.js
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | angular.module('myMod', []).controller('MyCtrl', function($scope) { | |
| $scope.var = 'hello'; | |
| $scope.thing = 'before'; | |
| $scope.change = function(word) { | |
| $scope.var = word; | |
| }; | |
| $scope.$watch('var', function() { | |
| $scope.thing = 'after'; | |
| }) | |
| }); | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | describe('controller', function() { | |
| beforeEach(module('myMod')); | |
| describe('MyCtrl', function() { | |
| var scope, createController; | |
| beforeEach(inject(function ($rootScope, $controller) { | |
| scope = $rootScope.$new(); | |
| createController = function() { | |
| return $controller('MyCtrl', { | |
| '$scope': scope | |
| }); | |
| }; | |
| })); | |
| it('should initially have "hello" as the $scope.var value', inject(function() { | |
| var controller = createController(); | |
| expect(scope.var).toEqual('hello'); | |
| })); | |
| it('should change the $scope.var value', inject(function() { | |
| var controller = createController(); | |
| scope.change('world'); | |
| expect(scope.var).toEqual('world'); | |
| })); | |
| it('should initially have "before" as the $scope.thing value', inject(function() { | |
| var controller = createController(); | |
| expect(scope.thing).toEqual('before'); | |
| })); | |
| it('should change the $scope.thing when $scope.var changes', inject(function() { | |
| var controller = createController(); | |
| scope.change('world'); | |
| scope.$digest(); | |
| expect(scope.thing).toEqual('after'); | |
| })); | |
| }); | |
| }); | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | angular.module('myMod').factory('wordsNotInAccountWords', function() { | |
| return function wordsNotInAccountWords(words, accountWords) { | |
| if (words && Array.isArray(accountWords)) { | |
| var wordsList = accountWords.map(function(word) { | |
| return word.name | |
| }); | |
| return words.split(',').filter(function(word) { | |
| return wordsList.indexOf(word) < 0 | |
| }) | |
| } else { | |
| return [] | |
| } | |
| } | |
| }); | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | describe('factory', function() { | |
| beforeEach(module('myMod')); | |
| describe('myMod.wordsNotInAccountWords', function() { | |
| it('should return an empty array if inputs are null', inject(function(wordssNotInAccountWords) { | |
| var result = wordsNotInAccountWords(null, null); | |
| expect(result).toEqual([]) | |
| })); | |
| it('should return [] for an empty "words"', inject(function(wordsNotInAccountWords) { | |
| var words = '' | |
| var accountWords = [ | |
| { name: 'abc' }, | |
| { name: 'def' } | |
| ] | |
| var result = wordsNotInAccountWords(words, accountWords); | |
| expect(result).toEqual([]) | |
| })); | |
| it('should return the words that are not in accountWords', inject(function(wordsNotInAccountWords) { | |
| var words = 'abc,def' | |
| var accountWords = [ | |
| { name: 'def' }, | |
| { name: 'ghi' } | |
| ] | |
| var result = wordsNotInAccountWords(words, accountWords); | |
| expect(result).toEqual(['abc']) | |
| })); | |
| it('should return all "words" when "accountWords" is empty', inject(function(wordsNotInAccountWords) { | |
| var words = 'abc,def' | |
| var accountWords = [] | |
| var result = wordsNotInAccountWords(words, accountWords); | |
| expect(result).toEqual(['abc','def']) | |
| })); | |
| }); | |
| }); | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment