我有這個單元測試設置,並且想要檢查<rating>
元素的shadowDOM
。 但shadowDOM
沒有childNodes。如何在角度分量測試中查詢shadowDOM
在下面的代碼中,我嘗試檢查getStars()
方法中的元素shadowDOM。
我的方法有缺陷嗎? 這項工作應該如何?
library bootstrap_angular.test.elements.rating;
import 'dart:html' as dom;
import 'dart:async';
import 'package:unittest/unittest.dart';
import 'package:unittest/html_enhanced_config.dart';
import 'package:angular/angular.dart' as ng;
import 'package:angular/mock/module.dart' as ngMock;
import 'package:bootstrap_angular/rating/rating.dart';
void main() {
useHtmlEnhancedConfiguration();
ng.Scope _rootScope;
dom.Element _element;
ngMock.TestBed _tb;
ngMock.MockHttpBackend _backend;
setUp(() {
Future f;
try {
ngMock.setUpInjector();
ngMock.module((ng.Module module) {
module.install(new RatingModule());
});
ngMock.inject((ng.Scope scope, ngMock.TestBed testBed) {
_rootScope = scope;
_rootScope['rate'] = 3;
_tb = testBed;
f = dom.HttpRequest.getString('/bootstrap_angular/packages/bootstrap_angular/rating/rating.html')
.then((ratingTemplate) {
_backend = new ngMock.MockHttpBackend();
assert(ratingTemplate.contains('<i '));
_backend.expect('GET').respond(ratingTemplate);
_element = _tb.compile('<rating value="rate"></rating>', scope: _rootScope);
var element =_element.shadowRoot.querySelector('i');
_rootScope.$digest();
}).catchError((e) => print(e));
});
} catch(e) {
print(e);
}
return f;
});
List<dom.Element> getStars() {
print(_element.shadowRoot.querySelectorAll('i'));
return _element.shadowRoot.querySelectorAll('i');
}
test("rating component", () {
expect(getStars().length, equals(3));
});
}
元素模板HTML從HTTP請求
<span ng-mouseleave="ctrl.reset()">
<i ng-repeat="r in ctrl.range" ng-mouseenter="ctrl.enter($index + 1)" ng-click="ctrl.rate($index + 1)" class="glyphicon" ng-class="ctrl.stateClass($index, r)"></i>
</span>
忘了提及:插入到演示頁面時,元素的影子DOM有內容。 –