MOBILE-3320 tests: Fix tests

main
Noel De Martin 2020-10-07 11:13:54 +02:00 committed by Dani Palou
parent 1617c30cc3
commit 909444a2a6
4 changed files with 81 additions and 6 deletions

View File

@ -27,7 +27,10 @@ export type CoreInjectionToken<Service> = Type<Service> | Type<unknown> | string
/** /**
* Singleton class created using the factory. * Singleton class created using the factory.
*/ */
export type CoreSingletonClass<Service> = typeof CoreSingleton & { instance: Service }; export type CoreSingletonClass<Service> = typeof CoreSingleton & {
instance: Service;
setInstance(instance: Service): void;
};
/** /**
* Factory used to create CoreSingleton classes that get instances from an injector. * Factory used to create CoreSingleton classes that get instances from an injector.
@ -71,6 +74,10 @@ export class CoreSingletonsFactory {
return this.serviceInstance; return this.serviceInstance;
} }
static setInstance(instance: Service): void {
this.serviceInstance = instance;
}
}; };
} }
} }

View File

@ -12,13 +12,24 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
import { createComponent, prepareComponentTest } from '@/tests/utils'; import { CoreInit } from '@services/init';
import { CoreLoginInitPage } from '@core/login/pages/init/init.page'; import { CoreLoginInitPage } from '@core/login/pages/init/init.page';
import { CoreApp } from '@/app/services/app';
import { createComponent, preparePageTest, PageTestMocks, mockSingleton } from '@/tests/utils';
describe('CoreLogin Init Page', () => { describe('CoreLogin Init Page', () => {
beforeEach(() => prepareComponentTest(CoreLoginInitPage)); let mocks: PageTestMocks;
beforeEach(async () => {
const initPromise = Promise.resolve();
mockSingleton(CoreInit, [], { ready: () => initPromise });
mockSingleton(CoreApp, [], { getRedirect: () => ({}) });
mocks = await preparePageTest(CoreLoginInitPage);
});
it('should render', () => { it('should render', () => {
const fixture = createComponent(CoreLoginInitPage); const fixture = createComponent(CoreLoginInitPage);
@ -27,4 +38,13 @@ describe('CoreLogin Init Page', () => {
expect(fixture.nativeElement.querySelector('ion-spinner')).toBeTruthy(); expect(fixture.nativeElement.querySelector('ion-spinner')).toBeTruthy();
}); });
it('navigates to site page after loading', async () => {
const fixture = createComponent(CoreLoginInitPage);
fixture.componentInstance.ngOnInit();
await CoreInit.instance.ready();
expect(mocks.router.navigate).toHaveBeenCalledWith(['/login/site']);
});
}); });

View File

@ -14,15 +14,61 @@
import { CUSTOM_ELEMENTS_SCHEMA, Type } from '@angular/core'; import { CUSTOM_ELEMENTS_SCHEMA, Type } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing'; import { ComponentFixture, TestBed } from '@angular/core/testing';
import { Component } from '@angular/compiler/src/core'; import { Router } from '@angular/router';
import { CoreSingletonClass } from '@app/classes/singletons-factory';
export async function prepareComponentTest(component: Type<Component>): Promise<void> { export interface ComponentTestMocks {
//
};
export interface PageTestMocks extends ComponentTestMocks {
router: Router;
}
export function createMock<T>(methods: string[] = [], properties: Record<string, unknown> = {}): T {
const mockObject = properties;
for (const method of methods) {
mockObject[method] = jest.fn();
}
return mockObject as T;
}
export function mockSingleton(
singletonClass: CoreSingletonClass<unknown>,
methods: string[] = [],
properties: Record<string, unknown> = {},
): void {
singletonClass.setInstance(createMock(methods, properties));
}
export async function prepareComponentTest<T>(component: Type<T>, providers: unknown[] = []): Promise<ComponentTestMocks> {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
declarations: [component], declarations: [component],
schemas: [CUSTOM_ELEMENTS_SCHEMA], schemas: [CUSTOM_ELEMENTS_SCHEMA],
providers,
}); });
await TestBed.compileComponents(); await TestBed.compileComponents();
return {};
}
export async function preparePageTest<T>(component: Type<T>, providers: unknown[] = []): Promise<PageTestMocks> {
const mocks = {
router: createMock<Router>(['navigate']),
};
const componentTestMocks = await prepareComponentTest(component, [
...providers,
{ provide: Router, useValue: mocks.router },
]);
return {
...componentTestMocks,
...mocks,
};
} }
export function createComponent<T>(component: Type<T>): ComponentFixture<T> { export function createComponent<T>(component: Type<T>): ComponentFixture<T> {

View File

@ -1,7 +1,9 @@
{ {
"extends": "./tsconfig.json", "extends": "./tsconfig.json",
"compilerOptions": { "compilerOptions": {
"allowJs": true,
"esModuleInterop": true, "esModuleInterop": true,
"emitDecoratorMetadata": true,
"outDir": "./out-tsc/tests", "outDir": "./out-tsc/tests",
"types": [ "types": [
"jest", "jest",