Best JavaScript code snippet using ng-mocks
evo-autocomplete.component.ts
Source:evo-autocomplete.component.ts
1import {2 AfterViewInit,3 ChangeDetectionStrategy,4 ChangeDetectorRef,5 Component,6 ContentChild,7 EventEmitter,8 HostBinding,9 Input,10 OnDestroy,11 Output,12 TemplateRef,13 ViewChild,14 ViewEncapsulation15} from '@angular/core';16import { ControlValueAccessor, NgControl } from '@angular/forms';17import { merge, Subject } from 'rxjs';18import { NgSelectComponent } from '@ng-select/ng-select';19import { takeUntil, tap, delay } from 'rxjs/operators';20import { isNull } from 'lodash-es';21import { EvoInputTheme } from '../../evo-input';22import { iconDecline } from '@evo/ui-kit/icons/system';23export type DropdownPosition = 'bottom' | 'top' | 'auto';24export type AddTagFn = ((term: string) => any | Promise<any>);25export type CompareWithFn = (a: any, b: any) => boolean;26export type GroupValueFn = (key: string | object, children: any[]) => string | object;27@Component({28 // tslint:disable-next-line29 selector: 'evo-autocomplete',30 templateUrl: './evo-autocomplete.component.html',31 styleUrls: ['./evo-autocomplete.component.scss'],32 changeDetection: ChangeDetectionStrategy.OnPush,33 encapsulation: ViewEncapsulation.None,34})35export class EvoAutocompleteComponent implements ControlValueAccessor, AfterViewInit, OnDestroy {36 theme: EvoInputTheme = EvoInputTheme.default;37 isFocused = false;38 isSelectbox = false;39 // Inputs40 @Input() isOpen;41 @Input() items: any[];42 @Input() bindLabel: string;43 @Input() bindValue: string;44 @Input() markFirst = true;45 @Input() placeholder: string;46 @Input() notFoundText = 'Ðе найдено';47 @Input() typeToSearchText = 'ÐаÑниÑе пиÑаÑÑ...';48 @Input() addTagText = 'ÐобавиÑÑ ÑÑг';49 @Input() loadingText = 'ÐÐ´ÐµÑ Ð¿Ð¾Ð¸Ñк...';50 @Input() clearAllText: string;51 @Input() dropdownPosition: DropdownPosition = 'auto';52 @Input() appendTo: string;53 @Input() loading: boolean;54 @Input() closeOnSelect = true;55 @Input() hideSelected: boolean;56 @Input() selectOnTab: boolean;57 @Input() openOnEnter: boolean;58 @Input() maxSelectedItems: number;59 // tslint:disable-next-line60 @Input() groupBy: string | Function;61 @Input() groupValue: GroupValueFn;62 @Input() bufferAmount = 4;63 @Input() virtualScroll: boolean;64 @Input() selectableGroup: boolean;65 @Input() selectableGroupAsModel = true;66 @Input() searchFn: () => {};67 @Input() clearOnBackspace = true;68 @Input() typeahead: Subject<string>;69 @Input() multiple: boolean;70 @Input() multipleInline: boolean;71 @Input() addTag: boolean | AddTagFn;72 @Input() searchable = true;73 @Input() clearable = true;74 @Input() errorsMessages: {[key: string]: string};75 @Input() compareWith: CompareWithFn;76 // Fix: https://github.com/ng-select/ng-select/pull/125777 // Don't work with custom template - labelTemp78 @Input() editQuery = false;79 // Outputs80 // tslint:disable:no-output-rename no-output-native81 @Output('blur') blurEvent = new EventEmitter();82 @Output('focus') focusEvent = new EventEmitter();83 @Output('change') changeEvent = new EventEmitter();84 @Output('open') openEvent = new EventEmitter();85 @Output('close') closeEvent = new EventEmitter();86 @Output('search') searchEvent = new EventEmitter<{term: string, items: any[]}>();87 @Output('clear') clearEvent = new EventEmitter();88 @Output('add') addEvent = new EventEmitter();89 @Output('remove') removeEvent = new EventEmitter();90 @Output('scroll') scrollEvent = new EventEmitter<{start: number; end: number}>();91 @Output('scrollToEnd') scrollToEndEvent = new EventEmitter();92 // tslint:enable:no-output-rename no-output-native93 @ViewChild(NgSelectComponent)94 ngSelectComponent: NgSelectComponent;95 @HostBinding('attr.class') hostClassName = 'evo-autocomplete';96 @ContentChild('labelTemp', { read: TemplateRef }) labelTemp: TemplateRef<any>;97 @ContentChild('multiLabelTemp', { read: TemplateRef }) multiLabelTemp: TemplateRef<any>;98 @ContentChild('optionTemp', {read: TemplateRef}) optionTemp: TemplateRef<any>;99 protected inputVal: string;100 protected readonly _destroy$ = new Subject<void>();101 protected _value: any;102 protected inputEl: HTMLInputElement;103 constructor(104 private cdr: ChangeDetectorRef,105 public control: NgControl,106 ) {107 control.valueAccessor = this;108 }109 @Input('isSelectbox') set setSelectbox(isSelectbox: boolean) {110 this.clearable = false;111 this.editQuery = false;112 this.searchable = false;113 this.notFoundText = '';114 this.isSelectbox = isSelectbox;115 }116 @Input('theme') set setTheme(theme: string | EvoInputTheme) {117 if (EvoInputTheme[theme]) {118 this.theme = EvoInputTheme[theme];119 }120 }121 get hasErrors(): boolean {122 return this.control.dirty && this.control.touched && this.control.invalid;123 }124 get classes(): {[key: string]: boolean} {125 return {126 'is-multiple-inline': this.multipleInline,127 'is-edit-query': this.editQuery,128 'is-selectbox': this.isSelectbox,129 'disabled': this.control.disabled,130 'touched': this.control.touched,131 'valid': this.control.valid,132 'invalid': this.control.invalid,133 [`theme-${ this.theme }`]: true,134 };135 }136 get value(): any {137 return this._value;138 }139 set value(value: any) {140 if (value !== this._value) {141 this._value = value;142 this._onChange(value);143 }144 }145 ngAfterViewInit(): void {146 if (this.editQuery) {147 this.editQueryMode();148 }149 this.patchClearButtonIcon();150 // prevent option click to close evo-modal151 this.ngSelectComponent.element.addEventListener('click', (e) => {152 if ((e.target as HTMLElement).closest('.ng-option')) {153 e.stopPropagation();154 }155 });156 // Allows to mark view for check157 // if control was validated with FormHelper.validate158 // and it's touched159 this.control.control.statusChanges.pipe(160 takeUntil(this._destroy$),161 tap(() => this.cdr.markForCheck()),162 ).subscribe();163 }164 ngOnDestroy(): void {165 this._destroy$.next();166 this._destroy$.complete();167 }168 writeValue(value: any): void {169 this.value = value;170 if (this.ngSelectComponent) {171 this.ngSelectComponent.writeValue(value);172 }173 }174 registerOnChange(fn: any): void {175 this._onChange = fn;176 }177 registerOnTouched(fn: any): void {178 this._onTouched = fn;179 }180 setDisabledState(isDisabled: boolean): void {181 if (this.ngSelectComponent) {182 this.ngSelectComponent.setDisabledState(isDisabled);183 }184 }185 // TODO: investigate and remove186 editQueryMode(): void {187 const ngSelectEl: HTMLElement = this.ngSelectComponent.element;188 this.inputEl = ngSelectEl.querySelector('.ng-input input');189 const streams$ = [190 this.changeEvent.pipe(191 delay(0),192 tap(() => {193 this.resetSearchQuery();194 this.inputEl.value = this.inputVal || '';195 }),196 ),197 this.closeEvent.pipe(198 delay(0),199 tap(() => {200 this.resetSearchQuery();201 if (ngSelectEl.classList.contains('ng-select-focused')) {202 this.inputEl.value = this.inputVal || '';203 }204 }),205 ),206 this.focusEvent.pipe(207 tap(() => {208 this.resetSearchQuery();209 this.inputEl.value = this.inputVal || '';210 }),211 ),212 this.blurEvent.pipe(213 tap(() => {214 this.inputEl.value = '';215 }),216 ),217 ];218 if (this.control.valueChanges) {219 streams$.push(220 this.control.valueChanges.pipe(221 tap((value) => {222 if (!isNull(value)) {223 this.resetSearchQuery();224 return;225 }226 this.inputVal = '';227 this.inputEl.value = '';228 }),229 )230 );231 }232 merge(...streams$).pipe(233 takeUntil(this._destroy$),234 ).subscribe();235 }236 resetSearchQuery(): void {237 const currentItem = this.ngSelectComponent.selectedItems[0];238 this.inputVal = currentItem?.label || '';239 }240 focus(): void {241 this.ngSelectComponent.focus();242 }243 blur(): void {244 this.ngSelectComponent.blur();245 }246 open(): void {247 this.ngSelectComponent.open();248 }249 close(): void {250 this.ngSelectComponent.close();251 }252 onFocus(event: FocusEvent): void {253 this.isFocused = true;254 this._onTouched();255 if (this.control.value) {256 this.focusEvent.emit(event);257 }258 }259 onBlur(event: FocusEvent): void {260 this.isFocused = false;261 this.blurEvent.emit(event);262 }263 onClearClick(): void {264 this.ngSelectComponent.handleClearClick();265 }266 protected _onChange = (value) => {267 }268 protected _onTouched = () => {269 }270 /**271 * Try to patch clear button icon272 */273 protected patchClearButtonIcon(): void {274 const originalShowClearFn = this.ngSelectComponent.showClear;275 const ngSelectElement = this.ngSelectComponent;276 let patchTimeout = null;277 this.ngSelectComponent.showClear = function () {278 const isClearButtonVisible = originalShowClearFn.bind(this)();279 if (isClearButtonVisible) {280 if (patchTimeout) {281 clearTimeout(patchTimeout);282 }283 patchTimeout = setTimeout(() => {284 const ngClearWrapperElement = ngSelectElement.element.querySelector('.ng-clear-wrapper');285 if (!ngClearWrapperElement) {286 return;287 }288 // tslint:disable-next-line:max-line-length289 ngClearWrapperElement.innerHTML = `<span class="ng-clear ng-clear_patched"><svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">${ iconDecline }</svg></span>`;290 });291 }292 return isClearButtonVisible;293 };294 }...
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
1import { ngMocks } from 'ng-mocks';2import { MyComponent } from './my.component';3describe('MyComponent', () => {4 it('should select an option', () => {5 const fixture = MockRender(MyComponent);6 const select = ngMocks.find('select');7 ngMocks.selectEl(select, '1');8 expect(ngMocks.formatText(fixture)).toContain('1');9 });10});11import { ngMocks } from 'ng-mocks';12import { MyComponent } from './my.component';13describe('MyComponent', () => {14 it('should trigger an event', () => {15 const fixture = MockRender(MyComponent);16 const button = ngMocks.find('button');17 ngMocks.trigger(button, 'click');18 expect(ngMocks.formatText(fixture)).toContain('1');19 });20});21import { ngMocks } from 'ng-mocks';22import { MyComponent } from './my.component';23describe('MyComponent', () => {24 it('should focus an input', () => {25 const fixture = MockRender(MyComponent);26 const input = ngMocks.find('input');27 ngMocks.focus(input);28 expect(ngMocks.formatText(fixture)).toContain('focus');29 });30});31import { ngMocks } from 'ng-mocks';32import { MyComponent } from './my.component';33describe('MyComponent', () => {34 it('should blur an input', () => {35 const fixture = MockRender(MyComponent);36 const input = ngMocks.find('input');37 ngMocks.blur(input);38 expect(ngMocks.formatText(fixture)).toContain('blur');39 });40});41import { ngMocks } from 'ng-mocks';42import { MyComponent } from './my.component';43describe('MyComponent', () => {
Using AI Code Generation
1import { ngMocks } from 'ng-mocks';2import { SelectComponent } from './select.component';3describe('SelectComponent', () => {4 it('should select an option', () => {5 const fixture = MockRender(SelectComponent);6 ngMocks.selectEl(fixture.debugElement, 'option1');7 expect(ngMocks.formatText(fixture)).toEqual('option1');8 });9});10import { ngMocks } from 'ng-mocks';11import { SelectComponent } from './select.component';12describe('SelectComponent', () => {13 it('should select an option', () => {14 const fixture = MockRender(SelectComponent);15 const instance = ngMocks.selectInstance(fixture.debugElement, SelectComponent);16 instance.select('option1');17 expect(ngMocks.formatText(fixture)).toEqual('option1');18 });19});20import { ngMocks } from 'ng-mocks';21import { SelectComponent } from './select.component';22describe('SelectComponent', () => {23 it('should select an option', () => {24 const fixture = MockRender(SelectComponent);25 const instance = ngMocks.selectInstance(fixture.debugElement, SelectComponent);26 ngMocks.selectInput(fixture.debugElement, 'option', 'option1');27 expect(ngMocks.formatText(fixture)).toEqual('option1');28 });29});30import { ngMocks } from 'ng-mocks';31import { SelectComponent } from './select.component';32describe('SelectComponent', () => {33 it('should select an option', () => {34 const fixture = MockRender(SelectComponent);
Using AI Code Generation
1import { ngMocks } from 'ng-mocks';2import { TestComponent } from './test.component';3import { TestModule } from './test.module';4import { TestBed } from '@angular/core/testing';5import { By } from '@angular/platform-browser';6describe('TestComponent', () => {7 beforeEach(() => TestBed.configureTestingModule({ imports: [TestModule] }));8 it('should work', () => {9 const fixture = TestBed.createComponent(TestComponent);10 fixture.detectChanges();11 expect(ngMocks.formatText(fixture)).toEqual(12 );13 expect(ngMocks.formatHtml(fixture)).toEqual(14 );15 expect(ngMocks.input(fixture, TestComponent, 'name')).toEqual(16 );17 expect(ngMocks.findInstance(fixture, TestComponent)).toEqual(18 );19 expect(ngMocks.findInstance(fixture, TestComponent, 1)).toEqual(20 );21 expect(ngMocks.findInstances(fixture, TestComponent)).toEqual(22 );23 expect(ngMocks.findInstances(fixture, TestComponent, 1)).toEqual(24 );25 expect(ngMocks.find(fixture, 'div')).toEqual(26 fixture.debugElement.query(By.css('div')),27 );28 expect(ngMocks.find(fixture, 'div', 1)).toEqual(29 );30 expect(ngMocks.findAll(fixture, 'div')).toEqual(31 fixture.debugElement.queryAll(By.css('div')),32 );33 expect(ngMocks.findAll(fixture, 'div', 1)).toEqual(34 );35 expect(ngMocks.output(fixture, TestComponent, 'changed')).toEqual(36 new EventEmitter(),37 );38 expect(ngMocks.output(fixture, TestComponent, 'changed', 1)).toEqual(39 );40 expect(ngMocks.trigger(fixture, 'div', 'click')).toEqual(41 );42 expect(ngMocks.trigger(fixture, 'div', 'click',
Using AI Code Generation
1import { ngMocks } from 'ng-mocks';2import { MyComponent } from './my.component';3describe('ng-mocks', () => {4 it('should use ngSelectEl', () => {5 const fixture = ngMocks.find(MyComponent);6 const select = ngMocks.selectEl(fixture, 'select');7 console.log(select.value);8 });9});10import { MockBuilder, MockRender } from 'ng-mocks';11import { MyComponent } from './my.component';12describe('MyComponent', () => {13 beforeEach(() => MockBuilder(MyComponent));14 it('should render', () => {15 const fixture = MockRender(MyComponent);16 const select = ngMocks.selectEl(fixture, 'select');17 console.log(select.value);18 });19});20import { ngMocks } from 'ng-mocks';21import { MyComponent } from './my.component';22describe('ng-mocks', () => {23 it('should use ngSelectAll', () => {24 const fixture = ngMocks.find(MyComponent);25 const select = ngMocks.selectAll(fixture, 'select');26 console.log(select.length);27 });28});29import { MockBuilder, MockRender } from 'ng-mocks';30import { MyComponent } from './my.component';31describe('MyComponent', () => {32 beforeEach(() => MockBuilder(MyComponent));33 it('should render', () => {34 const fixture = MockRender(MyComponent);35 const select = ngMocks.selectAll(fixture, 'select');36 console.log(select.length);37 });38});39import { ngMocks } from 'ng-mocks';40import { MyComponent } from './my.component';41describe('ng-mocks', () => {42 it('should use ngSelectAllDebugElements', () => {43 const fixture = ngMocks.find(MyComponent);44 const select = ngMocks.selectAllDebugElements(fixture, 'select');45 console.log(select.length);46 });47});48import
Using AI Code Generation
1import { ngMocks } from 'ng-mocks';2import { SelectComponent } from './select.component';3describe('SelectComponent', () => {4 it('should select an option', () => {5 const fixture = ngMocks.faster.create<SelectComponent>(SelectComponent);6 const component = fixture.componentInstance;7 component.options = ['A', 'B', 'C'];8 fixture.detectChanges();9 const select = ngMocks.find('select');10 ngMocks.selectOption(select, 'B');11 expect(component.selected).toBe('B');12 });13});14import { ngMocks } from 'ng-mocks';15import { InputComponent } from './input.component';16describe('InputComponent', () => {17 it('should trigger an event', () => {18 const fixture = ngMocks.faster.create<InputComponent>(InputComponent);19 const component = fixture.componentInstance;20 const input = ngMocks.find('input');21 ngMocks.trigger(input, 'input');22 expect(component.value).toBe('input');23 });24});25import { ngMocks } from 'ng-mocks';26import { InputComponent } from './input.component';27describe('InputComponent', () => {28 it('should format text', () => {29 const fixture = ngMocks.faster.create<InputComponent>(InputComponent);30 const component = fixture.componentInstance;31 const input = ngMocks.find('input');32 ngMocks.formatText(input, 'test');33 expect(component.value).toBe('test');34 });35});36import { ngMocks } from 'ng-mocks';37import { InputComponent } from './input.component';38describe('InputComponent', () => {39 it('should change an element', () => {40 const fixture = ngMocks.faster.create<InputComponent>(InputComponent);41 const component = fixture.componentInstance;42 const input = ngMocks.find('input');43 ngMocks.change(input, 'test');44 expect(component.value).toBe('test');45 });46});
Using AI Code Generation
1import { ngMocks } from 'ng-mocks';2import { NgSelectComponent } from '@ng-select/ng-select';3describe('NgSelectComponent', () => {4 it('should select the first option', () => {5 const fixture = MockRender(`6 `);7 const select = ngMocks.find(fixture.debugElement, NgSelectComponent);8 ngMocks.selectEl(select, '1');9 expect(select.writeValue).toHaveBeenCalledWith('1');10 });11});12import { ngMocks } from 'ng-mocks';13import { NgSelectComponent } from '@ng-select/ng-select';14describe('NgSelectComponent', () => {15 it('should select the first option', () => {16 const fixture = MockRender(`17 `);18 const select = ngMocks.findInstance(fixture.debugElement, NgSelectComponent);19 select.writeValue('1');20 expect(select.writeValue).toHaveBeenCalledWith('1');21 });22});23import { ngMocks } from 'ng-mocks';24import { NgSelectComponent } from '@ng-select/ng-select';25describe('NgSelectComponent', () => {26 it('should select the first option', () => {27 const fixture = MockRender(`28 `);29 const select = ngMocks.findInstanceAll(fixture.debugElement, NgSelectComponent);30 select[0].writeValue('1');31 expect(select[0].writeValue).toHaveBeenCalledWith('1');32 });33});34import { ngMocks } from 'ng-mocks';35describe('NgSelectComponent',
Using AI Code Generation
1import { ngMocks } from 'ng-mocks';2import { MyComponent } from './my.component';3describe('MyComponent', () => {4 it('should select the button', () => {5 const fixture = ngMocks.faster(MyComponent);6 const button = ngMocks.find(fixture.debugElement, 'button');7 expect(button.nativeElement.textContent).toEqual('Click me');8 });9});10import { ngMocks } from 'ng-mocks';11import { MyComponent } from './my.component';12describe('MyComponent', () => {13 it('should set input', () => {14 const fixture = ngMocks.faster(MyComponent);15 const component = ngMocks.findInstance(fixture.debugElement, MyComponent);16 ngMocks.input(component, 'myInput', 'Hello World');17 expect(component.myInput).toEqual('Hello World');18 });19});20import { ngMocks } from 'ng-mocks';21import { MyComponent } from './my.component';22describe('MyComponent', () => {23 it('should keep the input', () => {24 const fixture = ngMocks.faster(MyComponent);25 const component = ngMocks.findInstance(fixture.debugElement, MyComponent);26 ngMocks.keep(component, 'myInput');27 ngMocks.input(component, 'myInput', 'Hello World');28 expect(component.myInput).toEqual('Hello World');29 });30});31import { ngMocks } from 'ng-mocks';32import { MyComponent } from './my.component';33describe('MyComponent', () => {34 it('should keep all the inputs', () => {35 const fixture = ngMocks.faster(MyComponent);36 const component = ngMocks.findInstance(fixture.debugElement, MyComponent);37 ngMocks.keepAll(component);38 ngMocks.input(component, 'myInput', 'Hello World');39 expect(component.myInput).toEqual('Hello World');40 });41});42import { ngMocks
Using AI Code Generation
1describe('ng-mocks', () => {2 let fixture;3 beforeEach(() => {4 fixture = TestBed.configureTestingModule({5 }).createComponent(AppComponent);6 fixture.detectChanges();7 });8 it('should have a select element', () => {9 const select = fixture.ngSelectEl('select');10 expect(select).toBeTruthy();11 });12});13describe('ng-mocks', () => {14 let fixture;15 beforeEach(() => {16 fixture = TestBed.configureTestingModule({17 }).createComponent(AppComponent);18 fixture.detectChanges();19 });20 it('should have a select element', () => {21 const select = fixture.ngSelectElAll('select');22 expect(select).toBeTruthy();23 });24});25describe('ng-mocks', () => {26 let fixture;27 beforeEach(() => {28 fixture = TestBed.configureTestingModule({29 }).createComponent(AppComponent);30 fixture.detectChanges();31 });32 it('should have a select element', () => {33 const select = fixture.ngSelectElAllAs('select', HTMLSelectElement);34 expect(select).toBeTruthy();35 });36});37describe('ng-mocks', () => {
Using AI Code Generation
1it('should set the value of the select', () => {2 const fixture = TestBed.createComponent(SelectComponent);3 fixture.detectChanges();4 const selectEl = fixture.debugElement.query(By.css('select'));5 ngMocks.selectEl(selectEl, 'option-2');6 expect(selectEl.nativeElement.value).toEqual('option-2');7});8it('should set the value of the input', () => {9 const fixture = TestBed.createComponent(InputComponent);10 fixture.detectChanges();11 const inputEl = fixture.debugElement.query(By.css('input'));12 ngMocks.input(inputEl, 'test');13 expect(inputEl.nativeElement.value).toEqual('test');14});15it('should trigger the click event', () => {16 const fixture = TestBed.createComponent(ClickComponent);17 fixture.detectChanges();18 const buttonEl = fixture.debugElement.query(By.css('button'));19 ngMocks.trigger(buttonEl, 'click');20 expect(fixture.componentInstance.clicked).toEqual(true);21});22it('should format the text', () => {23 const fixture = TestBed.createComponent(FormatTextComponent);24 fixture.detectChanges();25 const divEl = fixture.debugElement.query(By.css('div'));26 expect(ngMocks.formatText(divEl)).toEqual('test');27});28it('should clear the input', () => {29 const fixture = TestBed.createComponent(InputComponent);30 fixture.detectChanges();31 const inputEl = fixture.debugElement.query(By.css('input'));32 ngMocks.input(inputEl, 'test');33 expect(inputEl.nativeElement.value).toEqual('test');34 ngMocks.clearInput(inputEl);35 expect(inputEl.nativeElement.value).toEqual('');36});37it('should clear all inputs', () => {38 const fixture = TestBed.createComponent(AllInputsComponent);39 fixture.detectChanges();
Using AI Code Generation
1const selectEl = ngMocks.find('select').nativeElement;2const select = ngMocks.find('select');3const selectEl1 = ngMocks.find('select').nativeElement;4const select1 = ngMocks.find('select');5const selectEl2 = ngMocks.find('select').nativeElement;6const select2 = ngMocks.find('select');7const selectEl3 = ngMocks.find('select').nativeElement;8const select3 = ngMocks.find('select');9const selectEl4 = ngMocks.find('select').nativeElement;10const select4 = ngMocks.find('select');11const selectEl5 = ngMocks.find('select').nativeElement;12const select5 = ngMocks.find('select');13const selectEl6 = ngMocks.find('select').nativeElement;14const select6 = ngMocks.find('select');15const selectEl7 = ngMocks.find('select').nativeElement;16const select7 = ngMocks.find('select');17const selectEl8 = ngMocks.find('select').nativeElement;18const select8 = ngMocks.find('select');19const selectEl9 = ngMocks.find('select').nativeElement;20const select9 = ngMocks.find('select');21const selectEl10 = ngMocks.find('select').nativeElement;22const select10 = ngMocks.find('select');23const selectEl11 = ngMocks.find('select').nativeElement;24const select11 = ngMocks.find('select');25const selectEl12 = ngMocks.find('select').nativeElement;26const select12 = ngMocks.find('select');27const selectEl13 = ngMocks.find('select').nativeElement;28const select13 = ngMocks.find('select');29const selectEl14 = ngMocks.find('select').nativeElement;30const select14 = ngMocks.find('select');31const selectEl15 = ngMocks.find('select').nativeElement;32const select15 = ngMocks.find('select');33const selectEl16 = ngMocks.find('select').nativeElement;34const select16 = ngMocks.find('select');35const selectEl17 = ngMocks.find('select').nativeElement;36const select17 = ngMocks.find('select');37const selectEl18 = ngMocks.find('select').nativeElement;38const select18 = ngMocks.find('select');39const selectEl19 = ngMocks.find('select').nativeElement;40const select19 = ngMocks.find('select');41const selectEl20 = ngMocks.find('select').nativeElement;
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!!