UNPKG

7.43 kBPlain TextView Raw
1import {Location} from '@angular/common';
2import {Component, ModuleWithProviders, NgModule} from "@angular/core";
3import {ComponentFixture, fakeAsync, inject, TestBed, tick} from "@angular/core/testing";
4import {Route, Router, RouterModule} from "@angular/router";
5import {RouterTestingModule} from "@angular/router/testing";
6import {TranslateModule, TranslateService} from "../public-api";
7
8@Component({
9 selector: 'root-cmp',
10 template: `
11 <router-outlet></router-outlet>`
12})
13class RootComponent {
14 constructor(public translate: TranslateService) {
15 translate.setTranslation('en', {
16 "TEST": "Root",
17 'ROOT': 'Root'
18 });
19 translate.use('en');
20 }
21}
22
23@Component({
24 selector: 'lazy',
25 template: 'lazy-loaded-parent [<router-outlet></router-outlet>]'
26})
27class ParentLazyLoadedComponent {
28}
29
30function getLazyLoadedModule(importedModule: ModuleWithProviders<{}>) {
31 @Component({selector: 'lazy', template: 'lazy-loaded-child'})
32 class ChildLazyLoadedComponent {
33 constructor(public translate: TranslateService) {
34 translate.setTranslation('en', {
35 "TEST": "Lazy",
36 'CHILD': 'Child'
37 });
38 translate.use('en');
39 expect(translate.instant('TEST')).toEqual('Lazy');
40 }
41 }
42
43 @NgModule({
44 declarations: [ParentLazyLoadedComponent, ChildLazyLoadedComponent],
45 imports: [
46 RouterModule.forChild([<Route>{
47 path: 'loaded',
48 component: ParentLazyLoadedComponent,
49 children: [{path: 'child', component: ChildLazyLoadedComponent}]
50 }]),
51 importedModule
52 ]
53 })
54 class LoadedModule {
55 }
56
57 return LoadedModule;
58}
59
60function advance(fixture: ComponentFixture<any>): void {
61 tick();
62 fixture.detectChanges();
63}
64
65function createRoot(router: Router, type: any): ComponentFixture<any> {
66 const f = TestBed.createComponent(type);
67 advance(f);
68 router.initialNavigation();
69 advance(f);
70 return f;
71}
72
73describe("module", () => {
74 beforeEach(() => {
75 TestBed.configureTestingModule({
76 imports: [
77 RouterTestingModule,
78 TranslateModule.forRoot(),
79 ],
80 declarations: [RootComponent]
81 });
82 });
83
84 it("should work when lazy loaded using forChild", fakeAsync(inject(
85 [Router, Location],
86 (router: Router, location: Location) => {
87 const LoadedModule = getLazyLoadedModule(TranslateModule.forChild());
88
89 const fixture = createRoot(router, RootComponent),
90 translate = TestBed.inject(TranslateService);
91
92 expect(translate.instant('TEST')).toEqual('Root');
93
94 router.resetConfig([{path: 'lazy', loadChildren: () => LoadedModule}]);
95
96 router.navigateByUrl('/lazy/loaded/child');
97 advance(fixture);
98
99 expect(location.path()).toEqual('/lazy/loaded/child');
100
101 // since the root module imports the TranslateModule with forRoot and the lazy loaded module with forChild
102 // the translate service is shared between both modules
103 // the constructor of the ChildLazyLoadedComponent overwrote the "TEST" key of the root TranslateService
104 expect(translate.instant('TEST')).toEqual('Lazy');
105 }))
106 );
107
108 it("should create 2 instances of the service when lazy loaded using forRoot", fakeAsync(inject(
109 [Router, Location],
110 (router: Router, location: Location) => {
111 const LoadedModule = getLazyLoadedModule(TranslateModule.forRoot());
112
113 const fixture = createRoot(router, RootComponent),
114 translate = TestBed.inject(TranslateService);
115
116 expect(translate.instant('TEST')).toEqual('Root');
117
118 router.resetConfig([{path: 'lazy', loadChildren: () => LoadedModule}]);
119
120 router.navigateByUrl('/lazy/loaded/child');
121 advance(fixture);
122
123 expect(location.path()).toEqual('/lazy/loaded/child');
124
125 // since both the root module and the lazy loaded module use forRoot to define the TranslateModule
126 // the translate service is NOT shared, and 2 instances co-exist
127 // the constructor of the ChildLazyLoadedComponent didn't overwrote the "TEST" key of the root TranslateService
128 expect(translate.instant('TEST')).toEqual('Root');
129 }))
130 );
131
132 it("should create 2 instances of the service when lazy loaded using forChild and isolate true", fakeAsync(inject(
133 [Router, Location],
134 (router: Router, location: Location) => {
135 const LoadedModule = getLazyLoadedModule(TranslateModule.forChild({isolate: true}));
136
137 const fixture = createRoot(router, RootComponent),
138 translate = TestBed.inject(TranslateService);
139
140 expect(translate.instant('TEST')).toEqual('Root');
141
142 router.resetConfig([{path: 'lazy', loadChildren: () => LoadedModule}]);
143
144 router.navigateByUrl('/lazy/loaded/child');
145 advance(fixture);
146
147 expect(location.path()).toEqual('/lazy/loaded/child');
148
149 // since both the root module and the lazy loaded module use forRoot to define the TranslateModule
150 // the translate service is NOT shared, and 2 instances co-exist
151 // the constructor of the ChildLazyLoadedComponent didn't overwrote the "TEST" key of the root TranslateService
152 expect(translate.instant('TEST')).toEqual('Root');
153 }))
154 );
155
156 it("should relay events when lazy loading & using forChild with isolate false", fakeAsync(inject(
157 [Router],
158 (router: Router) => {
159 const LoadedModule = getLazyLoadedModule(TranslateModule.forChild());
160
161 const fixture = createRoot(router, RootComponent),
162 translate = TestBed.inject(TranslateService);
163
164 let spy = jest.fn();
165 let sub = translate.onTranslationChange.subscribe(spy);
166
167 expect(spy).toHaveBeenCalledTimes(0);
168
169 router.resetConfig([{path: 'lazy', loadChildren: () => LoadedModule}]);
170
171 router.navigateByUrl('/lazy/loaded/child');
172 advance(fixture);
173
174 expect(spy).toHaveBeenCalledTimes(1);
175 sub.unsubscribe();
176 }))
177 );
178
179 it("should not relay events when lazy loading & using forChild with isolate true", fakeAsync(inject(
180 [Router],
181 (router: Router) => {
182 const LoadedModule = getLazyLoadedModule(TranslateModule.forChild({isolate: true}));
183
184 const fixture = createRoot(router, RootComponent),
185 translate = TestBed.inject(TranslateService);
186
187 let spy = jest.fn();
188 let sub = translate.onTranslationChange.subscribe(spy);
189
190 expect(spy).toHaveBeenCalledTimes(0);
191
192 router.resetConfig([{path: 'lazy', loadChildren: () => LoadedModule}]);
193
194 router.navigateByUrl('/lazy/loaded/child');
195 advance(fixture);
196
197 expect(spy).toHaveBeenCalledTimes(0);
198 sub.unsubscribe();
199 }))
200 );
201
202 it('should extend translations with extend true', fakeAsync(inject(
203 [Router],
204 (router: Router) => {
205 const LoadedModule = getLazyLoadedModule(TranslateModule.forChild({ extend: true }));
206
207 const fixture = createRoot(router, RootComponent);
208 const translate: TranslateService = TestBed.inject(TranslateService);
209
210 router.resetConfig([{path: 'lazy', loadChildren: () => LoadedModule}]);
211
212 router.navigateByUrl('/lazy/loaded/child');
213 advance(fixture);
214
215 expect(translate.instant('TEST')).toEqual('Lazy');
216 expect(translate.instant('ROOT')).toEqual('Root');
217 expect(translate.instant('CHILD')).toEqual('Child');
218 }))
219 );
220});