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');
});