From 564e735b14687bf8fbfe9a4414e709e76a826095 Mon Sep 17 00:00:00 2001 From: Gabriel De Los Rios Date: Sun, 22 Feb 2026 22:49:20 -0300 Subject: [PATCH] test: extends coverage --- .../bar-code-input/bar-code-input.spec.ts | 47 ++++++++++++++----- .../bar-code-input/bar-code-input.ts | 1 + 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/src/app/components/bar-code-input/bar-code-input.spec.ts b/src/app/components/bar-code-input/bar-code-input.spec.ts index 04af543..72135d7 100644 --- a/src/app/components/bar-code-input/bar-code-input.spec.ts +++ b/src/app/components/bar-code-input/bar-code-input.spec.ts @@ -4,34 +4,35 @@ import { BarCodeInput } from './bar-code-input'; import { Component } from '@angular/core'; import { FormControl, FormGroup, ReactiveFormsModule } from '@angular/forms'; import { provideTranslateService } from '@ngx-translate/core'; +import { DetectedBarcode } from '../../types/globalThis'; +import { By } from '@angular/platform-browser'; @Component({ selector: 'app-bar-code-input-mock', - imports: [ - BarCodeInput, - ReactiveFormsModule - ], + imports: [BarCodeInput, ReactiveFormsModule], template: ` -
- - +
+ + `, - styleUrl: './bar-code-input.scss' + styleUrl: './bar-code-input.scss', }) class BarCodeInputTestbed { - form = new FormGroup({mock: new FormControl(null)}) + form = new FormGroup({ mock: new FormControl(null) }); } describe('BarCodeInput', () => { let component: BarCodeInputTestbed; let fixture: ComponentFixture; - + + let BARCODE_MOCK: { + code: Partial | null; + }; beforeEach(async () => { await TestBed.configureTestingModule({ imports: [BarCodeInputTestbed], - providers: [provideTranslateService()] - }) - .compileComponents(); + providers: [provideTranslateService()], + }).compileComponents(); fixture = TestBed.createComponent(BarCodeInputTestbed); component = fixture.componentInstance; @@ -41,4 +42,24 @@ describe('BarCodeInput', () => { it('should create', () => { expect(component).toBeTruthy(); }); + + describe('on result', () => { + it('should patch input value with barcode value ', () => { + const patchValueSpy = vi.spyOn(component.form.controls.mock, 'patchValue'); + const markAsDirtySpy = vi.spyOn(component.form.controls.mock, 'markAsDirty'); + BARCODE_MOCK = {code: {'rawValue': 'mock'}} + const barcodeReader = fixture.debugElement.query(By.css('app-bar-code-reader')); + barcodeReader.triggerEventHandler('result', new CustomEvent('result', {detail: BARCODE_MOCK})); + expect(patchValueSpy).toHaveBeenCalledExactlyOnceWith(BARCODE_MOCK.code?.rawValue); + expect(markAsDirtySpy).toHaveBeenCalledTimes(1); + }); + + it('should not patch input value if barcode value is null', () => { + const patchValueSpy = vi.spyOn(component.form.controls.mock, 'patchValue'); + BARCODE_MOCK = {code: null} + const barcodeReader = fixture.debugElement.query(By.css('app-bar-code-reader')); + barcodeReader.triggerEventHandler('result', new CustomEvent('result', {detail: BARCODE_MOCK})); + expect(patchValueSpy).not.toHaveBeenCalled(); + }); + }); }); diff --git a/src/app/components/bar-code-input/bar-code-input.ts b/src/app/components/bar-code-input/bar-code-input.ts index 4b75863..a455a44 100644 --- a/src/app/components/bar-code-input/bar-code-input.ts +++ b/src/app/components/bar-code-input/bar-code-input.ts @@ -52,6 +52,7 @@ export class BarCodeInput implements ControlValueAccessor, OnInit { this.control.markAsDirty(); } } + writeValue(obj: any): void {} registerOnChange(fn: any): void {} registerOnTouched(fn: any): void {}