Best JavaScript code snippet using ng-mocks
test.spec.ts
Source:test.spec.ts  
1import { Component, NgModule } from '@angular/core';2import { FormsModule } from '@angular/forms';3import { NgSelectModule } from '@ng-select/ng-select';4import { MockBuilder, MockRender, ngMocks } from 'ng-mocks';5@Component({6  selector: 'target',7  template: `8    <ng-select9      [items]="cities"10      groupBy="avatar"11      [(ngModel)]="selectedCity"12      bindLabel="name"13      bindValue="name"14    >15      <ng-template ng-label-tmp let-item="item">16        <strong>{{ item.name }}</strong>17      </ng-template>18      <ng-template ng-optgroup-tmp let-item="item" let-index="index">19        {{ index }} <img [src]="item.avatar" [alt]="item.name" />20      </ng-template>21      <ng-template22        ng-option-tmp23        let-item="item"24        let-index="index"25        let-search="searchTerm"26      >27        <span class="ng-option-tmp"28          >{{ search }} {{ item.name }}</span29        >30      </ng-template>31    </ng-select>32  `,33})34class TargetComponent {35  public readonly cities = [36    {37      avatar:38        '//www.gravatar.com/avatar/b0d8c6e5ea589e6fc3d3e08afb1873bb?d=retro&r=g&s=30 2x',39      id: 1,40      name: 'Vilnius',41    },42  ];43  public selectedCity = this.cities[0].name;44}45@NgModule({46  declarations: [TargetComponent],47  imports: [NgSelectModule, FormsModule],48})49class TargetModule {}50describe('ng-select:props', () => {51  ngMocks.faster();52  beforeEach(() => MockBuilder(TargetComponent, TargetModule));53  it('binds inputs', () => {54    // Rendering TargetComponent and accessing its instance.55    const targetComponent =56      MockRender(TargetComponent).point.componentInstance;57    // Looking for a debug element of the ng-select.58    const ngSelectEl = ngMocks.find('ng-select');59    // Asserting bound properties.60    expect(ngMocks.input(ngSelectEl, 'items')).toBe(61      targetComponent.cities,62    );63    expect(ngMocks.input(ngSelectEl, 'ngModel')).toBe(64      targetComponent.selectedCity,65    );66    // Asserting static properties.67    expect(ngMocks.input(ngSelectEl, 'groupBy')).toEqual('avatar');68    expect(ngMocks.input(ngSelectEl, 'bindLabel')).toEqual('name');69    expect(ngMocks.input(ngSelectEl, 'bindValue')).toEqual('name');70  });71  it('binds outputs', () => {72    // Rendering TargetComponent and accessing its instance.73    const targetComponent =74      MockRender(TargetComponent).point.componentInstance;75    // Looking for a debug element of the ng-select.76    const ngSelectEl = ngMocks.find('ng-select');77    // Simulating an emit.78    ngMocks.output(ngSelectEl, 'ngModelChange').emit('test');79    // Asserting the effect of the emit.80    expect(targetComponent.selectedCity).toEqual('test');81  });82  it('provides correct template for ng-label-tmp', () => {83    // Rendering TargetComponent.84    MockRender(TargetComponent);85    // Looking for a debug element of the ng-select.86    const ngSelectEl = ngMocks.find('ng-select');87    // Looking for the ng-label-tmp template88    const ngLabelTmp = ngMocks.findTemplateRef(89      ngSelectEl,90      // attr name91      ['ng-label-tmp'],92    );93    // Verifies that ngSelect can access ngLabelTmp,94    // and renders it.95    ngMocks.render(96      ngSelectEl.componentInstance,97      ngLabelTmp,98      {},99      // Providing context variables.100      { item: { name: 'test' } },101    );102    // Asserting the rendered html.103    expect(ngSelectEl.nativeElement.innerHTML).toContain(104      '<strong>test</strong>',105    );106  });107  it('provides correct template for ng-optgroup-tmp', () => {108    // Rendering TargetComponent and accessing its instance.109    MockRender(TargetComponent);110    // Looking for a debug element of the ng-select.111    const ngSelectEl = ngMocks.find('ng-select');112    // Looking for the ng-optgroup-tmp template113    const ngOptgroupTmp = ngMocks.findTemplateRef(114      ngSelectEl,115      // attr name116      ['ng-optgroup-tmp'],117    );118    // Verifies that ngSelect can access ngOptgroupTmp,119    // and renders it.120    ngMocks.render(121      ngSelectEl.componentInstance,122      ngOptgroupTmp,123      {},124      // Providing context variables.125      {126        index: 7,127        item: {128          avatar: 'test.jpeg',129          name: 'test',130        },131      },132    );133    // Asserting the rendered html.134    expect(ngSelectEl.nativeElement.innerHTML).toContain(135      '7 <img src="test.jpeg" alt="test">',136    );137  });138  it('provides correct template for ng-option-tmp', () => {139    // Rendering TargetComponent and accessing its instance.140    MockRender(TargetComponent);141    // Looking for a debug element of the ng-select.142    const ngSelectEl = ngMocks.find('ng-select');143    // Looking for the ng-option-tmp template144    const ngOptionTmp = ngMocks.findTemplateRef(145      ngSelectEl,146      // attr name147      ['ng-option-tmp'],148    );149    // Verifying that the instance has been mocked.150    // And rendering its property,151    // which points to the desired TemplateRef.152    ngMocks.render(153      ngSelectEl.componentInstance,154      ngOptionTmp,155      {},156      // Providing context variables.157      {158        item: {159          name: 'test',160        },161        searchTerm: 'search',162      },163    );164    // Asserting the rendered html.165    expect(ngSelectEl.nativeElement.innerHTML).toContain(166      '<span class="ng-option-tmp">search test</span>',167    );168  });...Using AI Code Generation
1describe('ngOptionTmp', function() {2  var $compile, $rootScope;3  beforeEach(module('ngOptionTmp'));4  beforeEach(inject(function(_$compile_, _$rootScope_){5    $compile = _$compile_;6    $rootScope = _$rootScope_;7  }));8  it('should render correct content', function() {9    var element = $compile('<div>{{1 + 1}}</div>')($rootScope);10    $rootScope.$digest();11    expect(element.html()).toEqual('2');12  });13});14describe('ngOptionTmp', function() {15  var $compile, $rootScope;16  beforeEach(module('ngOptionTmp'));17  beforeEach(inject(function(_$compile_, _$rootScope_){18    $compile = _$compile_;19    $rootScope = _$rootScope_;20  }));21  it('should render correct content', function() {22    var element = $compile('<div>{{1 + 1}}</div>')($rootScope);23    $rootScope.$digest();24    expect(element.html()).toEqual('2');25  });26});27describe('ngOptionTmp', function() {28  var $compile, $rootScope;29  beforeEach(module('ngOptionTmp'));30  beforeEach(inject(function(_$compile_, _$rootScope_){31    $compile = _$compile_;32    $rootScope = _$rootScope_;33  }));34  it('should render correct content', function() {35    var element = $compile('<div>{{1 + 1}}</div>')($rootScope);36    $rootScope.$digest();37    expect(element.html()).toEqual('2');38  });39});40describe('ngOptionTmp', function() {41  var $compile, $rootScope;42  beforeEach(module('ngOptionTmp'));43  beforeEach(inject(function(_$compile_, _$rootScope_){44    $compile = _$compile_;45    $rootScope = _$rootScope_;46  }));47  it('should render correct content', function() {48    var element = $compile('<div>{{1 + 1}}</div>')($rootScope);49    $rootScope.$digest();50    expect(element.html()).toEqual('2');51  });52});53describe('ngOptionTmp', function() {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!!
