-
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: support of modules with providers in MockBuilder
closes #197
- Loading branch information
Showing
6 changed files
with
156 additions
and
14 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
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,13 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
import { DomSanitizer } from '@angular/platform-browser'; | ||
import { MockBuilder } from 'ng-mocks'; | ||
|
||
describe('issue-197:abstract', () => { | ||
const expected = {}; | ||
beforeEach(() => MockBuilder().mock(DomSanitizer, expected)); | ||
|
||
it('mocks abstract classes', () => { | ||
const actual = TestBed.get(DomSanitizer); | ||
expect(actual).toBe(expected); | ||
}); | ||
}); |
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,91 @@ | ||
// tslint:disable:no-unnecessary-class | ||
|
||
import { Component, Injectable, NgModule } from '@angular/core'; | ||
import { TestBed } from '@angular/core/testing'; | ||
import { MockBuilder, MockRender, NgModuleWithProviders } from 'ng-mocks'; | ||
|
||
@Injectable() | ||
class DependencyService { | ||
private readonly name: string = 'dependency'; | ||
|
||
public echo(): string { | ||
return this.name; | ||
} | ||
} | ||
|
||
@NgModule({}) | ||
class DependencyModule { | ||
static withProviders(): NgModuleWithProviders<DependencyModule> { | ||
return { | ||
ngModule: DependencyModule, | ||
providers: [ | ||
{ | ||
provide: DependencyService, | ||
useValue: { | ||
echo: () => 'via-provider', | ||
}, | ||
}, | ||
], | ||
}; | ||
} | ||
|
||
public readonly service: DependencyService; | ||
|
||
constructor(service: DependencyService) { | ||
this.service = service; | ||
} | ||
} | ||
|
||
@Component({ | ||
selector: 'target', | ||
template: '{{ service.echo() }}', | ||
}) | ||
class TargetComponent { | ||
public readonly service: DependencyService; | ||
|
||
constructor(service: DependencyService) { | ||
this.service = service; | ||
} | ||
} | ||
|
||
@NgModule({ | ||
declarations: [TargetComponent], | ||
}) | ||
class TargetModule {} | ||
|
||
describe('issue-197:with-providers:manually-injection', () => { | ||
beforeEach(() => { | ||
const module = MockBuilder(TargetComponent, TargetModule).build(); | ||
|
||
return TestBed.configureTestingModule({ | ||
declarations: module.declarations, | ||
imports: [...module.imports, DependencyModule.withProviders()], | ||
providers: module.providers, | ||
}).compileComponents(); | ||
}); | ||
|
||
it('creates component with provided dependencies', () => { | ||
const fixture = MockRender(TargetComponent); | ||
expect(fixture.nativeElement.innerHTML).toEqual('<target>via-provider</target>'); | ||
}); | ||
}); | ||
|
||
describe('issue-197:with-providers', () => { | ||
beforeEach(() => MockBuilder(TargetComponent, TargetModule).keep(DependencyModule.withProviders())); | ||
|
||
it('creates component with provided dependencies', () => { | ||
const fixture = MockRender(TargetComponent); | ||
|
||
expect(fixture.nativeElement.innerHTML).toEqual('<target>via-provider</target>'); | ||
}); | ||
}); | ||
|
||
describe('issue-197:with-providers', () => { | ||
beforeEach(() => MockBuilder(TargetComponent, TargetModule).mock(DependencyModule.withProviders())); | ||
|
||
it('creates component with provided dependencies', () => { | ||
const fixture = MockRender(TargetComponent); | ||
|
||
expect(fixture.nativeElement.innerHTML).toEqual('<target></target>'); | ||
}); | ||
}); |