diff --git a/src/app/classes/singletons-factory.ts b/src/app/classes/singletons-factory.ts index 68a7d87dc..3415762a2 100644 --- a/src/app/classes/singletons-factory.ts +++ b/src/app/classes/singletons-factory.ts @@ -27,7 +27,10 @@ export type CoreInjectionToken = Type | Type | string /** * Singleton class created using the factory. */ -export type CoreSingletonClass = typeof CoreSingleton & { instance: Service }; +export type CoreSingletonClass = typeof CoreSingleton & { + instance: Service; + setInstance(instance: Service): void; +}; /** * Factory used to create CoreSingleton classes that get instances from an injector. @@ -71,6 +74,10 @@ export class CoreSingletonsFactory { return this.serviceInstance; } + static setInstance(instance: Service): void { + this.serviceInstance = instance; + } + }; } } diff --git a/src/app/core/login/tests/init.page.test.ts b/src/app/core/login/tests/init.page.test.ts index 8e7c63069..fad3fed51 100644 --- a/src/app/core/login/tests/init.page.test.ts +++ b/src/app/core/login/tests/init.page.test.ts @@ -12,13 +12,24 @@ // See the License for the specific language governing permissions and // 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 { CoreApp } from '@/app/services/app'; + +import { createComponent, preparePageTest, PageTestMocks, mockSingleton } from '@/tests/utils'; 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', () => { const fixture = createComponent(CoreLoginInitPage); @@ -27,4 +38,13 @@ describe('CoreLogin Init Page', () => { 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']); + }); + }); diff --git a/src/tests/utils.ts b/src/tests/utils.ts index 869c925bc..6747a41df 100644 --- a/src/tests/utils.ts +++ b/src/tests/utils.ts @@ -14,15 +14,61 @@ import { CUSTOM_ELEMENTS_SCHEMA, Type } from '@angular/core'; 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): Promise { +export interface ComponentTestMocks { + // +}; + +export interface PageTestMocks extends ComponentTestMocks { + router: Router; +} + +export function createMock(methods: string[] = [], properties: Record = {}): T { + const mockObject = properties; + + for (const method of methods) { + mockObject[method] = jest.fn(); + } + + return mockObject as T; +} + +export function mockSingleton( + singletonClass: CoreSingletonClass, + methods: string[] = [], + properties: Record = {}, +): void { + singletonClass.setInstance(createMock(methods, properties)); +} + +export async function prepareComponentTest(component: Type, providers: unknown[] = []): Promise { TestBed.configureTestingModule({ declarations: [component], schemas: [CUSTOM_ELEMENTS_SCHEMA], + providers, }); await TestBed.compileComponents(); + + return {}; +} + +export async function preparePageTest(component: Type, providers: unknown[] = []): Promise { + const mocks = { + router: createMock(['navigate']), + }; + + const componentTestMocks = await prepareComponentTest(component, [ + ...providers, + { provide: Router, useValue: mocks.router }, + ]); + + return { + ...componentTestMocks, + ...mocks, + }; } export function createComponent(component: Type): ComponentFixture { diff --git a/tsconfig.test.json b/tsconfig.test.json index f2372eab2..2d14a2529 100644 --- a/tsconfig.test.json +++ b/tsconfig.test.json @@ -1,7 +1,9 @@ { "extends": "./tsconfig.json", "compilerOptions": { + "allowJs": true, "esModuleInterop": true, + "emitDecoratorMetadata": true, "outDir": "./out-tsc/tests", "types": [ "jest",