|
| 1 | +import { |
| 2 | + Component, |
| 3 | + Inject, |
| 4 | + Injectable, |
| 5 | + InjectionToken, |
| 6 | + LOCALE_ID, |
| 7 | + VERSION, |
| 8 | +} from '@angular/core'; |
| 9 | +import { TestBed } from '@angular/core/testing'; |
| 10 | +import { MockBuilder, MockRender, ngMocks } from 'ng-mocks'; |
| 11 | + |
| 12 | +const TOKEN = new (InjectionToken as any)('TOKEN', { |
| 13 | + factory: () => 'ROOT_TOKEN', |
| 14 | + providedIn: 'root', |
| 15 | +}); |
| 16 | + |
| 17 | +@((Injectable as any)({ |
| 18 | + providedIn: 'root', |
| 19 | +})) |
| 20 | +class Service { |
| 21 | + public readonly value: string = 'ROOT_SERVICE'; |
| 22 | +} |
| 23 | + |
| 24 | +@Component({ |
| 25 | + selector: 'target', |
| 26 | + template: ':{{ service.value }}:{{ token }}:{{localeId}}:', |
| 27 | +}) |
| 28 | +class TargetComponent { |
| 29 | + public constructor( |
| 30 | + public service: Service, |
| 31 | + @Inject(TOKEN) public token: string, |
| 32 | + @Inject(LOCALE_ID) public localeId: string, |
| 33 | + ) {} |
| 34 | +} |
| 35 | + |
| 36 | +// tokens provided on the root level, shouldn't be mocked unless it's explicitly specified. |
| 37 | +// @see https://github.com/ike18t/ng-mocks/issues/1932 |
| 38 | +describe('providedIn:root', () => { |
| 39 | + if (parseInt(VERSION.major, 10) <= 5) { |
| 40 | + it('a5', () => { |
| 41 | + // pending('Need Angular > 5'); |
| 42 | + expect(true).toBeTruthy(); |
| 43 | + }); |
| 44 | + |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + describe('native', () => { |
| 49 | + beforeEach(() => |
| 50 | + TestBed.configureTestingModule({ |
| 51 | + declarations: [TargetComponent], |
| 52 | + }), |
| 53 | + ); |
| 54 | + |
| 55 | + it('finds token the root token', () => { |
| 56 | + const fixture = MockRender(TargetComponent); |
| 57 | + expect(ngMocks.formatHtml(fixture)).toEqual( |
| 58 | + '<target>:ROOT_SERVICE:ROOT_TOKEN:en-US:</target>', |
| 59 | + ); |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + describe('MockBuilder:default', () => { |
| 64 | + beforeEach(() => MockBuilder(TargetComponent)); |
| 65 | + |
| 66 | + it('mocks all root provides apart from ApplicationModule:LOCALE_ID', () => { |
| 67 | + const fixture = MockRender(TargetComponent); |
| 68 | + expect(ngMocks.formatHtml(fixture)).toEqual( |
| 69 | + '<target>:::en-US:</target>', |
| 70 | + ); |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + describe('MockBuilder:keep', () => { |
| 75 | + beforeEach(() => |
| 76 | + MockBuilder(TargetComponent) |
| 77 | + .keep(Service) |
| 78 | + .keep(TOKEN) |
| 79 | + .keep(LOCALE_ID), |
| 80 | + ); |
| 81 | + |
| 82 | + it('keeps all original values', () => { |
| 83 | + const fixture = MockRender(TargetComponent); |
| 84 | + expect(ngMocks.formatHtml(fixture)).toEqual( |
| 85 | + '<target>:ROOT_SERVICE:ROOT_TOKEN:en-US:</target>', |
| 86 | + ); |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + describe('MockBuilder:mock', () => { |
| 91 | + beforeEach(() => |
| 92 | + MockBuilder(TargetComponent) |
| 93 | + .mock(Service, { value: 'MOCK_SERVICE' }) |
| 94 | + .mock(TOKEN, 'MOCK_TOKEN') |
| 95 | + .mock(LOCALE_ID, 'MOCK_LOCALE_ID'), |
| 96 | + ); |
| 97 | + |
| 98 | + it('mocks all values', () => { |
| 99 | + const fixture = MockRender(TargetComponent); |
| 100 | + expect(ngMocks.formatHtml(fixture)).toEqual( |
| 101 | + '<target>:MOCK_SERVICE:MOCK_TOKEN:MOCK_LOCALE_ID:</target>', |
| 102 | + ); |
| 103 | + }); |
| 104 | + }); |
| 105 | +}); |
0 commit comments