mirror of
https://github.com/zhigang1992/angular.js.git
synced 2026-04-04 22:49:48 +08:00
The $$testability service is a collection of methods for use when debugging or by automated testing tools. It is available globally through the function `angular.getTestability`. For reference, see the Angular.Dart version at https://github.com/angular/angular.dart/pull/1191
173 lines
5.9 KiB
JavaScript
173 lines
5.9 KiB
JavaScript
'use strict';
|
|
|
|
describe('$$testability', function() {
|
|
describe('finding elements', function() {
|
|
var $$testability, $compile, scope, element;
|
|
|
|
beforeEach(inject(function(_$$testability_, _$compile_, $rootScope) {
|
|
$$testability = _$$testability_;
|
|
$compile = _$compile_;
|
|
scope = $rootScope.$new();
|
|
}));
|
|
|
|
afterEach(function() {
|
|
dealoc(element);
|
|
});
|
|
|
|
it('should find partial bindings', function() {
|
|
element =
|
|
'<div>' +
|
|
' <span>{{name}}</span>' +
|
|
' <span>{{username}}</span>' +
|
|
'</div>';
|
|
element = $compile(element)(scope);
|
|
var names = $$testability.findBindings(element[0], 'name');
|
|
expect(names.length).toBe(2);
|
|
expect(names[0]).toBe(element.find('span')[0]);
|
|
expect(names[1]).toBe(element.find('span')[1]);
|
|
});
|
|
|
|
it('should find exact bindings', function() {
|
|
element =
|
|
'<div>' +
|
|
' <span>{{name}}</span>' +
|
|
' <span>{{username}}</span>' +
|
|
'</div>';
|
|
element = $compile(element)(scope);
|
|
var users = $$testability.findBindings(element[0], 'name', true);
|
|
expect(users.length).toBe(1);
|
|
expect(users[0]).toBe(element.find('span')[0]);
|
|
});
|
|
|
|
it('should ignore filters for exact bindings', function() {
|
|
element =
|
|
'<div>' +
|
|
' <span>{{name | uppercase}}</span>' +
|
|
' <span>{{username}}</span>' +
|
|
'</div>';
|
|
element = $compile(element)(scope);
|
|
var users = $$testability.findBindings(element[0], 'name', true);
|
|
expect(users.length).toBe(1);
|
|
expect(users[0]).toBe(element.find('span')[0]);
|
|
});
|
|
|
|
it('should ignore whitespace for exact bindings', function() {
|
|
element =
|
|
'<div>' +
|
|
' <span>{{ name }}</span>' +
|
|
' <span>{{username}}</span>' +
|
|
'</div>';
|
|
element = $compile(element)(scope);
|
|
var users = $$testability.findBindings(element[0], 'name', true);
|
|
expect(users.length).toBe(1);
|
|
expect(users[0]).toBe(element.find('span')[0]);
|
|
});
|
|
|
|
it('should find bindings by class', function() {
|
|
element =
|
|
'<div>' +
|
|
' <span ng-bind="name"></span>' +
|
|
' <span>{{username}}</span>' +
|
|
'</div>';
|
|
element = $compile(element)(scope);
|
|
var names = $$testability.findBindings(element[0], 'name');
|
|
expect(names.length).toBe(2);
|
|
expect(names[0]).toBe(element.find('span')[0]);
|
|
expect(names[1]).toBe(element.find('span')[1]);
|
|
});
|
|
|
|
it('should only search within the context element', function() {
|
|
element =
|
|
'<div>' +
|
|
' <ul><li>{{name}}</li></ul>' +
|
|
' <ul><li>{{name}}</li></ul>' +
|
|
'</div>';
|
|
element = $compile(element)(scope);
|
|
var names = $$testability.findBindings(element.find('ul')[0], 'name');
|
|
expect(names.length).toBe(1);
|
|
expect(names[0]).toBe(element.find('li')[0]);
|
|
});
|
|
|
|
it('should find partial models', function() {
|
|
element =
|
|
'<div>' +
|
|
' <input type="text" ng-model="name"/>' +
|
|
' <input type="text" ng-model="username"/>' +
|
|
'</div>';
|
|
element = $compile(element)(scope);
|
|
var names = $$testability.findModels(element[0], 'name');
|
|
expect(names.length).toBe(2);
|
|
expect(names[0]).toBe(element.find('input')[0]);
|
|
expect(names[1]).toBe(element.find('input')[1]);
|
|
});
|
|
|
|
it('should find exact models', function() {
|
|
element =
|
|
'<div>' +
|
|
' <input type="text" ng-model="name"/>' +
|
|
' <input type="text" ng-model="username"/>' +
|
|
'</div>';
|
|
element = $compile(element)(scope);
|
|
var users = $$testability.findModels(element[0], 'name', true);
|
|
expect(users.length).toBe(1);
|
|
expect(users[0]).toBe(element.find('input')[0]);
|
|
});
|
|
|
|
it('should find models in different input types', function() {
|
|
element =
|
|
'<div>' +
|
|
' <input type="text" ng-model="name"/>' +
|
|
' <textarea ng-model="username"/>' +
|
|
'</div>';
|
|
element = $compile(element)(scope);
|
|
var names = $$testability.findModels(element[0], 'name');
|
|
expect(names.length).toBe(2);
|
|
expect(names[0]).toBe(element.find('input')[0]);
|
|
expect(names[1]).toBe(element.find('textarea')[0]);
|
|
});
|
|
|
|
it('should only search for models within the context element', function() {
|
|
element =
|
|
'<div>' +
|
|
' <ul><li><input type="text" ng-model="name"/></li></ul>' +
|
|
' <ul><li><input type="text" ng-model="name"/></li></ul>' +
|
|
'</div>';
|
|
element = $compile(element)(scope);
|
|
var names = $$testability.findModels(element.find('ul')[0], 'name');
|
|
expect(names.length).toBe(1);
|
|
expect(names[0]).toBe(angular.element(element.find('li')[0]).find('input')[0]);
|
|
});
|
|
});
|
|
|
|
describe('location', function() {
|
|
beforeEach(module(function() {
|
|
return function($httpBackend) {
|
|
$httpBackend.when('GET', 'foo.html').respond('foo');
|
|
$httpBackend.when('GET', 'baz.html').respond('baz');
|
|
$httpBackend.when('GET', 'bar.html').respond('bar');
|
|
$httpBackend.when('GET', '404.html').respond('not found');
|
|
};
|
|
}));
|
|
|
|
it('should return the current URL', inject(function($location, $$testability) {
|
|
$location.path('/bar.html');
|
|
expect($$testability.getLocation()).toMatch(/bar.html$/);
|
|
}));
|
|
|
|
it('should change the URL', inject(function($location, $$testability) {
|
|
$location.path('/bar.html');
|
|
$$testability.setLocation('foo.html');
|
|
expect($location.path()).toEqual('/foo.html');
|
|
}));
|
|
});
|
|
|
|
describe('waiting for stability', function() {
|
|
it('should process callbacks immediately with no outstanding requests',
|
|
inject(function($$testability) {
|
|
var callback = jasmine.createSpy('callback');
|
|
$$testability.whenStable(callback);
|
|
expect(callback).toHaveBeenCalled();
|
|
}));
|
|
});
|
|
});
|