-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from worktile/why520crazy/planet-test
test(planet): add some test cases for planet service and module
- Loading branch information
Showing
5 changed files
with
242 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
import { NgxPlanetModule } from './module'; | ||
import { NgModule } from '@angular/core'; | ||
import { PlanetApplicationService } from './application/planet-application.service'; | ||
import { Planet } from './planet'; | ||
import { RouterModule } from '@angular/router'; | ||
|
||
const app1 = { | ||
name: 'app1', | ||
host: '.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' | ||
} | ||
}; | ||
|
||
@NgModule({ | ||
imports: [NgxPlanetModule, RouterModule.forRoot([])] | ||
}) | ||
class AppModule {} | ||
|
||
@NgModule({ | ||
imports: [NgxPlanetModule.forRoot([app1]), RouterModule.forRoot([])] | ||
}) | ||
class AppModuleWithApps {} | ||
|
||
describe('NgxPlanetModule', () => { | ||
describe('basic', () => { | ||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [AppModule] | ||
}); | ||
}); | ||
|
||
it('should get NgxPlanetModule', () => { | ||
expect(TestBed.get(NgxPlanetModule)).toBeTruthy(); | ||
}); | ||
|
||
it('should get planet', () => { | ||
const planet: Planet = TestBed.get(Planet); | ||
expect(planet).toBeTruthy(); | ||
expect(planet.getApps()).toEqual([]); | ||
}); | ||
}); | ||
|
||
describe('forRoot', () => { | ||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [AppModuleWithApps] | ||
}); | ||
}); | ||
|
||
it('should get NgxPlanetModule', () => { | ||
expect(TestBed.get(NgxPlanetModule)).toBeTruthy(); | ||
}); | ||
|
||
it('should register app1 when use forRoot', () => { | ||
const planet: Planet = TestBed.get(Planet); | ||
expect(planet).toBeTruthy(); | ||
expect(planet.getApps()).toEqual([app1]); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
import { TestBed, async, tick, fakeAsync } from '@angular/core/testing'; | ||
import { Planet } from './planet'; | ||
import { NgxPlanetModule } from './module'; | ||
import { RouterModule, Router } from '@angular/router'; | ||
import { SwitchModes } from './planet.class'; | ||
import { PlanetApplicationService } from './application/planet-application.service'; | ||
import { PlanetApplicationLoader } from './application/planet-application-loader'; | ||
import { EmptyComponent } from './empty/empty.component'; | ||
import { NgZone } from '@angular/core'; | ||
|
||
const app1 = { | ||
name: 'app1', | ||
host: '.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', | ||
host: '.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' | ||
} | ||
}; | ||
|
||
describe('Planet', () => { | ||
let planet: Planet; | ||
let planetApplicationService: PlanetApplicationService; | ||
let planetApplicationLoader: PlanetApplicationLoader; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [ | ||
NgxPlanetModule, | ||
RouterModule.forRoot([ | ||
{ | ||
path: '**', | ||
component: EmptyComponent | ||
} | ||
]) | ||
] | ||
}); | ||
planet = TestBed.get(Planet); | ||
planetApplicationService = TestBed.get(PlanetApplicationService); | ||
planetApplicationLoader = TestBed.get(PlanetApplicationLoader); | ||
}); | ||
|
||
it('should create planet', () => { | ||
expect(planet).toBeTruthy(); | ||
}); | ||
|
||
it('should register app1', () => { | ||
const registerSpy = spyOn(planetApplicationService, 'register'); | ||
expect(registerSpy).not.toHaveBeenCalled(); | ||
planet.registerApp(app1); | ||
expect(registerSpy).toHaveBeenCalled(); | ||
expect(registerSpy).toHaveBeenCalledWith(app1); | ||
}); | ||
|
||
it('should register multiple apps', () => { | ||
const registerSpy = spyOn(planetApplicationService, 'register'); | ||
expect(registerSpy).not.toHaveBeenCalled(); | ||
planet.registerApps([app1, app2]); | ||
expect(registerSpy).toHaveBeenCalled(); | ||
expect(registerSpy).toHaveBeenCalledWith([app1, app2]); | ||
}); | ||
|
||
it('should unregister app', () => { | ||
const unregisterSpy = spyOn(planetApplicationService, 'unregister'); | ||
expect(unregisterSpy).not.toHaveBeenCalled(); | ||
planet.unregisterApp('app1'); | ||
expect(unregisterSpy).toHaveBeenCalled(); | ||
expect(unregisterSpy).toHaveBeenCalledWith('app1'); | ||
}); | ||
|
||
it('should get apps', () => { | ||
const getAppsSpy = spyOn(planetApplicationService, 'getApps'); | ||
getAppsSpy.and.returnValue([app1, app2]); | ||
expect(getAppsSpy).not.toHaveBeenCalled(); | ||
const apps = planet.getApps(); | ||
expect(getAppsSpy).toHaveBeenCalled(); | ||
expect(apps).toEqual([app1, app2]); | ||
}); | ||
|
||
it('should set options success', () => { | ||
const setOptionsSpy = spyOn(planetApplicationLoader, 'setOptions'); | ||
expect(setOptionsSpy).not.toHaveBeenCalled(); | ||
const options = { | ||
switchMode: SwitchModes.coexist, | ||
errorHandler: () => {} | ||
}; | ||
planet.setOptions(options); | ||
expect(setOptionsSpy).toHaveBeenCalled(); | ||
expect(setOptionsSpy).toHaveBeenCalledWith(options); | ||
}); | ||
|
||
it('should reroute when navigateByUrl', fakeAsync(() => { | ||
const router: Router = TestBed.get(Router); | ||
const ngZone: NgZone = TestBed.get(NgZone); | ||
const rerouteSpy = spyOn(planetApplicationLoader, 'reroute'); | ||
expect(rerouteSpy).not.toHaveBeenCalled(); | ||
planet.start(); | ||
expect(rerouteSpy).not.toHaveBeenCalled(); | ||
|
||
ngZone.run(() => { | ||
router.navigateByUrl('/app1/dashboard'); | ||
}); | ||
|
||
tick(); | ||
|
||
expect(rerouteSpy).toHaveBeenCalledTimes(1); | ||
expect(rerouteSpy).toHaveBeenCalledWith({ | ||
url: '/app1/dashboard' | ||
}); | ||
|
||
ngZone.run(() => { | ||
router.navigateByUrl('/app1/users'); | ||
}); | ||
tick(); | ||
|
||
expect(rerouteSpy).toHaveBeenCalledTimes(2); | ||
expect(rerouteSpy).toHaveBeenCalledWith({ | ||
url: '/app1/users' | ||
}); | ||
})); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters