test: extends coverage

This commit is contained in:
2026-02-22 22:49:20 -03:00
parent ec5055e8d6
commit 564e735b14
2 changed files with 35 additions and 13 deletions

View File

@@ -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: `
<form [formGroup]="form">
<app-bar-code-input formControlName="mock"/>
<app-bar-code-input formControlName="mock" />
</form>
`,
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<BarCodeInputTestbed>;
let BARCODE_MOCK: {
code: Partial<DetectedBarcode> | 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();
});
});
});

View File

@@ -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 {}