Skip to content

Commit

Permalink
Merge pull request #15723 from dobanisola-scottlogic/fix-picklist-uni…
Browse files Browse the repository at this point in the history
…t-tests

fix picklist unit tests
  • Loading branch information
cetincakiroglu authored May 30, 2024
2 parents 067476f + c36b57d commit ecc37a8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
21 changes: 9 additions & 12 deletions src/app/components/picklist/picklist.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ describe('PickList', () => {

let event = { ctrlKey: true };
let callback = new EventEmitter();
picklist.onItemClick(event, picklist.source, picklist.selectedItemsSource, picklist.SOURCE_LIST, callback);
picklist.onItemClick(event, picklist.selectedItemsSource[0], picklist.selectedItemsSource, picklist.SOURCE_LIST, callback);
fixture.detectChanges();

picklist.cd.detectChanges();
Expand Down Expand Up @@ -756,28 +756,25 @@ describe('PickList', () => {
it('should change focused item with up and down arrows', () => {
fixture.detectChanges();

const itemList = fixture.debugElement.query(By.css('.p-picklist-list'));
let items = fixture.debugElement.queryAll(By.css('.p-picklist-item'));
items[0].nativeElement.click();
fixture.detectChanges();

expect(picklist.selectedItemsSource.length).toEqual(1);
expect(picklist.selectedItemsSource[0].brand).toBe('VW');
const keydownEvent: any = document.createEvent('CustomEvent');
keydownEvent.which = 40;
keydownEvent.initEvent('keydown', true, true);
items[0].nativeElement.dispatchEvent(keydownEvent);
itemList.nativeElement.dispatchEvent(new KeyboardEvent('keydown', { code: 'ArrowDown' }));
fixture.detectChanges();

expect(document.activeElement).toEqual(items[1].nativeElement);
keydownEvent.which = 38;
items[1].nativeElement.dispatchEvent(keydownEvent);
expect(items[1].nativeElement.className).toContain('p-focus');
itemList.nativeElement.dispatchEvent(new KeyboardEvent('keydown', { code: 'ArrowUp' }));
fixture.detectChanges();

expect(document.activeElement).toEqual(items[0].nativeElement);
keydownEvent.which = 13;
items[1].nativeElement.dispatchEvent(keydownEvent);
expect(items[0].nativeElement.className).toContain('p-focus');
itemList.nativeElement.dispatchEvent(new KeyboardEvent('keydown', { code: 'ArrowDown' }));
itemList.nativeElement.dispatchEvent(new KeyboardEvent('keydown', { code: 'Enter' }));
fixture.detectChanges();

expect(picklist.selectedItemsSource[0].brand).toBe('Audi');
expect(picklist.selectedItemsSource[1].brand).toBe('Audi');
});
});
5 changes: 2 additions & 3 deletions src/app/components/picklist/picklist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ import {
>
<ng-template ngFor let-item [ngForOf]="source" [ngForTrackBy]="sourceTrackBy || trackBy" let-i="index" let-l="last">
<li
[ngClass]="{ 'p-picklist-item': true, 'p-highlight': isSelected(item, selectedItemsSource), 'p-disabled': disabled }"
pRipple
cdkDrag
[id]="idSource + '_' + i"
Expand Down Expand Up @@ -273,7 +272,6 @@ import {
>
<ng-template ngFor let-item [ngForOf]="target" [ngForTrackBy]="targetTrackBy || trackBy" let-i="index" let-l="last">
<li
[ngClass]="{ 'p-picklist-item': true, 'p-highlight': isSelected(item, selectedItemsTarget), 'p-disabled': disabled }"
pRipple
cdkDrag
[id]="idTarget + '_' + i"
Expand Down Expand Up @@ -1258,7 +1256,8 @@ export class PickList implements AfterViewChecked, AfterContentInit {
return {
'p-picklist-item': true,
'p-highlight': this.isSelected(item, selectedItems),
'p-focus': id === this.focusedOptionId
'p-focus': id === this.focusedOptionId,
'p-disabled': this.disabled
};
}

Expand Down

0 comments on commit ecc37a8

Please sign in to comment.