refactor(compiler): remove direct accesses to DOM in tests

Closes #899
This commit is contained in:
Marc Laval 2015-03-09 11:37:59 +01:00
parent f1f06018c1
commit 5acde20c7d
8 changed files with 32 additions and 27 deletions

View File

@ -29,13 +29,13 @@ export function main() {
return new CompilePipeline([ return new CompilePipeline([
new MockStep((parent, current, control) => { new MockStep((parent, current, control) => {
var hasBinding = false; var hasBinding = false;
if (isPresent(current.element.getAttribute('text-binding'))) { if (isPresent(DOM.getAttribute(current.element, 'text-binding'))) {
MapWrapper.forEach(textNodeBindings, (v,k) => { MapWrapper.forEach(textNodeBindings, (v,k) => {
current.addTextNodeBinding(k, parser.parseBinding(v, null)); current.addTextNodeBinding(k, parser.parseBinding(v, null));
}); });
hasBinding = true; hasBinding = true;
} }
if (isPresent(current.element.getAttribute('prop-binding'))) { if (isPresent(DOM.getAttribute(current.element, 'prop-binding'))) {
if (isPresent(propertyBindings)) { if (isPresent(propertyBindings)) {
MapWrapper.forEach(propertyBindings, (v,k) => { MapWrapper.forEach(propertyBindings, (v,k) => {
current.addPropertyBinding(k, parser.parseBinding(v, null)); current.addPropertyBinding(k, parser.parseBinding(v, null));
@ -43,13 +43,13 @@ export function main() {
} }
hasBinding = true; hasBinding = true;
} }
if (isPresent(current.element.getAttribute('event-binding'))) { if (isPresent(DOM.getAttribute(current.element, 'event-binding'))) {
MapWrapper.forEach(eventBindings, (v,k) => { MapWrapper.forEach(eventBindings, (v,k) => {
current.addEventBinding(k, parser.parseAction(v, null)); current.addEventBinding(k, parser.parseAction(v, null));
}); });
hasBinding = true; hasBinding = true;
} }
if (isPresent(current.element.getAttribute('directives'))) { if (isPresent(DOM.getAttribute(current.element, 'directives'))) {
hasBinding = true; hasBinding = true;
for (var i=0; i<directives.length; i++) { for (var i=0; i<directives.length; i++) {
var dirMetadata = reflector.read(directives[i]); var dirMetadata = reflector.read(directives[i]);
@ -61,13 +61,13 @@ export function main() {
DOM.addClass(current.element, 'ng-binding'); DOM.addClass(current.element, 'ng-binding');
} }
if (isPresent(protoElementInjector) && if (isPresent(protoElementInjector) &&
(isPresent(current.element.getAttribute('text-binding')) || (isPresent(DOM.getAttribute(current.element, 'text-binding')) ||
isPresent(current.element.getAttribute('prop-binding')) || isPresent(DOM.getAttribute(current.element, 'prop-binding')) ||
isPresent(current.element.getAttribute('directives')) || isPresent(DOM.getAttribute(current.element, 'directives')) ||
isPresent(current.element.getAttribute('event-binding')))) { isPresent(DOM.getAttribute(current.element, 'event-binding')))) {
current.inheritedProtoElementInjector = protoElementInjector; current.inheritedProtoElementInjector = protoElementInjector;
} }
if (isPresent(current.element.getAttribute('viewroot'))) { if (isPresent(DOM.getAttribute(current.element, 'viewroot'))) {
current.isViewRoot = true; current.isViewRoot = true;
current.inheritedProtoView = new ProtoView( current.inheritedProtoView = new ProtoView(
current.element, current.element,
@ -338,11 +338,11 @@ export function main() {
evalContext.prop1 = 'red'; evalContext.prop1 = 'red';
changeDetector.detectChanges(); changeDetector.detectChanges();
expect(view.nodes[0].style.color).toEqual('red'); expect(DOM.getStyle(view.nodes[0], 'color')).toEqual('red');
evalContext.prop1 = 'blue'; evalContext.prop1 = 'blue';
changeDetector.detectChanges(); changeDetector.detectChanges();
expect(view.nodes[0].style.color).toEqual('blue'); expect(DOM.getStyle(view.nodes[0], 'color')).toEqual('blue');
}); });
it('should bind style with a dot and suffix', () => { it('should bind style with a dot and suffix', () => {

View File

@ -105,7 +105,7 @@ export function main() {
var newChild = new CompileElement(el('<div id="3"></div>')); var newChild = new CompileElement(el('<div id="3"></div>'));
var pipeline = new CompilePipeline([ var pipeline = new CompilePipeline([
new MockStep((parent, current, control) => { new MockStep((parent, current, control) => {
if (StringWrapper.equals(current.element.id, '1')) { if (StringWrapper.equals(DOM.getAttribute(current.element, 'id'), '1')) {
control.addChild(newChild); control.addChild(newChild);
} }
}), }),
@ -144,9 +144,9 @@ export class IgnoreChildrenStep extends CompileStep {
function logEntry(log, parent, current) { function logEntry(log, parent, current) {
var parentId = ''; var parentId = '';
if (isPresent(parent)) { if (isPresent(parent)) {
parentId = parent.element.getAttribute('id')+'<'; parentId = DOM.getAttribute(parent.element, 'id') + '<';
} }
ListWrapper.push(log, parentId+current.element.getAttribute('id')); ListWrapper.push(log, parentId + DOM.getAttribute(current.element, 'id'));
} }
function createLoggerStep(log) { function createLoggerStep(log) {
@ -158,7 +158,7 @@ function createLoggerStep(log) {
function createWrapperStep(wrapperId, log) { function createWrapperStep(wrapperId, log) {
var nextElementId = 0; var nextElementId = 0;
return new MockStep((parent, current, control) => { return new MockStep((parent, current, control) => {
var parentCountStr = current.element.getAttribute(wrapperId); var parentCountStr = DOM.getAttribute(current.element, wrapperId);
if (isPresent(parentCountStr)) { if (isPresent(parentCountStr)) {
var parentCount = NumberWrapper.parseInt(parentCountStr, 10); var parentCount = NumberWrapper.parseInt(parentCountStr, 10);
while (parentCount > 0) { while (parentCount > 0) {

View File

@ -1,5 +1,6 @@
import {describe, beforeEach, it, expect, iit, ddescribe, el} from 'angular2/test_lib'; import {describe, beforeEach, it, expect, iit, ddescribe, el} from 'angular2/test_lib';
import {isPresent, isBlank} from 'angular2/src/facade/lang'; import {isPresent, isBlank} from 'angular2/src/facade/lang';
import {DOM} from 'angular2/src/dom/dom_adapter';
import {List, ListWrapper, MapWrapper} from 'angular2/src/facade/collection'; import {List, ListWrapper, MapWrapper} from 'angular2/src/facade/collection';
import {ProtoElementInjectorBuilder} from 'angular2/src/core/compiler/pipeline/proto_element_injector_builder'; import {ProtoElementInjectorBuilder} from 'angular2/src/core/compiler/pipeline/proto_element_injector_builder';
@ -33,11 +34,11 @@ export function main() {
} }
var reader = new DirectiveMetadataReader(); var reader = new DirectiveMetadataReader();
return new CompilePipeline([new MockStep((parent, current, control) => { return new CompilePipeline([new MockStep((parent, current, control) => {
if (isPresent(current.element.getAttribute('viewroot'))) { if (isPresent(DOM.getAttribute(current.element, 'viewroot'))) {
current.isViewRoot = true; current.isViewRoot = true;
} }
if (isPresent(current.element.getAttribute('directives'))) { if (isPresent(DOM.getAttribute(current.element, 'directives'))) {
for (var i=0; i<directives.length; i++) { for (var i=0; i<directives.length; i++) {
var dirMetadata = reader.read(directives[i]); var dirMetadata = reader.read(directives[i]);
current.addDirective(dirMetadata); current.addDirective(dirMetadata);
@ -45,7 +46,7 @@ export function main() {
} }
// Check only for the hard-coded var- attribute from ELEMENT_WITH_VAR test element. // Check only for the hard-coded var- attribute from ELEMENT_WITH_VAR test element.
if (isPresent(current.element.getAttribute('var-name'))) { if (isPresent(DOM.getAttribute(current.element, 'var-name'))) {
current.variableBindings = MapWrapper.create(); current.variableBindings = MapWrapper.create();
MapWrapper.set(current.variableBindings, '\$implicit', 'name'); MapWrapper.set(current.variableBindings, '\$implicit', 'name');
} }

View File

@ -1,5 +1,6 @@
import {describe, beforeEach, it, expect, iit, ddescribe, el} from 'angular2/test_lib'; import {describe, beforeEach, it, expect, iit, ddescribe, el} from 'angular2/test_lib';
import {isPresent} from 'angular2/src/facade/lang'; import {isPresent} from 'angular2/src/facade/lang';
import {DOM} from 'angular2/src/dom/dom_adapter';
import {dynamicChangeDetection} from 'angular2/change_detection'; import {dynamicChangeDetection} from 'angular2/change_detection';
import {ElementBinder} from 'angular2/src/core/compiler/element_binder'; import {ElementBinder} from 'angular2/src/core/compiler/element_binder';
import {ProtoViewBuilder} from 'angular2/src/core/compiler/pipeline/proto_view_builder'; import {ProtoViewBuilder} from 'angular2/src/core/compiler/pipeline/proto_view_builder';
@ -14,10 +15,10 @@ export function main() {
describe('ProtoViewBuilder', () => { describe('ProtoViewBuilder', () => {
function createPipeline(variableBindings = null) { function createPipeline(variableBindings = null) {
return new CompilePipeline([new MockStep((parent, current, control) => { return new CompilePipeline([new MockStep((parent, current, control) => {
if (isPresent(current.element.getAttribute('viewroot'))) { if (isPresent(DOM.getAttribute(current.element, 'viewroot'))) {
current.isViewRoot = true; current.isViewRoot = true;
} }
if (isPresent(current.element.getAttribute('var-binding'))) { if (isPresent(DOM.getAttribute(current.element, 'var-binding'))) {
current.variableBindings = MapWrapper.createFromStringMap(variableBindings); current.variableBindings = MapWrapper.createFromStringMap(variableBindings);
} }
current.inheritedElementBinder = new ElementBinder(null, null, null); current.inheritedElementBinder = new ElementBinder(null, null, null);

View File

@ -25,6 +25,7 @@ export function main() {
it('should move the content into a new <template> element and mark that as viewRoot', () => { it('should move the content into a new <template> element and mark that as viewRoot', () => {
var rootElement = el('<div><template if="true">a</template></div>'); var rootElement = el('<div><template if="true">a</template></div>');
var results = createPipeline().process(rootElement); var results = createPipeline().process(rootElement);
expect(DOM.getOuterHTML(results[1].element)).toEqual('<template if="true"></template>'); expect(DOM.getOuterHTML(results[1].element)).toEqual('<template if="true"></template>');
expect(results[1].isViewRoot).toBe(false); expect(results[1].isViewRoot).toBe(false);
expect(DOM.getOuterHTML(results[2].element)).toEqual('<template>a</template>'); expect(DOM.getOuterHTML(results[2].element)).toEqual('<template>a</template>');
@ -60,7 +61,7 @@ export function main() {
it('should work with top-level template node', () => { it('should work with top-level template node', () => {
var rootElement = DOM.createTemplate('<div template>x</div>'); var rootElement = DOM.createTemplate('<div template>x</div>');
var originalChild = rootElement.content.childNodes[0]; var originalChild = DOM.content(rootElement).childNodes[0];
var results = createPipeline().process(rootElement); var results = createPipeline().process(rootElement);
expect(results[0].element).toBe(rootElement); expect(results[0].element).toBe(rootElement);
@ -119,7 +120,7 @@ export function main() {
it('should work with top-level template node', () => { it('should work with top-level template node', () => {
var rootElement = DOM.createTemplate('<div *foo>x</div>'); var rootElement = DOM.createTemplate('<div *foo>x</div>');
var originalChild = rootElement.content.childNodes[0]; var originalChild = DOM.content(rootElement).childNodes[0];
var results = createPipeline().process(rootElement); var results = createPipeline().process(rootElement);
expect(results[0].element).toBe(rootElement); expect(results[0].element).toBe(rootElement);

View File

@ -1,4 +1,5 @@
import {describe, it, expect, beforeEach, ddescribe, iit, xit, el} from 'angular2/test_lib'; import {describe, it, expect, beforeEach, ddescribe, iit, xit, el} from 'angular2/test_lib';
import {DOM} from 'angular2/src/dom/dom_adapter';
import {SelectorMatcher} from 'angular2/src/core/compiler/selector'; import {SelectorMatcher} from 'angular2/src/core/compiler/selector';
import {CssSelector} from 'angular2/src/core/compiler/selector'; import {CssSelector} from 'angular2/src/core/compiler/selector';
import {List, ListWrapper, MapWrapper} from 'angular2/src/facade/collection'; import {List, ListWrapper, MapWrapper} from 'angular2/src/facade/collection';
@ -70,7 +71,7 @@ export function main() {
var elementSelector = new CssSelector(); var elementSelector = new CssSelector();
var element = el('<div attr></div>'); var element = el('<div attr></div>');
var empty = element.getAttribute('attr'); var empty = DOM.getAttribute(element, 'attr');
elementSelector.addAttribute('some-decor', empty); elementSelector.addAttribute('some-decor', empty);
matcher.match(elementSelector, selectableCollector); matcher.match(elementSelector, selectableCollector);
expect(matched).toEqual([s1,1]); expect(matched).toEqual([s1,1]);

View File

@ -1,4 +1,5 @@
import {describe, it, expect, beforeEach, ddescribe, iit, xit, el} from 'angular2/test_lib'; import {describe, it, expect, beforeEach, ddescribe, iit, xit, el} from 'angular2/test_lib';
import {DOM} from 'angular2/src/dom/dom_adapter';
import {TemplateLoader} from 'angular2/src/core/compiler/template_loader'; import {TemplateLoader} from 'angular2/src/core/compiler/template_loader';
import {UrlResolver} from 'angular2/src/core/compiler/url_resolver'; import {UrlResolver} from 'angular2/src/core/compiler/url_resolver';
@ -17,7 +18,7 @@ export function main() {
it('should load inline templates synchronously', () => { it('should load inline templates synchronously', () => {
var template = new Template({inline: 'inline template'}); var template = new Template({inline: 'inline template'});
expect(loader.load(template).content).toHaveText('inline template'); expect(DOM.content(loader.load(template))).toHaveText('inline template');
}); });
it('should load templates through XHR', (done) => { it('should load templates through XHR', (done) => {
@ -25,7 +26,7 @@ export function main() {
var template = new Template({url: '/foo'}); var template = new Template({url: '/foo'});
loader.setBaseUrl(template, 'base'); loader.setBaseUrl(template, 'base');
loader.load(template).then((el) => { loader.load(template).then((el) => {
expect(el.content).toHaveText('xhr template'); expect(DOM.content(el)).toHaveText('xhr template');
done(); done();
}); });
xhr.flush(); xhr.flush();
@ -43,7 +44,7 @@ export function main() {
}) })
.then((el) =>{ .then((el) =>{
expect(el).toBe(firstEl); expect(el).toBe(firstEl);
expect(el.content).toHaveText('xhr template'); expect(DOM.content(el)).toHaveText('xhr template');
done(); done();
}); });
xhr.flush(); xhr.flush();

View File

@ -138,7 +138,7 @@ export function main() {
var view = pv.instantiate(null, null, reflector); var view = pv.instantiate(null, null, reflector);
view.hydrate(null, null, null); view.hydrate(null, null, null);
expect(view.nodes.length).toBe(1); expect(view.nodes.length).toBe(1);
expect(view.nodes[0].getAttribute('id')).toEqual('1'); expect(DOM.getAttribute(view.nodes[0], 'id')).toEqual('1');
}); });
describe('collect elements with property bindings', () => { describe('collect elements with property bindings', () => {