From c1edcba74e7750322735fdfe58d931c5ff6b8c3d Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Wed, 17 Jul 2024 12:37:40 -0300 Subject: [PATCH] test: fix tests --- apps/nativescript-demo-ng/.eslintrc.json | 2 +- .../src/app/app.component.ts | 4 +- .../src/app/home/home.component.ts | 6 +- .../src/app/item/items.component.ts | 11 +- .../src/app/item2/item-detail2.component.ts | 10 +- .../src/app/item3/items.component.ts | 15 ++- apps/nativescript-demo-ng/src/main.spec.ts | 2 +- .../src/tests/modal-dialog.spec.ts | 120 +++++++++--------- 8 files changed, 90 insertions(+), 80 deletions(-) diff --git a/apps/nativescript-demo-ng/.eslintrc.json b/apps/nativescript-demo-ng/.eslintrc.json index d2408a4..95d7d1e 100644 --- a/apps/nativescript-demo-ng/.eslintrc.json +++ b/apps/nativescript-demo-ng/.eslintrc.json @@ -1,6 +1,6 @@ { "extends": "../../.eslintrc.json", - "ignorePatterns": ["!**/*", "node_modules/**/*"], + "ignorePatterns": ["!**/*", "node_modules/**/*", "platforms/**/*"], "overrides": [ { "files": ["*.ts"], diff --git a/apps/nativescript-demo-ng/src/app/app.component.ts b/apps/nativescript-demo-ng/src/app/app.component.ts index b1310de..b4bb4ed 100644 --- a/apps/nativescript-demo-ng/src/app/app.component.ts +++ b/apps/nativescript-demo-ng/src/app/app.component.ts @@ -1,4 +1,4 @@ -import { Component, ViewContainerRef } from '@angular/core'; +import { Component, ViewContainerRef, OnInit, OnDestroy } from '@angular/core'; // registerElement('ns-app', () => GridLayout); @Component({ @@ -6,7 +6,7 @@ import { Component, ViewContainerRef } from '@angular/core'; moduleId: module.id, templateUrl: './app.component.html', }) -export class AppComponent { +export class AppComponent implements OnInit, OnDestroy { constructor(private vcRef: ViewContainerRef) {} ngOnInit() { console.log('ngOnInit'); diff --git a/apps/nativescript-demo-ng/src/app/home/home.component.ts b/apps/nativescript-demo-ng/src/app/home/home.component.ts index e441056..48c9728 100644 --- a/apps/nativescript-demo-ng/src/app/home/home.component.ts +++ b/apps/nativescript-demo-ng/src/app/home/home.component.ts @@ -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'; @@ -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 } = {}; @@ -20,7 +20,7 @@ export class HomeComponent { private _activeRoute: ActivatedRoute, private _page: Page, private _ngRouter: Router, - private _router: RouterExtensions + private _router: RouterExtensions, ) { this._initMenu(); } diff --git a/apps/nativescript-demo-ng/src/app/item/items.component.ts b/apps/nativescript-demo-ng/src/app/item/items.component.ts index 3c7824d..69ac276 100644 --- a/apps/nativescript-demo-ng/src/app/item/items.component.ts +++ b/apps/nativescript-demo-ng/src/app/item/items.component.ts @@ -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'; @@ -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; - 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'); diff --git a/apps/nativescript-demo-ng/src/app/item2/item-detail2.component.ts b/apps/nativescript-demo-ng/src/app/item2/item-detail2.component.ts index e72abf3..fee6010 100644 --- a/apps/nativescript-demo-ng/src/app/item2/item-detail2.component.ts +++ b/apps/nativescript-demo-ng/src/app/item2/item-detail2.component.ts @@ -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'; @@ -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'); } diff --git a/apps/nativescript-demo-ng/src/app/item3/items.component.ts b/apps/nativescript-demo-ng/src/app/item3/items.component.ts index dac24ea..a414985 100644 --- a/apps/nativescript-demo-ng/src/app/item3/items.component.ts +++ b/apps/nativescript-demo-ng/src/app/item3/items.component.ts @@ -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'; @@ -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; 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; @@ -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)); diff --git a/apps/nativescript-demo-ng/src/main.spec.ts b/apps/nativescript-demo-ng/src/main.spec.ts index af8fee9..c5528fa 100644 --- a/apps/nativescript-demo-ng/src/main.spec.ts +++ b/apps/nativescript-demo-ng/src/main.spec.ts @@ -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'; diff --git a/apps/nativescript-demo-ng/src/tests/modal-dialog.spec.ts b/apps/nativescript-demo-ng/src/tests/modal-dialog.spec.ts index 36644ac..92fdbf3 100644 --- a/apps/nativescript-demo-ng/src/tests/modal-dialog.spec.ts +++ b/apps/nativescript-demo-ng/src/tests/modal-dialog.spec.ts @@ -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'; @@ -45,7 +45,12 @@ export class FailComponent { `, }) 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({ @@ -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 = 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 = fixture.componentRef.instance.service; - const locStrategy = 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 = 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 = 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 = fixture.componentRef.instance.service; + const locStrategy = 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 = fixture.componentRef.instance; + service + .showModal(ModalComponent, { + viewContainerRef: comp.vcRef, + context: context, + }) + .then((res) => { + expect(res).toEqual(context); + }) + .catch((e) => fail(e)); + })); });