|
| 1 | +import { CommonModule, DatePipe } from '@angular/common'; |
| 2 | +import { Component, NgModule } from '@angular/core'; |
| 3 | +import { MockBuilder, MockRender, ngMocks } from 'ng-mocks'; |
| 4 | + |
| 5 | +@Component({ |
| 6 | + selector: 'target', |
| 7 | + template: `{{ '2022-03-03' | date }}`, |
| 8 | +}) |
| 9 | +class TargetComponent {} |
| 10 | + |
| 11 | +@NgModule({ |
| 12 | + declarations: [TargetComponent], |
| 13 | + imports: [CommonModule], |
| 14 | +}) |
| 15 | +class TargetModule {} |
| 16 | + |
| 17 | +// @see https://github.com/ike18t/ng-mocks/issues/1957 |
| 18 | +describe('issue-1957:MockBuilder', () => { |
| 19 | + describe('mock', () => { |
| 20 | + const dateSpy = (value: string) => value.length; |
| 21 | + ngMocks.faster(); |
| 22 | + |
| 23 | + beforeAll(() => |
| 24 | + MockBuilder(TargetComponent, TargetModule).mock( |
| 25 | + DatePipe, |
| 26 | + dateSpy, |
| 27 | + ), |
| 28 | + ); |
| 29 | + |
| 30 | + it('uses a mock', () => { |
| 31 | + const fixture = MockRender(TargetComponent); |
| 32 | + const text = ngMocks.formatText(fixture); |
| 33 | + expect(text).toEqual('10'); |
| 34 | + }); |
| 35 | + }); |
| 36 | + |
| 37 | + describe('restored', () => { |
| 38 | + ngMocks.faster(); |
| 39 | + |
| 40 | + beforeAll(() => MockBuilder(TargetComponent, TargetModule)); |
| 41 | + |
| 42 | + it('uses a mock', () => { |
| 43 | + const fixture = MockRender(TargetComponent); |
| 44 | + const text = ngMocks.formatText(fixture); |
| 45 | + expect(text).toEqual('Mar 3, 2022'); |
| 46 | + }); |
| 47 | + }); |
| 48 | +}); |
0 commit comments