Skip to content
This repository has been archived by the owner on Dec 16, 2024. It is now read-only.

Commit

Permalink
issue-19211-Apps-Move-fetch-data-to-resolver (#1445)
Browse files Browse the repository at this point in the history
* core/dotCMS#19211 fix

* Feedback
  • Loading branch information
alfredo-dotcms authored Oct 5, 2020
1 parent d46f68f commit f2dd1e6
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -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) {}
Expand All @@ -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');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -12,10 +14,29 @@ import { take } from 'rxjs/operators';
* @implements {Resolve<DotApps[]>}
*/
@Injectable()
export class DotAppsListResolver implements Resolve<boolean> {
constructor(private dotLicenseService: DotLicenseService) {}
export class DotAppsListResolver implements Resolve<DotAppsListResolverData> {
constructor(
private dotLicenseService: DotLicenseService,
private dotAppsService: DotAppsService
) {}

resolve(_route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
return this.dotLicenseService.canAccessEnterprisePortlet(state.url).pipe(take(1));
resolve(
_route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Observable<DotAppsListResolverData> {
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
};
})
);
})
);
}
}
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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,
Expand All @@ -39,7 +42,12 @@ const appsResponse = [
}
];

let canAccessPortletResponse = { canAccessPortlet: true };
let canAccessPortletResponse = {
dotAppsListResolverData: {
apps: appsResponse,
isEnterpriseLicense: true
}
};

class ActivatedRouteMock {
get data() {
Expand All @@ -57,8 +65,8 @@ describe('DotAppsListComponent', () => {
'apps.search.placeholder': 'Search'
});

beforeEach(async(() => {
DOTTestBed.configureTestingModule({
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
RouterTestingModule.withRoutes([
{
Expand All @@ -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
Expand All @@ -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', () => {
Expand Down Expand Up @@ -132,7 +141,12 @@ describe('DotAppsListComponent', () => {

describe('Without access to portlet', () => {
beforeEach(() => {
canAccessPortletResponse = { canAccessPortlet: false };
canAccessPortletResponse = {
dotAppsListResolverData: {
apps: null,
isEnterpriseLicense: false
}
};
fixture.detectChanges();
});

Expand Down
29 changes: 12 additions & 17 deletions src/app/portlets/dot-apps/dot-apps-list/dot-apps-list.component.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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;
});
}

Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/app/portlets/dot-apps/dot-apps-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const routes: Routes = [
path: '',
component: DotAppsListComponent,
resolve: {
canAccessPortlet: DotAppsListResolver
dotAppsListResolverData: DotAppsListResolver
}
}
];
Expand Down
5 changes: 5 additions & 0 deletions src/app/shared/models/dot-apps/dot-apps.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,8 @@ export interface DotAppsSaveData {
value: string;
};
}

export interface DotAppsListResolverData {
apps: DotApps[];
isEnterpriseLicense: boolean;
}

0 comments on commit f2dd1e6

Please sign in to comment.