Best JavaScript code snippet using ng-mocks
historyBuffer.Test.js
Source:historyBuffer.Test.js
...130 });131 it('registerOnChange is called on push', function () {132 var hb = new HistoryBuffer(10);133 var spy = jasmine.createSpy('registerOnChange');134 hb.registerOnChange(onChangekey, spy);135 hb.push(1);136 expect(spy).toHaveBeenCalled();137 });138 it('registerOnChange is called on appendArray', function () {139 var hb = new HistoryBuffer(10);140 var spy = jasmine.createSpy('registerOnChange');141 hb.registerOnChange(onChangekey, spy);142 hb.appendArray([1, 2]);143 expect(spy).toHaveBeenCalled();144 });145 it('registerOnChange is called on setCapacity', function () {146 var hb = new HistoryBuffer(10);147 var spy = jasmine.createSpy('registerOnChange');148 hb.appendArray([1, 2]);149 hb.registerOnChange(onChangekey, spy);150 hb.setCapacity(20);151 expect(spy).toHaveBeenCalled();152 });153 it('registerOnChange is called on setWidth', function () {154 var hb = new HistoryBuffer(10);155 var spy = jasmine.createSpy('registerOnChange');156 hb.appendArray([1, 2]);157 hb.registerOnChange(onChangekey, spy);158 hb.setWidth(2);159 expect(spy).toHaveBeenCalled();160 });161 it('registerOnChange is called on clear', function () {162 var hb = new HistoryBuffer(10);163 var spy = jasmine.createSpy('registerOnChange');164 hb.appendArray([1, 2]);165 hb.registerOnChange(onChangekey, spy);166 hb.clear();167 expect(spy).toHaveBeenCalled();168 });169 it('registerOnChange is called when changing types from numeric to analogWaveform', function () {170 var hb = new HistoryBuffer(10);171 var spy = jasmine.createSpy('registerOnChange');172 hb.registerOnChange(onChangekey, spy);173 hb.setType('analogWaveform');174 expect(spy).toHaveBeenCalled();175 });176 it('registerOnChange is called when changing types from analogWaveform to numeric', function () {177 var hb = new HistoryBuffer(10);178 hb.setType('analogWaveform');179 var spy = jasmine.createSpy('registerOnChange');180 hb.registerOnChange(onChangekey, spy);181 hb.setType('numeric');182 expect(spy).toHaveBeenCalled();183 });184 it('registerOnChange is not called when the type of the buffer is the desired one already', function () {185 var hb = new HistoryBuffer(10);186 var spy = jasmine.createSpy('registerOnChange');187 hb.registerOnChange(onChangekey, spy);188 hb.setType('numeric');189 expect(spy).not.toHaveBeenCalled();190 });191 it('multiple callBacks can be registered and all of them get called', function () {192 var hb = new HistoryBuffer(10);193 var key2 = 'key2';194 var spy1 = jasmine.createSpy('registerOnChange');195 var spy2 = jasmine.createSpy('registerOnChange');196 hb.registerOnChange(onChangekey, spy1);197 hb.registerOnChange(key2, spy2);198 hb.appendArray([1, 2]);199 expect(spy1).toHaveBeenCalled();200 expect(spy2).toHaveBeenCalled();201 });202 it('able to deregister calback.', function () {203 var hb = new HistoryBuffer(10);204 var spy = jasmine.createSpy('registerOnChange');205 hb.registerOnChange(onChangekey, spy);206 hb.deregisterOnChange(onChangekey);207 hb.appendArray([1, 2]);208 expect(spy).not.toHaveBeenCalled();209 });210 });211});212describe('A History Buffer works with analogWaveform data', function () {213 'use strict';214 var onChangekey = 'key';215 var aw, aw1, aw2, aw3, serializedHb;216 var utc = new Date(Date.UTC(1904, 0, 1, 0, 0, 0));217 var TimeZero = new NITimestamp(utc);218 function analogWaveformHB(capacity, width) {219 var hb = new HistoryBuffer(capacity, width);220 hb.setType('analogWaveform');221 return hb222 }223 beforeEach(function () {224 aw = new NIAnalogWaveform({225 t0: TimeZero + 4,226 dt: 1,227 Y:[1, 2, 3]228 });229 aw1 = new NIAnalogWaveform({230 t0: TimeZero + 1,231 dt: 1,232 Y:[1, 2, 3]233 });234 aw2 = new NIAnalogWaveform({235 t0: TimeZero + 8,236 dt: 1,237 Y:[4, 3, 2]238 });239 aw3 = new NIAnalogWaveform({240 t0: TimeZero + 10,241 dt: 1,242 Y:[0, 1, 2]243 });244 serializedHb = {245 data: [[aw, aw1], [aw2, aw3]],246 width: 2,247 capacity: 10,248 valueType: 'HistoryBuffer',249 startIndex: 0,250 count: 2251 };252 });253 it('has a setter and getter for count', function () {254 var hb = analogWaveformHB(10);255 expect(hb.count).toBe(0);256 hb.count = 10;257 expect(hb.count).toBe(10);258 });259 it('has a clear method', function () {260 var hb = analogWaveformHB(10);261 hb.clear();262 expect(hb.capacity).toBe(10);263 });264 it('clear method clears the data', function () {265 var hb = analogWaveformHB(10);266 hb.push(aw);267 hb.clear();268 expect(hb.count).toBe(0);269 });270 it('has a capacity property', function () {271 var hb = analogWaveformHB(10);272 expect(hb.capacity).toBe(10);273 });274 it('has a setCapacity method', function () {275 var hb = analogWaveformHB(10);276 hb.setCapacity(20);277 expect(hb.capacity).toBe(20);278 });279 it('setCapacity method clears the data', function () {280 var hb = analogWaveformHB(10);281 hb.appendArray([aw, aw1]);282 hb.setCapacity(20);283 expect(hb.count).toBe(0);284 });285 it('has a width property', function () {286 var hb = analogWaveformHB(10, 3);287 expect(hb.width).toBe(3);288 });289 it('has a setWidth method', function () {290 var hb = analogWaveformHB(10, 1);291 hb.setWidth(2);292 expect(hb.width).toBe(2);293 });294 it('setWidth method clears the data', function () {295 var hb = analogWaveformHB(10, 1);296 hb.appendArray([aw, aw1]);297 hb.setWidth(2);298 expect(hb.count).toBe(0);299 });300 it('has an appendArray method', function () {301 var hb = analogWaveformHB(10);302 hb.appendArray([aw, aw1]);303 [aw, aw1, undefined].forEach(function (exp, i) {304 expect(hb.get(i)).toBe(exp);305 });306 });307 it('appendArray method works with arrays bigger that the hb capacity', function () {308 var hb = analogWaveformHB(2);309 hb.appendArray([aw, aw1, aw2]);310 [aw1, aw2].forEach(function (exp, i) {311 expect(hb.get(i + 1)).toBe(exp);312 });313 });314 it('appendArray method works for plots with two data series', function () {315 var hb = analogWaveformHB(10, 2);316 hb.appendArray([[aw, aw1], [aw2, aw3]]);317 [[aw, aw1], [aw2, aw3], [undefined, undefined]].forEach(function (exp, i) {318 expect(hb.get(i)).toEqual(exp);319 });320 });321 it('has a toArray method', function () {322 var hb = analogWaveformHB(10);323 hb.appendArray([aw, aw1]);324 expect(hb.toArray()).toEqual([aw, aw1]);325 });326 it('has a toJSON method', function () {327 var hb = analogWaveformHB(10, 2);328 hb.appendArray([[aw, aw1], [aw2,aw3]]);329 expect(hb.toJSON()).toEqual(serializedHb);330 });331 it('toArray method works for plots with two data series', function () {332 var hb = analogWaveformHB(10, 2);333 hb.appendArray([[aw, aw1], [aw2, aw3]]);334 expect(hb.toArray()).toEqual([[aw, aw1], [aw2, aw3]]);335 });336 it('has a toDataSeries method', function () {337 var hb = analogWaveformHB(10);338 hb.appendArray([aw, aw1]);339 expect(hb.toDataSeries(0)).toEqual([[4, 1], [5, 2], [6, 3], [1, 1], [2, 2], [3, 3]]);340 });341 it('toDataSeries method works for history buffers with width > 1', function () {342 var hb = analogWaveformHB(10,2);343 hb.appendArray([[aw, aw2], [aw1, aw3]]);344 expect(hb.toDataSeries(0)).toEqual([[4, 1], [5, 2], [6, 3], [1, 1], [2, 2], [3, 3]]);345 expect(hb.toDataSeries(1)).toEqual([[8, 4], [9, 3], [10, 2], [10, 0], [11, 1],[12, 2]]);346 });347 describe('registerOnChange notification', function () {348 it('has an registerOnChange method', function () {349 var hb = analogWaveformHB(10, 1);350 expect(hb.registerOnChange).toEqual(jasmine.any(Function));351 });352 it('registerOnChange is called on push', function () {353 var hb = analogWaveformHB(10);354 var spy = jasmine.createSpy('registerOnChange');355 hb.registerOnChange(onChangekey, spy);356 hb.push(aw);357 expect(spy).toHaveBeenCalled();358 });359 it('registerOnChange is called on appendArray', function () {360 var hb = analogWaveformHB(10);361 var spy = jasmine.createSpy('registerOnChange');362 hb.registerOnChange(onChangekey, spy);363 hb.appendArray([aw, aw1]);364 expect(spy).toHaveBeenCalled();365 });366 it('registerOnChange is called on setCapacity', function () {367 var hb = analogWaveformHB(10);368 var spy = jasmine.createSpy('registerOnChange');369 hb.appendArray([aw, aw1]);370 hb.registerOnChange(onChangekey, spy);371 hb.setCapacity(20);372 expect(spy).toHaveBeenCalled();373 });374 it('registerOnChange is called on setWidth', function () {375 var hb = analogWaveformHB(10);376 var spy = jasmine.createSpy('registerOnChange');377 hb.appendArray([aw, aw1]);378 hb.registerOnChange(onChangekey, spy);379 hb.setWidth(2);380 expect(spy).toHaveBeenCalled();381 });382 it('registerOnChange is called on clear', function () {383 var hb = analogWaveformHB(10);384 var spy = jasmine.createSpy('registerOnChange');385 hb.appendArray([aw, aw1]);386 hb.registerOnChange(onChangekey, spy);387 hb.clear();388 expect(spy).toHaveBeenCalled();389 });390 });...
input-tester.ts
Source:input-tester.ts
...102 assertEqual(ngModelValue, test[1], 'ionChange: ngmodel was not updated');103 ionChangeCalled++;104 });105 // Test registerOnChange106 input.registerOnChange((ev: any) => {107 assertEqual(OnChangeCalled, 0, 'registerOnChange: internal error');108 assertEqual(input.value, ev, 'registerOnChange: ev output does not match');109 assertEqual(input.value, test[1], 'registerOnChange: value does not match');110 ngModelValue = ev;111 OnChangeCalled++;112 });113 // Test registerOnChange114 input.registerOnTouched(() => {115 assertEqual(OnTouchedCalled, 0, 'registerOnTouched: internal error');116 OnTouchedCalled++;117 });118 // Run corpus119 for (i = 0; i < config.corpus.length; i++) {120 test = config.corpus[i];121 input.value = test[0];122 assertEqual(input.value, test[1], 'loop: input/output does not match');123 if (isInit) {124 assertEqual(ionChangeCalled, 1, 'loop: ionChange error');125 } else {126 assertEqual(ionChangeCalled, 0, 'loop: ionChange error');127 }128 assertEqual(OnChangeCalled, 1, 'loop: OnChangeCalled was not called');129 assertEqual(OnTouchedCalled, 1, 'loop: OnTouchedCalled was not called');130 OnTouchedCalled = OnChangeCalled = ionChangeCalled = 0;131 // Set same value (it should not redispatch)132 input.value = test[0];133 assertEqual(ionChangeCalled, 0, 'loop: ionChange should not be called');134 assertEqual(OnChangeCalled, 0, 'loop: OnChangeCalled should not be called');135 // TODO OnTouchedCalled?136 OnTouchedCalled = OnChangeCalled = ionChangeCalled = 0;137 }138 // Test undefined139 input.value = undefined;140 assertEqual(input.value, test[1], 'undefined should not change the value');141 assertEqual(ionChangeCalled, 0, 'undefined: ionChange should not be called');142 assertEqual(OnChangeCalled, 0, 'undefined: OnChangeCalled should not be called');143 assertEqual(OnTouchedCalled, 0, 'undefined: OnTouchedCalled should not be called');144 // Test null (reset)145 test = [null, config.defaultValue];146 input.value = null;147 assertEqual(input.value, config.defaultValue, 'null: wrong default value');148 assertEqual(OnChangeCalled, 1, 'null: OnChangeCalled was not called');149 assertEqual(OnTouchedCalled, 1, 'null: OnTouchedCalled was not called');150 input.registerOnChange(null);151 input.registerOnTouched(null);152 sub.unsubscribe();153}154function testNgModelChange<T>(input: BaseInput<T>, config: TestConfig, isInit: boolean) {155 let test: any;156 let i: number;157 let ionChangeCalled = 0;158 let OnChangeCalled = 0;159 let OnTouchedCalled = 0;160 // Test ionChange161 let sub = input.ionChange.subscribe((ev: any) => {162 assertEqual(ionChangeCalled, 0, 'internal error');163 assertEqual(ev, input, 'ev output does not match');164 assertEqual(test[1], ev.value, 'value does not match');165 ionChangeCalled++;166 });167 // Test registerOnChange168 input.registerOnChange(() => {169 OnChangeCalled++;170 });171 // Test registerOnChange172 input.registerOnTouched(() => {173 OnTouchedCalled++;174 });175 // Run corpus176 for (i = 0; i < config.corpus.length; i++) {177 test = config.corpus[i];178 input.writeValue(test[0]);179 assertEqual(input.value, test[1], 'input/output does not match');180 if (isInit) {181 assertEqual(ionChangeCalled, 1, 'ionChange error');182 } else {183 assertEqual(ionChangeCalled, 0, 'ionChange error');184 }185 assertEqual(OnChangeCalled, 0, 'OnChangeCalled should not be called');186 assertEqual(OnTouchedCalled, 0, 'OnTouchedCalled should not be called');187 OnTouchedCalled = OnChangeCalled = ionChangeCalled = 0;188 // Set same value (it should not redispatch)189 input.writeValue(test[0]);190 input.value = test[0];191 assertEqual(ionChangeCalled, 0, 'ionChange should not be called');192 assertEqual(OnChangeCalled, 0, 'OnChangeCalled should not be called');193 // TODO OnTouchedCalled?194 OnTouchedCalled = OnChangeCalled = ionChangeCalled = 0;195 }196 input.registerOnChange(null);197 input.registerOnTouched(null);198 sub.unsubscribe();199 input.value = config.defaultValue;200}201function assertEqual(a: any, b: any, message: string) {202 if (!equal(a, b)) {203 assert(false, a + ' != ' + b + ' ' + message);204 }205}206function equal(a: any, b: any): boolean {207 if (a === b) {208 return true;209 }210 // return false;...
historyBuffer_aw.Test.js
Source:historyBuffer_aw.Test.js
...144 });145 it('registerOnChange is called on push', function () {146 var hb = new HistoryBufferWaveform(10);147 var spy = jasmine.createSpy('registerOnChange');148 hb.registerOnChange(onChangeKey, spy);149 hb.push(aw);150 expect(spy).toHaveBeenCalled();151 });152 it('registerOnChange is called on appendArray', function () {153 var hb = new HistoryBufferWaveform(10);154 var spy = jasmine.createSpy('registerOnChange');155 hb.registerOnChange(onChangeKey, spy);156 hb.appendArray([aw, aw1]);157 expect(spy).toHaveBeenCalled();158 });159 it('registerOnChange is called on setCapacity', function () {160 var hb = new HistoryBufferWaveform(10);161 var spy = jasmine.createSpy('registerOnChange');162 hb.appendArray([aw, aw1]);163 hb.registerOnChange(onChangeKey, spy);164 hb.setCapacity(20);165 expect(spy).toHaveBeenCalled();166 });167 it('registerOnChange is called on setWidth', function () {168 var hb = new HistoryBufferWaveform(10);169 var spy = jasmine.createSpy('registerOnChange');170 hb.appendArray([aw, aw1]);171 hb.registerOnChange(onChangeKey, spy);172 hb.setWidth(2);173 expect(spy).toHaveBeenCalled();174 });175 it('registerOnChange is called on clear', function () {176 var hb = new HistoryBufferWaveform(10);177 var spy = jasmine.createSpy('registerOnChange');178 hb.appendArray([aw, aw1]);179 hb.registerOnChange(onChangeKey,spy);180 hb.clear();181 expect(spy).toHaveBeenCalled();182 });183 });...
Using AI Code Generation
1import { Component } from '@angular/core';2import { ComponentFixture, TestBed } from '@angular/core/testing';3import { MockBuilder, MockRender, ngMocks } from 'ng-mocks';4@Component({5 <input type="text" [(ngModel)]="test" />6})7class TestComponent {8 public test: string;9}10describe('TestComponent', () => {11 let fixture: ComponentFixture<TestComponent>;12 let component: TestComponent;13 beforeEach(() => MockBuilder(TestComponent));14 beforeEach(() => {15 fixture = MockRender(TestComponent);16 component = fixture.componentInstance;17 });18 it('should call registerOnChange', () => {19 const spy = spyOn(component, 'registerOnChange');20 ngMocks.change('test', 'test');21 expect(spy).toHaveBeenCalled();22 });23});24import { TestComponent } from './test';25describe('TestComponent', () => {26 let fixture: ComponentFixture<TestComponent>;27 let component: TestComponent;28 beforeEach(() => MockBuilder(TestComponent));29 beforeEach(() => {30 fixture = MockRender(TestComponent);31 component = fixture.componentInstance;32 });33 it('should call registerOnChange', () => {34 const spy = spyOn(component, 'registerOnChange');35 ngMocks.change('test', 'test');36 expect(spy).toHaveBeenCalled();37 });38});
Using AI Code Generation
1import { registerOnChange } from 'ng-mocks';2import { MyComponent } from './my-component';3describe('MyComponent', () => {4 let component: MyComponent;5 let fixture: ComponentFixture<MyComponent>;6 beforeEach(async(() => {7 TestBed.configureTestingModule({8 }).compileComponents();9 }));10 beforeEach(() => {11 fixture = TestBed.createComponent(MyComponent);12 component = fixture.componentInstance;13 fixture.detectChanges();14 });15 it('should call onChange method', () => {16 const onChange = jasmine.createSpy('onChange');17 registerOnChange(component, 'prop', onChange);18 component.prop = 'changed';19 expect(onChange).toHaveBeenCalled();20 });21});22import { Component, Input, OnInit } from '@angular/core';23@Component({24})25export class MyComponent implements OnInit {26 @Input() prop: string;27 ngOnInit() {28 this.prop = 'initial';29 }30}
Using AI Code Generation
1import { MockBuilder, MockedComponentFixture, MockRender, ngMocks } from 'ng-mocks';2import { AppModule } from './app.module';3import { AppComponent } from './app.component';4import { FormControl, ReactiveFormsModule } from '@angular/forms';5describe('AppComponent', () => {6 ngMocks.faster();7 let component: AppComponent;8 let fixture: MockedComponentFixture<AppComponent>;9 beforeEach(() => MockBuilder(AppComponent, AppModule)10 .keep(ReactiveFormsModule)11 );12 beforeEach(() => {13 fixture = MockRender(AppComponent);14 component = fixture.point.componentInstance;15 });16 it('should call registerOnChange when value is changed', () => {17 const onChangeSpy = spyOn(component.control, 'registerOnChange');18 const formControl = ngMocks.find('input');19 formControl.value = 'test';20 formControl.dispatchEvent(new Event('input'));21 expect(onChangeSpy).toHaveBeenCalled();22 });23});24import { Component, forwardRef, Input } from '@angular/core';25import { ControlValueAccessor, FormControl, NG_VALUE_ACCESSOR } from '@angular/forms';26@Component({27 {28 useExisting: forwardRef(() => AppComponent),29 }30})31export class AppComponent implements ControlValueAccessor {32 @Input() control: FormControl;33 writeValue(value: any): void {34 this.control.setValue(value);35 }36 registerOnChange(fn: any): void {37 this.control.valueChanges.subscribe(fn);38 }39 registerOnTouched(fn: any): void {}40 setDisabledState?(isDisabled: boolean): void {}41}42import { NgModule } from '@angular/core';43import { BrowserModule } from '@angular/platform-browser';44import { ReactiveFormsModule } from '@angular/forms';45import { AppComponent } from './app.component';46@NgModule({47 imports: [ BrowserModule, ReactiveFormsModule ],48})49export class AppModule { }50input {51 border: 1px solid #000;52 border-radius: 4px;53 height: 30px;54 padding: 0 10px;55}
Using AI Code Generation
1import { registerOnChange } from 'ng-mocks';2import { Component } from '@angular/core';3import { FormGroup, FormBuilder } from '@angular/forms';4@Component({5})6export class TestComponent {7 form: FormGroup;8 constructor(private fb: FormBuilder) {9 this.form = this.fb.group({10 });11 }12 ngOnInit() {13 registerOnChange(this.form.get('name'), (value) => {14 console.log(value);15 });16 }17}18import { async, ComponentFixture, TestBed } from '@angular/core/testing';19import { TestComponent } from './test.component';20describe('TestComponent', () => {21 let component: TestComponent;22 let fixture: ComponentFixture<TestComponent>;23 beforeEach(async(() => {24 TestBed.configureTestingModule({25 })26 .compileComponents();27 }));28 beforeEach(() => {29 fixture = TestBed.createComponent(TestComponent);30 component = fixture.componentInstance;31 fixture.detectChanges();32 });33 it('should create', () => {34 expect(component).toBeTruthy();35 });36 it('should call the onChange function', () => {37 const input = fixture.debugElement.nativeElement.querySelector('input');38 input.value = 'test';39 input.dispatchEvent(new Event('input'));40 expect(input.value).toBe('test');41 });42});43import { async, ComponentFixture, TestBed } from '@angular/core/testing';44import { Router } from '@angular/router';45import { FormsModule, ReactiveFormsModule } from '@angular/forms';46import { RouterTestingModule } from '@angular/router/testing';47import { MockModule } from 'ng-mocks';48import { of } from 'rxjs';49import { TestComponent } from './test.component';50import { TestService } from './test.service';51describe('
Using AI Code Generation
1import { TestBed } from '@angular/core/testing';2import { MockBuilder, MockRender, ngMocks } from 'ng-mocks';3import { FormComponent } from './form.component';4beforeEach(() => MockBuilder(FormComponent));5it('changes the value', () => {6 const fixture = MockRender(FormComponent);7 const component = ngMocks.findInstance(FormComponent);8 const spy = spyOn(component, 'onChange');9 ngMocks.change(fixture.debugElement.children[0], 'value');10 expect(spy).toHaveBeenCalledWith('value');11});12import { Component, OnInit, Output, EventEmitter } from '@angular/core';13@Component({14 <input type="text" (input)="onChange($event.target.value)" />15})16export class FormComponent implements OnInit {17 @Output() valueChange = new EventEmitter<string>();18 constructor() {}19 ngOnInit(): void {}20 onChange(value: string) {21 this.valueChange.emit(value);22 }23}24import { ComponentFixture, TestBed } from '@angular/core/testing';25import { FormComponent } from './form.component';26describe('FormComponent', () => {27 let component: FormComponent;28 let fixture: ComponentFixture<FormComponent>;29 beforeEach(async () => {30 await TestBed.configureTestingModule({31 }).compileComponents();32 });33 beforeEach(() => {34 fixture = TestBed.createComponent(FormComponent);35 component = fixture.componentInstance;36 fixture.detectChanges();37 });38 it('should create', () => {39 expect(component).toBeTruthy();40 });41});
Using AI Code Generation
1import { registerOnChange } from 'ng-mocks';2describe('InputComponent', () => {3 let component: InputComponent;4 let fixture: ComponentFixture<InputComponent>;5 beforeEach(async () => {6 await TestBed.configureTestingModule({7 }).compileComponents();8 });9 beforeEach(() => {10 fixture = TestBed.createComponent(InputComponent);11 component = fixture.componentInstance;12 fixture.detectChanges();13 });14 it('should create', () => {15 expect(component).toBeTruthy();16 });17 it('should call registerOnChange', () => {18 const spy = registerOnChange(component, 'onChange');19 component.onChange('test');20 expect(spy).toHaveBeenCalledWith('test');21 });22});23 ✓ should create (3ms)24 ✓ should call registerOnChange (1ms)
Using AI Code Generation
1import { MockBuilder, MockRender } from 'ng-mocks';2import { Component, NgModule } from '@angular/core';3import { FormsModule } from '@angular/forms';4import { MyComponent } from './my.component';5MockBuilder(MyComponent, FormsModule).then(() => {6 const fixture = MockRender(MyComponent);7 const component = fixture.point.componentInstance;8 component.registerOnChange((value: any) => {9 console.log(value);10 });11 component.registerOnTouched((value: any) => {12 console.log(value);13 });14});15import { Component, Input, OnInit } from '@angular/core';16import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';17@Component({18 {19 },20})21export class MyComponent implements ControlValueAccessor {22 @Input() value: string = '';23 onChange: any = () => {};24 onTouched: any = () => {};25 writeValue(value: any): void {26 this.value = value;27 }28 registerOnChange(fn: any): void {29 this.onChange = fn;30 }
Using AI Code Generation
1import { Component, OnInit } from '@angular/core';2import { FormBuilder, FormGroup, Validators } from '@angular/forms';3import { MockBuilder, MockRender } from 'ng-mocks';4import { MyComponent } from './my.component';5import { MyModule } from './my.module';6describe('MyComponent', () => {7 beforeEach(() => MockBuilder(MyComponent, MyModule));8 it('should create', () => {9 const fixture = MockRender(MyComponent);10 const component = fixture.point.componentInstance;11 expect(component).toBeTruthy();12 });13 it('should call registerOnChange method', () => {14 const fixture = MockRender(MyComponent);15 const component = fixture.point.componentInstance;16 const spy = spyOn(component, 'registerOnChange');17 const input = fixture.debugElement.nativeElement.querySelector('input');18 input.value = 'test';19 input.dispatchEvent(new Event('input'));20 expect(spy).toHaveBeenCalled();21 });22});23import { async, ComponentFixture, TestBed } from '@angular/core/testing';24import { ReactiveFormsModule } from '@angular/forms';25import { MyComponent } from './my.component';26describe('MyComponent', () => {27 let component: MyComponent;28 let fixture: ComponentFixture<MyComponent>;29 beforeEach(async(() => {30 TestBed.configureTestingModule({31 imports: [ReactiveFormsModule],32 }).compileComponents();33 }));34 beforeEach(() => {35 fixture = TestBed.createComponent(MyComponent);36 component = fixture.componentInstance;37 fixture.detectChanges();38 });39 it('should create', () => {40 expect(component).toBeTruthy();41 });42 it('should call registerOnChange method', () => {43 const spy = spyOn(component, 'registerOnChange');44 const input = fixture.debugElement.nativeElement.querySelector('input');45 input.value = 'test';46 input.dispatchEvent(new Event('input'));47 expect(spy).toHaveBeenCalled();48 });49});
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!!