How to use TOKEN_FACTORY 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

BasicToken.test.ts

Source:BasicToken.test.ts Github

copy

Full Screen

1import { ethers, waffle } from "hardhat";2import { expect } from "chai";3import { BasicToken, BasicToken__factory } from "@/typechain-types";4const { provider: mock_provider } = waffle;5describe("BasicToken", () => {6 const wallet_initial_balance = 1000;7 const other_initial_balance = 0;8 const transfer_value = 1;9 let token: BasicToken;10 const [11 wallet,12 other,13 ] = mock_provider.getWallets();14 beforeEach(async () => {15 const token_factory = await ethers.getContractFactory("BasicToken") as BasicToken__factory;16 token = await token_factory.deploy(wallet_initial_balance);17 await token.deployed();18 });19 it("Assigns initial balance", async () => {20 expect(await token.balanceOf(wallet.address)).to.be.equal(wallet_initial_balance);21 });22 it("Transfer adds amount to destination account", async () => {23 await (await token.transfer(other.address, transfer_value));24 expect(await token.balanceOf(wallet.address))25 .to.equal(wallet_initial_balance - transfer_value);26 expect(await token.balanceOf(other.address))27 .to.equal(other_initial_balance + transfer_value);28 });29 it("Transfer emits event", async () => {30 await expect(token.transfer(other.address, transfer_value))31 .to.emit(token, "Transfer")32 .withArgs(wallet.address, other.address, transfer_value);33 });34 it("Can not transfer above the amount", async () => {35 await expect(token.transfer(other.address, wallet_initial_balance + 1))36 .to.be.reverted;37 });38 it("Can not transfer from empty account", async () => {39 const token_from_other_wallet = await token.connect(other.address);40 await expect(token_from_other_wallet.transfer(wallet.address, transfer_value))41 .to.be.reverted;42 });...

Full Screen

Full Screen

test-init.ts

Source:test-init.ts Github

copy

Full Screen

1import {ethers} from 'hardhat';2const initialize = async (accounts: any) => {3 const setup : any = {};4 setup.roles = {5 root: accounts[0],6 beneficiary: accounts[1],7 };8 return setup;9};10const getContractInstance = async (factoryName: string, address: any, args: any) => {11 const Factory = await ethers.getContractFactory(factoryName, address);12 const parameters = args ? args : [];13 return await Factory.deploy(...parameters);14};15const gettokenInstances = async (setup: any) => {16 const Token_Factory = await ethers.getContractFactory(17 "CustomToken",18 setup.roles.root19 );20 const stakingToken = await Token_Factory.deploy("Staking Token", "STT");21 const rewardsToken = await Token_Factory.deploy("Rewards Token", "RWT");22 return { 23 stakingToken,24 rewardsToken25 };26};27module.exports = {28 initialize,29 gettokenInstances,30 getContractInstance,...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1import { TOKEN_FACTORY } from 'ng-mocks';2import { MockBuilder } from 'ng-mocks';3import { MockRender } from 'ng-mocks';4import { MockInstance } from 'ng-mocks';5import { MockService } from 'ng-mocks';6import { MockProvider } from 'ng-mocks';7import { MockDirective } from 'ng-mocks';8import { MockComponent } from 'ng-mocks';9import { MockPipe } from 'ng-mocks';10import { MockRender } from 'ng-mocks';11import { MockRender } from 'ng-mocks';12import { MockRender } from 'ng-mocks';13import { MockRender } from 'ng-mocks';14import { MockRender } from 'ng-mocks';15import { MockRender } from 'ng-mocks';16import { MockRender } from 'ng-mocks';17import { MockRender } from 'ng-mocks';18import { MockRender } from 'ng-mocks';19import { MockRender } from 'ng-mocks';20import { MockRender } from 'ng-mocks';21import { TOKEN_FACTORY } from 'ng-mocks';22import { MockBuilder } from 'ng-mocks

Full Screen

Using AI Code Generation

copy

Full Screen

1import { TOKEN_FACTORY } from 'ng-mocks';2import { Observable } from 'rxjs';3import { map } from 'rxjs/operators';4import { HttpClient } from '@angular/common/http';5describe('Test', () => {6 it('should work', () => {7 const http = TOKEN_FACTORY(HttpClient);8 expect(response).toBeInstanceOf(Observable);9 .pipe(map((res) => res['title']))10 .subscribe((title) => {11 expect(title).toBe('delectus aut autem');12 });13 });14});15import 'zone.js/dist/zone-testing';16import { getTestBed } from '@angular/core/testing';17import {18} from '@angular/platform-browser-dynamic/testing';19import { MockBuilder } from 'ng-mocks';20beforeAll(() => {21 getTestBed().initTestEnvironment(22 platformBrowserDynamicTesting(),23 );24});25beforeEach(async () => {26 await MockBuilder().mock(HttpClient);27});28{29 "compilerOptions": {30 }31}32import { HttpClient } from '@angular/common/http';33import { MockBuilder, MockRender } from 'ng-mocks';34import { of } from 'rxjs';35import { TestComponent } from './test.component';36describe('TestComponent', () => {37 beforeEach(async () => {38 await MockBuilder().mock(HttpClient, {39 get: () => of({ title: 'delectus aut autem' }),40 });41 MockRender(TestComponent);42 });43 it('should create', () => {44 expect(TestComponent).toBeTruthy();45 });46});47import { HttpClient } from '@angular/common/http';48import { MockBuilder, MockRender } from 'ng-mocks';49import { of } from 'rxjs';50import { TestComponent } from './test.component';51describe('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