forked from help-me-mom/ng-mocks
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: allows mocking providers of an kept component
closes #172
- Loading branch information
Showing
7 changed files
with
221 additions
and
36 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
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,99 @@ | ||
import { Component, Injectable, NgModule, OnInit } from '@angular/core'; | ||
import { TestBed } from '@angular/core/testing'; | ||
import { MockBuilder, MockRender } from 'ng-mocks'; | ||
|
||
@Injectable() | ||
class Target1Service { | ||
protected readonly name = 'Target1Service'; | ||
|
||
public echo(): string { | ||
return this.name; | ||
} | ||
} | ||
|
||
@Injectable() | ||
class Target2Service { | ||
protected readonly name = 'Target2Service'; | ||
|
||
public echo(): string { | ||
return this.name; | ||
} | ||
} | ||
|
||
@Component({ | ||
providers: [Target1Service, Target2Service], | ||
selector: 'app-target', | ||
template: '{{echo}}', | ||
}) | ||
class TargetComponent implements OnInit { | ||
public echo = ''; | ||
|
||
protected readonly target1Service: Target1Service; | ||
protected readonly target2Service: Target2Service; | ||
|
||
constructor(target1Service: Target1Service, target2Service: Target2Service) { | ||
this.target1Service = target1Service; | ||
this.target2Service = target2Service; | ||
} | ||
|
||
public ngOnInit(): void { | ||
this.echo = `${this.target1Service.echo()}${this.target2Service.echo()}`; | ||
} | ||
} | ||
|
||
@NgModule({ | ||
declarations: [TargetComponent], | ||
exports: [TargetComponent], | ||
}) | ||
class TargetModule {} | ||
|
||
describe('issue-172:real', () => { | ||
beforeEach(() => | ||
TestBed.configureTestingModule({ | ||
imports: [TargetModule], | ||
}).compileComponents() | ||
); | ||
|
||
it('renders echo', () => { | ||
const fixture = MockRender(TargetComponent); | ||
expect(fixture.nativeElement.innerHTML).toContain('<app-target>Target1ServiceTarget2Service</app-target>'); | ||
}); | ||
}); | ||
|
||
describe('issue-172:test', () => { | ||
beforeEach(() => | ||
TestBed.configureTestingModule({ | ||
imports: [TargetModule], | ||
}).compileComponents() | ||
); | ||
|
||
it('renders echo', () => { | ||
TestBed.overrideComponent(TargetComponent, { | ||
add: { | ||
providers: [ | ||
{ | ||
provide: Target1Service, | ||
useValue: { | ||
echo: () => 'MockService', | ||
}, | ||
}, | ||
], | ||
}, | ||
}); | ||
const fixture = MockRender(TargetComponent); | ||
expect(fixture.nativeElement.innerHTML).toContain('<app-target>MockServiceTarget2Service</app-target>'); | ||
}); | ||
}); | ||
|
||
describe('issue-172:mock', () => { | ||
beforeEach(() => | ||
MockBuilder(TargetComponent, TargetModule).mock(Target1Service, { | ||
echo: () => 'MockService', | ||
}) | ||
); | ||
|
||
it('renders mocked echo', () => { | ||
const fixture = MockRender(TargetComponent); | ||
expect(fixture.nativeElement.innerHTML).toContain('<app-target>MockServiceTarget2Service</app-target>'); | ||
}); | ||
}); |