Skip to content
Snippets Groups Projects
Commit d030b458 authored by OZGCloud's avatar OZGCloud
Browse files

OZG-6129 tdd onKeydownHandler method

parent c52502c5
No related branches found
No related tags found
No related merge requests found
...@@ -311,6 +311,98 @@ describe('InstantSearchComponent', () => { ...@@ -311,6 +311,98 @@ describe('InstantSearchComponent', () => {
}); });
}); });
describe('onKeydownHandler', () => {
const keyboardEvent: KeyboardEvent = new KeyboardEvent('a');
beforeEach(() => {
component.isSearchResultsEmpty = jest.fn();
component.isArrowNavigationKey = jest.fn();
component.isEscapeKey = jest.fn();
component.handleArrowNavigation = jest.fn();
component.handleEscape = jest.fn();
});
it('should check for empty result', () => {
component.onKeydownHandler(keyboardEvent);
expect(component.isSearchResultsEmpty).toHaveBeenCalled();
});
describe('search result is empty', () => {
beforeEach(() => {
component.isSearchResultsEmpty = jest.fn().mockReturnValue(true);
});
it('should ignore key navigation', () => {
component.onKeydownHandler(keyboardEvent);
expect(component.isArrowNavigationKey).not.toHaveBeenCalled();
});
it('should ignore escape key handling', () => {
component.onKeydownHandler(keyboardEvent);
expect(component.isEscapeKey).not.toHaveBeenCalled();
});
});
describe('search result is not empty', () => {
beforeEach(() => {
component.isSearchResultsEmpty = jest.fn().mockReturnValue(false);
});
it('should check if arrow navigation', () => {
component.onKeydownHandler(keyboardEvent);
expect(component.isArrowNavigationKey).toHaveBeenCalled();
});
it('should handle arrow navigation', () => {
component.isArrowNavigationKey = jest.fn().mockReturnValue(true);
component.onKeydownHandler(keyboardEvent);
expect(component.handleArrowNavigation).toHaveBeenCalled();
});
it('should not handle arrow navigation', () => {
component.isArrowNavigationKey = jest.fn().mockReturnValue(false);
component.onKeydownHandler(keyboardEvent);
expect(component.handleArrowNavigation).not.toHaveBeenCalled();
});
describe('is not arrow navigation', () => {
beforeEach(() => {
component.isArrowNavigationKey = jest.fn().mockReturnValue(false);
});
it('should check for escape key', () => {
component.onKeydownHandler(keyboardEvent);
expect(component.isEscapeKey).toHaveBeenCalled();
});
it('should handle escape key', () => {
component.isEscapeKey = jest.fn().mockReturnValue(true);
component.onKeydownHandler(keyboardEvent);
expect(component.handleEscape).toHaveBeenCalled();
});
it('should not handle escape key', () => {
component.isEscapeKey = jest.fn().mockReturnValue(false);
component.onKeydownHandler(keyboardEvent);
expect(component.handleEscape).not.toHaveBeenCalled();
});
});
});
});
describe('isSearchResultsEmpty', () => { describe('isSearchResultsEmpty', () => {
it('should return true', () => { it('should return true', () => {
component.results = []; component.results = [];
......
...@@ -108,12 +108,8 @@ export class InstantSearchComponent implements OnInit, OnDestroy { ...@@ -108,12 +108,8 @@ export class InstantSearchComponent implements OnInit, OnDestroy {
@HostListener('document:keydown', ['$event']) @HostListener('document:keydown', ['$event'])
onKeydownHandler(e: KeyboardEvent): void { onKeydownHandler(e: KeyboardEvent): void {
if (this.isSearchResultsEmpty()) return; if (this.isSearchResultsEmpty()) return;
if (this.isArrowNavigationKey(e)) { if (this.isArrowNavigationKey(e)) this.handleArrowNavigation(e);
this.handleArrowNavigation(e); if (this.isEscapeKey(e)) this.handleEscape(e);
}
if (this.isEscapeKey(e)) {
this.handleEscape(e);
}
} }
@HostListener('document:click', ['$event']) @HostListener('document:click', ['$event'])
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment