How to use TOKEN_EXISTING 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, MockRender, ngMocks } from 'ng-mocks';3const TOKEN_CLASS = new InjectionToken('CLASS');4const TOKEN_EXISTING = new InjectionToken('EXISTING');5const TOKEN_FACTORY = new InjectionToken('FACTORY');6const TOKEN_VALUE = new InjectionToken('VALUE');7class ServiceClass {8 public readonly name = 'class';9}10@Injectable()11class ServiceExisting {12 public readonly name = 'existing';13}14// A module that provides all services.15@NgModule({16 providers: [17 ServiceExisting,18 {19 provide: TOKEN_CLASS,20 useClass: ServiceClass,21 },22 {23 provide: TOKEN_EXISTING,24 useExisting: ServiceExisting,25 },26 {27 provide: TOKEN_FACTORY,28 useFactory: () => 'FACTORY',29 },30 {31 provide: TOKEN_VALUE,32 useValue: 'VALUE',33 },34 ],35})36class TargetModule {}37// fix for jest without jasmine assertions38const assertion: any =39 typeof jasmine === 'undefined' ? expect : jasmine;40describe('TestToken', () => {41 ngMocks.faster();42 // Because we want to test the tokens, we pass them in .keep in43 // the chain on MockBuilder. To correctly satisfy their44 // initialization we need to pass its module as the second45 // parameter.46 beforeEach(() => {47 return MockBuilder(48 [TOKEN_CLASS, TOKEN_EXISTING, TOKEN_FACTORY, TOKEN_VALUE],49 TargetModule,50 );51 });52 it('creates TOKEN_CLASS', () => {53 const token =54 MockRender<ServiceClass>(TOKEN_CLASS).point.componentInstance;55 // Verifying that the token is an instance of ServiceClass.56 expect(token).toEqual(assertion.any(ServiceClass));57 expect(token.name).toEqual('class');58 });59 it('creates TOKEN_EXISTING', () => {60 const token =61 MockRender<ServiceExisting>(TOKEN_EXISTING).point62 .componentInstance;63 // Verifying that the token is an instance of ServiceExisting.64 // But because it has been replaced with a mock copy,65 // we should see an empty name.66 expect(token).toEqual(assertion.any(ServiceExisting));67 expect(token.name).toBeUndefined();68 });69 it('creates TOKEN_FACTORY', () => {70 const token = MockRender(TOKEN_FACTORY).point.componentInstance;71 // Checking that we have here what factory has been created.72 expect(token).toEqual('FACTORY');73 });74 it('creates TOKEN_VALUE', () => {75 const token = MockRender(TOKEN_VALUE).point.componentInstance;76 // Checking the set value.77 expect(token).toEqual('VALUE');78 });...

Full Screen

Full Screen

settings.spec.ts

Source:settings.spec.ts Github

copy

Full Screen

1import supertest from "supertest"2import {app} from "../../../server_config"3import jwt from "../unit/mocks/mock_jwt"4import db from "./mocks/mock_db"5jwt.init()6db.init()7describe('check get settings routes', () => {8 test('getSettings with throw Settings', async () => {9 const response = await supertest(app).get("/settings").send().set('Cookie', ['token=token_existing']);10 expect(response.statusCode).toBe(500)11 })12})13describe('check create settings routes', () => {14 test('throw on creating setting', async () => {15 const response = await supertest(app).post("/settings").send({restrictionName: "real"}).set('Cookie', ['token=token_existing']);16 expect(response.statusCode).toBe(500)17 })18})19describe('check update settings routes catch', () => {20 test('throw in restriction id search', async () => {21 const response = await supertest(app).patch("/settings").send({restrictionName: "throw"}).set('Cookie', ['token=token_existing']);22 expect(response.statusCode).toBe(500)23 })24 test('throw in restriction connected to user search', async () => {25 const response = await supertest(app).patch("/settings").send({restrictionName: "throw2"}).set('Cookie', ['token=token_existing']);26 expect(response.statusCode).toBe(500)27 })28 test('throw in updateing settings', async () => {29 const response = await supertest(app).patch("/settings").send({restrictionName: "real"}).set('Cookie', ['token=token_existing']);30 expect(response.statusCode).toBe(500)31 })32})33describe('check delete settings routes', () => {34 test('delete Setting', async () => {35 const response = await supertest(app).delete("/settings").send({restrictionName: "nop"}).set('Cookie', ['token=token_existing']);36 expect(response.statusCode).toBe(500)37 })...

Full Screen

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