MOBILE-3320 tests: Fix tests
parent
1617c30cc3
commit
909444a2a6
|
@ -27,7 +27,10 @@ export type CoreInjectionToken<Service> = Type<Service> | Type<unknown> | string
|
|||
/**
|
||||
* 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.
|
||||
|
@ -71,6 +74,10 @@ export class CoreSingletonsFactory {
|
|||
return this.serviceInstance;
|
||||
}
|
||||
|
||||
static setInstance(instance: Service): void {
|
||||
this.serviceInstance = instance;
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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']);
|
||||
});
|
||||
|
||||
});
|
||||
|
|
|
@ -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<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({
|
||||
declarations: [component],
|
||||
schemas: [CUSTOM_ELEMENTS_SCHEMA],
|
||||
providers,
|
||||
});
|
||||
|
||||
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> {
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
{
|
||||
"extends": "./tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"allowJs": true,
|
||||
"esModuleInterop": true,
|
||||
"emitDecoratorMetadata": true,
|
||||
"outDir": "./out-tsc/tests",
|
||||
"types": [
|
||||
"jest",
|
||||
|
|
Loading…
Reference in New Issue