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(mock-render): apply overrides to components with no selectors hel…
- Loading branch information
Showing
3 changed files
with
85 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { | ||
Component, | ||
Directive, | ||
Injectable, | ||
Input, | ||
} from '@angular/core'; | ||
import { MockBuilder, MockRender, ngMocks } from 'ng-mocks'; | ||
|
||
@Injectable() | ||
export class HelloService { | ||
public description = 'real service'; | ||
} | ||
|
||
@Component({ | ||
providers: [HelloService], | ||
template: ``, | ||
}) | ||
export class HelloComponent { | ||
public serviceDescription = ''; | ||
|
||
public constructor(private readonly helloService: HelloService) { | ||
this.serviceDescription = this.helloService.description; | ||
} | ||
} | ||
|
||
@Directive({ | ||
providers: [HelloService], | ||
selector: 'hello', | ||
}) | ||
export class HelloDirective { | ||
@Input() public name = ''; | ||
public serviceDescription = ''; | ||
|
||
public constructor(private readonly helloService: HelloService) { | ||
this.serviceDescription = this.helloService.description; | ||
} | ||
} | ||
|
||
// Components without selectors should still inherit mocked providers. | ||
// https://github.com/ike18t/ng-mocks/issues/1876 | ||
describe('issue-1876', () => { | ||
beforeEach(() => | ||
MockBuilder([HelloComponent, HelloDirective]).mock(HelloService, { | ||
description: 'fake service', | ||
}), | ||
); | ||
|
||
it('uses fake service in components', () => { | ||
MockRender(HelloComponent); | ||
const instance = ngMocks.findInstance(HelloComponent); | ||
expect(instance.serviceDescription).toContain('fake service'); | ||
}); | ||
|
||
it('uses fake service in directives', () => { | ||
MockRender(HelloDirective); | ||
const instance = ngMocks.findInstance(HelloDirective); | ||
expect(instance.serviceDescription).toContain('fake service'); | ||
}); | ||
}); |