Best JavaScript code snippet using ng-mocks
mock-builder.promise.ts
Source:mock-builder.promise.ts
...85 }86 public exclude(def: any): this {87 this.wipe(def);88 this.excludeDef.add(def);89 this.setConfigDef(def);90 return this;91 }92 // istanbul ignore next93 public async finally(callback?: (() => void) | null | undefined): Promise<IMockBuilderResult> {94 return this.then().finally(callback);95 }96 public keep(input: any, config?: IMockBuilderConfig): this {97 const { def, providers } = normaliseModule(input);98 const existing = this.keepDef.has(def) ? this.defProviders.get(def) : [];99 this.wipe(def);100 this.keepDef.add(def);101 // a magic to support modules with providers.102 if (providers) {103 this.defProviders.set(def, [...(existing || /* istanbul ignore next */ []), ...providers]);104 }105 this.setConfigDef(def, config);106 return this;107 }108 public mock(input: any, a1: any = defaultMock, a2?: any): this {109 const { def, providers } = normaliseModule(input);110 const { config, mock } = parseMockArguments(def, a1, a2, defaultMock);111 if (isNgDef(mock) && isNgDef(input) && !isNgDef(input, 't')) {112 throw new Error(113 [114 `MockBuilder.mock(${funcGetName(input)}) received a class when its shape is expected.`,115 'Please try ngMocks.defaultMock instead.',116 ].join(' '),117 );118 }119 const existing = this.mockDef.has(def) ? this.defProviders.get(def) : [];120 this.wipe(def);121 this.mockDef.add(def);122 // a magic to support modules with providers.123 if (providers) {124 this.defProviders.set(def, [...(existing || /* istanbul ignore next */ []), ...providers]);125 }126 this.setDefValue(def, mock);127 this.setConfigDef(def, config);128 return this;129 }130 public provide(def: Provider): this {131 for (const provider of flatten(def)) {132 const { provide, multi } = parseProvider(provider);133 const existing = this.providerDef.has(provide) ? this.providerDef.get(provide) : [];134 this.providerDef.set(provide, generateProviderValue(provider, existing, multi));135 }136 return this;137 }138 public replace(source: Type<any>, destination: Type<any>, config?: IMockBuilderConfig): this {139 if (!isNgDef(destination) || !isNgDef(source) || isNgDef(destination, 'i') || isNgDef(source, 'i')) {140 throw new Error(141 'Cannot replace the declaration, both have to be a Module, a Component, a Directive or a Pipe, for Providers use `.mock` or `.provide`',142 );143 }144 this.wipe(source);145 this.replaceDef.add(source);146 this.defValue.set(source, destination);147 this.setConfigDef(source, config);148 return this;149 }150 // eslint-disable-next-line unicorn/no-thenable151 public async then<TResult1 = IMockBuilderResult>(152 fulfill?: ((value: IMockBuilderResult) => PromiseLike<TResult1>) | undefined | null,153 reject?: ((reason: any) => PromiseLike<any>) | undefined | null,154 ): Promise<TResult1> {155 const promise = new Promise((resolve: (value: IMockBuilderResult) => void): void => {156 const testBed: TestBedStatic = TestBed.configureTestingModule(this.build()) as never;157 for (const callback of mapValues(this.beforeCC)) {158 callback(testBed);159 }160 const testBedPromise = testBed.compileComponents();161 testBedPromise.then(() => {162 resolve({ testBed });163 });164 });165 return promise.then(fulfill, reject);166 }167 private combineParams(): BuilderData {168 return {169 configDef: this.configDef,170 configDefault: this.configDefault,171 defProviders: this.defProviders,172 defValue: this.defValue,173 excludeDef: this.excludeDef,174 keepDef: this.keepDef,175 mockDef: this.mockDef,176 providerDef: this.providerDef,177 replaceDef: this.replaceDef,178 };179 }180 private setConfigDef(def: any, config?: any): void {181 if (config || !this.configDef.has(def)) {182 this.configDef.set(def, config ?? this.configDefault);183 }184 }185 private setDefValue(def: any, mock: any): void {186 if (mock !== defaultMock) {187 this.defValue.set(def, mock);188 } else {189 this.defValue.delete(def);190 }191 }192 private wipe(def: Type<any>): void {193 this.defProviders.delete(def);194 this.defValue.delete(def);...
Using AI Code Generation
1import { setConfigDef } from 'ng-mocks';2setConfigDef({ autoSpy: false });3import { MockBuilder } from 'ng-mocks';4import { AppModule } from './app.module';5import { AppComponent } from './app.component';6describe('AppComponent', () => {7 beforeEach(() => MockBuilder(AppComponent, AppModule));8 it('should create the app', () => {9 const fixture = MockRender(AppComponent);10 const app = fixture.point.componentInstance;11 expect(app).toBeTruthy();12 });13});
Using AI Code Generation
1import { setConfigDef } from 'ng-mocks';2setConfigDef({3 render: {4 },5});6setConfigDef({7 render: {8 },9});10setConfigDef({11 render: {12 },13});14setConfigDef({15 render: {16 },17});18setConfigDef({19 render: {
Using AI Code Generation
1import { setConfigDef } from 'ng-mocks';2setConfigDef({3});4import { setConfigDef } from 'ng-mocks';5setConfigDef({6});
Using AI Code Generation
1import { setConfigDef } from 'ng-mocks';2import { MyModule } from './my-module';3import { MyService } from './my-service';4setConfigDef('mocks', {5 MyService: {6 getData: () => 'mocked data',7 },8});9import { MyService } from './my-service';10@Component({11 template: '{{ myService.getData() }}',12})13export class MyComponent {14 constructor(public myService: MyService) {}15}16import { Injectable } from '@angular/core';17@Injectable()18export class MyService {19 getData() {20 return 'real data';21 }22}23import { MyModule } from './my-module';24import { MyService } from './my-service';25import { MyComponent } from './my-component';26describe('MyComponent', () => {27 let fixture: ComponentFixture<MyComponent>;28 let component: MyComponent;29 let service: MyService;30 beforeEach(async(() => {31 TestBed.configureTestingModule({32 imports: [MyModule],33 }).compileComponents();34 }));35 beforeEach(() => {36 fixture = TestBed.createComponent(MyComponent);37 component = fixture.componentInstance;38 service = TestBed.get(MyService);39 });40 it('should use "mocked data"', () => {41 spyOn(service, 'getData').and.callThrough();42 fixture.detectChanges();43 expect(service.getData).toHaveBeenCalled();44 expect(fixture.nativeElement.textContent).toEqual('mocked data');45 });46});47import { MyModule } from './my-module';48import { MyService } from './my-service';49import { MyComponent } from './my-component';50describe('MyComponent', () => {51 let fixture: ComponentFixture<MyComponent>;52 let component: MyComponent;53 let service: MyService;54 beforeEach(async(() => {55 TestBed.configureTestingModule({56 imports: [MyModule],57 {58 useValue: {59 getData: () => 'mocked data',60 },61 },62 }).compileComponents();63 }));64 beforeEach(() => {65 fixture = TestBed.createComponent(MyComponent);66 component = fixture.componentInstance;67 service = TestBed.get(MyService);68 });
Using AI Code Generation
1import { setConfigDef } from "ng-mocks";2setConfigDef({3});4import "ng-mocks";5describe("MyComponent", () => {6 it("should create", () => {7 });8});9setConfigDef({10 id: {11 },12 mock: {13 },14 resolver: {15 },16 stringify: {17 },18 stub: {19 },20 template: {21 },22});23setConfigDef({ mock: { default: 'stub' } });24setConfigDef({ mock: { default: 'stub' } });25setConfigDef({ mock: { default: 'mock' } });26setConfigDef({ mock: { default: 'stub' } });27setConfigDef({ mock: { default: 'mock' } });28setConfigDef({ mock: { default: 'stub' } });29setConfigDef({ mock: { default: 'stub' } });30setConfigDef({ mock: { default: 'mock' } });31setConfigDef({ mock: { default: 'stub' } });32setConfigDef({ mock: { default: 'mock' } });33setConfigDef({ mock: { default: 'stub' } });34setConfigDef({ mock: { default: 'mock' } });35setConfigDef({ mock: { default: 'stub' } });36setConfigDef({ mock: { default: 'mock' } });37setConfigDef({ mock: { default: 'stub' } });38import { setConfigDef } from "ng
Using AI Code Generation
1import { setConfigDef } from 'ng-mocks';2setConfigDef({3 config: {4 }5});6@Component({7 template: `{{ config.value }}`8})9export class TestComponent {10 constructor(@Inject(CONFIG) public config: Config) { }11}12describe('TestComponent', () => {13 beforeEach(() => {14 TestBed.configureTestingModule({15 });16 });17 it('should use the default config', () => {18 const fixture = TestBed.createComponent(TestComponent);19 fixture.detectChanges();20 expect(fixture.nativeElement.textContent).toEqual('default');21 });22});23@Injectable()24export class AppService {25 constructor(private http: HttpClient) { }26 getItems() {27 }28}29describe('AppService', () => {30 let service: AppService;31 let httpMock: HttpTestingController;32 beforeEach(() => {33 TestBed.configureTestingModule({34 imports: [HttpClientTestingModule],35 });36 service = TestBed.get(AppService);37 httpMock = TestBed.get(HttpTestingController);38 });39 afterEach(() => {40 httpMock.verify();41 });42 it('should return an Observable of items', () => {43 { id: 1, name: 'Item1' },44 { id: 2, name: 'Item2' },
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!!