|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +describe('$$testability', function() { |
| 4 | + describe('finding elements', function() { |
| 5 | + var $$testability, $compile, scope, element; |
| 6 | + |
| 7 | + beforeEach(inject(function(_$$testability_, _$compile_, $rootScope) { |
| 8 | + $$testability = _$$testability_; |
| 9 | + $compile = _$compile_; |
| 10 | + scope = $rootScope.$new(); |
| 11 | + })); |
| 12 | + |
| 13 | + afterEach(function() { |
| 14 | + dealoc(element); |
| 15 | + }); |
| 16 | + |
| 17 | + it('should find partial bindings', function() { |
| 18 | + element = |
| 19 | + '<div>' + |
| 20 | + ' <span>{{name}}</span>' + |
| 21 | + ' <span>{{username}}</span>' + |
| 22 | + '</div>'; |
| 23 | + element = $compile(element)(scope); |
| 24 | + var names = $$testability.findBindings(element[0], 'name'); |
| 25 | + expect(names.length).toBe(2); |
| 26 | + expect(names[0]).toBe(element.find('span')[0]); |
| 27 | + expect(names[1]).toBe(element.find('span')[1]); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should find exact bindings', function() { |
| 31 | + element = |
| 32 | + '<div>' + |
| 33 | + ' <span>{{name}}</span>' + |
| 34 | + ' <span>{{username}}</span>' + |
| 35 | + '</div>'; |
| 36 | + element = $compile(element)(scope); |
| 37 | + var users = $$testability.findBindings(element[0], 'name', true); |
| 38 | + expect(users.length).toBe(1); |
| 39 | + expect(users[0]).toBe(element.find('span')[0]); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should find bindings by class', function() { |
| 43 | + element = |
| 44 | + '<div>' + |
| 45 | + ' <span ng-bind="name"></span>' + |
| 46 | + ' <span>{{username}}</span>' + |
| 47 | + '</div>'; |
| 48 | + element = $compile(element)(scope); |
| 49 | + var names = $$testability.findBindings(element[0], 'name'); |
| 50 | + expect(names.length).toBe(2); |
| 51 | + expect(names[0]).toBe(element.find('span')[0]); |
| 52 | + expect(names[1]).toBe(element.find('span')[1]); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should only search within the context element', function() { |
| 56 | + element = |
| 57 | + '<div>' + |
| 58 | + ' <ul><li>{{name}}</li></ul>' + |
| 59 | + ' <ul><li>{{name}}</li></ul>' + |
| 60 | + '</div>'; |
| 61 | + element = $compile(element)(scope); |
| 62 | + var names = $$testability.findBindings(element.find('ul')[0], 'name'); |
| 63 | + expect(names.length).toBe(1); |
| 64 | + expect(names[0]).toBe(element.find('li')[0]); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should find partial models', function() { |
| 68 | + element = |
| 69 | + '<div>' + |
| 70 | + ' <input type="text" ng-model="name"/>' + |
| 71 | + ' <input type="text" ng-model="username"/>' + |
| 72 | + '</div>'; |
| 73 | + element = $compile(element)(scope); |
| 74 | + var names = $$testability.findModels(element[0], 'name'); |
| 75 | + expect(names.length).toBe(2); |
| 76 | + expect(names[0]).toBe(element.find('input')[0]); |
| 77 | + expect(names[1]).toBe(element.find('input')[1]); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should find exact models', function() { |
| 81 | + element = |
| 82 | + '<div>' + |
| 83 | + ' <input type="text" ng-model="name"/>' + |
| 84 | + ' <input type="text" ng-model="username"/>' + |
| 85 | + '</div>'; |
| 86 | + element = $compile(element)(scope); |
| 87 | + var users = $$testability.findModels(element[0], 'name', true); |
| 88 | + expect(users.length).toBe(1); |
| 89 | + expect(users[0]).toBe(element.find('input')[0]); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should find models in different input types', function() { |
| 93 | + element = |
| 94 | + '<div>' + |
| 95 | + ' <input type="text" ng-model="name"/>' + |
| 96 | + ' <textarea ng-model="username"/>' + |
| 97 | + '</div>'; |
| 98 | + element = $compile(element)(scope); |
| 99 | + var names = $$testability.findModels(element[0], 'name'); |
| 100 | + expect(names.length).toBe(2); |
| 101 | + expect(names[0]).toBe(element.find('input')[0]); |
| 102 | + expect(names[1]).toBe(element.find('textarea')[0]); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should only search for models within the context element', function() { |
| 106 | + element = |
| 107 | + '<div>' + |
| 108 | + ' <ul><li><input type="text" ng-model="name"/></li></ul>' + |
| 109 | + ' <ul><li><input type="text" ng-model="name"/></li></ul>' + |
| 110 | + '</div>'; |
| 111 | + element = $compile(element)(scope); |
| 112 | + var names = $$testability.findModels(element.find('ul')[0], 'name'); |
| 113 | + expect(names.length).toBe(1); |
| 114 | + expect(names[0]).toBe(angular.element(element.find('li')[0]).find('input')[0]); |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + describe('location', function() { |
| 119 | + beforeEach(module(function() { |
| 120 | + return function($httpBackend) { |
| 121 | + $httpBackend.when('GET', 'foo.html').respond('foo'); |
| 122 | + $httpBackend.when('GET', 'baz.html').respond('baz'); |
| 123 | + $httpBackend.when('GET', 'bar.html').respond('bar'); |
| 124 | + $httpBackend.when('GET', '404.html').respond('not found'); |
| 125 | + }; |
| 126 | + })); |
| 127 | + |
| 128 | + it('should return the current URL', inject(function($location, $$testability) { |
| 129 | + $location.path('/bar.html'); |
| 130 | + expect($$testability.getLocation()).toMatch(/bar.html$/); |
| 131 | + })); |
| 132 | + |
| 133 | + it('should change the URL', inject(function($location, $$testability) { |
| 134 | + $location.path('/bar.html'); |
| 135 | + $$testability.setLocation('foo.html'); |
| 136 | + expect($location.path()).toEqual('/foo.html'); |
| 137 | + })); |
| 138 | + }); |
| 139 | + |
| 140 | + describe('waiting for stability', function() { |
| 141 | + it('should process callbacks immediately with no outstanding requests', |
| 142 | + inject(function($$testability) { |
| 143 | + var callback = jasmine.createSpy('callback'); |
| 144 | + $$testability.whenStable(callback); |
| 145 | + expect(callback).toHaveBeenCalled(); |
| 146 | + })); |
| 147 | + }); |
| 148 | +}); |
0 commit comments