From 2de3c39f093aef43c5b32a9d05996912e7af9882 Mon Sep 17 00:00:00 2001 From: Deborah Date: Tue, 14 May 2024 18:47:01 +0100 Subject: [PATCH] fix unit test --- src/app/components/inputmask/inputmask.ts | 2 +- src/app/components/inputnumber/inputnumber.ts | 2 +- src/app/components/listbox/listbox.spec.ts | 33 ++++++++----------- src/app/components/listbox/listbox.ts | 2 +- 4 files changed, 17 insertions(+), 22 deletions(-) diff --git a/src/app/components/inputmask/inputmask.ts b/src/app/components/inputmask/inputmask.ts index 404e5520f11..a6e8b27adc7 100755 --- a/src/app/components/inputmask/inputmask.ts +++ b/src/app/components/inputmask/inputmask.ts @@ -94,7 +94,7 @@ export const INPUTMASK_VALUE_ACCESSOR: any = { (keydown)="onInputKeydown($event)" (keypress)="onKeyPress($event)" pAutoFocus - [variant]="variant" + [attr.variant]="variant" [autofocus]="autofocus" (input)="onInputChange($event)" (paste)="handleInputChange($event)" diff --git a/src/app/components/inputnumber/inputnumber.ts b/src/app/components/inputnumber/inputnumber.ts index 6fe94ec2102..c5e68828dac 100644 --- a/src/app/components/inputnumber/inputnumber.ts +++ b/src/app/components/inputnumber/inputnumber.ts @@ -68,7 +68,7 @@ export const INPUTNUMBER_VALUE_ACCESSOR: any = { [ngStyle]="inputStyle" [class]="inputStyleClass" [value]="formattedValue()" - [variant]="variant" + [attr.variant]="variant" [attr.aria-valuemin]="min" [attr.aria-valuemax]="max" [attr.aria-valuenow]="value" diff --git a/src/app/components/listbox/listbox.spec.ts b/src/app/components/listbox/listbox.spec.ts index adace760cd4..730a626bd8b 100755 --- a/src/app/components/listbox/listbox.spec.ts +++ b/src/app/components/listbox/listbox.spec.ts @@ -43,7 +43,7 @@ describe('Listbox', () => { { label: 'VW', value: 'VW' }, { label: 'Volvo', value: 'Volvo' } ]; - const onOptionSelectSpy = spyOn(listbox, 'onOptionSelect').and.callThrough(); + const onClickSpy = spyOn(listbox.onClick, 'emit').and.callThrough(); fixture.detectChanges(); const bmwEl = fixture.debugElement.query(By.css('ul')).children[1].nativeElement; @@ -52,7 +52,7 @@ describe('Listbox', () => { const filterInputEl = fixture.debugElement.query(By.css('.p-listbox-filter-container')).query(By.css('input')).nativeElement; expect(filterInputEl.disabled).toEqual(true); - expect(onOptionSelectSpy).not.toHaveBeenCalled(); + expect(onClickSpy).not.toHaveBeenCalled(); }); it('should call onOptionTouchEnd', () => { @@ -101,7 +101,7 @@ describe('Listbox', () => { fixture.detectChanges(); expect(onOptionTouchEndSpy).toHaveBeenCalled(); - expect(listbox.optionTouched).toEqual(undefined); + expect(listbox.optionTouched).toBeTrue(); }); it('should change style and styleClass', () => { @@ -259,7 +259,6 @@ describe('Listbox', () => { fixture.detectChanges(); expect(listbox.value.length).toEqual(10); - expect(listbox.selectAll).toEqual(true); expect(selectAllEl.className).toContain('p-highlight'); expect(onToggleAllSpy).toHaveBeenCalled(); }); @@ -278,6 +277,7 @@ describe('Listbox', () => { { label: 'Volvo', value: 'Volvo' } ]; listbox.filter = true; + listbox.optionLabel = 'label'; fixture.detectChanges(); const filterInputEl = fixture.debugElement.query(By.css('.p-listbox-filter-container')).children[0].nativeElement; @@ -503,7 +503,6 @@ describe('Listbox', () => { fixture.detectChanges(); expect(listbox.value.length).toEqual(0); - expect(listbox.selectAll).toEqual(false); expect(selectAllEl.className).not.toContain('p-highlight'); expect(toggleAllSpy).toHaveBeenCalledTimes(2); }); @@ -577,6 +576,7 @@ describe('Listbox', () => { listbox.checkbox = true; listbox.filter = true; listbox.filterValue = 'o'; + listbox.optionLabel = 'label'; fixture.detectChanges(); const headerCheckBoxReadonlyEl = fixture.debugElement.query(By.css('.p-checkbox.p-component')).query(By.css('input')); @@ -609,27 +609,22 @@ describe('Listbox', () => { { label: 'VW', value: 'VW' }, { label: 'Volvo', value: 'Volvo' } ]; - const findNextOptionIndexSpy = spyOn(listbox, 'findNextOptionIndex').and.callThrough(); - const findPrevOptionIndexSpy = spyOn(listbox, 'findPrevOptionIndex').and.callThrough(); + const onArrowDownKeySpy = spyOn(listbox, 'onArrowDownKey').and.callThrough(); + const onArrowUpKeySpy = spyOn(listbox, 'onArrowUpKey').and.callThrough(); fixture.detectChanges(); - const bmwEl = fixture.debugElement.query(By.css('ul')).children[1].nativeElement; - const event: any = document.createEvent('CustomEvent'); - event.which = 40; - event.initEvent('keydown'); - bmwEl.dispatchEvent(event); + const bmwEl = fixture.debugElement.query(By.css('.p-listbox-list')).nativeElement; + bmwEl.dispatchEvent(new KeyboardEvent('keydown', { code: 'ArrowDown' })); fixture.detectChanges(); - event.which = 38; - bmwEl.dispatchEvent(event); + bmwEl.dispatchEvent(new KeyboardEvent('keydown', { code: 'ArrowUp' })); fixture.detectChanges(); - event.which = 13; - bmwEl.dispatchEvent(event); + bmwEl.dispatchEvent(new KeyboardEvent('keydown', { code: 'Enter' })); fixture.detectChanges(); - expect(findNextOptionIndexSpy).toHaveBeenCalled(); - expect(findPrevOptionIndexSpy).toHaveBeenCalled(); - expect(listbox.value).toEqual('BMW'); + expect(onArrowDownKeySpy).toHaveBeenCalled(); + expect(onArrowUpKeySpy).toHaveBeenCalled(); + expect(listbox.value).toEqual('Audi'); }); }); diff --git a/src/app/components/listbox/listbox.ts b/src/app/components/listbox/listbox.ts index c7679b2cb2b..8873b611fbc 100755 --- a/src/app/components/listbox/listbox.ts +++ b/src/app/components/listbox/listbox.ts @@ -179,7 +179,7 @@ export const LISTBOX_VALUE_ACCESSOR: any = { [attr.aria-selected]="isSelected(option)" [attr.aria-disabled]="isOptionDisabled(option)" [attr.aria-setsize]="ariaSetSize" - [ariaPosInset]="getAriaPosInset(getOptionIndex(i, scrollerOptions))" + [attr.ariaPosInset]="getAriaPosInset(getOptionIndex(i, scrollerOptions))" (click)="onOptionSelect($event, option, getOptionIndex(i, scrollerOptions))" (dblclick)="onOptionDoubleClick($event, option)" (mousedown)="onOptionMouseDown($event, getOptionIndex(i, scrollerOptions))"