Best JavaScript code snippet using ng-mocks
spec.components.fixtures.ts
Source:spec.components.fixtures.ts
1import { Component, ContentChild, Inject, Input, Optional, TemplateRef } from '@angular/core';2import {3 AnythingKeep1,4 AnythingKeep2,5 MyCustomProvider1,6 MyCustomProvider2,7 MyCustomProvider3,8 MyService1,9 MyService2,10 ServiceCustomize,11 ServiceKeep,12 ServiceMock,13} from './spec.services.fixtures';14import { TOKEN_CUSTOMIZE, TOKEN_KEEP, TOKEN_MOCK } from './spec.tokens.fixtures';15@Component({16 selector: 'c-structural',17 template: `18 <div *ngIf="items && items.length">19 <ng-template ngFor [ngForOf]="items" [ngForTemplate]="injectedBlock"></ng-template>20 </div>21 `,22})23export class ContentChildComponent<T> {24 @ContentChild('block', {} as any) public readonly injectedBlock: TemplateRef<any> | undefined;25 @Input() public items: T[] | undefined;26}27@Component({28 selector: 'c-my',29 template: `30 <div>My Content</div>31 <div>MyComponent1: <c-1></c-1></div>32 <div>MyComponent2: <c-2></c-2></div>33 <div>MyComponent3: <c-3></c-3></div>34 <div>KeepComponent: <c-keep></c-keep></div>35 <div>MockComponent: <c-mock></c-mock></div>36 <div>MyDirective: <d-my></d-my></div>37 <div>KeepDirective: <d-keep></d-keep></div>38 <div>39 MockDirective 1: <span *d-mock="let z = a">render {{ z.b }}</span>40 </div>41 <div>42 MockDirective 2: <ng-template d-mock let-z>render {{ z.a }}</ng-template>43 </div>44 <div>MyPipe: {{ 'text' | my }}</div>45 <div>KeepPipe: {{ 'text' | keep }}</div>46 <div>MockPipe: {{ 'text' | mock }}</div>47 <div>CustomizePipe: {{ 'text' | customize }}</div>48 <div>RestorePipe: {{ 'text' | restore }}</div>49 <div>TOKEN_KEEP: {{ t1 }}</div>50 <div>TOKEN_MOCK: {{ t2 }}</div>51 <div>TOKEN_CUSTOMIZE: {{ t3 }}</div>52 <div>AnythingKeep1: {{ anythingKeep1?.getName() }}</div>53 <div>AnythingKeep2: {{ anythingKeep2?.getName() }}</div>54 <div>myCustomProvider1: {{ myCustomProvider1?.getName() }}</div>55 <div>myCustomProvider2: {{ myCustomProvider2?.getName() }}</div>56 <div>myCustomProvider3: {{ myCustomProvider3?.getName() }}</div>57 <div>myService1: {{ myService1?.getName() }}</div>58 <div>myService2: {{ myService2?.getName() }}</div>59 <div>serviceKeep: {{ serviceKeep?.getName() }}</div>60 <div>serviceCustomize: {{ serviceCustomize?.getName() }}</div>61 <div>serviceMock: {{ serviceMock?.getName() }}</div>62 <c-structural>63 <ng-template let-value let-b="a" #block>64 <div>ComponentStructural: {{ value }} {{ b.z }}</div>65 </ng-template>66 </c-structural>67 `,68})69export class MyComponent {70 public constructor(71 @Optional() @Inject(TOKEN_KEEP) public readonly t1: string,72 @Optional() @Inject(TOKEN_MOCK) public readonly t2: string,73 @Optional() @Inject(TOKEN_CUSTOMIZE) public readonly t3: string,74 @Optional() public readonly anythingKeep1: AnythingKeep1,75 @Optional() public readonly anythingKeep2: AnythingKeep2,76 @Optional() public readonly myCustomProvider1: MyCustomProvider1,77 @Optional() public readonly myCustomProvider2: MyCustomProvider2,78 @Optional() public readonly myCustomProvider3: MyCustomProvider3,79 @Optional() public readonly myService1: MyService1,80 @Optional() public readonly myService2: MyService2,81 @Optional() public readonly serviceKeep: ServiceKeep,82 @Optional() public readonly serviceMock: ServiceMock,83 @Optional() public readonly serviceCustomize: ServiceCustomize,84 ) {}85}86@Component({87 selector: 'c-1',88 template: 'MyComponent1',89})90export class My1Component {}91@Component({92 selector: 'c-2',93 template: 'MyComponent2',94})95export class My2Component {}96@Component({97 selector: 'c-3',98 template: 'MyComponent3',99})100export class My3Component {}101@Component({102 selector: 'c-keep',103 template: 'KeepComponent',104})105export class KeepComponent {}106@Component({107 selector: 'c-mock',108 template: 'MockComponent',109})...
spec.modules.fixtures.ts
Source:spec.modules.fixtures.ts
1import { CommonModule } from '@angular/common';2import { HttpClientModule } from '@angular/common/http';3import { NgModule } from '@angular/core';4import {5 ContentChildComponent,6 KeepComponent,7 MockComponent,8 My1Component,9 My2Component,10 My3Component,11 MyComponent,12} from './spec.components.fixtures';13import { KeepDirective, MockDirective, MyDirective } from './spec.directives.fixtures';14import { CustomizePipe, KeepPipe, MockPipe, MyPipe, RestorePipe } from './spec.pipes.fixtures';15import { MyService1, MyService2, ServiceCustomize, ServiceKeep, ServiceMock } from './spec.services.fixtures';16import { TOKEN_CUSTOMIZE, TOKEN_KEEP, TOKEN_MOCK } from './spec.tokens.fixtures';17@NgModule({18 declarations: [19 KeepComponent,20 MockComponent,21 KeepDirective,22 MockDirective,23 KeepPipe,24 MockPipe,25 CustomizePipe,26 RestorePipe,27 ],28 exports: [KeepComponent, MockComponent, KeepDirective, MockDirective, KeepPipe, MockPipe, CustomizePipe, RestorePipe],29 providers: [30 ServiceKeep,31 ServiceMock,32 {33 provide: TOKEN_KEEP,34 useValue: 'TOKEN_KEEP',35 },36 {37 provide: TOKEN_MOCK,38 useValue: 'TOKEN_MOCK',39 },40 {41 provide: TOKEN_CUSTOMIZE,42 useValue: 'TOKEN_CUSTOMIZE',43 },44 ],45})46export class ModuleMock {}47@NgModule({48 declarations: [My1Component, My2Component, My3Component, ContentChildComponent],49 exports: [My1Component, My2Component, My3Component, ContentChildComponent],50 imports: [CommonModule],51})52export class ModuleKeep {}53@NgModule({54 declarations: [MyComponent, MyDirective, MyPipe],55 exports: [MyComponent, MyDirective, MyPipe],56 imports: [HttpClientModule, ModuleMock, ModuleKeep],57 providers: [MyService1, MyService2, ServiceCustomize],58})...
spec.tokens.fixtures.ts
Source:spec.tokens.fixtures.ts
1import { InjectionToken } from '@angular/core';2export const TOKEN_KEEP = new InjectionToken('TOKEN_KEEP');3export const TOKEN_MOCK = new InjectionToken('TOKEN_MOCK');...
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!!