How to use TOKEN_EXISTING_KEEP method in ng-mocks

Best JavaScript code snippet using ng-mocks

test.spec.ts

Source:test.spec.ts Github

copy

Full Screen

1import { Injectable, InjectionToken, NgModule } from '@angular/core';2import { MockBuilder, ngMocks } from 'ng-mocks';3@Injectable()4class Exist1Service {5 public readonly name = 'exist1';6}7@Injectable()8class Exist2Service {9 public readonly name = 'exist2';10}11const TOKEN_EXISTING_MOCK = new InjectionToken('MOCK');12const TOKEN_EXISTING_KEEP = new InjectionToken('KEEP');13@NgModule({14 providers: [15 Exist1Service,16 Exist2Service,17 {18 provide: TOKEN_EXISTING_MOCK,19 useExisting: Exist1Service,20 },21 {22 provide: TOKEN_EXISTING_KEEP,23 useExisting: Exist2Service,24 },25 ],26})27class TargetModule {}28// fix for jest without jasmine assertions29const assertion: any =30 typeof jasmine === 'undefined' ? expect : jasmine;31// We should do nothing about a useExisting provider, because32// the question comes whether its pointer has been replaced with a mock copy or not.33describe('tokens-existing', () => {34 ngMocks.faster();35 beforeEach(() =>36 MockBuilder().mock(TargetModule).keep(Exist2Service),37 );38 it('resolves TOKEN_EXISTING_MOCK as a mock service', () => {39 const actual = ngMocks.findInstance<any>(TOKEN_EXISTING_MOCK);40 expect(actual).toEqual(assertion.any(Exist1Service));41 expect(actual.name).toBeUndefined();42 });43 it('resolves TOKEN_EXISTING_KEEP as a real service', () => {44 const actual = ngMocks.findInstance<any>(TOKEN_EXISTING_KEEP);45 expect(actual).toEqual(assertion.any(Exist2Service));46 expect(actual.name).toEqual('exist2');47 });...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1import { MockBuilder, MockRender, ngMocks } from 'ng-mocks';2import { AppModule } from './app.module';3import { AppComponent } from './app.component';4const mockService = ngMocks.defaultMock(AppService, {5 getData: () => 'mocked',6});7const mockComponent = ngMocks.defaultMock(AppComponent, {8});9const mockComponent2 = ngMocks.defaultMock(AppComponent, {10});11describe('AppComponent', () => {12 beforeEach(() => MockBuilder(AppComponent, AppModule));13 it('should render title', () => {14 const fixture = MockRender(AppComponent);15 expect(fixture.nativeElement.querySelector('h1').textContent).toContain(16 );17 });18});19import { NgModule } from '@angular/core';20import { BrowserModule } from '@angular/platform-browser';21import { AppComponent } from './app.component';22import { AppService } from './app.service';23@NgModule({24 imports: [BrowserModule],25})26export class AppModule {}27import { Component } from '@angular/core';28import { AppService } from './app.service';29@Component({30 {{ getData() }}31})32export class AppComponent {33 constructor(private readonly service: AppService) {}34 public getData(): string {35 return this.service.getData();36 }37}38import { Injectable } from '@angular/core';39@Injectable()40export class AppService {41 public getData(): string {42 return 'real';43 }44}45import { TestBed } from '@angular/core/testing';46import { AppService } from './app.service';47describe('AppService', () => {48 let service: AppService;49 beforeEach(() => {50 TestBed.configureTestingModule({});51 service = TestBed.inject(AppService);

Full Screen

Using AI Code Generation

copy

Full Screen

1import {TOKEN_EXISTING_KEEP} from 'ng-mocks';2import {APP_CONFIG} from './app-config';3export const config = {4 useValue: {5 },6};

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