From f2dd1e6d80586018438d88204221e8e74edcc456 Mon Sep 17 00:00:00 2001 From: alfredo-dotcms <37185433+alfredo-dotcms@users.noreply.github.com> Date: Mon, 5 Oct 2020 15:18:06 -0600 Subject: [PATCH] issue-19211-Apps-Move-fetch-data-to-resolver (#1445) * core/dotCMS#19211 fix * Feedback --- .../dot-apps-list-resolver.service.spec.ts | 21 ++++++--- .../dot-apps-list-resolver.service.ts | 31 +++++++++++-- .../dot-apps-list.component.spec.ts | 46 ++++++++++++------- .../dot-apps-list/dot-apps-list.component.ts | 29 +++++------- .../dot-apps/dot-apps-routing.module.ts | 2 +- .../shared/models/dot-apps/dot-apps.model.ts | 5 ++ 6 files changed, 89 insertions(+), 45 deletions(-) diff --git a/src/app/portlets/dot-apps/dot-apps-list/dot-apps-list-resolver.service.spec.ts b/src/app/portlets/dot-apps/dot-apps-list/dot-apps-list-resolver.service.spec.ts index 99d0653a41..8d1fb9b76d 100644 --- a/src/app/portlets/dot-apps/dot-apps-list/dot-apps-list-resolver.service.spec.ts +++ b/src/app/portlets/dot-apps/dot-apps-list/dot-apps-list-resolver.service.spec.ts @@ -1,8 +1,10 @@ -import { of as observableOf } from 'rxjs'; -import { async, TestBed } from '@angular/core/testing'; +import { of as observableOf, of } from 'rxjs'; +import { TestBed } from '@angular/core/testing'; import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router'; import { DotAppsListResolver } from './dot-apps-list-resolver.service'; import { DotLicenseService } from '@services/dot-license/dot-license.service'; +import { DotAppsService } from '@services/dot-apps/dot-apps.service'; +import { appsResponse, AppsServicesMock } from './dot-apps-list.component.spec'; class DotLicenseServicesMock { canAccessEnterprisePortlet(_url: string) {} @@ -20,30 +22,37 @@ routerStateSnapshotMock.url = '/apps'; describe('DotAppsListResolver', () => { let dotLicenseServices: DotLicenseService; + let dotAppsService: DotAppsService; let dotAppsListResolver: DotAppsListResolver; - beforeEach(async(() => { + beforeEach(() => { const testbed = TestBed.configureTestingModule({ providers: [ DotAppsListResolver, { provide: DotLicenseService, useClass: DotLicenseServicesMock }, + { provide: DotAppsService, useClass: AppsServicesMock }, { provide: ActivatedRouteSnapshot, useValue: activatedRouteSnapshotMock } ] }); + dotAppsService = testbed.get(DotAppsService); dotLicenseServices = testbed.get(DotLicenseService); dotAppsListResolver = testbed.get(DotAppsListResolver); - })); + }); it('should get if portlet can be accessed', () => { spyOn(dotLicenseServices, 'canAccessEnterprisePortlet').and.returnValue(observableOf(true)); + spyOn(dotAppsService, 'get').and.returnValue(of(appsResponse)); dotAppsListResolver .resolve(activatedRouteSnapshotMock, routerStateSnapshotMock) - .subscribe((canAccess: any) => { - expect(canAccess).toEqual(true); + .subscribe((resolverData: any) => { + expect(resolverData).toEqual({ + apps: appsResponse, + isEnterpriseLicense: true + }); }); expect(dotLicenseServices.canAccessEnterprisePortlet).toHaveBeenCalledWith('/apps'); }); diff --git a/src/app/portlets/dot-apps/dot-apps-list/dot-apps-list-resolver.service.ts b/src/app/portlets/dot-apps/dot-apps-list/dot-apps-list-resolver.service.ts index 509b3f180a..811f289d7e 100644 --- a/src/app/portlets/dot-apps/dot-apps-list/dot-apps-list-resolver.service.ts +++ b/src/app/portlets/dot-apps/dot-apps-list/dot-apps-list-resolver.service.ts @@ -2,7 +2,9 @@ import { Observable } from 'rxjs'; import { Injectable } from '@angular/core'; import { Resolve, RouterStateSnapshot, ActivatedRouteSnapshot } from '@angular/router'; import { DotLicenseService } from '@services/dot-license/dot-license.service'; -import { take } from 'rxjs/operators'; +import { map, mergeMap, take } from 'rxjs/operators'; +import { DotApps, DotAppsListResolverData } from '@shared/models/dot-apps/dot-apps.model'; +import { DotAppsService } from '@services/dot-apps/dot-apps.service'; /** * Returns apps list from the system @@ -12,10 +14,29 @@ import { take } from 'rxjs/operators'; * @implements {Resolve} */ @Injectable() -export class DotAppsListResolver implements Resolve { - constructor(private dotLicenseService: DotLicenseService) {} +export class DotAppsListResolver implements Resolve { + constructor( + private dotLicenseService: DotLicenseService, + private dotAppsService: DotAppsService + ) {} - resolve(_route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable { - return this.dotLicenseService.canAccessEnterprisePortlet(state.url).pipe(take(1)); + resolve( + _route: ActivatedRouteSnapshot, + state: RouterStateSnapshot + ): Observable { + return this.dotLicenseService.canAccessEnterprisePortlet(state.url).pipe( + take(1), + mergeMap((enterpriseLicense: boolean) => { + return this.dotAppsService.get().pipe( + take(1), + map((apps: DotApps[]) => { + return { + isEnterpriseLicense: enterpriseLicense, + apps: apps + }; + }) + ); + }) + ); } } diff --git a/src/app/portlets/dot-apps/dot-apps-list/dot-apps-list.component.spec.ts b/src/app/portlets/dot-apps/dot-apps-list/dot-apps-list.component.spec.ts index cfcfb5c6b4..7085b2e3ee 100644 --- a/src/app/portlets/dot-apps/dot-apps-list/dot-apps-list.component.spec.ts +++ b/src/app/portlets/dot-apps/dot-apps-list/dot-apps-list.component.spec.ts @@ -1,10 +1,9 @@ import { of } from 'rxjs'; -import { async, ComponentFixture } from '@angular/core/testing'; +import { ComponentFixture, TestBed } from '@angular/core/testing'; import { RouterTestingModule } from '@angular/router/testing'; import { MockDotMessageService } from '@tests/dot-message-service.mock'; import { DotMessageService } from '@services/dot-message/dot-messages.service'; import { ActivatedRoute } from '@angular/router'; -import { DOTTestBed } from '@tests/dot-test-bed'; import { DotAppsListComponent } from './dot-apps-list.component'; import { InputTextModule } from 'primeng/primeng'; import { DotAppsListResolver } from './dot-apps-list-resolver.service'; @@ -15,12 +14,16 @@ import { By } from '@angular/platform-browser'; import { DotAppsCardComponent } from './dot-apps-card/dot-apps-card.component'; import { DotAppsService } from '@services/dot-apps/dot-apps.service'; import { NotLicensedModule } from '@components/not-licensed/not-licensed.module'; +import { DotPipesModule } from '@pipes/dot-pipes.module'; +import { DotLicenseService } from '@services/dot-license/dot-license.service'; +import { CoreWebService } from 'dotcms-js'; +import { CoreWebServiceMock } from 'projects/dotcms-js/src/lib/core/core-web.service.mock'; -class AppsServicesMock { +export class AppsServicesMock { get() {} } -const appsResponse = [ +export const appsResponse = [ { allowExtraParams: true, configurationsCount: 0, @@ -39,7 +42,12 @@ const appsResponse = [ } ]; -let canAccessPortletResponse = { canAccessPortlet: true }; +let canAccessPortletResponse = { + dotAppsListResolverData: { + apps: appsResponse, + isEnterpriseLicense: true + } +}; class ActivatedRouteMock { get data() { @@ -57,8 +65,8 @@ describe('DotAppsListComponent', () => { 'apps.search.placeholder': 'Search' }); - beforeEach(async(() => { - DOTTestBed.configureTestingModule({ + beforeEach(() => { + TestBed.configureTestingModule({ imports: [ RouterTestingModule.withRoutes([ { @@ -68,11 +76,13 @@ describe('DotAppsListComponent', () => { ]), DotAppsCardModule, InputTextModule, - NotLicensedModule + NotLicensedModule, + DotPipesModule ], declarations: [DotAppsListComponent], providers: [ { provide: DotMessageService, useValue: messageServiceMock }, + { provide: CoreWebService, useClass: CoreWebServiceMock }, { provide: ActivatedRoute, useClass: ActivatedRouteMock @@ -82,16 +92,15 @@ describe('DotAppsListComponent', () => { useClass: MockDotRouterService }, { provide: DotAppsService, useClass: AppsServicesMock }, - DotAppsListResolver + DotAppsListResolver, + DotLicenseService ] - }); - })); + }).compileComponents(); - beforeEach(() => { - fixture = DOTTestBed.createComponent(DotAppsListComponent); + fixture = TestBed.createComponent(DotAppsListComponent); component = fixture.debugElement.componentInstance; - routerService = fixture.debugElement.injector.get(DotRouterService); - dotAppsService = fixture.debugElement.injector.get(DotAppsService); + routerService = TestBed.get(DotRouterService); + dotAppsService = TestBed.get(DotAppsService); }); describe('With access to portlet', () => { @@ -132,7 +141,12 @@ describe('DotAppsListComponent', () => { describe('Without access to portlet', () => { beforeEach(() => { - canAccessPortletResponse = { canAccessPortlet: false }; + canAccessPortletResponse = { + dotAppsListResolverData: { + apps: null, + isEnterpriseLicense: false + } + }; fixture.detectChanges(); }); diff --git a/src/app/portlets/dot-apps/dot-apps-list/dot-apps-list.component.ts b/src/app/portlets/dot-apps/dot-apps-list/dot-apps-list.component.ts index 10e6123f4f..9d9e5d1fc6 100644 --- a/src/app/portlets/dot-apps/dot-apps-list/dot-apps-list.component.ts +++ b/src/app/portlets/dot-apps/dot-apps-list/dot-apps-list.component.ts @@ -1,7 +1,7 @@ import { Component, OnInit, ViewChild, ElementRef, OnDestroy } from '@angular/core'; import { debounceTime, pluck, takeUntil } from 'rxjs/operators'; import { fromEvent as observableFromEvent, Subject } from 'rxjs'; -import { DotApps } from '@shared/models/dot-apps/dot-apps.model'; +import { DotApps, DotAppsListResolverData } from '@shared/models/dot-apps/dot-apps.model'; import * as _ from 'lodash'; import { DotRouterService } from '@services/dot-router/dot-router.service'; import { ActivatedRoute } from '@angular/router'; @@ -29,12 +29,12 @@ export class DotAppsListComponent implements OnInit, OnDestroy { ngOnInit() { this.route.data - .pipe(pluck('canAccessPortlet'), takeUntil(this.destroy$)) - .subscribe((canAccessPortlet: boolean) => { - if (canAccessPortlet) { - this.getApps(); + .pipe(pluck('dotAppsListResolverData'), takeUntil(this.destroy$)) + .subscribe((resolverData: DotAppsListResolverData) => { + if (resolverData.isEnterpriseLicense) { + this.getApps(resolverData.apps); } - this.canAccessPortlet = canAccessPortlet; + this.canAccessPortlet = resolverData.isEnterpriseLicense; }); } @@ -53,17 +53,12 @@ export class DotAppsListComponent implements OnInit, OnDestroy { this.dotRouterService.goToAppsConfiguration(key); } - private getApps(): void { - this.dotAppsService - .get() - .pipe(takeUntil(this.destroy$)) - .subscribe((apps: DotApps[]) => { - this.apps = apps; - this.appsCopy = _.cloneDeep(apps); - setTimeout(() => { - this.attachFilterEvents(); - }, 0); - }); + private getApps(apps: DotApps[]): void { + this.apps = apps; + this.appsCopy = _.cloneDeep(apps); + setTimeout(() => { + this.attachFilterEvents(); + }, 0); } private attachFilterEvents(): void { diff --git a/src/app/portlets/dot-apps/dot-apps-routing.module.ts b/src/app/portlets/dot-apps/dot-apps-routing.module.ts index 769e10d097..3cc86bf127 100644 --- a/src/app/portlets/dot-apps/dot-apps-routing.module.ts +++ b/src/app/portlets/dot-apps/dot-apps-routing.module.ts @@ -33,7 +33,7 @@ const routes: Routes = [ path: '', component: DotAppsListComponent, resolve: { - canAccessPortlet: DotAppsListResolver + dotAppsListResolverData: DotAppsListResolver } } ]; diff --git a/src/app/shared/models/dot-apps/dot-apps.model.ts b/src/app/shared/models/dot-apps/dot-apps.model.ts index 4cf80efb95..b073291759 100644 --- a/src/app/shared/models/dot-apps/dot-apps.model.ts +++ b/src/app/shared/models/dot-apps/dot-apps.model.ts @@ -38,3 +38,8 @@ export interface DotAppsSaveData { value: string; }; } + +export interface DotAppsListResolverData { + apps: DotApps[]; + isEnterpriseLicense: boolean; +}