AngularJS – $internal

var app = angular.module("app", []);
 
app.controller("AppCtrl", function ($q, $interval) {
    var timer = $interval(function () {
    }, 100, 10);
 
    function success() {
        console.log("done");
    }
 
    function error() {
        console.log("cancelled or error");
    }
 
    function notify() {
        console.log("updating");
    }
 
    timer.then(success, error, notify);
 
    this.cancel = function () {
        $interval.cancel(timer);
    };
});

AngularJS – Inject Jasmine Spies With $provide

app.js

var app = angular.module('app', []);
 
app.controller('ProfileCtrl',
    ['$scope', 'ProfileSvc',
        function ($scope,
                  profileSvc) {
 
            $scope.favoriteBooks = profileSvc
                .favoriteBooks();
 
        }]);
 
app.service('ProfileSvc', function () {
    this.favoriteBooks = function () {
        return JSON.parse(
            localStorage.getItem('favoriteBooks')
        );
    };
});

app.spec.js

describe("Providers", function () {
    var $scope,
        controller,
        profileSvcSpy,
        favoriteBooks = ["Lowside of the Road", "Life", "The Grand Pursuit"];
 
    beforeEach(module('app', function($provide) {
        profileSvcSpy = jasmine.createSpyObj("ProfileSvc", ["favoriteBooks"]);
        profileSvcSpy.favoriteBooks.andReturn(favoriteBooks);
 
        $provide.value("ProfileSvc", profileSvcSpy);
    }));
    beforeEach(inject(function($rootScope, $controller) {
        $scope = $rootScope.$new();
        controller = $controller('ProfileCtrl', {$scope: $scope});
    }));
 
    describe("Initialization", function () {
        it("Should call the profileSvc.favoriteBooks with no arguments", function () {
            expect(profileSvcSpy.favoriteBooks).toHaveBeenCalledWith();
        });
 
        it("Should set the $scope.favoriteBooks from the profileService", function () {
            expect($scope.favoriteBooks).toEqual(favoriteBooks);
        });
    });
 
});

AngularJS – $q.all

var app = angular.module("app", []);
 
app.controller("AppCtrl", function ($q, $timeout) {
    var one = $q.defer();
    var two = $q.defer();
    var three = $q.defer();
 
    var all = $q.all([one.promise, two.promise, three.promise]);
    all.then(success);
 
    function success(data) {
        console.log(data);
    }
    one.promise.then(success);
    two.promise.then(success);
    three.promise.then(success);
 
    $timeout(function () {
        one.resolve("one done");
    }, Math.random() * 1000);
 
    $timeout(function () {
        two.resolve("two done");
    }, Math.random() * 1000);
 
    $timeout(function () {
        three.resolve("three done");
    }, Math.random() * 1000);
});

AngularJS – Test Simple Binding With Protractor

describe("hello-protractor", function () {
 
  describe("index", function () {
    it("should display the correct title", function () {
      browser.get('/#');
      expect(browser.getTitle()).toBe('hello protractor');
    });
 
    it("should display the message when button clicked", function () {
      var button = element(by.id('button1')),
        message = element(by.binding('messageText'));
 
      button.click();
 
      expect(message.getText()).toBe('button 1 clicked');
    });
  });
});
<div class="row text-center">
    <a class="btn btn-primary"
       id="button1"
       ng-click="updateMessageText('button 1 clicked')">
      Button 1
    </a>
</div>
 
<div class="row h3 text-center">{{ messageText }}</div>

AngularJs – Promises

<!DOCTYPE html>
<html>
<head>
    <title>Egghead.io</title>
    <link rel="stylesheet" href="bower_components/bootstrap.css/css/bootstrap.css"/>
</head>
<body ng-app="app" ng-controller="TodoCtrl as todoCtrl">
 
<div ng-repeat="todo in todoCtrl.todos">
    {{todo.item}}
</div>
<form>
    <input type="text" ng-model="newTodo"/>
    <input type="submit" ng-click="todoCtrl.addTodo(newTodo)"/>
</form>
<script src="bower_components/angular/angular.js"></script>
<script src="app-promises.js"></script>
</body>
</html>
var app = angular.module("app", []);
app.service("TodoService", function ($q, $timeout) {
 
    this.getTodos = function () {
        var d = $q.defer();
 
        $timeout(function () {
            d.resolve(
                [
                    {item: "Clean room", done: false},
                    {item: "Eat lunch", done: false},
                    {item: "Wash car", done: false}
                ]
 
            )
        }, 3000)
 
        return d.promise;
    }
 
 
    this.addTodo = function (item) {
        this.todos.push({item: item, done: false})
    }
})
app.controller("TodoCtrl", function (TodoService) {
    var todoCtrl = this;
 
    TodoService.getTodos().then(function (result) {
        todoCtrl.todos = result;
    });
    todoCtrl.addTodo = TodoService.addTodo;
})

Código fonte das aulas 1 a 12 do curso egghead.oi

– Quando estamos usando o state podemos usar esse recurso:
HTML

<div ui-sref=“/teste”>Url Teste</div>

– Criando um test:
JSSpec

describe(‘filter’, function(){
     beforeEach(module(‘myApp’));
 
     describe(‘reverse’, function() {
          it(’should reverse a string’, inject(function(reverseFilter) {
               expect(reverseFilter(‘ABCD’)).toEqual(‘DCBA’);
               expect(reverseFilter(‘John’)).toEqual(’nhoJ’);
          }))
     })
})

   

– uiRepeat com um filter:
JS

<div ng-app="myApp">
     <div ng-controller="AvengersCtrl as avengersCtrl">
          <input type="text" ng-model="search.$">
          <table>
               <tr ng-repeat="actor in avengersCtrl.avengers.cast | filter:search">
                    <td>{{actor.name}}</td>
                    <td>{{actor.character}}</td>
               </tr>
          </table>
     </div>
</div>

– uiRepeat com filter, limitTo e orderBy:
HTML

<tr ng-repeat="actor in avengers.cast | orderBy:'name' | limitTo:5 | filter:search">

– Diretivas em uma tag só:
HTML

<div ng-app="superhero">
     <div superman flash></div>
</div>

JS

app.directive("superman", function() {
     return {
          restrict: "A",
          link: function() {
               alert("I'm working stronger");
          }
     };
});
 
app.directive("flash", function() {
     return {
          restrict: "A",
          link: function() {
               alert("I'm working faster");
          }
     };
});