From 1ba90279705b2ec797d979a6de61318ec2f3fb70 Mon Sep 17 00:00:00 2001 From: OZGCloud <ozgcloud@mgm-tp.com> Date: Fri, 9 Aug 2024 09:34:31 +0200 Subject: [PATCH] OZG-6129 improve test --- .../instant-search.component.spec.ts | 25 +++++++------------ 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/alfa-client/libs/design-system/src/lib/instant-search/instant-search/instant-search.component.spec.ts b/alfa-client/libs/design-system/src/lib/instant-search/instant-search/instant-search.component.spec.ts index 3155008ef5..f6f686486f 100644 --- a/alfa-client/libs/design-system/src/lib/instant-search/instant-search/instant-search.component.spec.ts +++ b/alfa-client/libs/design-system/src/lib/instant-search/instant-search/instant-search.component.spec.ts @@ -146,11 +146,12 @@ describe('InstantSearchComponent', () => { }); describe('handleArrowNavigation', () => { + const event: KeyboardEvent = new KeyboardEvent('arrow'); + beforeEach(() => { - component.setFocusOnResultItem = jest.fn(); component.getResultIndexForKey = jest.fn(); + component.setFocusOnResultItem = jest.fn(); }); - const event: KeyboardEvent = new KeyboardEvent('arrow'); it('should call prevent default', () => { event.preventDefault = jest.fn(); @@ -160,13 +161,13 @@ describe('InstantSearchComponent', () => { expect(event.preventDefault).toHaveBeenCalled(); }); - it('should call getResultIndexForKey with key of event', () => { + it('should call getResultIndexForKey', () => { component.handleArrowNavigation(event); expect(component.getResultIndexForKey).toHaveBeenCalledWith(event.key); }); - it('should call setFocusOnResultItem new index', () => { + it('should call setFocusOnResultItem', () => { component.getResultIndexForKey = jest.fn().mockReturnValue(0); component.handleArrowNavigation(event); @@ -422,20 +423,12 @@ describe('InstantSearchComponent', () => { }); describe('isArrowNavigationKey', () => { - it('should return true if key is ArrowDown', () => { - const arrowDownEvent = { ...new KeyboardEvent('key'), key: 'ArrowDown' }; + it.each(['ArrowUp', 'ArrowDown'])('should return true for key %s', (key: string) => { + const keyboardEvent: KeyboardEvent = { ...new KeyboardEvent('key'), key }; - const result: boolean = component.isArrowNavigationKey(arrowDownEvent); + const result: boolean = component.isArrowNavigationKey(keyboardEvent); - expect(result).toBe(true); - }); - - it('should return true if key is ArrowUp', () => { - const arrowUpEvent = { ...new KeyboardEvent('navigation'), key: 'ArrowUp' }; - - const result: boolean = component.isArrowNavigationKey(arrowUpEvent); - - expect(result).toBe(true); + expect(result).toBeTruthy(); }); it('should return false', () => { -- GitLab