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.
feat: support to inject a library-related service mocker
Closes #87
- Loading branch information
Showing
13 changed files
with
171 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Component } from '@angular/core'; | ||
import { TargetService } from './fixtures.providers'; | ||
|
||
@Component({ | ||
selector: 'target', | ||
template: '<ng-content></ng-content>', | ||
}) | ||
export class TargetComponent { | ||
protected service: TargetService; | ||
|
||
constructor(service: TargetService) { | ||
this.service = service; | ||
this.service.echo('constructor'); | ||
} | ||
|
||
public echo(): string { | ||
return this.service.echo('TargetComponent'); | ||
} | ||
} |
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,7 @@ | ||
import { NgModule } from '@angular/core'; | ||
import { TargetService } from './fixtures.providers'; | ||
|
||
@NgModule({ | ||
providers: [TargetService], | ||
}) | ||
export class TargetModule {} |
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,10 @@ | ||
import { Injectable } from '@angular/core'; | ||
|
||
@Injectable() | ||
export class TargetService { | ||
protected value = 'TargetService'; | ||
|
||
public echo(value?: string): string { | ||
return value ? value : this.value; | ||
} | ||
} |
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,73 @@ | ||
import { inject, TestBed } from '@angular/core/testing'; | ||
import { By } from '@angular/platform-browser'; | ||
import { MockModule, MockRender } from 'ng-mocks'; | ||
|
||
import { TargetComponent } from './fixtures.components'; | ||
import { TargetModule } from './fixtures.modules'; | ||
import { TargetService } from './fixtures.providers'; | ||
import createSpyObj = jasmine.createSpyObj; | ||
import Spy = jasmine.Spy; | ||
|
||
describe('spies:real', () => { | ||
beforeEach(() => | ||
TestBed.configureTestingModule({ | ||
declarations: [TargetComponent], | ||
imports: [TargetModule], | ||
}).compileComponents() | ||
); | ||
|
||
it('should render', () => { | ||
const fixture = MockRender(TargetComponent); | ||
const component = fixture.debugElement.query(By.directive(TargetComponent)).componentInstance as TargetComponent; | ||
expect(component).toBeDefined(); | ||
expect(component.echo()).toEqual('TargetComponent'); | ||
}); | ||
}); | ||
|
||
describe('spies:manual-mock', () => { | ||
beforeEach(() => { | ||
const spy = createSpyObj<TargetService>('TargetService', ['echo']); | ||
spy.echo.and.returnValue('fake'); | ||
|
||
return TestBed.configureTestingModule({ | ||
declarations: [TargetComponent], | ||
imports: [MockModule(TargetModule)], | ||
providers: [ | ||
{ | ||
provide: TargetService, | ||
useValue: spy, | ||
}, | ||
], | ||
}).compileComponents(); | ||
}); | ||
|
||
it('should get manually mocked service', inject([TargetService], (targetService: TargetService) => { | ||
const fixture = MockRender(TargetComponent); | ||
const component = fixture.debugElement.query(By.directive(TargetComponent)).componentInstance as TargetComponent; | ||
expect(component).toBeDefined(); | ||
expect(targetService.echo).toHaveBeenCalledTimes(1); | ||
expect(targetService.echo).toHaveBeenCalledWith('constructor'); | ||
expect(component.echo()).toEqual('fake'); | ||
expect(targetService.echo).toHaveBeenCalledTimes(2); // tslint:disable-line:no-magic-numbers | ||
})); | ||
}); | ||
|
||
describe('spies:auto-mock', () => { | ||
beforeEach(() => | ||
TestBed.configureTestingModule({ | ||
declarations: [TargetComponent], | ||
imports: [MockModule(TargetModule)], | ||
}).compileComponents() | ||
); | ||
|
||
it('should get already mocked service', inject([TargetService], (targetService: TargetService) => { | ||
const fixture = MockRender(TargetComponent); | ||
const component = fixture.debugElement.query(By.directive(TargetComponent)).componentInstance as TargetComponent; | ||
expect(component).toBeDefined(); | ||
expect(targetService.echo).toHaveBeenCalledTimes(1); | ||
expect(targetService.echo).toHaveBeenCalledWith('constructor'); | ||
(targetService.echo as Spy).and.returnValue('faked'); | ||
expect(component.echo()).toEqual('faked'); | ||
expect(targetService.echo).toHaveBeenCalledTimes(2); // tslint:disable-line:no-magic-numbers | ||
})); | ||
}); |
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,5 @@ | ||
import { mockServiceHelper } from '../lib/mock-service'; | ||
|
||
declare const jasmine: any; | ||
|
||
mockServiceHelper.registerMockFunction(jasmine.createSpy); |
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,5 @@ | ||
import { mockServiceHelper } from '../lib/mock-service'; | ||
|
||
declare const jest: any; | ||
|
||
mockServiceHelper.registerMockFunction(jest.fn); |
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