forked from DSpace/dspace-angular
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.component.spec.ts
More file actions
189 lines (160 loc) · 7.59 KB
/
Copy pathapp.component.spec.ts
File metadata and controls
189 lines (160 loc) · 7.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import { Store, StoreModule } from '@ngrx/store';
import { ComponentFixture, fakeAsync, flush, inject, TestBed, tick, waitForAsync } from '@angular/core/testing';
import { ApplicationRef, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ActivatedRoute, Router } from '@angular/router';
import { TranslateLoader, TranslateModule } from '@ngx-translate/core';
import { BehaviorSubject } from 'rxjs';
// Load the implementations that should be tested
import { AppComponent } from './app.component';
import { HostWindowState } from './shared/search/host-window.reducer';
import { HostWindowResizeAction } from './shared/host-window.actions';
import { MetadataService } from './core/metadata/metadata.service';
import { NativeWindowRef, NativeWindowService } from './core/services/window.service';
import { TranslateLoaderMock } from './shared/mocks/translate-loader.mock';
import { MetadataServiceMock } from './shared/mocks/metadata-service.mock';
import { AngularticsProviderMock } from './shared/mocks/angulartics-provider.service.mock';
import { AuthServiceMock } from './shared/mocks/auth.service.mock';
import { AuthService } from './core/auth/auth.service';
import { MenuService } from './shared/menu/menu.service';
import { CSSVariableService } from './shared/sass-helper/css-variable.service';
import { CSSVariableServiceStub } from './shared/testing/css-variable-service.stub';
import { MenuServiceStub } from './shared/testing/menu-service.stub';
import { HostWindowService } from './shared/host-window.service';
import { HostWindowServiceStub } from './shared/testing/host-window-service.stub';
import { RouteService } from './core/services/route.service';
import { MockActivatedRoute } from './shared/mocks/active-router.mock';
import { RouterMock } from './shared/mocks/router.mock';
import { Angulartics2DSpace } from './statistics/angulartics/dspace-provider';
import { storeModuleConfig } from './app.reducer';
import { LocaleService } from './core/locale/locale.service';
import { authReducer } from './core/auth/auth.reducer';
import { provideMockStore } from '@ngrx/store/testing';
import { ThemeService } from './shared/theme-support/theme.service';
import { getMockThemeService } from './shared/mocks/theme-service.mock';
import { BreadcrumbsService } from './breadcrumbs/breadcrumbs.service';
import { APP_CONFIG } from '../config/app-config.interface';
import { environment } from '../environments/environment';
let comp: AppComponent;
let fixture: ComponentFixture<AppComponent>;
const menuService = new MenuServiceStub();
const initialState = {
core: { auth: { loading: false } }
};
export function getMockLocaleService(): LocaleService {
return jasmine.createSpyObj('LocaleService', {
setCurrentLanguageCode: jasmine.createSpy('setCurrentLanguageCode')
});
}
describe('App component', () => {
let breadcrumbsServiceSpy;
const getDefaultTestBedConf = () => {
breadcrumbsServiceSpy = jasmine.createSpyObj(['listenForRouteChanges']);
return {
imports: [
CommonModule,
StoreModule.forRoot(authReducer, storeModuleConfig),
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useClass: TranslateLoaderMock
}
}),
],
declarations: [AppComponent], // declare the test component
providers: [
{ provide: NativeWindowService, useValue: new NativeWindowRef() },
{ provide: MetadataService, useValue: new MetadataServiceMock() },
{ provide: Angulartics2DSpace, useValue: new AngularticsProviderMock() },
{ provide: AuthService, useValue: new AuthServiceMock() },
{ provide: Router, useValue: new RouterMock() },
{ provide: ActivatedRoute, useValue: new MockActivatedRoute() },
{ provide: MenuService, useValue: menuService },
{ provide: CSSVariableService, useClass: CSSVariableServiceStub },
{ provide: HostWindowService, useValue: new HostWindowServiceStub(800) },
{ provide: LocaleService, useValue: getMockLocaleService() },
{ provide: ThemeService, useValue: getMockThemeService() },
{ provide: BreadcrumbsService, useValue: breadcrumbsServiceSpy },
{ provide: APP_CONFIG, useValue: environment },
provideMockStore({ initialState }),
AppComponent,
RouteService
],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
};
};
// waitForAsync beforeEach
beforeEach(waitForAsync(() => {
return TestBed.configureTestingModule(getDefaultTestBedConf());
}));
// synchronous beforeEach
beforeEach(() => {
fixture = TestBed.createComponent(AppComponent);
comp = fixture.componentInstance; // component test instance
fixture.detectChanges();
});
it('should create component', inject([AppComponent], (app: AppComponent) => {
// Perform test using fixture and service
expect(app).toBeTruthy();
}));
describe('when the window is resized', () => {
let width: number;
let height: number;
let store: Store<HostWindowState>;
beforeEach(() => {
store = fixture.debugElement.injector.get(Store) as Store<HostWindowState>;
spyOn(store, 'dispatch');
window.dispatchEvent(new Event('resize'));
width = window.innerWidth;
height = window.innerHeight;
});
it('should dispatch a HostWindowResizeAction with the width and height of the window as its payload', () => {
expect(store.dispatch).toHaveBeenCalledWith(new HostWindowResizeAction(width, height));
});
});
describe('removeSsrOverlayWhenStable', () => {
// The inline bootstrap script in src/index.html injects window.__dspaceRemoveSsrOverlay
// and AppComponent must call it exactly once when ApplicationRef.isStable first emits true.
let appRef: ApplicationRef;
let isStable$: BehaviorSubject<boolean>;
let originalRaF: typeof window.requestAnimationFrame;
beforeEach(() => {
appRef = TestBed.inject(ApplicationRef);
isStable$ = new BehaviorSubject<boolean>(false);
// Patch isStable to our controllable subject for this test only
Object.defineProperty(appRef, 'isStable', { value: isStable$.asObservable() });
// Force rAF to a synchronous shim so we can flush() through the chain deterministically.
originalRaF = window.requestAnimationFrame;
(window as any).requestAnimationFrame = (cb: FrameRequestCallback) => {
cb(0);
return 0 as any;
};
});
afterEach(() => {
(window as any).requestAnimationFrame = originalRaF;
delete (window as any).__dspaceRemoveSsrOverlay;
});
it('removes the overlay once isStable emits true', fakeAsync(() => {
const spy = jasmine.createSpy('__dspaceRemoveSsrOverlay');
window.__dspaceRemoveSsrOverlay = spy;
// Re-construct so the constructor-time subscription picks up our patched isStable + global.
const f = TestBed.createComponent(AppComponent);
f.detectChanges();
expect(spy).not.toHaveBeenCalled();
isStable$.next(true);
tick(50); // matches the 50ms pad after rAF in removeSsrOverlayWhenStable
flush();
expect(spy).toHaveBeenCalledTimes(1);
}));
it('is a no-op when the global is not injected (e.g. CSR-only route, SSR skipped)', fakeAsync(() => {
// Global intentionally absent; constructor should not throw and should not break later.
delete (window as any).__dspaceRemoveSsrOverlay;
const f = TestBed.createComponent(AppComponent);
expect(() => f.detectChanges()).not.toThrow();
isStable$.next(true);
tick(50);
flush();
expect(window.__dspaceRemoveSsrOverlay).toBeUndefined();
}));
});
});