// (C) Copyright 2015 Moodle Pty Ltd. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. import { Component, CUSTOM_ELEMENTS_SCHEMA, Type, ViewChild } from '@angular/core'; import { ComponentFixture, TestBed } from '@angular/core/testing'; import { CoreSingletonClass } from '@classes/singletons-factory'; abstract class WrapperComponent { child!: U; }; export interface RenderConfig { declarations: unknown[]; providers: unknown[]; } export type WrapperComponentFixture = ComponentFixture>; export function mock(instance?: Record): T; export function mock(methods: string[], instance?: Record): T; export function mock( methodsOrInstance: string[] | Record = [], instance: Record = {}, ): T { instance = Array.isArray(methodsOrInstance) ? instance : methodsOrInstance; const methods = Array.isArray(methodsOrInstance) ? methodsOrInstance : []; for (const method of methods) { instance[method] = jest.fn(); } return instance as T; } export function mockSingleton(singletonClass: CoreSingletonClass, instance?: Record): void; export function mockSingleton( singletonClass: CoreSingletonClass, methods: string[], instance?: Record, ): void; export function mockSingleton( singletonClass: CoreSingletonClass, methodsOrInstance: string[] | Record = [], instance: Record = {}, ): void { instance = Array.isArray(methodsOrInstance) ? instance : methodsOrInstance; const methods = Array.isArray(methodsOrInstance) ? methodsOrInstance : []; singletonClass.setInstance(mock(methods, instance)); } export async function renderComponent(component: Type, config: Partial = {}): Promise> { return renderAngularComponent(component, { declarations: [], providers: [], ...config, }); } export async function renderTemplate( component: Type, template: string, config: Partial = {}, ): Promise> { config.declarations = config.declarations ?? []; config.declarations.push(component); return renderAngularComponent( createWrapperComponent(template, component), { declarations: [], providers: [], ...config, }, ); } export async function renderWrapperComponent( component: Type, tag: string, inputs: Record = {}, config: Partial = {}, ): Promise> { const inputAttributes = Object .entries(inputs) .map(([name, value]) => `${name}="${value.toString().replace(/"/g, '"')}"`) .join(' '); return renderTemplate(component, `<${tag} ${inputAttributes}>`, config); } async function renderAngularComponent(component: Type, config: RenderConfig): Promise> { config.declarations.push(component); TestBed.configureTestingModule({ declarations: config.declarations, schemas: [CUSTOM_ELEMENTS_SCHEMA], providers: config.providers, }); await TestBed.compileComponents(); const fixture = TestBed.createComponent(component); fixture.autoDetectChanges(true); await fixture.whenRenderingDone(); await fixture.whenStable(); return fixture; } function createWrapperComponent(template: string, componentClass: Type): Type> { @Component({ template }) class HostComponent extends WrapperComponent { @ViewChild(componentClass) child!: U; } return HostComponent; }