Skip to content

Commit fef7692

Browse files
committed
fix(a14): root providers for ApplicationModule #1932
1 parent 720bac0 commit fef7692

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

libs/ng-mocks/src/lib/common/core.config.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,24 @@ export default {
1111
// https://github.com/ike18t/ng-mocks/issues/538
1212
'Sanitizer',
1313
'DomSanitizer',
14+
15+
// ApplicationModule, A14 made them global at root level
16+
'ApplicationInitStatus',
17+
'ApplicationRef',
18+
'Compiler',
19+
'IterableDiffers',
20+
'KeyValueDiffers',
1421
],
1522
neverMockToken: [
1623
'InjectionToken Set Injector scope.', // INJECTOR_SCOPE // ivy only
1724
'InjectionToken EventManagerPlugins', // EVENT_MANAGER_PLUGINS
1825
'InjectionToken HammerGestureConfig', // HAMMER_GESTURE_CONFIG
26+
27+
// ApplicationModule, A14 made them global at root level
28+
'InjectionToken AppId', // APP_ID
29+
'InjectionToken DefaultCurrencyCode', // DEFAULT_CURRENCY_CODE
30+
'InjectionToken LocaleId', // LOCALE_ID
31+
'InjectionToken SCHEDULER_TOKEN', // SCHEDULER
1932
],
2033
onMockInstanceRestoreNeed: 'warn',
2134
onTestBedFlushNeed: 'warn',

tests/providedin-root/test.spec.ts

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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

Comments
 (0)