MOBILE-3833 test: Set up default singletons

main
Noel De Martin 2021-08-12 13:08:19 +02:00
parent 9a5772c72b
commit ae351a49b2
1 changed files with 16 additions and 7 deletions

View File

@ -15,12 +15,10 @@
import { AbstractType, Component, CUSTOM_ELEMENTS_SCHEMA, Type, ViewChild } from '@angular/core'; import { AbstractType, Component, CUSTOM_ELEMENTS_SCHEMA, Type, ViewChild } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser'; import { BrowserModule } from '@angular/platform-browser';
import { ComponentFixture, TestBed } from '@angular/core/testing'; import { ComponentFixture, TestBed } from '@angular/core/testing';
import { Network } from '@ionic-native/network/ngx';
import { Observable, Subject } from 'rxjs'; import { Observable, Subject } from 'rxjs';
import { Platform } from '@ionic/angular';
import { CORE_SITE_SCHEMAS } from '@services/sites'; import { CORE_SITE_SCHEMAS } from '@services/sites';
import { CoreSingletonProxy } from '@singletons'; import { CoreSingletonProxy, Network, Platform } from '@singletons';
import { CoreTextUtilsProvider } from '@services/utils/text'; import { CoreTextUtilsProvider } from '@services/utils/text';
import { TranslatePipeStub } from './stubs/pipes/translate'; import { TranslatePipeStub } from './stubs/pipes/translate';
@ -36,6 +34,10 @@ type ServiceInjectionToken = AbstractType<unknown> | Type<unknown> | string;
let testBedInitialized = false; let testBedInitialized = false;
const textUtils = new CoreTextUtilsProvider(); const textUtils = new CoreTextUtilsProvider();
const DEFAULT_SERVICE_SINGLETON_MOCKS: [CoreSingletonProxy, Record<string, unknown>][] = [
[Platform, mock({ is: () => false, ready: () => Promise.resolve(), resume: new Subject<void>() })],
[Network, { onChange: () => new Observable() }],
];
async function renderAngularComponent<T>(component: Type<T>, config: RenderConfig): Promise<ComponentFixture<T>> { async function renderAngularComponent<T>(component: Type<T>, config: RenderConfig): Promise<ComponentFixture<T>> {
config.declarations.push(component); config.declarations.push(component);
@ -86,12 +88,15 @@ function getDefaultDeclarations(): unknown[] {
} }
function getDefaultProviders(): unknown[] { function getDefaultProviders(): unknown[] {
const platformMock = mock<Platform>({ is: () => false, ready: () => Promise.resolve(), resume: new Subject<void>() }); const serviceProviders = DEFAULT_SERVICE_SINGLETON_MOCKS.map(
const networkMock = mock<Network>({ onChange: () => new Observable() }); ([singleton, mockInstance]) => ({
provide: singleton.injectionToken,
useValue: mockInstance,
}),
);
return [ return [
{ provide: Platform, useValue: platformMock }, ...serviceProviders,
{ provide: Network, useValue: networkMock },
{ provide: CORE_SITE_SCHEMAS, multiple: true, useValue: [] }, { provide: CORE_SITE_SCHEMAS, multiple: true, useValue: [] },
]; ];
} }
@ -175,6 +180,10 @@ export function mockSingleton<T>(
export function resetTestingEnvironment(): void { export function resetTestingEnvironment(): void {
testBedInitialized = false; testBedInitialized = false;
for (const [singleton, mockInstance] of DEFAULT_SERVICE_SINGLETON_MOCKS) {
mockSingleton(singleton, mockInstance);
}
} }
export function getServiceInstance(injectionToken: ServiceInjectionToken): Record<string, unknown> { export function getServiceInstance(injectionToken: ServiceInjectionToken): Record<string, unknown> {