How to use NG_MOCKS_OVERRIDES method in ng-mocks

Best JavaScript code snippet using ng-mocks

core.tokens.ts

Source:core.tokens.ts Github

copy

Full Screen

1import { InjectionToken } from '@angular/core';2import { MetadataOverride } from '@angular/core/testing';3import { AnyType } from './core.types';4/**5 * NG_MOCKS token is a map from a declaration to its mock copy.6 *7 * @internal8 *9 * ```ts10 * const MockClass = TestBed.inject(NG_MOCKS).get(RealClass);11 * ```12 */13export const NG_MOCKS = new InjectionToken<Map<any, any>>('NG_MOCKS');14(NG_MOCKS as any).__ngMocksSkip = true;15/**16 * NG_MOCKS_TOUCHES token is a set of all touched declarations during mock process.17 *18 * @internal19 *20 * ```ts21 * const touched = TestBed.inject(NG_MOCKS_TOUCHES).has(RealClass);22 * ```23 */24export const NG_MOCKS_TOUCHES = new InjectionToken<Set<any>>('NG_MOCKS_TOUCHES');25(NG_MOCKS_TOUCHES as any).__ngMocksSkip = true;26/**27 * NG_MOCKS_OVERRIDES token contains overrides for:28 * - TestBed.overrideModule29 * - TestBed.overrideComponent30 * - TestBed.overrideDirective31 * - TestBed.overrideProvider32 *33 * It is used when there is no way to provide a mock copy and an override is required.34 * For example, if we want to keep a component, but to override one of its local providers.35 *36 * @internal37 */38export const NG_MOCKS_OVERRIDES = new InjectionToken<Map<AnyType<any>, MetadataOverride<any>>>('NG_MOCKS_OVERRIDES');39(NG_MOCKS_OVERRIDES as any).__ngMocksSkip = true;40/**41 * NG_MOCKS_GUARDS token influences on provided guards in MockBuilder.42 * More info by the links below.43 *44 * @see https://ng-mocks.sudo.eu/api/MockBuilder#ng_mocks_guards-token45 * @see https://ng-mocks.sudo.eu/guides/routing-guard46 */47export const NG_MOCKS_GUARDS = new InjectionToken<void>('NG_MOCKS_GUARDS');48(NG_MOCKS_GUARDS as any).__ngMocksSkip = true;49/**50 * NG_MOCKS_INTERCEPTORS token influences on provided interceptors in MockBuilder.51 * More info by the links below.52 *53 * @see https://ng-mocks.sudo.eu/api/MockBuilder#ng_mocks_interceptors-token54 * @see https://ng-mocks.sudo.eu/guides/http-interceptor55 */56export const NG_MOCKS_INTERCEPTORS = new InjectionToken<void>('NG_MOCKS_INTERCEPTORS');57(NG_MOCKS_INTERCEPTORS as any).__ngMocksSkip = true;58/**59 * NG_MOCKS_ROOT_PROVIDERS token influences on root providers in MockBuilder,60 * which aren't provided in specified modules.61 * It helps to mock or keep them automatically.62 *63 * @see https://ng-mocks.sudo.eu/api/MockBuilder#ng_mocks_root_providers-token64 */65export const NG_MOCKS_ROOT_PROVIDERS = new InjectionToken<void>('NG_MOCKS_ROOT_PROVIDERS');...

Full Screen

Full Screen

create-ng-mocks-overrides-token.ts

Source:create-ng-mocks-overrides-token.ts Github

copy

Full Screen

1import { ValueProvider } from '@angular/core';2import { MetadataOverride } from '@angular/core/testing';3import { mapValues } from '../../common/core.helpers';4import coreReflectMeta from '../../common/core.reflect.meta';5import { NG_MOCKS_OVERRIDES } from '../../common/core.tokens';6import { Type } from '../../common/core.types';7import ngMocksUniverse from '../../common/ng-mocks-universe';8import getOverrideDef from './get-override-def';9import skipOverride from './skip-override';10export default (replaceDef: Set<any>, defValue: Map<any, any>): ValueProvider => {11 const overrides: Map<Type<any>, [MetadataOverride<any>, MetadataOverride<any>]> = new Map();12 for (const proto of mapValues(ngMocksUniverse.touches)) {13 const source: any = proto;14 const value = ngMocksUniverse.getBuildDeclaration(source) || source;15 if (skipOverride(replaceDef, defValue, source, value)) {16 continue;17 }18 const original = coreReflectMeta(value);19 const override = getOverrideDef(original);20 if (!override) {21 continue;22 }23 // We need to delete standalone, because Angular was too lazy to check whether it has been really changed.24 const patchedOriginal: Partial<typeof original> = {};25 for (const key of Object.keys(override)) {26 patchedOriginal[key] = original[key];27 }28 overrides.set(value, [{ set: override }, { set: patchedOriginal }]);29 }30 return {31 provide: NG_MOCKS_OVERRIDES,32 useValue: overrides,33 };...

Full Screen

Full Screen

func.extract-tokens.ts

Source:func.extract-tokens.ts Github

copy

Full Screen

1import { MetadataOverride } from '@angular/core/testing';2import { flatten } from '../common/core.helpers';3import { NG_MOCKS, NG_MOCKS_OVERRIDES, NG_MOCKS_TOUCHES } from '../common/core.tokens';4import { AnyType } from '../common/core.types';5export default (6 providers: any,7): {8 mocks?: Map<any, any>;9 overrides?: Map<AnyType<any>, [MetadataOverride<any>, MetadataOverride<any>]>;10 touches?: Set<any>;11} => {12 let mocks: Map<any, any> | undefined;13 let overrides: Map<AnyType<any>, [MetadataOverride<any>, MetadataOverride<any>]> | undefined;14 let touches: Set<any> | undefined;15 for (const provide of flatten(providers || [])) {16 if (typeof provide !== 'object') {17 continue;18 }19 if (provide.provide === NG_MOCKS) {20 mocks = provide.useValue;21 }22 if (provide.provide === NG_MOCKS_OVERRIDES) {23 overrides = provide.useValue;24 }25 if (provide.provide === NG_MOCKS_TOUCHES) {26 touches = provide.useValue;27 }28 }29 return {30 mocks,31 overrides,32 touches,33 };...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1import { NG_MOCKS_OVERRIDES } from 'ng-mocks';2import { TestComponent } from './test.component';3describe('TestComponent', () => {4 let component: TestComponent;5 let fixture: ComponentFixture<TestComponent>;6 beforeEach(async(() => {7 TestBed.configureTestingModule({8 NG_MOCKS_OVERRIDES(TestComponent),9 }).compileComponents();10 }));11 beforeEach(() => {12 fixture = TestBed.createComponent(TestComponent);13 component = fixture.componentInstance;14 fixture.detectChanges();15 });16 it('should create', () => {17 expect(component).toBeTruthy();18 });19});20import { NG_MOCKS_OVERRIDES } from 'ng-mocks';21import { TestComponent } from './test.component';22describe('TestComponent', () => {23 let component: TestComponent;24 let fixture: ComponentFixture<TestComponent>;25 beforeEach(async(() => {26 TestBed.configureTestingModule({27 NG_MOCKS_OVERRIDES(TestComponent),28 }).compileComponents();29 }));30 beforeEach(() => {31 fixture = TestBed.createComponent(TestComponent);32 component = fixture.componentInstance;33 fixture.detectChanges();34 });35 it('should create', () => {36 expect(component).toBeTruthy();37 });38});39import { NG_MOCKS_IMPORTS } from 'ng-mocks';40import { TestComponent } from './test.component';41describe('TestComponent', () => {42 let component: TestComponent;43 let fixture: ComponentFixture<TestComponent>;44 beforeEach(async(() => {45 TestBed.configureTestingModule({46 imports: [47 NG_MOCKS_IMPORTS(TestComponent),48 }).compileComponents();49 }));50 beforeEach(() => {51 fixture = TestBed.createComponent(TestComponent);

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