2016-08-02 10:38:14 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
2017-05-16 18:14:55 -04:00
|
|
|
import {Injector} from '@angular/core';
|
2018-11-05 23:31:54 -05:00
|
|
|
import {APP_INITIALIZER, ApplicationInitStatus} from '@angular/core/src/application_init';
|
2020-04-13 19:40:21 -04:00
|
|
|
|
|
|
|
import {async, inject, TestBed} 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`',
|
2016-08-19 15:10:53 -04:00
|
|
|
async(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`',
|
2016-08-02 10:54:14 -04:00
|
|
|
async(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
|
|
|
})));
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('with async initializers', () => {
|
2016-08-02 18:53:34 -04:00
|
|
|
let resolve: (result: any) => void;
|
|
|
|
let promise: Promise<any>;
|
2017-05-16 18:14:55 -04:00
|
|
|
let completerResolver = false;
|
2016-08-02 10:38:14 -04:00
|
|
|
beforeEach(() => {
|
2017-05-16 18:14:55 -04:00
|
|
|
let initializerFactory = (injector: Injector) => {
|
|
|
|
return () => {
|
|
|
|
const initStatus = injector.get(ApplicationInitStatus);
|
2020-04-13 19:40:21 -04:00
|
|
|
initStatus.donePromise.then(() => {
|
|
|
|
expect(completerResolver).toBe(true);
|
|
|
|
});
|
2017-07-27 19:13:16 -04:00
|
|
|
};
|
2017-05-16 18:14:55 -04:00
|
|
|
};
|
2020-04-13 19:40:21 -04:00
|
|
|
promise = new Promise((res) => {
|
|
|
|
resolve = res;
|
|
|
|
});
|
2017-05-16 18:14:55 -04:00
|
|
|
TestBed.configureTestingModule({
|
|
|
|
providers: [
|
|
|
|
{provide: APP_INITIALIZER, multi: true, useValue: () => promise},
|
|
|
|
{
|
|
|
|
provide: APP_INITIALIZER,
|
|
|
|
multi: true,
|
|
|
|
useFactory: initializerFactory,
|
|
|
|
deps: [Injector]
|
|
|
|
},
|
|
|
|
]
|
|
|
|
});
|
2016-08-02 10:38:14 -04:00
|
|
|
});
|
|
|
|
|
2016-12-29 12:41:21 -05:00
|
|
|
it('should update the status once all async initializers are done',
|
2016-08-02 10:54:14 -04:00
|
|
|
async(inject([ApplicationInitStatus], (status: ApplicationInitStatus) => {
|
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(() => {
|
|
|
|
completerResolver = 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);
|
|
|
|
expect(completerResolver).toBe(true);
|
|
|
|
});
|
|
|
|
})));
|
|
|
|
});
|
|
|
|
});
|
2016-08-02 18:53:34 -04:00
|
|
|
}
|