Best JavaScript code snippet using ng-mocks
test.ng-mocks.spec.ts
Source:test.ng-mocks.spec.ts
1import { HttpClientModule } from '@angular/common/http';2import { HttpClientTestingModule } from '@angular/common/http/testing';3import { inject, TestBed } from '@angular/core/testing';4import { isMockedNgDefOf, MockBuilder, NG_MOCKS } from 'ng-mocks';5import {6 KeepComponent,7 MockComponent,8 My1Component,9 My2Component,10 My3Component,11 MyComponent,12} from './spec.components.fixtures';13import {14 KeepDirective,15 MockDirective,16} from './spec.directives.fixtures';17import {18 ModuleKeep,19 ModuleMock,20 MyModule,21} from './spec.modules.fixtures';22import {23 KeepPipe,24 MockPipe,25 RestorePipe,26} from './spec.pipes.fixtures';27import {28 ServiceCustomize,29 ServiceKeep,30 ServiceMock,31} from './spec.services.fixtures';32import {33 TOKEN_CUSTOMIZE,34 TOKEN_KEEP,35 TOKEN_MOCK,36} from './spec.tokens.fixtures';37describe('MockBuilder:ngMocks', () => {38 beforeEach(async () => {39 const ngModule = MockBuilder(MyComponent, MyModule)40 .keep(ModuleKeep)41 .keep(KeepComponent)42 .keep(KeepDirective)43 .keep(KeepPipe)44 .keep(ServiceKeep)45 .keep(TOKEN_KEEP)46 .replace(HttpClientModule, HttpClientTestingModule)47 .mock(ModuleMock)48 .mock(MockComponent)49 .mock(MockDirective)50 .mock(MockPipe)51 .mock(ServiceMock) // makes all methods an empty function52 .mock(TOKEN_MOCK) // makes its value undefined53 .mock(ServiceCustomize, {54 getName: () => 'My Customized String',55 })56 .mock(TOKEN_CUSTOMIZE, 'My_Token')57 // Now the pipe will not be replaced with its mock copy.58 .keep(RestorePipe)59 // Even it belongs to the module we want to keep,60 // it will be still replaced with a mock copy.61 .mock(My3Component)62 // and now we want to build our NgModule.63 .build();64 TestBed.configureTestingModule(ngModule);65 // Extra configuration66 TestBed.overrideTemplate(67 My1Component,68 'If we need to tune testBed',69 );70 TestBed.overrideTemplate(My2Component, 'More callbacks');71 return TestBed.compileComponents();72 });73 it('should contain mocks', inject(74 [NG_MOCKS],75 (mocks: Map<any, any>) => {76 // main part77 const myComponent = mocks.get(MyComponent);78 expect(myComponent).toBe(MyComponent);79 const myModule = mocks.get(MyModule);80 expect(isMockedNgDefOf(myModule, MyModule, 'm')).toBeTruthy();81 // keep82 const keepComponent = mocks.get(KeepComponent);83 expect(keepComponent).toBe(keepComponent);84 const keepDirective = mocks.get(KeepDirective);85 expect(keepDirective).toBe(keepDirective);86 const keepPipe = mocks.get(KeepPipe);87 expect(keepPipe).toBe(keepPipe);88 const serviceKeep = mocks.get(ServiceKeep);89 expect(serviceKeep).toBe(ServiceKeep);90 const tokenKeep = mocks.get(TOKEN_KEEP);91 expect(tokenKeep).toBe(TOKEN_KEEP);92 // replace93 const httpClientModule = mocks.get(HttpClientModule);94 expect(httpClientModule).toBe(HttpClientTestingModule);95 // mimic96 const moduleMock = mocks.get(ModuleMock);97 expect(98 isMockedNgDefOf(moduleMock, ModuleMock, 'm'),99 ).toBeTruthy();100 const mockComponent = mocks.get(MockComponent);101 expect(102 isMockedNgDefOf(mockComponent, MockComponent, 'c'),103 ).toBeTruthy();104 const mockDirective = mocks.get(MockDirective);105 expect(106 isMockedNgDefOf(mockDirective, MockDirective, 'd'),107 ).toBeTruthy();108 const mockPipe = mocks.get(MockPipe);109 expect(isMockedNgDefOf(mockPipe, MockPipe, 'p')).toBeTruthy();110 const serviceMock = mocks.get(ServiceMock);111 expect(serviceMock).toBeDefined();112 expect(serviceMock.useFactory).toBeDefined();113 const serviceMockInstance = serviceMock.useFactory();114 expect(serviceMockInstance.getName).toBeDefined();115 expect(serviceMockInstance.getName()).toBeUndefined();116 expect(mocks.has(TOKEN_MOCK)).toBeDefined();117 expect(mocks.get(TOKEN_MOCK)).toBeDefined();118 // customize119 const serviceCustomize = mocks.get(ServiceCustomize);120 expect(serviceCustomize).toBeDefined();121 expect(serviceCustomize.useFactory).toBeDefined();122 const serviceCustomizeInstance = serviceCustomize.useFactory();123 expect(serviceCustomizeInstance.getName).toBeDefined();124 expect(serviceCustomizeInstance.getName()).toEqual(125 'My Customized String',126 );127 const tokenCustomize = mocks.get(TOKEN_CUSTOMIZE);128 expect(tokenCustomize).toBeDefined();129 expect(tokenCustomize.useFactory).toBeDefined();130 const tokenCustomizeValue = tokenCustomize.useFactory();131 expect(tokenCustomizeValue).toEqual('My_Token');132 // restore133 const restorePipe = mocks.get(RestorePipe);134 expect(restorePipe).toBe(restorePipe);135 // mock nested136 const myComponent3 = mocks.get(My3Component);137 expect(138 isMockedNgDefOf(myComponent3, My3Component, 'c'),139 ).toBeTruthy();140 },141 ));...
Using AI Code Generation
1import { serviceCustomizeInstance } from 'ng-mocks';2import { Service } from './service';3import { TestBed } from '@angular/core/testing';4import { ServiceModule } from './service.module';5describe('Service', () => {6 let service: Service;7 beforeEach(() => {8 TestBed.configureTestingModule({9 imports: [ServiceModule],10 });11 service = TestBed.inject(Service);12 });13 it('should be created', () => {14 expect(service).toBeTruthy();15 });16 it('should have a spy on method', () => {17 serviceCustomizeInstance(service, {18 method: () => 'mocked method',19 });20 expect(service.method()).toEqual('mocked method');21 });22});23import { serviceCustomizeInstance } from 'ng-mocks';24import { Component } from '@angular/core';25import { TestBed } from '@angular/core/testing';26import { Service } from './service';27import { ServiceModule } from './service.module';28@Component({29 <div>{{ service.method() }}</div>30})31class Component {32 constructor(public readonly service: Service) {}33}34describe('Component', () => {35 let component: Component;36 beforeEach(() => {37 TestBed.configureTestingModule({38 imports: [ServiceModule],39 });40 component = TestBed.createComponent(Component).componentInstance;41 });42 it('should be created', () => {43 expect(component).toBeTruthy();44 });45 it('should have a spy on method', () => {46 serviceCustomizeInstance(component.service, {47 method: () => 'mocked method',48 });49 expect(component.service.method()).toEqual('mocked method');50 });51});52import { serviceCustomizeInstance } from 'ng-mocks';53import { Service } from './service';54import { TestBed } from '@angular/core/testing';55import { ServiceModule } from './service.module';56describe('Service', () => {57 let service: Service;58 beforeEach(() => {59 TestBed.configureTestingModule({60 imports: [ServiceModule],61 });
Using AI Code Generation
1import { serviceCustomizeInstance } from 'ng-mocks';2class TestClass {3 public testMethod() {4 return 'test';5 }6}7serviceCustomizeInstance(TestClass, {8 testMethod: () => 'mocked',9});10import { serviceCustomizeInstance } from 'ng-mocks';11describe('serviceCustomizeInstance', () => {12 it('should customize instance', () => {13 const service = new TestClass();14 serviceCustomizeInstance(service, {15 testMethod: () => 'mocked',16 });17 expect(service.testMethod()).toBe('mocked');18 });19});20serviceCustomizeInstance(TestClass, {21 testMethod: () => 'mocked',22});23serviceCustomizeInstance(new TestClass(), {24 testMethod: () => 'mocked',25});26serviceCustomizeInstance(TestClass, {27 testMethod: () => 'mocked',28});29serviceCustomizeInstance(new TestClass(), {30 testMethod: () => 'mocked',31});32serviceCustomizeInstance(TestClass, {33 testMethod: () => 'mocked',34});35serviceCustomizeInstance(new TestClass(), {36 testMethod: () => 'mocked',37});38serviceCustomizeInstance(TestClass, {39 testMethod: () => 'mocked',40});41serviceCustomizeInstance(new
Using AI Code Generation
1import { serviceCustomizeInstance } from 'ng-mocks';2describe('mocking a service', () => {3 it('should mock a service', () => {4 const mockService = serviceCustomizeInstance(MyService, {5 myMethod: () => {6 return 'mocked value';7 },8 });9 const service = new MyService();10 expect(service.myMethod()).toBe('mocked value');11 });12});13import { Injectable } from '@angular/core';14@Injectable()15export class MyService {16 myMethod() {17 return 'real value';18 }19}20import { serviceCustomizeInstance } from 'ng-mocks';21describe('mocking a service', () => {22 it('should mock a service', () => {23 const mockService = serviceCustomizeInstance(MyService, {24 myMethod: () => {25 return 'mocked value';26 },27 });28 const service = new MyService();29 expect(service.myMethod()).toBe('mocked value');30 });31});32import { Injectable } from '@angular/core';33@Injectable()34export class MyService {35 myMethod() {36 return 'real value';37 }38}
Using AI Code Generation
1import { serviceCustomizeInstance } from 'ng-mocks';2import { UserService } from './user.service';3import { User } from './user.model';4describe('UserService', () => {5 let service: UserService;6 beforeEach(() => {7 service = serviceCustomizeInstance(UserService, {8 getUsers() {9 return of([10 { id: 1, name: 'User 1' },11 { id: 2, name: 'User 2' },12 { id: 3, name: 'User 3' },13 ]);14 },15 addUser(user: User) {16 this.users.push(user);17 },18 removeUser(user: User) {19 this.users.splice(this.users.indexOf(user), 1);20 },21 });22 });23 it('should return a list of users', () => {24 service.getUsers().subscribe((users) => {25 expect(users.length).toBe(3);26 });27 });28 it('should add a user to the list of users', () => {29 let users: User[];30 service.getUsers().subscribe((users) => {31 expect(users.length).toBe(3);32 });33 service.addUser({ id: 4, name: 'User 4' });34 service.getUsers().subscribe((users) => {35 expect(users.length).toBe(4);36 });37 });38 it('should remove a user from the list of users', () => {39 let users: User[];40 service.getUsers().subscribe((users) => {41 expect(users.length).toBe(3);42 });43 service.removeUser({ id: 2, name: 'User 2' });44 service.getUsers().subscribe((users) => {45 expect(users.length).toBe(2);46 });47 });48});
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!