Skip to content

Commit

Permalink
fix(app-loader): backwards compatibility get current url, add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
why520crazy committed Oct 11, 2019
1 parent 33ac4f4 commit bd754e4
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,7 @@ import { SwitchModes, PlanetApplication } from '../planet.class';
import { PlanetApplicationService } from './planet-application.service';
import { NgZone } from '@angular/core';
import { PlanetApplicationRef } from './planet-application-ref';

const app1 = {
name: 'app1',
hostParent: '.host-selector',
selector: 'app1-root-container',
routerPathPrefix: '/app1',
hostClass: 'app1-host',
preload: false,
switchMode: SwitchModes.default,
resourcePathPrefix: '/static/app1/',
styles: ['styles/main.css'],
scripts: ['vendor.js', 'main.js'],
loadSerial: false,
manifest: '',
extra: {
appName: '应用1'
}
};

const app2 = {
name: 'app2',
hostParent: '.host-selector',
selector: 'app2-root-container',
routerPathPrefix: '/app2',
hostClass: 'app2-host',
preload: false,
switchMode: SwitchModes.coexist,
resourcePathPrefix: '/static/app2',
styles: ['styles/main.css'],
scripts: ['vendor.js', 'main.js'],
loadSerial: false,
extra: {
appName: '应用2'
}
};
import { app1, app2 } from '../test/applications';

class PlanetApplicationRefFaker {
planetAppRef: PlanetApplicationRef;
Expand Down
7 changes: 5 additions & 2 deletions packages/planet/src/application/planet-application-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,10 @@ export class PlanetApplicationLoader {
of(app).pipe(
tap(() => {
const appRef = getPlanetApplicationRef(app.name);
const currentUrl = appRef.getCurrentRouterStateUrl();
// Backwards compatibility sub app use old version which has not getCurrentRouterStateUrl
const currentUrl = appRef.getCurrentRouterStateUrl
? appRef.getCurrentRouterStateUrl()
: '';
if (currentUrl !== event.url) {
appRef.navigateByUrl(event.url);
}
Expand Down Expand Up @@ -309,7 +312,7 @@ export class PlanetApplicationLoader {
}
}
let result = appRef.bootstrap(this.portalApp);
// Forward compatibility promise for bootstrap
// Backwards compatibility promise for bootstrap
if (result['then']) {
result = from(result) as Observable<PlanetApplicationRef>;
}
Expand Down
17 changes: 0 additions & 17 deletions packages/planet/src/application/planet-application-ref.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,6 @@ class EmptyComponent {}
})
class AppModule {}

const app1 = {
name: 'app1',
hostParent: '.host-selector',
selector: 'app1-root-container',
routerPathPrefix: '/app1',
hostClass: 'app1-host',
preload: false,
resourcePathPrefix: '/static/app1/',
styles: ['styles/main.css'],
scripts: ['vendor.js', 'main.js'],
loadSerial: false,
manifest: '',
extra: {
appName: '应用1'
}
};

describe('PlanetApplicationRef', () => {
afterEach(() => {
// delete all apps
Expand Down
82 changes: 42 additions & 40 deletions packages/planet/src/application/planet-application.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { TestBed } from '@angular/core/testing';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { PlanetApplicationService } from './planet-application.service';
import { SwitchModes } from '../planet.class';
import { HttpClient } from '@angular/common/http';
import { app1, app2, app2WithPreload } from '../test/applications';

describe('PlanetApplicationService', () => {
let planetApplicationService: PlanetApplicationService;
Expand All @@ -13,42 +15,6 @@ describe('PlanetApplicationService', () => {
planetApplicationService = TestBed.get(PlanetApplicationService);
});

const app1 = {
name: 'app1',
hostParent: '.host-selector',
selector: 'app1-root-container',
routerPathPrefix: '/app1',
hostClass: 'app1-host',
preload: false,
switchMode: SwitchModes.coexist,
resourcePathPrefix: '/static/app1/',
styles: ['styles/main.css'],
scripts: ['vendor.js', 'main.js'],
loadSerial: false,
manifest: '/static/app/manifest.json',
extra: {
appName: '应用1'
}
};

const app2 = {
name: 'app2',
hostParent: '.host-selector',
selector: 'app2-root-container',
routerPathPrefix: '/app2',
hostClass: 'app2-host',
preload: true,
switchMode: SwitchModes.coexist,
resourcePathPrefix: '/static/app2',
styles: ['styles/main.css'],
scripts: ['vendor.js', 'main.js'],
loadSerial: false,
manifest: '/static/app/manifest.json',
extra: {
appName: '应用2'
}
};

describe('register', () => {
it('should register signal app1 success', () => {
planetApplicationService.register(app1);
Expand All @@ -66,6 +32,42 @@ describe('PlanetApplicationService', () => {
planetApplicationService.register([app1, app2]);
expect(planetApplicationService.getApps()).toEqual([app1, app2]);
});

describe('registerByUrl', () => {
let httpClient: HttpClient;
let httpTestingController: HttpTestingController;
beforeEach(() => {
httpClient = TestBed.get(HttpClient);
httpTestingController = TestBed.get(HttpTestingController);
});

afterEach(() => {
// After every test, assert that there are no more pending requests.
httpTestingController.verify();
});

it('should register multiple apps by url', () => {
planetApplicationService.registerByUrl('/static/apps.json');
const req = httpTestingController.expectOne('/static/apps.json');

// Assert that the request is a GET.
expect(req.request.method).toEqual('GET');

// Respond with mock data, causing Observable to resolve.
// Subscribe callback asserts that correct data was returned.
req.flush([app1, app2]);

expect(planetApplicationService.getApps()).toEqual([app1, app2]);
});

it('should register one app by url', () => {
planetApplicationService.registerByUrl('/static/apps.json');
const req = httpTestingController.expectOne('/static/apps.json');
expect(req.request.method).toEqual('GET');
req.flush(app1);
expect(planetApplicationService.getApps()).toEqual([app1]);
});
});
});

describe('unregister', () => {
Expand Down Expand Up @@ -110,14 +112,14 @@ describe('PlanetApplicationService', () => {
describe('getAppsToPreload', () => {
it('should get correct preload apps', () => {
planetApplicationService.register(app1);
planetApplicationService.register(app2);
planetApplicationService.register(app2WithPreload);
const appsToPreload = planetApplicationService.getAppsToPreload();
expect(appsToPreload).toEqual([app2]);
expect(appsToPreload).toEqual([app2WithPreload]);
});

it('should get correct preload apps exclude app names', () => {
planetApplicationService.register(app1);
planetApplicationService.register(app2);
planetApplicationService.register(app2WithPreload);
const appsToPreload = planetApplicationService.getAppsToPreload(['app2']);
expect(appsToPreload).toEqual([]);
});
Expand Down
9 changes: 7 additions & 2 deletions packages/planet/src/application/planet-application.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,18 @@ export class PlanetApplicationService {
}

registerByUrl(url: string): Observable<void> {
return this.http.get(`${url}`).pipe(
const result = this.http.get(`${url}`).pipe(
map(apps => {
if (apps && Array.isArray(apps)) {
this.register(apps);
} else {
this.register(apps as PlanetApplication);
}
})
}),
shareReplay()
);
result.subscribe();
return result;
}

unregister(name: string) {
Expand Down
52 changes: 52 additions & 0 deletions packages/planet/src/application/portal-application.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { TestBed } from '@angular/core/testing';
import { RouterModule, Router } from '@angular/router';
import { PlanetPortalApplication } from './portal-application';
import { NgZone, ApplicationRef } from '@angular/core';

describe('PlanetPortalApplication', () => {
let router: Router;
let planetPortalApplication: PlanetPortalApplication;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [RouterModule.forRoot([])]
});
router = TestBed.get(Router);
planetPortalApplication = new PlanetPortalApplication();
planetPortalApplication.ngZone = TestBed.get(NgZone);
planetPortalApplication.applicationRef = TestBed.get(ApplicationRef);
planetPortalApplication.router = TestBed.get(Router);
});

it(`should create PlanetPortalApplication`, () => {
expect(planetPortalApplication).toBeTruthy();
});

it(`should run function`, () => {
const spy = jasmine.createSpy('spy');
expect(spy).not.toHaveBeenCalled();
planetPortalApplication.run(spy);
expect(spy).toHaveBeenCalled();
});

it(`should run function in ngZone`, () => {
const ngZoneRunSpy = spyOn(planetPortalApplication.ngZone, 'run');
expect(ngZoneRunSpy).not.toHaveBeenCalled();
planetPortalApplication.run(() => {});
expect(ngZoneRunSpy).toHaveBeenCalled();
});

it(`should run tick`, () => {
const tickSpy = spyOn(planetPortalApplication.applicationRef, 'tick');
expect(tickSpy).not.toHaveBeenCalled();
planetPortalApplication.tick();
expect(tickSpy).toHaveBeenCalled();
});

it(`should run navigateByUrl`, () => {
const navigateByUrlSpy = spyOn(planetPortalApplication.router, 'navigateByUrl');
expect(navigateByUrlSpy).not.toHaveBeenCalled();
planetPortalApplication.navigateByUrl('/app1/dashboard', { skipLocationChange: true });
expect(navigateByUrlSpy).toHaveBeenCalled();
expect(navigateByUrlSpy).toHaveBeenCalledWith('/app1/dashboard', { skipLocationChange: true });
});
});
57 changes: 57 additions & 0 deletions packages/planet/src/test/applications.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { SwitchModes } from '../planet.class';
const app1 = {
name: 'app1',
hostParent: '.host-selector',
selector: 'app1-root-container',
routerPathPrefix: '/app1',
hostClass: 'app1-host',
preload: false,
switchMode: SwitchModes.default,
resourcePathPrefix: '/static/app1/',
styles: ['styles/main.css'],
scripts: ['vendor.js', 'main.js'],
loadSerial: false,
manifest: '',
extra: {
appName: '应用1'
}
};

const app1WithManifest = {
...app1,
manifest: '/static/app/manifest.json'
};

const app1WithPreload = {
...app1,
preload: true
};

const app2 = {
name: 'app2',
hostParent: '.host-selector',
selector: 'app2-root-container',
routerPathPrefix: '/app2',
hostClass: 'app2-host',
preload: false,
switchMode: SwitchModes.coexist,
resourcePathPrefix: '/static/app2',
styles: ['styles/main.css'],
scripts: ['vendor.js', 'main.js'],
loadSerial: false,
extra: {
appName: '应用2'
}
};

const app2WithManifest = {
...app2,
manifest: '/static/app/manifest.json'
};

const app2WithPreload = {
...app2,
preload: true
};

export { app1, app1WithManifest, app1WithPreload, app2, app2WithManifest, app2WithPreload };

0 comments on commit bd754e4

Please sign in to comment.