Skip to content

Commit

Permalink
test: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
edusperoni committed Jul 17, 2024
1 parent c49fb86 commit c1edcba
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 80 deletions.
2 changes: 1 addition & 1 deletion apps/nativescript-demo-ng/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"extends": "../../.eslintrc.json",
"ignorePatterns": ["!**/*", "node_modules/**/*"],
"ignorePatterns": ["!**/*", "node_modules/**/*", "platforms/**/*"],
"overrides": [
{
"files": ["*.ts"],
Expand Down
4 changes: 2 additions & 2 deletions apps/nativescript-demo-ng/src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Component, ViewContainerRef } from '@angular/core';
import { Component, ViewContainerRef, OnInit, OnDestroy } from '@angular/core';

// registerElement('ns-app', () => GridLayout);
@Component({
selector: 'ns-app',
moduleId: module.id,
templateUrl: './app.component.html',
})
export class AppComponent {
export class AppComponent implements OnInit, OnDestroy {
constructor(private vcRef: ViewContainerRef) {}
ngOnInit() {
console.log('ngOnInit');
Expand Down
6 changes: 3 additions & 3 deletions apps/nativescript-demo-ng/src/app/home/home.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, NgZone } from '@angular/core';
import { Component, NgZone, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { RouterExtensions } from '@nativescript/angular';
import { Page, TabView } from '@nativescript/core';
Expand All @@ -8,7 +8,7 @@ import { Page, TabView } from '@nativescript/core';
selector: 'demo-home',
templateUrl: './home.component.html',
})
export class HomeComponent {
export class HomeComponent implements OnInit {
tabItems: { [key: string]: { index: number; title?: string; iconSource?: string; textTransform?: string } } = {};
private _tabs = ['start'];
private _hasInitTab: { start?: boolean } = {};
Expand All @@ -20,7 +20,7 @@ export class HomeComponent {
private _activeRoute: ActivatedRoute,
private _page: Page,
private _ngRouter: Router,
private _router: RouterExtensions
private _router: RouterExtensions,
) {
this._initMenu();
}
Expand Down
11 changes: 8 additions & 3 deletions apps/nativescript-demo-ng/src/app/item/items.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, OnInit } from '@angular/core';
import { Component, OnInit, OnDestroy } from '@angular/core';
import { HttpClient } from '@angular/common/http';

import { Item } from './item';
Expand All @@ -11,11 +11,16 @@ import { ModalDialogService, NativeDialogService } from '@nativescript/angular';
moduleId: module.id,
templateUrl: './items.component.html',
})
export class ItemsComponent implements OnInit {
export class ItemsComponent implements OnInit, OnDestroy {
message = 'Hello Angular 18!';
items: Array<Item>;

constructor(private itemService: ItemService, private nativeDialog: NativeDialogService, private modalDialog: ModalDialogService, private http: HttpClient) {}
constructor(
private itemService: ItemService,
private nativeDialog: NativeDialogService,
private modalDialog: ModalDialogService,
private http: HttpClient,
) {}

ngOnInit(): void {
console.log('ItemsComponent ngOnInit');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, OnInit } from '@angular/core';
import { Component, OnInit, OnDestroy } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { RouterExtensions } from '@nativescript/angular';

Expand All @@ -10,10 +10,14 @@ import { ItemService } from '../item/item.service';
moduleId: module.id,
templateUrl: './item-detail2.component.html',
})
export class ItemDetailComponent implements OnInit {
export class ItemDetailComponent implements OnInit, OnDestroy {
item: Item;

constructor(private itemService: ItemService, private route: ActivatedRoute, private router: RouterExtensions) {
constructor(
private itemService: ItemService,
private route: ActivatedRoute,
private router: RouterExtensions,
) {
console.log('ItemDetail2Component construct');
}

Expand Down
15 changes: 10 additions & 5 deletions apps/nativescript-demo-ng/src/app/item3/items.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, OnInit } from '@angular/core';
import { Component, OnInit, OnDestroy } from '@angular/core';
import { HttpClient } from '@angular/common/http';

import { Item } from '../item/item';
Expand All @@ -11,13 +11,18 @@ import { ModalDialogService, NativeDialogService } from '@nativescript/angular';
moduleId: module.id,
templateUrl: './items.component.html',
})
export class ItemsComponent implements OnInit {
export class ItemsComponent implements OnInit, OnDestroy {
message = 'Hello Angular 18';
items: Array<Item>;
borderRadius: number;
fontSize: number;

constructor(private itemService: ItemService, private nativeDialog: NativeDialogService, private modalDialog: ModalDialogService, private http: HttpClient) {
constructor(
private itemService: ItemService,
private nativeDialog: NativeDialogService,
private modalDialog: ModalDialogService,
private http: HttpClient,
) {
if (global.isAndroid) {
this.borderRadius = 25;
this.fontSize = 15;
Expand All @@ -40,8 +45,8 @@ export class ItemsComponent implements OnInit {
openModal() {
const ref = this.nativeDialog.open(ModalComponent, {
nativeOptions: {
fullscreen: !!global.isAndroid
}
fullscreen: !!global.isAndroid,
},
});
ref.afterOpened().subscribe(() => console.log('after openend'));
ref.beforeClosed().subscribe((result) => console.log('beforeClosed', result));
Expand Down
2 changes: 1 addition & 1 deletion apps/nativescript-demo-ng/src/main.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import './polyfills';
import 'zone.js/dist/zone-testing.js';
import 'zone.js/testing';
import { TestBed } from '@angular/core/testing';
import { platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing';
import { NativeScriptTestingModule } from '@nativescript/angular/testing';
Expand Down
120 changes: 58 additions & 62 deletions apps/nativescript-demo-ng/src/tests/modal-dialog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { NgModule, Component, ViewContainerRef, NO_ERRORS_SCHEMA } from '@angula
import { Page, Frame, isIOS } from '@nativescript/core';
import { ModalDialogParams, ModalDialogService } from '@nativescript/angular';

import { ComponentFixture, TestBed, async, waitForAsync } from '@angular/core/testing';
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { nsTestBedRender, nsTestBedAfterEach, nsTestBedBeforeEach, NATIVESCRIPT_TESTING_PROVIDERS, NativeScriptTestingModule } from '@nativescript/angular/testing';
import { NSLocationStrategy, Outlet } from '@nativescript/angular';
import { FrameService } from '@nativescript/angular';
Expand Down Expand Up @@ -45,7 +45,12 @@ export class FailComponent {
</GridLayout>`,
})
export class SuccessComponent {
constructor(public service: ModalDialogService, public vcRef: ViewContainerRef, public locationStrategy: NSLocationStrategy, public fakeFrameService: FrameService) {}
constructor(
public service: ModalDialogService,
public vcRef: ViewContainerRef,
public locationStrategy: NSLocationStrategy,
public fakeFrameService: FrameService,
) {}
}

@NgModule({
Expand Down Expand Up @@ -89,64 +94,55 @@ describe('modal-dialog', () => {
}
});

it(
'showModal does not throws when there is no viewContainer provided',
waitForAsync(async () => {
const fixture = TestBed.createComponent(FailComponent);
const service = <ModalDialogService>fixture.componentRef.instance.service;
await fixture.whenRenderingDone();
// expect(() => service.showModal(ModalComponent, {})).toThrow("No viewContainerRef: Make sure you pass viewContainerRef in ModalDialogOptions.");
expect(() => service.showModal(ModalComponent, {})).not.toThrow();
})
);

it(
'showModal succeeds when there is viewContainer provided',
waitForAsync(async () => {
const fixture = TestBed.createComponent(SuccessComponent);
const service = fixture.componentRef.instance.service;
const locStrategy = fixture.componentRef.instance.locationStrategy;
await fixture.whenRenderingDone();
const outlet = new Outlet('primary', null, 'primary', 0);
let parentView = fixture.componentRef.instance.vcRef.element.nativeElement;
parentView = parentView.page && parentView.page.frame;
outlet.frames.push(parentView);
locStrategy._getOutlets().push(outlet);

locStrategy.pushState(null, 'test', '/test', null);

const comp = fixture.componentRef.instance;
service.showModal(ModalComponent, { viewContainerRef: comp.vcRef }).catch((e) => fail(e));
})
);

it(
'showModal passes modal params and gets result when resolved',
waitForAsync(async () => {
const context = { property: 'my context' };
const fixture = TestBed.createComponent(SuccessComponent);

const service = <ModalDialogService>fixture.componentRef.instance.service;
const locStrategy = <NSLocationStrategy>fixture.componentRef.instance.locationStrategy;
const outlet = new Outlet('primary', null, 'primary', 0);

let parentView = fixture.componentRef.instance.vcRef.element.nativeElement;
parentView = parentView.page && parentView.page.frame;
outlet.frames.push(parentView);
locStrategy._getOutlets().push(outlet);

locStrategy.pushState(null, 'test', '/test', null);

const comp = <SuccessComponent>fixture.componentRef.instance;
service
.showModal(ModalComponent, {
viewContainerRef: comp.vcRef,
context: context,
})
.then((res) => {
expect(res).toEqual(context);
})
.catch((e) => fail(e));
})
);
it('showModal does not throws when there is no viewContainer provided', waitForAsync(async () => {
const fixture = TestBed.createComponent(FailComponent);
const service = <ModalDialogService>fixture.componentRef.instance.service;
await fixture.whenRenderingDone();
// expect(() => service.showModal(ModalComponent, {})).toThrow("No viewContainerRef: Make sure you pass viewContainerRef in ModalDialogOptions.");
expect(() => service.showModal(ModalComponent, {})).not.toThrow();
}));

it('showModal succeeds when there is viewContainer provided', waitForAsync(async () => {
const fixture = TestBed.createComponent(SuccessComponent);
const service = fixture.componentRef.instance.service;
const locStrategy = fixture.componentRef.instance.locationStrategy;
await fixture.whenRenderingDone();
const outlet = new Outlet('primary', null, 'primary', 0);
let parentView = fixture.componentRef.instance.vcRef.element.nativeElement;
parentView = parentView.page && parentView.page.frame;
outlet.frames.push(parentView);
locStrategy._getOutlets().push(outlet);

locStrategy.pushState(null, 'test', '/test', null);

const comp = fixture.componentRef.instance;
service.showModal(ModalComponent, { viewContainerRef: comp.vcRef }).catch((e) => fail(e));
}));

it('showModal passes modal params and gets result when resolved', waitForAsync(async () => {
const context = { property: 'my context' };
const fixture = TestBed.createComponent(SuccessComponent);

const service = <ModalDialogService>fixture.componentRef.instance.service;
const locStrategy = <NSLocationStrategy>fixture.componentRef.instance.locationStrategy;
const outlet = new Outlet('primary', null, 'primary', 0);

let parentView = fixture.componentRef.instance.vcRef.element.nativeElement;
parentView = parentView.page && parentView.page.frame;
outlet.frames.push(parentView);
locStrategy._getOutlets().push(outlet);

locStrategy.pushState(null, 'test', '/test', null);

const comp = <SuccessComponent>fixture.componentRef.instance;
service
.showModal(ModalComponent, {
viewContainerRef: comp.vcRef,
context: context,
})
.then((res) => {
expect(res).toEqual(context);
})
.catch((e) => fail(e));
}));
});

0 comments on commit c1edcba

Please sign in to comment.