2016-08-02 10:38:14 -04:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 15:08:49 -04:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2016-08-02 10:38:14 -04:00
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
2018-11-05 23:31:54 -05:00
|
|
|
import {APP_INITIALIZER, ApplicationInitStatus} from '@angular/core/src/application_init';
|
2019-10-17 05:48:35 -04:00
|
|
|
import {Observable, Subscriber} from 'rxjs';
|
2020-04-13 19:40:21 -04:00
|
|
|
|
2020-07-31 15:43:18 -04:00
|
|
|
import {inject, TestBed, waitForAsync} from '../testing';
|
2016-08-02 10:38:14 -04:00
|
|
|
|
2017-12-15 19:28:41 -05:00
|
|
|
{
|
2016-08-02 10:54:14 -04:00
|
|
|
describe('ApplicationInitStatus', () => {
|
2016-08-02 10:38:14 -04:00
|
|
|
describe('no initializers', () => {
|
|
|
|
it('should return true for `done`',
|
2020-07-31 15:43:18 -04:00
|
|
|
waitForAsync(inject([ApplicationInitStatus], (status: ApplicationInitStatus) => {
|
2017-12-06 09:56:49 -05:00
|
|
|
(status as any).runInitializers();
|
2016-08-02 10:54:14 -04:00
|
|
|
expect(status.done).toBe(true);
|
2016-08-19 15:10:53 -04:00
|
|
|
})));
|
2016-08-02 10:38:14 -04:00
|
|
|
|
|
|
|
it('should return a promise that resolves immediately for `donePromise`',
|
2020-07-31 15:43:18 -04:00
|
|
|
waitForAsync(inject([ApplicationInitStatus], (status: ApplicationInitStatus) => {
|
2017-12-06 09:56:49 -05:00
|
|
|
(status as any).runInitializers();
|
2020-04-13 19:40:21 -04:00
|
|
|
status.donePromise.then(() => {
|
|
|
|
expect(status.done).toBe(true);
|
|
|
|
});
|
2016-08-02 10:38:14 -04:00
|
|
|
})));
|
|
|
|
});
|
|
|
|
|
2019-10-17 05:48:35 -04:00
|
|
|
describe('with async promise initializers', () => {
|
2016-08-02 18:53:34 -04:00
|
|
|
let resolve: (result: any) => void;
|
2019-10-17 05:48:35 -04:00
|
|
|
let reject: (reason?: any) => void;
|
2016-08-02 18:53:34 -04:00
|
|
|
let promise: Promise<any>;
|
2019-10-17 05:48:35 -04:00
|
|
|
let initFnInvoked = false;
|
2016-08-02 10:38:14 -04:00
|
|
|
beforeEach(() => {
|
2019-10-17 05:48:35 -04:00
|
|
|
promise = new Promise((res, rej) => {
|
2020-04-13 19:40:21 -04:00
|
|
|
resolve = res;
|
2019-10-17 05:48:35 -04:00
|
|
|
reject = rej;
|
2020-04-13 19:40:21 -04:00
|
|
|
});
|
2017-05-16 18:14:55 -04:00
|
|
|
TestBed.configureTestingModule({
|
|
|
|
providers: [
|
|
|
|
{provide: APP_INITIALIZER, multi: true, useValue: () => promise},
|
|
|
|
]
|
|
|
|
});
|
2016-08-02 10:38:14 -04:00
|
|
|
});
|
|
|
|
|
2019-10-17 05:48:35 -04:00
|
|
|
it('should update the status once all async promise initializers are done',
|
2020-07-31 15:43:18 -04:00
|
|
|
waitForAsync(inject([ApplicationInitStatus], (status: ApplicationInitStatus) => {
|
2019-10-17 05:48:35 -04:00
|
|
|
// Accessing internal `runInitializers` function of the `ApplicationInitStatus` class
|
|
|
|
// instance for testing purposes to invoke initializer functions.
|
2017-12-06 09:56:49 -05:00
|
|
|
(status as any).runInitializers();
|
2017-05-16 18:14:55 -04:00
|
|
|
|
2016-08-02 10:38:14 -04:00
|
|
|
setTimeout(() => {
|
2019-10-17 05:48:35 -04:00
|
|
|
initFnInvoked = true;
|
2016-08-02 18:53:34 -04:00
|
|
|
resolve(null);
|
2016-08-02 10:38:14 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(status.done).toBe(false);
|
|
|
|
status.donePromise.then(() => {
|
|
|
|
expect(status.done).toBe(true);
|
2019-10-17 05:48:35 -04:00
|
|
|
expect(initFnInvoked).toBe(true);
|
2016-08-02 10:38:14 -04:00
|
|
|
});
|
|
|
|
})));
|
2019-10-17 05:48:35 -04:00
|
|
|
|
|
|
|
it('should handle a case when promise is rejected',
|
|
|
|
waitForAsync(inject([ApplicationInitStatus], (status: ApplicationInitStatus) => {
|
|
|
|
// Accessing internal `runInitializers` function of the `ApplicationInitStatus` class
|
|
|
|
// instance for testing purposes to invoke initializer functions.
|
|
|
|
(status as any).runInitializers();
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
initFnInvoked = true;
|
|
|
|
reject();
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(status.done).toBe(false);
|
|
|
|
status.donePromise
|
|
|
|
.then(
|
|
|
|
() => fail('`donePromise.then` should not be invoked when promise is rejected'))
|
|
|
|
.catch(() => {
|
|
|
|
expect(status.done).toBe(false);
|
|
|
|
expect(initFnInvoked).toBe(true);
|
|
|
|
});
|
|
|
|
})));
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('with app initializers represented using observables', () => {
|
|
|
|
let subscriber: Subscriber<any>;
|
|
|
|
let observable: Observable<any>;
|
|
|
|
let initFnInvoked = false;
|
|
|
|
beforeEach(() => {
|
|
|
|
observable = new Observable((res) => {
|
|
|
|
subscriber = res;
|
|
|
|
});
|
|
|
|
TestBed.configureTestingModule({
|
|
|
|
providers: [
|
|
|
|
{provide: APP_INITIALIZER, multi: true, useValue: () => observable},
|
|
|
|
]
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should update the status once all async observable initializers are completed',
|
|
|
|
waitForAsync(inject([ApplicationInitStatus], (status: ApplicationInitStatus) => {
|
|
|
|
// Accessing internal `runInitializers` function of the `ApplicationInitStatus` class
|
|
|
|
// instance for testing purposes to invoke initializer functions.
|
|
|
|
(status as any).runInitializers();
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
initFnInvoked = true;
|
|
|
|
subscriber.complete();
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(status.done).toBe(false);
|
|
|
|
status.donePromise.then(() => {
|
|
|
|
expect(status.done).toBe(true);
|
|
|
|
expect(initFnInvoked).toBe(true);
|
|
|
|
});
|
|
|
|
})));
|
|
|
|
|
|
|
|
it('should update the status once all async observable initializers nexted and completed',
|
|
|
|
waitForAsync(inject([ApplicationInitStatus], (status: ApplicationInitStatus) => {
|
|
|
|
// Accessing internal `runInitializers` function of the `ApplicationInitStatus` class
|
|
|
|
// instance for testing purposes to invoke initializer functions.
|
|
|
|
(status as any).runInitializers();
|
|
|
|
|
|
|
|
subscriber.next('one');
|
|
|
|
subscriber.next('two');
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
initFnInvoked = true;
|
|
|
|
subscriber.complete();
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(status.done).toBe(false);
|
|
|
|
status.donePromise.then(() => {
|
|
|
|
expect(status.done).toBe(true);
|
|
|
|
expect(initFnInvoked).toBe(true);
|
|
|
|
});
|
|
|
|
})));
|
|
|
|
|
|
|
|
it('should update the status if all async observable initializers are completed before runInitializers',
|
|
|
|
waitForAsync(inject([ApplicationInitStatus], (status: ApplicationInitStatus) => {
|
|
|
|
subscriber.complete();
|
|
|
|
// Accessing internal `runInitializers` function of the `ApplicationInitStatus` class
|
|
|
|
// instance for testing purposes to invoke initializer functions.
|
|
|
|
(status as any).runInitializers();
|
|
|
|
|
|
|
|
expect(status.done).toBe(false);
|
|
|
|
|
|
|
|
status.donePromise.then(() => {
|
|
|
|
expect(status.done).toBe(true);
|
|
|
|
});
|
|
|
|
})));
|
|
|
|
|
|
|
|
it('should handle a case when observable emits an error',
|
|
|
|
waitForAsync(inject([ApplicationInitStatus], (status: ApplicationInitStatus) => {
|
|
|
|
// Accessing internal `runInitializers` function of the `ApplicationInitStatus` class
|
|
|
|
// instance for testing purposes to invoke initializer functions.
|
|
|
|
(status as any).runInitializers();
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
initFnInvoked = true;
|
|
|
|
subscriber.error();
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(status.done).toBe(false);
|
|
|
|
status.donePromise
|
|
|
|
.then(
|
|
|
|
() => fail(
|
|
|
|
'`donePromise.then` should not be invoked when observable emits an error'))
|
|
|
|
.catch(() => {
|
|
|
|
expect(status.done).toBe(false);
|
|
|
|
expect(initFnInvoked).toBe(true);
|
|
|
|
});
|
|
|
|
})));
|
2016-08-02 10:38:14 -04:00
|
|
|
});
|
|
|
|
});
|
2016-08-02 18:53:34 -04:00
|
|
|
}
|