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(core): respecting transform in mock pipes help-me-mom#4564
- Loading branch information
Showing
4 changed files
with
109 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { | ||
Component, CUSTOM_ELEMENTS_SCHEMA, Inject, Injectable, InjectionToken, | ||
NgModule, | ||
Pipe, | ||
PipeTransform, | ||
} from '@angular/core'; | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import {MockModule, MockPipe, MockProvider, MockRender, ngMocks} from 'ng-mocks'; | ||
|
||
const TOKEN = new InjectionToken('TOKEN'); | ||
|
||
@Injectable() | ||
class TargetService { | ||
func() { | ||
return 'real'; | ||
} | ||
} | ||
|
||
@Pipe({ | ||
name: 'target', | ||
}) | ||
class TargetPipe implements PipeTransform { | ||
transform() { | ||
return 'real'; | ||
} | ||
} | ||
|
||
@Pipe({ | ||
name: 'standard', | ||
}) | ||
class StandardPipe implements PipeTransform { | ||
transform() { | ||
return 'standard'; | ||
} | ||
} | ||
|
||
@NgModule({ | ||
declarations: [TargetPipe, StandardPipe], | ||
exports: [TargetPipe, StandardPipe], | ||
providers: [ | ||
{ | ||
provide: TOKEN, | ||
useValue: 'real', | ||
}, | ||
TargetService, | ||
], | ||
}) | ||
class PipeModule {} | ||
|
||
@Component({ | ||
selector: 'target', | ||
template: '{{ null | target }}:{{ token }}:{{ service.func() }}', | ||
}) | ||
class TargetComponent { | ||
constructor(@Inject(TOKEN) public readonly token: string, public readonly service: TargetService) {} | ||
} | ||
|
||
@NgModule({ | ||
imports: [PipeModule], | ||
declarations: [TargetComponent], | ||
exports: [TargetComponent], | ||
}) | ||
class ComponentModule {} | ||
|
||
@Component({ | ||
selector: 'sut', | ||
template: '<custom-component><target></target></custom-component>', | ||
}) | ||
class SubjectUnderTestComponent {} | ||
|
||
// @see https://github.com/help-me-mom/ng-mocks/issues/4564 | ||
// mixed imports forget pipe customizations. | ||
describe('issue-4564', () => { | ||
ngMocks.throwOnConsole(); | ||
|
||
beforeEach(() => | ||
TestBed.configureTestingModule({ | ||
imports: [ComponentModule, MockModule(PipeModule)], | ||
declarations: [ | ||
SubjectUnderTestComponent, | ||
MockPipe(TargetPipe, () => 'mock'), | ||
MockPipe(StandardPipe), | ||
], | ||
providers: [ | ||
MockProvider(TOKEN, 'mock'), | ||
MockProvider(TargetService, { | ||
func: () => 'mock', | ||
}), | ||
], | ||
schemas: [CUSTOM_ELEMENTS_SCHEMA], | ||
}).compileComponents(), | ||
); | ||
|
||
it('customizes pipe', () => { | ||
const fixture = MockRender(SubjectUnderTestComponent); | ||
expect(ngMocks.formatText(fixture)).toEqual('mock:mock:mock'); | ||
}); | ||
}); |