-
-
Notifications
You must be signed in to change notification settings - Fork 85
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 #2312 from satanTime/issues/2311
fix(ngMocks.findInstance): works without fixture #2311
- Loading branch information
Showing
3 changed files
with
97 additions
and
24 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,50 @@ | ||
import { Injectable, NgModule } from '@angular/core'; | ||
|
||
import { MockBuilder, ngMocks } from 'ng-mocks'; | ||
|
||
@Injectable() | ||
class TargetService { | ||
echo() { | ||
return this.constructor.name; | ||
} | ||
} | ||
|
||
@Injectable() | ||
class FakeService { | ||
echo() { | ||
return this.constructor.name; | ||
} | ||
} | ||
|
||
@NgModule({ | ||
providers: [TargetService], | ||
}) | ||
class TargetModule {} | ||
|
||
describe('issue-2311', () => { | ||
beforeEach(() => MockBuilder(TargetService, TargetModule)); | ||
|
||
it('finds instance', () => { | ||
const instance = ngMocks.findInstance(TargetService); | ||
expect(instance.echo()).toEqual('TargetService'); | ||
}); | ||
|
||
it('finds instances', () => { | ||
const instances = ngMocks.findInstances(TargetService); | ||
const [instance] = instances; | ||
expect(instances.length).toEqual(1); | ||
expect(instance.echo()).toEqual('TargetService'); | ||
}); | ||
|
||
it('fails to find instance', () => { | ||
const instance = ngMocks.findInstance(FakeService, undefined); | ||
expect(instance).toBeUndefined(); | ||
}); | ||
|
||
it('fails to find instances', () => { | ||
const instances = ngMocks.findInstances(FakeService); | ||
const [instance] = instances; | ||
expect(instances.length).toEqual(0); | ||
expect(instance).toBeUndefined(); | ||
}); | ||
}); |