|
| 1 | +import { CommonModule } from '@angular/common'; |
| 2 | +import { Component, Directive, NgModule } from '@angular/core'; |
| 3 | +import { |
| 4 | + MockBuilder, |
| 5 | + MockInstance, |
| 6 | + MockProvider, |
| 7 | + MockRender, |
| 8 | +} from 'ng-mocks'; |
| 9 | + |
| 10 | +@Directive({ |
| 11 | + selector: 'target', |
| 12 | +}) |
| 13 | +class MockDirective { |
| 14 | + public readonly boolean = false; |
| 15 | + public readonly number = 0; |
| 16 | + public readonly string = ''; |
| 17 | +} |
| 18 | + |
| 19 | +@Component({ |
| 20 | + selector: 'target', |
| 21 | + template: ``, |
| 22 | +}) |
| 23 | +class TargetComponent { |
| 24 | + public constructor(public readonly mock: MockDirective) {} |
| 25 | +} |
| 26 | + |
| 27 | +@NgModule({ |
| 28 | + declarations: [TargetComponent, MockDirective], |
| 29 | + imports: [CommonModule], |
| 30 | +}) |
| 31 | +class TargetModule {} |
| 32 | + |
| 33 | +// MockInstance doesn't provide falsy values. |
| 34 | +// @see https://github.com/ike18t/ng-mocks/issues/2087 |
| 35 | +describe('issue-2087', () => { |
| 36 | + MockInstance.scope(); |
| 37 | + beforeEach(() => MockBuilder(TargetComponent, TargetModule)); |
| 38 | + |
| 39 | + const tests: Array<[keyof MockDirective, any, any]> = [ |
| 40 | + ['string', '', 'test'], |
| 41 | + ['number', 0, 1], |
| 42 | + ['boolean', false, true], |
| 43 | + ]; |
| 44 | + |
| 45 | + tests.forEach(([kind, falsy, truthy]) => |
| 46 | + describe(kind, () => { |
| 47 | + it(`works for falsy`, () => { |
| 48 | + MockInstance(MockDirective, kind, falsy); |
| 49 | + const fixture = MockRender(TargetComponent); |
| 50 | + expect(fixture.componentInstance.mock[kind]).toEqual(falsy); |
| 51 | + }); |
| 52 | + |
| 53 | + it(`works for truthy`, () => { |
| 54 | + MockInstance(MockDirective, kind, truthy); |
| 55 | + const fixture = MockRender(TargetComponent); |
| 56 | + expect(fixture.componentInstance.mock[kind]).toEqual(truthy); |
| 57 | + }); |
| 58 | + }), |
| 59 | + ); |
| 60 | +}); |
0 commit comments