From 79214d1c09c14290c5e992f8a20648d6b24e43cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vi=CC=81ctor=20CG?= Date: Wed, 29 May 2024 13:07:52 +0200 Subject: [PATCH] feat(keyboard-navigation): migrate keyboard-navigation component to composition API --- .../src/components/index.ts | 1 + .../test-base-keyboard-navigation.vue | 27 +++ packages/_vue3-migration-test/src/router.ts | 8 +- .../base-keyboard-navigation.spec.ts | 28 +-- .../components/base-keyboard-navigation.vue | 226 +++++++++--------- 5 files changed, 166 insertions(+), 124 deletions(-) create mode 100644 packages/_vue3-migration-test/src/components/test-base-keyboard-navigation.vue diff --git a/packages/_vue3-migration-test/src/components/index.ts b/packages/_vue3-migration-test/src/components/index.ts index 9bc8c5951e..0210867c1e 100644 --- a/packages/_vue3-migration-test/src/components/index.ts +++ b/packages/_vue3-migration-test/src/components/index.ts @@ -11,3 +11,4 @@ export { default as TestBaseSuggestions } from './suggestions/test-base-suggesti export { default as TestHighlight } from './test-highlight.vue'; export { default as TestBaseResultImages } from './result/test-base-result-images.vue'; export { default as TestBasePanel } from './panels/test-base-panel.vue'; +export { default as TestBaseKeyboardNavigation } from './test-base-keyboard-navigation.vue'; diff --git a/packages/_vue3-migration-test/src/components/test-base-keyboard-navigation.vue b/packages/_vue3-migration-test/src/components/test-base-keyboard-navigation.vue new file mode 100644 index 0000000000..0f3ed3d068 --- /dev/null +++ b/packages/_vue3-migration-test/src/components/test-base-keyboard-navigation.vue @@ -0,0 +1,27 @@ + + + + + diff --git a/packages/_vue3-migration-test/src/router.ts b/packages/_vue3-migration-test/src/router.ts index b379899eae..409c149dd9 100644 --- a/packages/_vue3-migration-test/src/router.ts +++ b/packages/_vue3-migration-test/src/router.ts @@ -25,7 +25,8 @@ import { TestBaseSuggestions, TestHighlight, TestBaseResultImages, - TestBasePanel + TestBasePanel, + TestBaseKeyboardNavigation } from './'; const routes = [ @@ -158,6 +159,11 @@ const routes = [ path: '/test-base-panel', name: 'TestBasePanel', component: TestBasePanel + }, + { + path: '/base-keyboard-navigation', + name: 'TestBaseKeyboardNavigation', + component: TestBaseKeyboardNavigation } ]; diff --git a/packages/x-components/src/components/__tests__/base-keyboard-navigation.spec.ts b/packages/x-components/src/components/__tests__/base-keyboard-navigation.spec.ts index 079b156860..8f10cde4b1 100644 --- a/packages/x-components/src/components/__tests__/base-keyboard-navigation.spec.ts +++ b/packages/x-components/src/components/__tests__/base-keyboard-navigation.spec.ts @@ -3,6 +3,7 @@ import Vue from 'vue'; import { SearchInput } from '../../x-modules/search-box/components/index'; import { installNewXPlugin } from '../../__tests__/utils'; import BaseKeyboardNavigation from '../base-keyboard-navigation.vue'; +import { DirectionalFocusNavigationService } from '../../services/directional-focus-navigation.service'; describe('testing keyboard navigation component', () => { let localVue: typeof Vue; @@ -12,9 +13,12 @@ describe('testing keyboard navigation component', () => { }); it('takes control of the navigation when a defined condition is triggered', () => { - const mockedFocusNextNavigableElement = jest.fn(); + const navigateToSpy = jest.spyOn( + DirectionalFocusNavigationService.prototype as any, + 'navigateTo' + ); const searchInput = mount(SearchInput, { localVue }); - const keyboardNavigation = mount(BaseKeyboardNavigation, { + mount(BaseKeyboardNavigation, { localVue, propsData: { navigationHijacker: [ @@ -26,26 +30,21 @@ describe('testing keyboard navigation component', () => { ] } }); - Object.defineProperty(keyboardNavigation.vm, 'focusNextNavigableElement', { - value: mockedFocusNextNavigableElement - }); searchInput.trigger('keydown', { key: 'ArrowUp' }); - expect(mockedFocusNextNavigableElement).not.toHaveBeenCalled(); + expect(navigateToSpy).not.toHaveBeenCalled(); searchInput.trigger('keydown', { key: 'ArrowDown' }); - expect(mockedFocusNextNavigableElement).toHaveBeenCalled(); + expect(navigateToSpy).toHaveBeenCalled(); }); it('emits the defined event when reaching the limit in the direction of the navigation', () => { const listener = jest.fn(); - const htmlElement = document.createElement('div'); + // As cannot mock elementToFocus (it will be undefined), making the navigateTo method return undefined + jest + .spyOn(DirectionalFocusNavigationService.prototype as any, 'navigateTo') + .mockReturnValue(undefined); const keyboardNavigation = mount(BaseKeyboardNavigation, { localVue, - data() { - return { - elementToFocus: htmlElement - }; - }, propsData: { takeNavigationControl: [], eventsForDirectionLimit: { @@ -53,9 +52,6 @@ describe('testing keyboard navigation component', () => { } } }); - Object.defineProperty((keyboardNavigation.vm as any).navigationService, 'navigateTo', { - value: (): HTMLElement => htmlElement - }); keyboardNavigation.vm.$x.on('UserReachedEmpathizeTop').subscribe(listener); keyboardNavigation.trigger('keydown', { key: 'ArrowUp' }); diff --git a/packages/x-components/src/components/base-keyboard-navigation.vue b/packages/x-components/src/components/base-keyboard-navigation.vue index 5ecef210d1..e4bf015386 100644 --- a/packages/x-components/src/components/base-keyboard-navigation.vue +++ b/packages/x-components/src/components/base-keyboard-navigation.vue @@ -1,5 +1,6 @@