How to use vcrInstance method in ng-mocks

Best JavaScript code snippet using ng-mocks

ng-mocks-global-overrides.ts

Source:ng-mocks-global-overrides.ts Github

copy

Full Screen

1import { ViewContainerRef } from '@angular/core';2import { getTestBed, MetadataOverride, TestBed, TestBedStatic, TestModuleMetadata } from '@angular/core/testing';3import funcExtractTokens from '../mock-builder/func.extract-tokens';4import getOverrideDef from '../mock-builder/promise/get-override-def';5import { ngMocks } from '../mock-helper/mock-helper';6import mockHelperFasterInstall from '../mock-helper/mock-helper.faster-install';7import { MockProvider } from '../mock-provider/mock-provider';8import helperCreateClone from '../mock-service/helper.create-clone';9import coreConfig from './core.config';10import coreDefineProperty from './core.define-property';11import { flatten, mapEntries, mapValues } from './core.helpers';12import coreInjector from './core.injector';13import coreReflectMeta from './core.reflect.meta';14import coreReflectModuleResolve from './core.reflect.module-resolve';15import coreReflectProvidedIn from './core.reflect.provided-in';16import { NG_MOCKS, NG_MOCKS_TOUCHES } from './core.tokens';17import { AnyType, dependencyKeys } from './core.types';18import funcGetProvider from './func.get-provider';19import { isNgDef } from './func.is-ng-def';20import { isNgModuleDefWithProviders } from './func.is-ng-module-def-with-providers';21import ngMocksUniverse from './ng-mocks-universe';22const applyOverride = (def: any, override: any) => {23 if (isNgDef(def, 'c')) {24 TestBed.overrideComponent(def, override);25 } else if (isNgDef(def, 'd')) {26 TestBed.overrideDirective(def, override);27 } else if (isNgDef(def, 'm')) {28 TestBed.overrideModule(def, override);29 }30 if (isNgDef(def, 't')) {31 TestBed.overrideProvider(def, override);32 } else if (isNgDef(def, 'i')) {33 TestBed.overrideProvider(def, override);34 }35};36const applyOverrides = (overrides: Map<AnyType<any>, [MetadataOverride<any>, MetadataOverride<any>]>): void => {37 for (const [def, [override, original]] of mapEntries(overrides)) {38 (TestBed as any).ngMocksOverrides.set(def, {39 ...original,40 override,41 });42 applyOverride(def, override);43 }44};45// Thanks Ivy and its TestBed.override - it does not clean up leftovers.46const applyNgMocksOverrides = (testBed: TestBedStatic & { ngMocksOverrides?: Map<any, any> }): void => {47 if (testBed.ngMocksOverrides?.size) {48 ngMocks.flushTestBed();49 for (const [def, original] of mapEntries(testBed.ngMocksOverrides)) {50 applyOverride(def, original);51 }52 }53 testBed.ngMocksOverrides = undefined;54};55const initTestBed = () => {56 if (!(TestBed as any).ngMocksSelectors) {57 coreDefineProperty(TestBed, 'ngMocksSelectors', new Map());58 }59 // istanbul ignore else60 if (!(TestBed as any).ngMocksOverrides) {61 coreDefineProperty(TestBed, 'ngMocksOverrides', new Map());62 }63};64const generateTouches = (moduleDef: Partial<Record<dependencyKeys, any>>, touches: Set<any>): void => {65 for (const key of coreConfig.dependencies) {66 for (const item of moduleDef[key] ? flatten(moduleDef[key]) : []) {67 let def = funcGetProvider(item);68 if (isNgModuleDefWithProviders(def)) {69 generateTouches(def, touches);70 def = def.ngModule;71 }72 if (touches.has(def)) {73 continue;74 }75 touches.add(def);76 if (typeof def !== 'function') {77 continue;78 }79 if (!Object.prototype.hasOwnProperty.call(def, '__ngMocksTouches')) {80 const local = new Set<any>();81 const meta = coreReflectMeta(def);82 coreDefineProperty(def, '__ngMocksTouches', local, false);83 if (meta) {84 generateTouches(meta, local);85 }86 }87 mapValues(def.__ngMocksTouches, touches);88 }89 }90};91const defineTouches = (testBed: TestBed, moduleDef: TestModuleMetadata, knownTouches?: Set<any>) => {92 let touches = knownTouches;93 if (!touches && ngMocksUniverse.getDefaults().size > 0) {94 touches = funcExtractTokens(95 (testBed as any)._providers || /* istanbul ignore next Ivy part */ (testBed as any)._compiler?.providers,96 ).touches;97 if (!touches) {98 touches = new Set();99 moduleDef.providers = moduleDef.providers || [];100 moduleDef.providers.push({ provide: NG_MOCKS_TOUCHES, useValue: touches });101 }102 generateTouches(moduleDef, touches);103 }104 return touches;105};106const applyPlatformOverrideDef = (def: any) => {107 const ngModule = isNgModuleDefWithProviders(def) ? /* istanbul ignore next */ def.ngModule : def;108 if ((TestBed as any).ngMocksOverrides.has(ngModule)) {109 return;110 }111 const original = coreReflectModuleResolve(ngModule);112 const set = getOverrideDef(original);113 if (set) {114 (TestBed as any).ngMocksOverrides.set(ngModule, { set: original });115 TestBed.overrideModule(ngModule, { set });116 }117};118const applyPlatformOverridesBasedOnProvidedIn = (provide: any, touches: Set<any>) => {119 const providedIn = coreReflectProvidedIn(provide);120 if (!providedIn) {121 return;122 }123 // knownTouches present from MockBuilder and we can rely on it,124 // otherwise we have to override the provider always.125 if (typeof providedIn !== 'string' && !touches.has(providedIn)) {126 return;127 }128 (TestBed as any).ngMocksOverrides.set(provide, {});129 TestBed.overrideProvider(provide, MockProvider(provide as never));130};131const applyPlatformOverridesBasedOnDefaults = (touches: Set<any>) => {132 for (const [provide, [config]] of mapEntries(ngMocksUniverse.getDefaults())) {133 if (config !== 'mock') {134 continue;135 }136 if (!isNgDef(provide, 'i') && !isNgDef(provide, 't')) {137 continue;138 }139 if (touches.has(provide)) {140 continue;141 }142 if ((TestBed as any).ngMocksOverrides.has(provide)) {143 continue;144 }145 applyPlatformOverridesBasedOnProvidedIn(provide, touches);146 }147};148const applyPlatformOverrides = (testBed: TestBed, touches: Set<any>) => {149 // istanbul ignore else150 if ((TestBed as any).ngMocksOverrides) {151 const backup = ngMocksUniverse.touches;152 ngMocksUniverse.touches = touches;153 for (const def of flatten(testBed.ngModule || /* istanbul ignore next */ [])) {154 applyPlatformOverrideDef(def);155 }156 applyPlatformOverridesBasedOnDefaults(touches);157 ngMocksUniverse.touches = backup;158 }159};160const configureTestingModule =161 (162 original: TestBedStatic['configureTestingModule'],163 instance: TestBedStatic,164 ): TestBedStatic['configureTestingModule'] =>165 (moduleDef: TestModuleMetadata) => {166 initTestBed();167 const testBed = getTestBed();168 const providers = funcExtractTokens(moduleDef.providers);169 const { mocks, overrides } = providers;170 // touches are important,171 // therefore we are trying to fetch them from the known providers.172 const touches = defineTouches(testBed, moduleDef, providers.touches);173 if (mocks) {174 ngMocks.flushTestBed();175 }176 // istanbul ignore else177 if (overrides) {178 applyOverrides(overrides);179 }180 // _testModuleRef exists only after the 1st call,181 // so we shouldn't override platform again.182 if (touches && !(testBed as any)._instantiated && !(testBed as any)._testModuleRef) {183 applyPlatformOverrides(testBed, touches);184 }185 return original.call(instance, moduleDef);186 };187const resetTestingModule =188 (original: TestBedStatic['resetTestingModule'], instance: TestBedStatic): TestBedStatic['resetTestingModule'] =>189 () => {190 ngMocksUniverse.global.delete('builder:config');191 ngMocksUniverse.global.delete('builder:module');192 (TestBed as any).ngMocksSelectors = undefined;193 applyNgMocksOverrides(TestBed);194 return original.call(instance);195 };196const viewContainerInstall = () => {197 const vcr: any = ViewContainerRef;198 // istanbul ignore else199 if (!vcr.ngMocksOverridesInstalled) {200 const ngElementId = vcr.__NG_ELEMENT_ID__;201 // istanbul ignore else202 if (ngElementId) {203 coreDefineProperty(204 vcr,205 '__NG_ELEMENT_ID__',206 helperCreateClone(ngElementId, undefined, undefined, (...ngElementIdArgs: any[]) => {207 const vcrInstance = ngElementId.apply(ngElementId, ngElementIdArgs);208 const createComponent = vcrInstance.createComponent;209 coreDefineProperty(210 vcrInstance,211 'createComponent',212 helperCreateClone(213 createComponent,214 undefined,215 undefined,216 (component: any, ...createComponentArgs: any[]) => {217 const map = coreInjector(NG_MOCKS, vcrInstance.injector);218 return createComponent.apply(vcrInstance, [219 map?.get(component) ?? component,220 ...createComponentArgs,221 ] as any);222 },223 ),224 true,225 );226 return vcrInstance;227 }),228 true,229 );230 }231 coreDefineProperty(ViewContainerRef, 'ngMocksOverridesInstalled', true);232 }233};234const install = () => {235 // istanbul ignore else236 if (!(TestBed as any).ngMocksOverridesInstalled) {237 const hooks = mockHelperFasterInstall();238 viewContainerInstall();239 // istanbul ignore else240 if (hooks.before.indexOf(configureTestingModule) === -1) {241 hooks.before.push(configureTestingModule);242 }243 // istanbul ignore else244 if (hooks.after.indexOf(resetTestingModule) === -1) {245 hooks.after.push(resetTestingModule);246 }247 coreDefineProperty(TestBed, 'ngMocksOverridesInstalled', true);248 }249};...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1import { vcrInstance } from 'ng-mocks';2describe('TestComponent', () => {3 let component: TestComponent;4 let fixture: ComponentFixture<TestComponent>;5 beforeEach(async(() => {6 TestBed.configureTestingModule({7 }).compileComponents();8 }));9 beforeEach(() => {10 fixture = TestBed.createComponent(TestComponent);11 component = fixture.componentInstance;12 fixture.detectChanges();13 });14 it('should create', () => {15 expect(component).toBeTruthy();16 });17 it('should have a button', () => {18 const button = vcrInstance(fixture.debugElement.query(By.css('button')));19 expect(button).toBeDefined();20 });21});22import { Component, OnInit } from '@angular/core';23import { FormGroup, FormControl } from '@angular/forms';24@Component({25})26export class TestComponent implements OnInit {27 testForm: FormGroup;28 constructor() { }29 ngOnInit() {30 this.testForm = new FormGroup({31 date: new FormControl()32 });33 }34}35import { Component, OnInit } from '@angular/core';36import { FormBuilder, FormGroup, Validators } from '@angular/forms';37import { Router } from '@angular/router';38import { UsersService } from '../users.service';39@Component({

Full Screen

Using AI Code Generation

copy

Full Screen

1import {vcrInstance} from 'ng-mocks';2describe('TestComponent', () => {3 let component: TestComponent;4 let fixture: ComponentFixture<TestComponent>;5 beforeEach(async(() => {6 TestBed.configureTestingModule({7 })8 .compileComponents();9 }));10 beforeEach(() => {11 fixture = TestBed.createComponent(TestComponent);12 component = fixture.componentInstance;13 fixture.detectChanges();14 });15 it('should create', () => {16 const vcr = vcrInstance(fixture, TestComponent, 0);17 expect(vcr).toBeTruthy();18 });19});20import { Component, ViewContainerRef } from '@angular/core';21@Component({22})23export class TestComponent {24 constructor(public vcr: ViewContainerRef) {}25}

Full Screen

Using AI Code Generation

copy

Full Screen

1import { vcrInstance } from 'ng-mocks';2import { TestBed, ComponentFixture } from '@angular/core/testing';3import { Component } from '@angular/core';4import { ChildComponent } from './child.component';5@Component({6})7class ParentComponent { }8describe('ParentComponent', () => {9 let fixture: ComponentFixture<ParentComponent>;10 let childComponent: ChildComponent;11 beforeEach(() => {12 TestBed.configureTestingModule({13 });14 fixture = TestBed.createComponent(ParentComponent);15 childComponent = vcrInstance(fixture, ChildComponent);16 });17 it('should create the app', () => {18 expect(childComponent).toBeTruthy();19 });20});21import { Component } from '@angular/core';22@Component({23})24export class ChildComponent { }25import { ChildComponent } from './child.component';26import { vcrInstance } from 'ng-mocks';27describe('ChildComponent', () => {28 let childComponent: ChildComponent;29 beforeEach(() => {30 childComponent = new ChildComponent();31 });32 it('should create the app', () => {33 expect(childComponent).toBeTruthy();34 });35});

Full Screen

Using AI Code Generation

copy

Full Screen

1import { vcrInstance } from 'ng-mocks';2import { vcrInstance } from 'ng-mocks';3import { vcrInstance } from 'ng-mocks';4import { vcrInstance } from 'ng-mocks';5import { vcrInstance } from 'ng-mocks';6import { vcrInstance } from 'ng-mocks';7import { vcrInstance } from 'ng-mocks';8import { vcrInstance } from 'ng-mocks';9import { vcrInstance } from 'ng-mocks';10import { vcrInstance } from 'ng-mocks';11import { vcrInstance } from 'ng-mocks';12import { vcrInstance } from 'ng-mocks';13import { vcrInstance } from 'ng-mocks';14import { vcrInstance } from 'ng-mocks';15import { vcrInstance } from 'ng-mocks';16import { vcrInstance } from 'ng-mocks';17import { vcrInstance } from 'ng-mocks';18import { vcrInstance } from 'ng-mocks';

Full Screen

Using AI Code Generation

copy

Full Screen

1import vcrInstance from 'ng-mocks/dist/vcr-instance';2import vcrInstance from 'ng-mocks/dist/vcr-instance';3import vcrInstance from 'ng-mocks/dist/vcr-instance';4import vcrInstance from 'ng-mocks/dist/vcr-instance';5import vcrInstance from 'ng-mocks/dist/vcr-instance';6import vcrInstance from 'ng-mocks/dist/vcr-instance';7import vcrInstance from 'ng-mocks/dist/vcr-instance';8import vcrInstance from 'ng-mocks/dist/vcr-instance';9import vcrInstance from 'ng-mocks/dist/vcr-instance';10import vcrInstance from 'ng-mocks/dist/vcr-instance';11import vcrInstance from 'ng-mocks/dist/vcr-instance';12import vcrInstance from 'ng-mocks/dist/vcr-instance';13import vcrInstance from 'ng-mocks/dist/vcr-instance';14import vcrInstance from 'ng-mocks/dist/vcr-instance';15import vcrInstance from 'ng-mocks/dist/vcr-instance';16import vcrInstance from 'ng-mocks/dist/vcr-instance';17import vcrInstance from 'ng-mocks/dist/vcr-instance';

Full Screen

Using AI Code Generation

copy

Full Screen

1const vcrInstance = ngMocks.vcrInstance(fixture, MyComponent);2const vcrInstance = ngMocks.vcrInstance(fixture, MyComponent);3const vcrInstance = ngMocks.vcrInstance(fixture, MyComponent);4const vcrInstance = ngMocks.vcrInstance(fixture, MyComponent);5const vcrInstance = ngMocks.vcrInstance(fixture, MyComponent);6const vcrInstance = ngMocks.vcrInstance(fixture, MyComponent);7const vcrInstance = ngMocks.vcrInstance(fixture, MyComponent);8const vcrInstance = ngMocks.vcrInstance(fixture, MyComponent);9const vcrInstance = ngMocks.vcrInstance(fixture, MyComponent);10const vcrInstance = ngMocks.vcrInstance(fixture, MyComponent);11const vcrInstance = ngMocks.vcrInstance(fixture, MyComponent);

Full Screen

Using AI Code Generation

copy

Full Screen

1import { vcrInstance } from 'ng-mocks';2import { Component } from '@angular/core';3import { TestBed } from '@angular/core/testing';4import { ComponentFixture } from '@angular/core/testing';5import { async } from '@angular/core/testing';6import { fakeAsync } from '@angular/core/testing';7import { tick } from '@angular/core/testing';8describe('test', () => {9 let fixture: ComponentFixture<TestComponent>;10 let component: TestComponent;11 beforeEach(async(() => {12 TestBed.configureTestingModule({13 }).compileComponents();14 }));15 beforeEach(() => {16 fixture = TestBed.createComponent(TestComponent);17 component = fixture.componentInstance;18 fixture.detectChanges();19 });20 it('should test', fakeAsync(() => {21 const vcr = vcrInstance(fixture, TestComponent);22 vcr.detectChanges();23 tick();24 expect(vcr).toBeDefined();25 }));26});27import { Component, ViewChild, ViewContainerRef } from '@angular/core';28@Component({29})30export class TestComponent {31 @ViewChild('container', { read: ViewContainerRef })32 public container: ViewContainerRef;33}34.test {35 background-color: blue;36}37import { vcrInstance } from 'ng-mocks';

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run ng-mocks automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful