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 { Component } from '@angular/core';
import { FormControl, FormGroup, ReactiveFormsModule } from '@angular/forms'; import { FormControl, FormGroup, ReactiveFormsModule } from '@angular/forms';
import { provideTranslateService } from '@ngx-translate/core'; import { provideTranslateService } from '@ngx-translate/core';
import { DetectedBarcode } from '../../types/globalThis';
import { By } from '@angular/platform-browser';
@Component({ @Component({
selector: 'app-bar-code-input-mock', selector: 'app-bar-code-input-mock',
imports: [ imports: [BarCodeInput, ReactiveFormsModule],
BarCodeInput,
ReactiveFormsModule
],
template: ` template: `
<form [formGroup]="form"> <form [formGroup]="form">
<app-bar-code-input formControlName="mock" /> <app-bar-code-input formControlName="mock" />
</form> </form>
`, `,
styleUrl: './bar-code-input.scss' styleUrl: './bar-code-input.scss',
}) })
class BarCodeInputTestbed { class BarCodeInputTestbed {
form = new FormGroup({mock: new FormControl(null)}) form = new FormGroup({ mock: new FormControl(null) });
} }
describe('BarCodeInput', () => { describe('BarCodeInput', () => {
let component: BarCodeInputTestbed; let component: BarCodeInputTestbed;
let fixture: ComponentFixture<BarCodeInputTestbed>; let fixture: ComponentFixture<BarCodeInputTestbed>;
let BARCODE_MOCK: {
code: Partial<DetectedBarcode> | null;
};
beforeEach(async () => { beforeEach(async () => {
await TestBed.configureTestingModule({ await TestBed.configureTestingModule({
imports: [BarCodeInputTestbed], imports: [BarCodeInputTestbed],
providers: [provideTranslateService()] providers: [provideTranslateService()],
}) }).compileComponents();
.compileComponents();
fixture = TestBed.createComponent(BarCodeInputTestbed); fixture = TestBed.createComponent(BarCodeInputTestbed);
component = fixture.componentInstance; component = fixture.componentInstance;
@@ -41,4 +42,24 @@ describe('BarCodeInput', () => {
it('should create', () => { it('should create', () => {
expect(component).toBeTruthy(); 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(); this.control.markAsDirty();
} }
} }
writeValue(obj: any): void {} writeValue(obj: any): void {}
registerOnChange(fn: any): void {} registerOnChange(fn: any): void {}
registerOnTouched(fn: any): void {} registerOnTouched(fn: any): void {}