-
-
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.
fix(core): mocking viewProviders #1507
- Loading branch information
Showing
6 changed files
with
213 additions
and
6 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,151 @@ | ||
import { Component, Injectable, Input } from '@angular/core'; | ||
import { | ||
MockBuilder, | ||
MockInstance, | ||
MockProvider, | ||
MockRender, | ||
ngMocks, | ||
} from 'ng-mocks'; | ||
|
||
@Injectable() | ||
export class ProviderService { | ||
public description = 'real provider'; | ||
} | ||
|
||
@Injectable() | ||
export class ViewProviderService { | ||
public description = 'real viewProvider'; | ||
} | ||
|
||
@Component({ | ||
providers: [ProviderService], | ||
selector: 'hello', | ||
template: ` | ||
<h1 class="name">{{ name }}</h1> | ||
<div class="provider">{{ provider.description }}</div> | ||
<div class="viewProvider">{{ viewProvider.description }}</div> | ||
`, | ||
viewProviders: [ViewProviderService], | ||
}) | ||
export class HelloComponent { | ||
@Input() public readonly name: string | null = null; | ||
|
||
public constructor( | ||
public readonly provider: ProviderService, | ||
public readonly viewProvider: ViewProviderService, | ||
) {} | ||
} | ||
|
||
// @see https://github.com/ike18t/ng-mocks/issues/1507 | ||
// Difference between MockBuilder.provide and MockBuilder.mock | ||
// Only MockBuilder.mock replaces providers on the component / directive level. | ||
// MockBuilder.provide simply adds a service to global scope. | ||
describe('issue-1507', () => { | ||
MockInstance.scope(); | ||
|
||
describe('default', () => { | ||
beforeEach(() => MockBuilder(HelloComponent)); | ||
|
||
it('keeps providers and viewProviders as they are', () => { | ||
MockRender(HelloComponent, { name: 'Test1' }); | ||
|
||
const name = ngMocks.formatText(ngMocks.find('.name')); | ||
expect(name).toEqual('Test1'); | ||
|
||
const provider = ngMocks.formatText(ngMocks.find('.provider')); | ||
expect(provider).toEqual('real provider'); | ||
|
||
const viewProvider = ngMocks.formatText( | ||
ngMocks.find('.viewProvider'), | ||
); | ||
expect(viewProvider).toEqual('real viewProvider'); | ||
}); | ||
}); | ||
|
||
describe(`.keep`, () => { | ||
beforeEach(() => | ||
MockBuilder(HelloComponent) | ||
.keep(ProviderService) | ||
.keep(ViewProviderService), | ||
); | ||
|
||
it('keeps providers and viewProviders as they are', () => { | ||
MockRender(HelloComponent, { name: 'Test2' }); | ||
|
||
const name = ngMocks.formatText(ngMocks.find('.name')); | ||
expect(name).toEqual('Test2'); | ||
|
||
const provider = ngMocks.formatText(ngMocks.find('.provider')); | ||
expect(provider).toEqual('real provider'); | ||
|
||
const viewProvider = ngMocks.formatText( | ||
ngMocks.find('.viewProvider'), | ||
); | ||
expect(viewProvider).toEqual('real viewProvider'); | ||
}); | ||
}); | ||
|
||
describe(`.provide`, () => { | ||
beforeEach(() => | ||
MockBuilder(HelloComponent) | ||
.provide( | ||
MockProvider(ProviderService, { | ||
description: 'provided provider', | ||
}), | ||
) | ||
.provide( | ||
MockProvider(ViewProviderService, { | ||
description: 'provided viewProvider', | ||
}), | ||
), | ||
); | ||
|
||
it('provides mocks on the root level', () => { | ||
MockRender(HelloComponent, { name: 'Test3' }); | ||
|
||
const name = ngMocks.formatText(ngMocks.find('.name')); | ||
expect(name).toEqual('Test3'); | ||
|
||
const provider = ngMocks.formatText(ngMocks.find('.provider')); | ||
expect(provider).toEqual('real provider'); | ||
expect( | ||
ngMocks.findInstance(ProviderService).description, | ||
).toEqual('provided provider'); | ||
|
||
const viewProvider = ngMocks.formatText( | ||
ngMocks.find('.viewProvider'), | ||
); | ||
expect(viewProvider).toEqual('real viewProvider'); | ||
expect( | ||
ngMocks.findInstance(ViewProviderService).description, | ||
).toEqual('provided viewProvider'); | ||
}); | ||
}); | ||
|
||
describe(`.mock`, () => { | ||
beforeEach(() => | ||
MockBuilder(HelloComponent) | ||
.mock(ProviderService, { | ||
description: 'mock provider', | ||
}) | ||
.mock(ViewProviderService, { | ||
description: 'mock viewProvider', | ||
}), | ||
); | ||
|
||
it('mocks services on the component level', () => { | ||
MockRender(HelloComponent, { name: 'Test4' }); | ||
|
||
const name = ngMocks.formatText(ngMocks.find('.name')); | ||
expect(name).toEqual('Test4'); | ||
|
||
const provider = ngMocks.formatText(ngMocks.find('.provider')); | ||
expect(provider).toEqual('mock provider'); | ||
|
||
const viewProvider = ngMocks.formatText( | ||
ngMocks.find('.viewProvider'), | ||
); | ||
expect(viewProvider).toEqual('mock viewProvider'); | ||
}); | ||
}); | ||
}); |
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,37 @@ | ||
import { Component, Injectable } from '@angular/core'; | ||
import { MockBuilder, MockProvider, MockRender } from 'ng-mocks'; | ||
|
||
@Injectable() | ||
class TargetService { | ||
public name = 'real'; | ||
} | ||
|
||
@Component({ | ||
selector: 'target', | ||
template: `{{ service.name }}`, | ||
}) | ||
class TargetComponent { | ||
public constructor(public readonly service: TargetService) {} | ||
} | ||
|
||
describe('MockRender.viewProviders', () => { | ||
beforeEach(() => MockBuilder(TargetComponent)); | ||
|
||
it('throws without the service', () => { | ||
expect(() => MockRender(TargetComponent)).toThrowError( | ||
/No provider for TargetService/, | ||
); | ||
}); | ||
|
||
it('providers services via viewProviders', () => { | ||
expect(() => | ||
MockRender(TargetComponent, null, { | ||
viewProviders: [ | ||
MockProvider(TargetService, { | ||
name: 'mock', | ||
}), | ||
], | ||
}), | ||
).not.toThrow(); | ||
}); | ||
}); |