Skip to content

chore(tests): Unit tests made green again #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion nativescript-angular/platform-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ export class NativeScriptSanitizer extends Sanitizer {
}

// Add a fake polyfill for the document object
(<any>global).document = (<any>global).document || {};
(<any>global).document = (<any>global).document || {
getElementById: () => { return undefined; }
};

const doc = (<any>global).document;
doc.body = Object.assign(doc.body || {}, {
isOverride: true,
Expand Down
13 changes: 8 additions & 5 deletions nativescript-angular/testing/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ export function nsTestBedAfterEach(resetEnv = true, resetFn = nsTestBedInit) {
const root = testingRootView() as LayoutBase;
const fixtures = activeTestFixtures.pop();
fixtures.forEach((fixture) => {
root.removeChild(fixture.nativeElement);
const fixtureView = <View>fixture.nativeElement;
if (fixtureView.parent === root) {
root.removeChild(fixtureView);
}
fixture.destroy();
});
TestBed.resetTestingModule();
Expand All @@ -144,10 +147,10 @@ export function nsTestBedRender<T>(componentType: Type<T>): Promise<ComponentFix
const fixture = TestBed.createComponent(componentType);
fixture.detectChanges();
return fixture.whenRenderingDone()
// TODO(jd): it seems that the whenStable and whenRenderingDone utilities of ComponentFixture
// do not work as expected. I looked at how to fix it and it's not clear how to provide
// a {N} specific subclass, because ComponentFixture is newed directly rather than injected
// What to do about it? Maybe fakeAsync can help? For now just setTimeout for 100ms (x_X)
// TODO(jd): it seems that the whenStable and whenRenderingDone utilities of ComponentFixture
// do not work as expected. I looked at how to fix it and it's not clear how to provide
// a {N} specific subclass, because ComponentFixture is newed directly rather than injected
// What to do about it? Maybe fakeAsync can help? For now just setTimeout for 100ms (x_X)
.then(promiseWait(100))
.then(() => {
const list = activeTestFixtures[activeTestFixtures.length - 1];
Expand Down
8 changes: 4 additions & 4 deletions tests/app/tests/platform-filter-components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe("Platform filter directives", () => {
return nsTestBedRender(IosSpecificComponent).then((fixture) => {
const componentRef = fixture.componentRef;
const componentRoot = componentRef.instance.elementRef.nativeElement;
assert.isTrue(dumpView(componentRoot, true).indexOf("(Label[text=IOS])") >= 0);
assert.isTrue(dumpView(componentRoot, true).indexOf("(label[text=IOS])") >= 0);
});
});
it("does not render android specific content", () => {
Expand All @@ -62,7 +62,7 @@ describe("Platform filter directives", () => {
const componentRef = fixture.componentRef;
const componentRoot = componentRef.instance.elementRef.nativeElement;
assert.equal(
"(ProxyViewContainer (StackLayout (Label[text=IOS])))",
"(proxyviewcontainer (stacklayout (label[text=IOS])))",
dumpView(componentRoot, true));
});
});
Expand All @@ -79,7 +79,7 @@ describe("Platform filter directives", () => {
return nsTestBedRender(AndroidSpecificComponent).then((fixture) => {
const componentRef = fixture.componentRef;
const componentRoot = componentRef.instance.elementRef.nativeElement;
assert.isTrue(dumpView(componentRoot, true).indexOf("(Label[text=ANDROID])") >= 0);
assert.isTrue(dumpView(componentRoot, true).indexOf("(label[text=ANDROID])") >= 0);
});
});
it("does not render ios specific content", () => {
Expand All @@ -94,7 +94,7 @@ describe("Platform filter directives", () => {
const componentRef = fixture.componentRef;
const componentRoot = componentRef.instance.elementRef.nativeElement;
assert.equal(
"(ProxyViewContainer (StackLayout (Label[text=ANDROID])))",
"(proxyviewcontainer (stacklayout (label[text=ANDROID])))",
dumpView(componentRoot, true));
});
});
Expand Down
58 changes: 29 additions & 29 deletions tests/app/tests/renderer-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,15 +262,15 @@ describe("Renderer E2E", () => {
return nsTestBedRender(LayoutWithLabel).then((fixture) => {
const componentRef: ComponentRef<LayoutWithLabel> = fixture.componentRef;
const componentRoot = componentRef.instance.elementRef.nativeElement;
assert.equal("(ProxyViewContainer (StackLayout (Label)))", dumpView(componentRoot));
assert.equal("(proxyviewcontainer (stacklayout (label)))", dumpView(componentRoot));
});
});

it("component without a layout", () => {
return nsTestBedRender(LabelContainer).then((fixture) => {
const componentRef: ComponentRef<LabelContainer> = fixture.componentRef;
const componentRoot = componentRef.instance.elementRef.nativeElement;
assert.equal("(ProxyViewContainer (GridLayout (ProxyViewContainer (Label))))", dumpView(componentRoot));
assert.equal("(proxyviewcontainer (gridlayout (proxyviewcontainer (label))))", dumpView(componentRoot));
});
});

Expand All @@ -279,7 +279,7 @@ describe("Renderer E2E", () => {
const componentRef: ComponentRef<ProjectionContainer> = fixture.componentRef;
const componentRoot = componentRef.instance.elementRef.nativeElement;
assert.equal(
"(ProxyViewContainer (GridLayout (ProxyViewContainer (StackLayout (Button)))))",
"(proxyviewcontainer (gridlayout (proxyviewcontainer (stacklayout (button)))))",
dumpView(componentRoot));
});
});
Expand Down Expand Up @@ -355,7 +355,7 @@ describe("Renderer E2E", () => {
return nsTestBedRender(NgIfLabel).then((fixture) => {
const componentRef: ComponentRef<NgIfLabel> = fixture.componentRef;
const componentRoot = componentRef.instance.elementRef.nativeElement;
assert.equal("(ProxyViewContainer)", dumpView(componentRoot));
assert.equal("(proxyviewcontainer)", dumpView(componentRoot));
});
});

Expand All @@ -367,7 +367,7 @@ describe("Renderer E2E", () => {

component.show = true;
fixture.detectChanges();
assert.equal("(ProxyViewContainer (Label))", dumpView(componentRoot));
assert.equal("(proxyviewcontainer (label))", dumpView(componentRoot));
});
});

Expand All @@ -380,7 +380,7 @@ describe("Renderer E2E", () => {
component.show = true;
fixture.detectChanges();
assert.equal(
"(ProxyViewContainer (StackLayout (Label), (Button)))",
"(proxyviewcontainer (stacklayout (label), (button)))",
dumpView(componentRoot));
});
});
Expand All @@ -394,13 +394,13 @@ describe("Renderer E2E", () => {
component.show = true;
fixture.detectChanges();
assert.equal(
"(ProxyViewContainer " +
"(StackLayout " +
"(Label[text=1]), " +
"(Label[text=2]), " +
"(Label[text=3]), " +
"(Label[text=4]), " + // the content to be conditionally displayed
"(Label[text=5])" +
"(proxyviewcontainer " +
"(stacklayout " +
"(label[text=1]), " +
"(label[text=2]), " +
"(label[text=3]), " +
"(label[text=4]), " + // the content to be conditionally displayed
"(label[text=5])" +
")" +
")",
dumpView(componentRoot, true));
Expand All @@ -416,9 +416,9 @@ describe("Renderer E2E", () => {
fixture.detectChanges();

assert.equal(
"(ProxyViewContainer " +
"(StackLayout " +
"(Label[text=If])" +
"(proxyviewcontainer " +
"(stacklayout " +
"(label[text=If])" +
")" +
")",

Expand All @@ -435,9 +435,9 @@ describe("Renderer E2E", () => {
component.show = false;
fixture.detectChanges();
assert.equal(
"(ProxyViewContainer " +
"(StackLayout " +
"(Label[text=Else])" +
"(proxyviewcontainer " +
"(stacklayout " +
"(label[text=Else])" +
")" +
")",

Expand All @@ -453,9 +453,9 @@ describe("Renderer E2E", () => {

fixture.detectChanges();
assert.equal(
"(ProxyViewContainer " +
"(StackLayout " +
"(Label[text=Then])" +
"(proxyviewcontainer " +
"(stacklayout " +
"(label[text=Then])" +
")" +
")",

Expand All @@ -473,9 +473,9 @@ describe("Renderer E2E", () => {
component.show = false;
fixture.detectChanges();
assert.equal(
"(ProxyViewContainer " +
"(StackLayout " +
"(Label[text=Else])" +
"(proxyviewcontainer " +
"(stacklayout " +
"(label[text=Else])" +
")" +
")",

Expand All @@ -488,7 +488,7 @@ describe("Renderer E2E", () => {
const componentRef: ComponentRef<NgForLabel> = fixture.componentRef;
const componentRoot = componentRef.instance.elementRef.nativeElement;
assert.equal(
"(ProxyViewContainer (Label[text=one]), (Label[text=two]), (Label[text=three]))",
"(proxyviewcontainer (label[text=one]), (label[text=two]), (label[text=three]))",
dumpView(componentRoot, true));
});
});
Expand All @@ -503,7 +503,7 @@ describe("Renderer E2E", () => {
fixture.detectChanges();

assert.equal(
"(ProxyViewContainer (Label[text=one]), (Label[text=three]))",
"(proxyviewcontainer (label[text=one]), (label[text=three]))",
dumpView(componentRoot, true));
});
});
Expand All @@ -518,8 +518,8 @@ describe("Renderer E2E", () => {
fixture.detectChanges();

assert.equal(
"(ProxyViewContainer " +
"(Label[text=one]), (Label[text=new]), (Label[text=two]), (Label[text=three]))",
"(proxyviewcontainer " +
"(label[text=one]), (label[text=new]), (label[text=two]), (label[text=three]))",
dumpView(componentRoot, true));
});
});
Expand Down
24 changes: 3 additions & 21 deletions tests/app/tests/snippets.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,12 @@
// make sure you import mocha-config before @angular/core
import { assert } from "./test-config";

import { NavigationEnd, NavigationStart } from "@angular/router";
import { Subscription } from "rxjs";

import { GestureComponent } from "../snippets/gestures.component";
import { LayoutsComponent } from "../snippets/layouts.component";
import { IconFontComponent } from "../snippets/icon-font.component";

import { PageNavigationApp } from "../snippets/navigation/page-outlet";
import { NavigationApp } from "../snippets/navigation/router-outlet";
import { FirstComponent, SecondComponent } from "../snippets/navigation/navigation-common";
import { routes } from "../snippets/navigation/app.routes";
import {
HeaderComponent,
ItemComponent,
DataService,
ListTemplateSelectorTest,
} from "../snippets/list-view/template-selector.component";

import { device, platformNames, isIOS } from "platform";
import {nsTestBedAfterEach, nsTestBedBeforeEach, nsTestBedRender} from "nativescript-angular/testing";
import {ComponentRef} from "@angular/core";
import { nsTestBedAfterEach, nsTestBedBeforeEach, nsTestBedRender } from "nativescript-angular/testing";
import { ComponentRef } from "@angular/core";

describe("Snippets", () => {

Expand All @@ -44,14 +29,11 @@ describe("Snippets", () => {
});
});

// TODO: Skip list-view test until karma test launcher double navigate bug is fixed
(isIOS ? it.skip : it)("Icon-font snippets can be loaded", (done) => {
it("Icon-font snippets can be loaded", () => {
return nsTestBedRender(IconFontComponent).then((fixture) => {
const componentRef: ComponentRef<IconFontComponent> = fixture.componentRef;
const componentInstance = componentRef.instance;
assert.instanceOf(componentInstance, IconFontComponent);
// Works around a "dehydrated change detector" exception.
setTimeout(done, 10);
});
});
});
6 changes: 4 additions & 2 deletions tests/app/tests/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ function getChildren(view: View): Array<View> {
}

export function dumpView(view: View, verbose: boolean = false): string {
let nodeName = (<any>view).nodeName;
let nodeName: string = (<any>view).nodeName;
if (!nodeName) {
// Strip off the source
nodeName = view.toString().replace(/(@[^;]*;)/g,'');
nodeName = view.toString().replace(/(@[^;]*;)/g, '');
}
nodeName = nodeName.toLocaleLowerCase();

let output = ["(", nodeName];
if (verbose) {
if (view instanceof TextBase) {
Expand Down