refactor(core): remove testing-only childNodes() and firstChild() fns from DomAdapters (#32291)

PR Close #32291
This commit is contained in:
Kara Erickson 2019-08-24 08:01:24 -07:00 committed by Miško Hevery
parent 30dabdf8fc
commit c0680602f9
17 changed files with 45 additions and 53 deletions

View File

@ -34,8 +34,8 @@ class SomeComponent {
function createRootEl(selector = 'bootstrap-app') {
const doc = TestBed.get(DOCUMENT);
const rootEl = <HTMLElement>getDOM().firstChild(
getContent(createTemplate(`<${selector}></${selector}>`)));
const rootEl =
<HTMLElement>getContent(createTemplate(`<${selector}></${selector}>`)).firstChild;
const oldRoots = getDOM().querySelectorAll(doc, selector);
for (let i = 0; i < oldRoots.length; i++) {
getDOM().remove(oldRoots[i]);

View File

@ -377,7 +377,7 @@ function declareTests(config?: {useJit: boolean}) {
fixture.detectChanges();
const childNodesOfWrapper = getDOM().childNodes(fixture.nativeElement);
const childNodesOfWrapper = fixture.nativeElement.childNodes;
// 1 template + 2 copies.
expect(childNodesOfWrapper.length).toBe(3);
expect(childNodesOfWrapper[1]).toHaveText('hello');
@ -427,7 +427,7 @@ function declareTests(config?: {useJit: boolean}) {
.overrideComponent(MyComp, {set: {template: '<ng-template></ng-template>'}})
.createComponent(MyComp);
const childNodesOfWrapper = getDOM().childNodes(fixture.nativeElement);
const childNodesOfWrapper = fixture.nativeElement.childNodes;
expect(childNodesOfWrapper.length).toBe(1);
expect(isCommentNode(childNodesOfWrapper[0])).toBe(true);
});
@ -572,7 +572,7 @@ function declareTests(config?: {useJit: boolean}) {
fixture.detectChanges();
// Get the element at index 2, since index 0 is the <ng-template>.
expect(getDOM().childNodes(fixture.nativeElement)[2]).toHaveText('1-hello');
expect(fixture.nativeElement.childNodes[2]).toHaveText('1-hello');
});
});
@ -2039,8 +2039,8 @@ function declareTests(config?: {useJit: boolean}) {
const fixture = TestBed.createComponent(MyComp);
const el = fixture.nativeElement;
const svg = getDOM().childNodes(el)[0];
const use = getDOM().childNodes(svg)[0];
const svg = el.childNodes[0];
const use = svg.childNodes[0];
expect(getDOM().getProperty(<Element>svg, 'namespaceURI'))
.toEqual('http://www.w3.org/2000/svg');
expect(getDOM().getProperty(<Element>use, 'namespaceURI'))
@ -2059,9 +2059,9 @@ function declareTests(config?: {useJit: boolean}) {
const fixture = TestBed.createComponent(MyComp);
const el = fixture.nativeElement;
const svg = getDOM().childNodes(el)[0];
const foreignObject = getDOM().childNodes(svg)[0];
const p = getDOM().childNodes(foreignObject)[0];
const svg = el.childNodes[0];
const foreignObject = svg.childNodes[0];
const p = foreignObject.childNodes[0];
expect(getDOM().getProperty(<Element>svg, 'namespaceURI'))
.toEqual('http://www.w3.org/2000/svg');
expect(getDOM().getProperty(<Element>foreignObject, 'namespaceURI'))
@ -2079,7 +2079,7 @@ function declareTests(config?: {useJit: boolean}) {
TestBed.overrideComponent(SomeCmp, {set: {template}});
const fixture = TestBed.createComponent(SomeCmp);
const useEl = getDOM().firstChild(fixture.nativeElement) as Element;
const useEl = fixture.nativeElement.firstChild;
expect(useEl.getAttributeNS('http://www.w3.org/1999/xlink', 'href')).toEqual('#id');
});
@ -2090,7 +2090,7 @@ function declareTests(config?: {useJit: boolean}) {
const fixture = TestBed.createComponent(SomeCmp);
const cmp = fixture.componentInstance;
const useEl = getDOM().firstChild(fixture.nativeElement) as Element;
const useEl = fixture.nativeElement.firstChild;
cmp.value = '#id';
fixture.detectChanges();

View File

@ -57,7 +57,7 @@ function declareTests(config?: {useJit: boolean}) {
fixture.detectChanges();
const el = fixture.nativeElement;
const children = getDOM().childNodes(el);
const children = el.childNodes;
expect(children.length).toBe(2);
expect(isCommentNode(children[0])).toBe(true);
expect((children[1] as Element).tagName.toUpperCase()).toEqual('P');
@ -73,7 +73,7 @@ function declareTests(config?: {useJit: boolean}) {
fixture.detectChanges();
const el = fixture.nativeElement;
const children = getDOM().childNodes(el);
const children = el.childNodes;
expect(children.length).toBe(5);
expect(isCommentNode(children[0])).toBe(true);
expect(children[1]).toHaveText('1');
@ -92,7 +92,7 @@ function declareTests(config?: {useJit: boolean}) {
fixture.detectChanges();
const el = fixture.nativeElement;
const children = getDOM().childNodes(el);
const children = el.childNodes;
expect(children.length).toBe(4);
// ngIf anchor

View File

@ -502,7 +502,7 @@ describe('projection', () => {
});
const main = TestBed.createComponent(MainComp);
const childNodes = getDOM().childNodes(main.nativeElement);
const childNodes = main.nativeElement.childNodes;
expect(childNodes[0]).toHaveText('div {color: red}SIMPLE1(A)');
expect(childNodes[1]).toHaveText('div {color: blue}SIMPLE2(B)');
main.destroy();
@ -522,7 +522,7 @@ describe('projection', () => {
const main = TestBed.createComponent(MainComp);
const mainEl = main.nativeElement;
const div1 = getDOM().firstChild(mainEl) as Element;
const div1 = mainEl.firstChild;
const div2 = getDOM().createElement('div');
getDOM().setAttribute(div2, 'class', 'redStyle');
getDOM().appendChild(mainEl, div2);
@ -542,7 +542,7 @@ describe('projection', () => {
const main = TestBed.createComponent(MainComp);
const mainEl = main.nativeElement;
const div1 = getDOM().firstChild(mainEl) as Element;
const div1 = mainEl.firstChild;
const div2 = getDOM().createElement('div');
getDOM().appendChild(mainEl, div2);
expect(getComputedStyle(div1).color).toEqual('rgb(255, 0, 0)');

View File

@ -36,7 +36,7 @@ import {compViewDef, createAndGetRootNodes} from './helper';
elementDef(0, NodeFlags.None, null, null, 1, 'div'),
anchorDef(NodeFlags.None, null, null, 0),
])).rootNodes;
expect(getDOM().childNodes(rootNodes[0]).length).toBe(1);
expect(rootNodes[0].childNodes.length).toBe(1);
});
it('should add debug information to the renderer', () => {

View File

@ -42,7 +42,7 @@ const addEventListener = '__zone_symbol__addEventListener' as 'addEventListener'
expect(compView.context).toBe(instance);
expect(compView.component).toBe(instance);
const compRootEl = getDOM().childNodes(rootNodes[0])[0];
const compRootEl = rootNodes[0].childNodes[0];
expect(getDOM().nodeName(compRootEl).toLowerCase()).toBe('span');
});

View File

@ -49,7 +49,7 @@ const removeEventListener = '__zone_symbol__removeEventListener' as 'removeEvent
elementDef(1, NodeFlags.None, null, null, 0, 'span'),
])).rootNodes;
expect(rootNodes.length).toBe(1);
const spanEl = getDOM().childNodes(rootNodes[0])[0];
const spanEl = rootNodes[0].childNodes[0];
expect(getDOM().nodeName(spanEl).toLowerCase()).toBe('span');
});

View File

@ -53,7 +53,7 @@ import {compViewDef, compViewDefFactory, createAndGetRootNodes, createEmbeddedVi
attachEmbeddedView(parentView, viewContainerData, 1, childView1);
// 2 anchors + 2 elements
const rootChildren = getDOM().childNodes(rootNodes[0]);
const rootChildren = rootNodes[0].childNodes;
expect(rootChildren.length).toBe(4);
expect(getDOM().getAttribute(rootChildren[1], 'name')).toBe('child0');
expect(getDOM().getAttribute(rootChildren[2], 'name')).toBe('child1');
@ -63,7 +63,7 @@ import {compViewDef, compViewDefFactory, createAndGetRootNodes, createEmbeddedVi
detachEmbeddedView(viewContainerData, 0);
rf.end !();
expect(getDOM().childNodes(rootNodes[0]).length).toBe(2);
expect(rootNodes[0].childNodes.length).toBe(2);
});
it('should move embedded views', () => {
@ -88,7 +88,7 @@ import {compViewDef, compViewDefFactory, createAndGetRootNodes, createEmbeddedVi
expect(viewContainerData.viewContainer !._embeddedViews).toEqual([childView1, childView0]);
// 2 anchors + 2 elements
const rootChildren = getDOM().childNodes(rootNodes[0]);
const rootChildren = rootNodes[0].childNodes;
expect(rootChildren.length).toBe(4);
expect(getDOM().getAttribute(rootChildren[1], 'name')).toBe('child1');
expect(getDOM().getAttribute(rootChildren[2], 'name')).toBe('child0');

View File

@ -39,7 +39,7 @@ import {compViewDef, compViewDefFactory, createEmbeddedView, createRootView, isB
const {view, rootNodes} = createAndGetRootNodes(
compViewDef(hostElDef(0, [textDef(2, 0, ['a'])], [ngContentDef(null, 0)])));
expect(getDOM().firstChild(rootNodes[0])).toBe(asTextData(view, 2).renderText);
expect(rootNodes[0].firstChild).toBe(asTextData(view, 2).renderText);
});
it('should create views with multiple root ng-content nodes', () => {
@ -47,8 +47,8 @@ import {compViewDef, compViewDefFactory, createEmbeddedView, createRootView, isB
0, [textDef(2, 0, ['a']), textDef(3, 1, ['b'])],
[ngContentDef(null, 0), ngContentDef(null, 1)])));
expect(getDOM().childNodes(rootNodes[0])[0]).toBe(asTextData(view, 2).renderText);
expect(getDOM().childNodes(rootNodes[0])[1]).toBe(asTextData(view, 3).renderText);
expect(rootNodes[0].childNodes[0]).toBe(asTextData(view, 2).renderText);
expect(rootNodes[0].childNodes[1]).toBe(asTextData(view, 3).renderText);
});
it('should create ng-content nodes with parents', () => {
@ -56,8 +56,7 @@ import {compViewDef, compViewDefFactory, createEmbeddedView, createRootView, isB
0, [textDef(2, 0, ['a'])],
[elementDef(0, NodeFlags.None, null, null, 1, 'div'), ngContentDef(null, 0)])));
expect(getDOM().firstChild(getDOM().firstChild(rootNodes[0])))
.toBe(asTextData(view, 2).renderText);
expect(rootNodes[0].firstChild.firstChild).toBe(asTextData(view, 2).renderText);
});
it('should reproject ng-content nodes', () => {
@ -65,8 +64,7 @@ import {compViewDef, compViewDefFactory, createEmbeddedView, createRootView, isB
hostElDef(0, [textDef(2, 0, ['a'])], hostElDef(0, [ngContentDef(0, 0)], [
elementDef(0, NodeFlags.None, null, null, 1, 'span'), ngContentDef(null, 0)
]))));
expect(getDOM().firstChild(getDOM().firstChild(getDOM().firstChild(rootNodes[0]))))
.toBe(asTextData(view, 2).renderText);
expect(rootNodes[0].firstChild.firstChild.firstChild).toBe(asTextData(view, 2).renderText);
});
it('should project already attached embedded views', () => {
@ -94,11 +92,10 @@ import {compViewDef, compViewDefFactory, createEmbeddedView, createRootView, isB
])));
const anchor = asElementData(view, 2);
expect((getDOM().childNodes(getDOM().firstChild(rootNodes[0]))[0]))
.toBe(anchor.renderElement);
const child = rootNodes[0].firstChild;
expect(child.childNodes[0]).toBe(anchor.renderElement);
const embeddedView = anchor.viewContainer !._embeddedViews[0];
expect((getDOM().childNodes(getDOM().firstChild(rootNodes[0]))[1]))
.toBe(asTextData(embeddedView, 0).renderText);
expect(child.childNodes[1]).toBe(asTextData(embeddedView, 0).renderText);
});
it('should include projected nodes when attaching / detaching embedded views', () => {
@ -117,14 +114,15 @@ import {compViewDef, compViewDefFactory, createEmbeddedView, createRootView, isB
const view0 = createEmbeddedView(componentView, componentView.def.nodes[1]);
attachEmbeddedView(view, asElementData(componentView, 1), 0, view0);
expect(getDOM().childNodes(getDOM().firstChild(rootNodes[0])).length).toBe(3);
expect(getDOM().childNodes(getDOM().firstChild(rootNodes[0]))[1])
.toBe(asTextData(view, 2).renderText);
let child = rootNodes[0].firstChild;
expect(child.childNodes.length).toBe(3);
expect(child.childNodes[1]).toBe(asTextData(view, 2).renderText);
rf.begin !();
detachEmbeddedView(asElementData(componentView, 1), 0);
rf.end !();
expect(getDOM().childNodes(getDOM().firstChild(rootNodes[0])).length).toBe(1);
child = rootNodes[0].firstChild;
expect(child.childNodes.length).toBe(1);
});
if (isBrowser()) {
@ -135,8 +133,8 @@ import {compViewDef, compViewDefFactory, createEmbeddedView, createRootView, isB
projectableNodes);
const rootNodes = rootRenderNodes(view);
expect(getDOM().childNodes(rootNodes[0])[0]).toBe(projectableNodes[0][0]);
expect(getDOM().childNodes(rootNodes[0])[1]).toBe(projectableNodes[1][0]);
expect(rootNodes[0].childNodes[0]).toBe(projectableNodes[0][0]);
expect(rootNodes[0].childNodes[1]).toBe(projectableNodes[1][0]);
});
}
});

View File

@ -36,7 +36,7 @@ import {ARG_TYPE_VALUES, checkNodeInlineOrDynamic, compViewDef, createAndGetRoot
textDef(1, null, ['a']),
])).rootNodes;
expect(rootNodes.length).toBe(1);
const textNode = getDOM().firstChild(rootNodes[0]) as Element;
const textNode = rootNodes[0].firstChild;
expect(textNode.textContent).toBe('a');
});

View File

@ -21,7 +21,7 @@ export class DOMTestComponentRenderer extends TestComponentRenderer {
insertRootElement(rootElId: string) {
const template = getDOM().getDefaultDocument().createElement('template');
template.innerHTML = `<div id="${rootElId}"></div>`;
const rootEl = <HTMLElement>getDOM().firstChild(getContent(template));
const rootEl = <HTMLElement>getContent(template).firstChild;
// TODO(juliemr): can/should this be optional?
const oldRoots = getDOM().querySelectorAll(this._doc, '[id^=root]');

View File

@ -142,10 +142,8 @@ export class BrowserDomAdapter extends GenericBrowserDomAdapter {
nodeName(node: Node): string { return node.nodeName; }
nodeValue(node: Node): string|null { return node.nodeValue; }
type(node: HTMLInputElement): string { return node.type; }
firstChild(el: Node): Node|null { return el.firstChild; }
nextSibling(el: Node): Node|null { return el.nextSibling; }
parentElement(el: Node): Node|null { return el.parentNode; }
childNodes(el: any): Node[] { return el.childNodes; }
clearNodes(el: Node) {
while (el.firstChild) {
el.removeChild(el.firstChild);

View File

@ -56,10 +56,8 @@ export abstract class DomAdapter {
abstract nodeName(node: any): string;
abstract nodeValue(node: any): string|null;
abstract type(node: any): string;
abstract firstChild(el: any): Node|null;
abstract nextSibling(el: any): Node|null;
abstract parentElement(el: any): Node|null;
abstract childNodes(el: any): Node[];
// Used by Meta
abstract remove(el: any): Node;

View File

@ -63,7 +63,7 @@ import {el} from '../../../testing/src/browser_util';
// Workaround for https://bugs.webkit.org/show_bug.cgi?id=122755
getDOM().appendChild(doc.body, element);
const child = getDOM().firstChild(element);
const child = element.firstChild;
const dispatchedEvent = getDOM().createMouseEvent('click');
let receivedEvent: any /** TODO #9100 */ = null;
const handler = (e: any /** TODO #9100 */) => { receivedEvent = e; };

View File

@ -95,7 +95,7 @@ export function dispatchEvent(element: any, eventType: any): void {
}
export function el(html: string): HTMLElement {
return <HTMLElement>getDOM().firstChild(getContent(createTemplate(html)));
return <HTMLElement>getContent(createTemplate(html)).firstChild;
}
export function normalizeCSS(css: string): string {
@ -148,7 +148,7 @@ export function stringifyElement(el: any /** TODO #9100 */): string {
// Children
const childrenRoot = templateAwareRoot(el);
const children = childrenRoot ? getDOM().childNodes(childrenRoot) : [];
const children = childrenRoot ? childrenRoot.childNodes : [];
for (let j = 0; j < children.length; j++) {
result += stringifyElement(children[j]);
}

View File

@ -280,7 +280,7 @@ _global.beforeEach(function() {
function elementText(n: any): string {
const hasNodes = (n: any) => {
const children = getDOM().childNodes(n);
const children = n.childNodes;
return children && children.length > 0;
};

View File

@ -65,10 +65,8 @@ export class WorkerDomAdapter extends DomAdapter {
nodeName(node: any): string { throw 'not implemented'; }
nodeValue(node: any): string { throw 'not implemented'; }
type(node: any): string { throw 'not implemented'; }
firstChild(el: any): Node { throw 'not implemented'; }
nextSibling(el: any): Node { throw 'not implemented'; }
parentElement(el: any): Node { throw 'not implemented'; }
childNodes(el: any): Node[] { throw 'not implemented'; }
clearNodes(el: any) { throw 'not implemented'; }
appendChild(el: any, node: any) { throw 'not implemented'; }
removeChild(el: any, node: any) { throw 'not implemented'; }