Skip to content

Commit

Permalink
Merge pull request #14462 from ashikjs/test/fix-breaking-test-case
Browse files Browse the repository at this point in the history
Rename autocomplete methods in test
  • Loading branch information
cetincakiroglu authored Jan 4, 2024
2 parents 00932a6 + a6fe6d3 commit ad7e213
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 142 deletions.
53 changes: 26 additions & 27 deletions src/app/components/autocomplete/autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ describe('AutoComplete', () => {
inputEl.nativeElement.click();
fixture.detectChanges();

const onKeydownSpy = spyOn(autocomplete, 'onKeydown').and.callThrough();
const onKeyupSpy = spyOn(autocomplete, 'onKeyup').and.callThrough();
const onKeydownSpy = spyOn(autocomplete, 'onKeyDown').and.callThrough();
const onKeyupSpy = spyOn(autocomplete, 'onKeyUp').and.callThrough();
const onInputSpy = spyOn(autocomplete, 'onInput').and.callThrough();
const handleSuggestionsChangeSpy = spyOn(autocomplete, 'handleSuggestionsChange').and.callThrough();
const filterBrandsSpy = spyOn(testComponent, 'filterBrands').and.callThrough();
Expand Down Expand Up @@ -353,7 +353,6 @@ describe('AutoComplete', () => {
inputEl.nativeElement.click();
fixture.detectChanges();

const selectItemSpy = spyOn(autocomplete, 'selectItem').and.callThrough();
inputEl.nativeElement.value = 'vsa';
inputEl.nativeElement.dispatchEvent(new Event('keydown'));
inputEl.nativeElement.dispatchEvent(new Event('input'));
Expand All @@ -376,7 +375,7 @@ describe('AutoComplete', () => {
inputEl.nativeElement.click();
fixture.detectChanges();

const selectItemSpy = spyOn(autocomplete, 'selectItem').and.callThrough();
const onOptionSelectSpy = spyOn(autocomplete, 'onOptionSelect').and.callThrough();
inputEl.nativeElement.value = 'v';
inputEl.nativeElement.dispatchEvent(new Event('keydown'));
inputEl.nativeElement.dispatchEvent(new Event('input'));
Expand All @@ -389,7 +388,7 @@ describe('AutoComplete', () => {
fixture.detectChanges();

expect(autocomplete.value).toEqual('Volvo');
expect(selectItemSpy).toHaveBeenCalled();
expect(onOptionSelectSpy).toHaveBeenCalled();
expect(inputEl.nativeElement.value).toEqual(autocomplete.value);
expect(testComponent.brand).toEqual(autocomplete.value);
flush();
Expand Down Expand Up @@ -436,7 +435,7 @@ describe('AutoComplete', () => {
inputEl.nativeElement.click();
fixture.detectChanges();

const selectItemSpy = spyOn(autocomplete2, 'selectItem').and.callThrough();
const onOptionSelectSpy = spyOn(autocomplete2, 'onOptionSelect').and.callThrough();
inputEl.nativeElement.value = 'v';
inputEl.nativeElement.dispatchEvent(new Event('keydown'));
inputEl.nativeElement.dispatchEvent(new Event('input'));
Expand All @@ -448,7 +447,7 @@ describe('AutoComplete', () => {
firstItemEl.click();
fixture.detectChanges();
expect(autocomplete2.value.brand).toEqual('Volvo');
expect(selectItemSpy).toHaveBeenCalled();
expect(onOptionSelectSpy).toHaveBeenCalled();
expect(inputEl.nativeElement.value).toEqual(autocomplete2.value.brand);
expect(testComponent.car).toEqual(autocomplete2.value);
flush();
Expand Down Expand Up @@ -505,7 +504,7 @@ describe('AutoComplete', () => {
inputEl.nativeElement.click();
fixture.detectChanges();

const selectItemSpy = spyOn(autocomplete, 'selectItem').and.callThrough();
const onOptionSelectSpy = spyOn(autocomplete, 'onOptionSelect').and.callThrough();
inputEl.nativeElement.value = 'v';
inputEl.nativeElement.dispatchEvent(new Event('keydown'));
inputEl.nativeElement.dispatchEvent(new Event('input'));
Expand All @@ -519,7 +518,7 @@ describe('AutoComplete', () => {
fixture.detectChanges();
expect(autocomplete.value[0]).toEqual('Volvo');
expect(autocomplete.value.length).toEqual(1);
expect(selectItemSpy).toHaveBeenCalled();
expect(onOptionSelectSpy).toHaveBeenCalled();
expect(testComponent.brand).toEqual(autocomplete.value);
flush();
}));
Expand All @@ -533,7 +532,7 @@ describe('AutoComplete', () => {
inputEl.nativeElement.click();
fixture.detectChanges();

const selectItemSpy = spyOn(autocomplete, 'selectItem').and.callThrough();
const onOptionSelectSpy = spyOn(autocomplete, 'onOptionSelect').and.callThrough();
inputEl.nativeElement.value = 'v';
inputEl.nativeElement.dispatchEvent(new Event('keydown'));
inputEl.nativeElement.dispatchEvent(new Event('input'));
Expand All @@ -547,7 +546,7 @@ describe('AutoComplete', () => {

expect(autocomplete.value[0]).toEqual('Volvo');
expect(autocomplete.value.length).toEqual(1);
expect(selectItemSpy).toHaveBeenCalled();
expect(onOptionSelectSpy).toHaveBeenCalled();
expect(testComponent.brand).toEqual(autocomplete.value);
flush();
}));
Expand All @@ -561,7 +560,7 @@ describe('AutoComplete', () => {
inputEl.nativeElement.click();
fixture.detectChanges();

const selectItemSpy = spyOn(autocomplete, 'selectItem').and.callThrough();
const onOptionSelectSpy = spyOn(autocomplete, 'onOptionSelect').and.callThrough();
inputEl.nativeElement.value = 'v';
inputEl.nativeElement.dispatchEvent(new Event('keydown'));
inputEl.nativeElement.dispatchEvent(new Event('input'));
Expand All @@ -574,12 +573,12 @@ describe('AutoComplete', () => {
fixture.detectChanges();
expect(autocomplete.value[0]).toEqual('Volvo');
expect(autocomplete.value.length).toEqual(1);
expect(selectItemSpy).toHaveBeenCalled();
expect(onOptionSelectSpy).toHaveBeenCalled();
expect(testComponent.brand).toEqual(autocomplete.value);
let backspaceEvent = new Event('keydown');
Object.defineProperty(backspaceEvent, 'which', { value: 8 });
Object.defineProperty(backspaceEvent, 'preventDefault', { value: () => {} });
autocomplete.onKeydown(backspaceEvent);
autocomplete.onKeyDown(backspaceEvent);
fixture.detectChanges();

expect(autocomplete.value[0]).toEqual(undefined);
Expand All @@ -596,7 +595,7 @@ describe('AutoComplete', () => {
inputEl.nativeElement.click();
fixture.detectChanges();

const selectItemSpy = spyOn(autocomplete, 'selectItem').and.callThrough();
const onOptionSelectSpy = spyOn(autocomplete, 'onOptionSelect').and.callThrough();
inputEl.nativeElement.value = 'v';
inputEl.nativeElement.dispatchEvent(new Event('keydown'));
inputEl.nativeElement.dispatchEvent(new Event('input'));
Expand All @@ -609,7 +608,7 @@ describe('AutoComplete', () => {
fixture.detectChanges();
expect(autocomplete.value[0]).toEqual('Volvo');
expect(autocomplete.value.length).toEqual(1);
expect(selectItemSpy).toHaveBeenCalled();
expect(onOptionSelectSpy).toHaveBeenCalled();
expect(testComponent.brand).toEqual(autocomplete.value);
const iconEl = fixture.debugElement.query(By.css('.p-autocomplete-token-icon'));
iconEl.nativeElement.click();
Expand All @@ -628,22 +627,22 @@ describe('AutoComplete', () => {
inputEl.nativeElement.click();
fixture.detectChanges();

const selectItemSpy = spyOn(autocomplete, 'selectItem').and.callThrough();
const onOptionSelectSpy = spyOn(autocomplete, 'onOptionSelect').and.callThrough();
autocomplete.suggestions = ['Volvo', 'VW'];
autocomplete.overlayVisible = true;
let navigateEvent = new Event('keydown');
Object.defineProperty(navigateEvent, 'which', { value: 40 });
Object.defineProperty(navigateEvent, 'preventDefault', { value: () => {} });
autocomplete.onKeydown(navigateEvent);
autocomplete.onKeyDown(navigateEvent);

let event = new Event('keydown');
Object.defineProperty(event, 'which', { value: 13 });
Object.defineProperty(event, 'preventDefault', { value: () => {} });
autocomplete.onKeydown(event);
autocomplete.onKeyDown(event);
fixture.detectChanges();

expect(autocomplete.value).toEqual('Volvo');
expect(selectItemSpy).toHaveBeenCalled();
expect(onOptionSelectSpy).toHaveBeenCalled();
expect(testComponent.brand).toEqual(autocomplete.value);
});

Expand All @@ -655,22 +654,22 @@ describe('AutoComplete', () => {
inputEl.nativeElement.click();
fixture.detectChanges();

const selectItemSpy = spyOn(autocomplete, 'selectItem').and.callThrough();
const onOptionSelectSpy = spyOn(autocomplete, 'onOptionSelect').and.callThrough();
autocomplete.suggestions = ['Volvo', 'VW'];
autocomplete.overlayVisible = true;

let navigateEvent = new Event('keydown');
Object.defineProperty(navigateEvent, 'which', { value: 40 });
Object.defineProperty(navigateEvent, 'preventDefault', { value: () => {} });
autocomplete.onKeydown(navigateEvent);
autocomplete.onKeyDown(navigateEvent);

let event = new Event('keydown');
Object.defineProperty(event, 'which', { value: 9 });
Object.defineProperty(event, 'preventDefault', { value: () => {} });
autocomplete.onKeydown(event);
autocomplete.onKeyDown(event);
fixture.detectChanges();
expect(autocomplete.value).toEqual('Volvo');
expect(selectItemSpy).toHaveBeenCalled();
expect(onOptionSelectSpy).toHaveBeenCalled();
expect(testComponent.brand).toEqual(autocomplete.value);
});

Expand All @@ -682,19 +681,19 @@ describe('AutoComplete', () => {
inputEl.nativeElement.click();
fixture.detectChanges();

const selectItemSpy = spyOn(autocomplete, 'selectItem').and.callThrough();
const onOptionSelectSpy = spyOn(autocomplete, 'onOptionSelect').and.callThrough();
const hideSpy = spyOn(autocomplete, 'hide').and.callThrough();
autocomplete.suggestions = ['Volvo', 'VW'];
autocomplete.overlayVisible = true;
let event = new Event('keydown');
Object.defineProperty(event, 'which', { value: 27 });
Object.defineProperty(event, 'preventDefault', { value: () => {} });

autocomplete.onKeydown(event);
autocomplete.onKeyDown(event);
fixture.detectChanges();

expect(autocomplete.value).toEqual(null);
expect(selectItemSpy).not.toHaveBeenCalled();
expect(onOptionSelectSpy).not.toHaveBeenCalled();
expect(hideSpy).toHaveBeenCalled();
expect(autocomplete.overlayVisible).toEqual(false);
});
Expand Down
11 changes: 0 additions & 11 deletions src/app/components/checkbox/checkbox.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,4 @@ describe('Checkbox', () => {
expect(iconEl.className).toContain('pi pi-new-check');
});

it('should call handleChange', () => {
fixture.detectChanges();

checkbox.binary = true;
const handleChangeSpy = spyOn(checkbox, 'handleChange').and.callThrough();
const input = fixture.nativeElement.querySelector('input');
input.dispatchEvent(new Event('change'));
fixture.detectChanges();

expect(handleChangeSpy).toHaveBeenCalled();
});
});
30 changes: 15 additions & 15 deletions src/app/components/chips/chips.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ describe('Chips', () => {
fixture.detectChanges();

expect(onInputFocusSpy).toHaveBeenCalled();
expect(chips.focus).toEqual(true);
expect(chips.focused).toEqual(true);
expect(focusData).toBeTruthy();
inputEl.nativeElement.dispatchEvent(new Event('blur'));
fixture.detectChanges();

expect(onInputBlurSpy).toHaveBeenCalled();
expect(chips.focus).toEqual(false);
expect(chips.focused).toEqual(false);
expect(blurData).toBeTruthy();
});

Expand All @@ -96,14 +96,14 @@ describe('Chips', () => {
let data;
chips.onAdd.subscribe((value) => (data = value));
const addItemSpy = spyOn(chips, 'addItem').and.callThrough();
const onKeydownSpy = spyOn(chips, 'onKeydown').and.callThrough();
const onKeydownSpy = spyOn(chips, 'onKeyDown').and.callThrough();
const updateMaxedOutSpy = spyOn(chips, 'updateMaxedOut').and.callThrough();
const inputEl = fixture.debugElement.query(By.css('input'));
inputEl.nativeElement.value = 'primeng';
fixture.detectChanges();

let event = { which: 13, preventDefault() {} };
chips.onKeydown(event as KeyboardEvent);
chips.onKeyDown(event as KeyboardEvent);
fixture.detectChanges();

chips.cd.detectChanges();
Expand All @@ -124,13 +124,13 @@ describe('Chips', () => {
fixture.detectChanges();

let event = { which: 13, preventDefault() {} };
chips.onKeydown(event as KeyboardEvent);
chips.onKeyDown(event as KeyboardEvent);
fixture.detectChanges();

inputEl.nativeElement.value = 'primeng';
fixture.detectChanges();

chips.onKeydown(event as KeyboardEvent);
chips.onKeyDown(event as KeyboardEvent);
fixture.detectChanges();

expect(chips.value.length).toEqual(2);
Expand All @@ -147,13 +147,13 @@ describe('Chips', () => {
fixture.detectChanges();

let event = { which: 13, preventDefault() {} };
chips.onKeydown(event as KeyboardEvent);
chips.onKeyDown(event as KeyboardEvent);
fixture.detectChanges();

inputEl.nativeElement.value = 'primeng';
fixture.detectChanges();

chips.onKeydown(event as KeyboardEvent);
chips.onKeyDown(event as KeyboardEvent);
fixture.detectChanges();

expect(chips.value.length).toEqual(1);
Expand All @@ -165,14 +165,14 @@ describe('Chips', () => {
fixture.detectChanges();

const addItemSpy = spyOn(chips, 'addItem').and.callThrough();
const onKeydownSpy = spyOn(chips, 'onKeydown').and.callThrough();
const onKeydownSpy = spyOn(chips, 'onKeyDown').and.callThrough();
const updateMaxedOutSpy = spyOn(chips, 'updateMaxedOut').and.callThrough();
const inputEl = fixture.debugElement.query(By.css('input'));
inputEl.nativeElement.value = 'primeng';
fixture.detectChanges();

let event = { which: 9, preventDefault() {} };
chips.onKeydown(event as KeyboardEvent);
chips.onKeyDown(event as KeyboardEvent);
fixture.detectChanges();

expect(addItemSpy).toHaveBeenCalled();
Expand Down Expand Up @@ -210,25 +210,25 @@ describe('Chips', () => {
fixture.detectChanges();

let event = { which: 13, preventDefault() {} };
chips.onKeydown(event as KeyboardEvent);
chips.onKeyDown(event as KeyboardEvent);
fixture.detectChanges();

inputEl.nativeElement.value = 'primeng';
fixture.detectChanges();

chips.onKeydown(event as KeyboardEvent);
chips.onKeyDown(event as KeyboardEvent);
fixture.detectChanges();

event.which = 81;
chips.onKeydown(event as KeyboardEvent);
chips.onKeyDown(event as KeyboardEvent);
fixture.detectChanges();

expect(chips.value.length).toEqual(2);
expect(chips.value[0]).toEqual('primeng');
expect(chips.value[1]).toEqual('primeng');
expect(inputEl.nativeElement.disabled).toEqual(true);
event.which = 8;
chips.onKeydown(event as KeyboardEvent);
chips.onKeyDown(event as KeyboardEvent);
chips.updateMaxedOut();
fixture.detectChanges();

Expand All @@ -246,7 +246,7 @@ describe('Chips', () => {
fixture.detectChanges();

let event = { which: 8, preventDefault() {} };
chips.onKeydown(event as KeyboardEvent);
chips.onKeyDown(event as KeyboardEvent);
fixture.detectChanges();

expect(data).toBeTruthy();
Expand Down
Loading

0 comments on commit ad7e213

Please sign in to comment.