-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
83 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
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,8 @@ | ||
import { InjectionToken } from '@angular/core'; | ||
|
||
export interface NzRootConfig { | ||
extraFontName: string; | ||
extraFontUrl: string; | ||
} | ||
|
||
export const NZ_ROOT_CONFIG = new InjectionToken<NzRootConfig>('NzRootConfig'); |
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,11 @@ | ||
import { Component, ViewEncapsulation } from '@angular/core'; | ||
|
||
@Component({ | ||
template : ``, | ||
styleUrls : [ | ||
'../style/index.less', | ||
'./style/index.less', | ||
], | ||
encapsulation : ViewEncapsulation.None, | ||
}) | ||
export class NzRootStyleComponent { } |
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,45 @@ | ||
import { ComponentFactoryResolver, Injector, ComponentRef, ComponentFactory } from '@angular/core'; | ||
import { async, inject, TestBed } from '@angular/core/testing'; | ||
import { DOCUMENT } from '@angular/common'; | ||
import { NzRootModule } from './nz-root.module'; | ||
import { NzRootStyleComponent } from './nz-root-style.component'; | ||
|
||
describe('NzRootModule', () => { | ||
let ngModule: NzRootModule; | ||
let mockDocument: Document; | ||
let mockInjector: Injector; | ||
let mockFactoryResolver: ComponentFactoryResolver; | ||
let mockElement: HTMLDivElement; | ||
let mockComponentFactory: ComponentFactory<NzRootStyleComponent>; | ||
let mockComponentRef: ComponentRef<NzRootStyleComponent>; | ||
|
||
beforeEach(() => { | ||
mockDocument = { createElement: () => null } as any; | ||
mockInjector = {} as any; | ||
mockFactoryResolver = { resolveComponentFactory: () => null } as any; | ||
mockElement = {} as any; | ||
mockComponentFactory = { create: () => null } as any; | ||
mockComponentRef = { destroy: () => null } as any; | ||
|
||
spyOn(mockDocument, 'createElement').and.returnValue(mockElement); | ||
spyOn(mockComponentRef, 'destroy'); | ||
spyOn(mockComponentFactory, 'create').and.returnValue(mockComponentRef); | ||
spyOn(mockFactoryResolver, 'resolveComponentFactory').and.returnValue(mockComponentFactory); | ||
}); | ||
|
||
beforeEach(() => { | ||
ngModule = new NzRootModule(mockDocument, mockInjector, mockFactoryResolver); | ||
}); | ||
|
||
it('should create style component when start', () => { | ||
expect(mockDocument.createElement).toHaveBeenCalledWith('div'); | ||
expect(mockFactoryResolver.resolveComponentFactory).toHaveBeenCalledWith(NzRootStyleComponent); | ||
expect(mockComponentFactory.create).toHaveBeenCalledWith(mockInjector, null, mockElement); | ||
}); | ||
|
||
it('should destroy style component when terminate', () => { | ||
ngModule.ngOnDestroy(); | ||
|
||
expect(mockComponentRef.destroy).toHaveBeenCalled(); | ||
}) | ||
}); |
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 |
---|---|---|
@@ -1,12 +1,24 @@ | ||
import { NgModule } from '@angular/core'; | ||
import { NgModule, OnDestroy, ComponentRef, ComponentFactoryResolver, Inject, Injector } from '@angular/core'; | ||
import { CommonModule, DOCUMENT } from '@angular/common'; | ||
import { NzRootComponent } from './nz-root.component'; | ||
import { CommonModule } from '@angular/common'; | ||
import { NzRootStyleComponent } from './nz-root-style.component'; | ||
|
||
@NgModule({ | ||
exports : [ NzRootComponent ], | ||
declarations: [ NzRootComponent ], | ||
imports : [ CommonModule ] | ||
exports : [ NzRootComponent ], | ||
declarations : [ NzRootComponent, NzRootStyleComponent ], | ||
imports : [ CommonModule ], | ||
entryComponents : [ NzRootStyleComponent ], | ||
}) | ||
export class NzRootModule implements OnDestroy { | ||
private styleHostComponent: ComponentRef<NzRootStyleComponent>; | ||
|
||
export class NzRootModule { | ||
constructor(@Inject(DOCUMENT) _document: Document, injector: Injector, resolver: ComponentFactoryResolver) { | ||
const componentFactory = resolver.resolveComponentFactory(NzRootStyleComponent); | ||
const div = _document.createElement('div'); | ||
this.styleHostComponent = componentFactory.create(injector, null, div); | ||
} | ||
|
||
ngOnDestroy() { | ||
this.styleHostComponent.destroy(); | ||
} | ||
} |