-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from borisbelmar/feature/update-handlers
Update Handlers
- Loading branch information
Showing
8 changed files
with
193 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import getViewNavigationStateMock from '@/__mocks__/viewNavigationState.mock' | ||
import { ArrowNavigationState } from '@/types' | ||
import createEventEmitter, { EventEmitter } from '@/utils/createEventEmitter' | ||
import updateElementHandler from './updateElementHandler' | ||
|
||
describe('updateElementHandler', () => { | ||
let state: ArrowNavigationState | ||
let emitter: EventEmitter | ||
|
||
beforeEach(() => { | ||
state = getViewNavigationStateMock() | ||
emitter = createEventEmitter() | ||
}) | ||
|
||
it('should update the element', () => { | ||
const updateElement = updateElementHandler({ | ||
state, | ||
emit: emitter.emit | ||
}) | ||
|
||
const element00 = state.elements.get('element-0-0') | ||
|
||
expect(element00).toBeDefined() | ||
expect(element00?.id).toBe('element-0-0') | ||
|
||
const handlerFocus = jest.fn() | ||
|
||
updateElement('element-0-0', { onFocus: handlerFocus }) | ||
|
||
const updatedElement00 = state.elements.get('element-0-0') | ||
|
||
expect(updatedElement00).toBeDefined() | ||
expect(updatedElement00?.id).toBe('element-0-0') | ||
expect(updatedElement00?.onFocus).toBe(handlerFocus) | ||
}) | ||
|
||
it('should not update the element if it does not exist', () => { | ||
const updateElement = updateElementHandler({ | ||
state, | ||
emit: emitter.emit | ||
}) | ||
|
||
const nonExistentElement = state.elements.get('non-existent-element') | ||
|
||
expect(nonExistentElement).toBeUndefined() | ||
|
||
const handlerFocus = jest.fn() | ||
|
||
updateElement('non-existent-element', { onFocus: handlerFocus }) | ||
|
||
const updatedNonExistentElement = state.elements.get('non-existent-element') | ||
|
||
expect(updatedNonExistentElement).toBeUndefined() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* eslint-disable no-param-reassign */ | ||
import EVENTS from '@/config/events' | ||
import type { ArrowNavigationState, FocusableElement, FocusableElementOptions } from '@/types' | ||
import type { EventEmitter } from '@/utils/createEventEmitter' | ||
|
||
interface UpdateElementHandlerProps { | ||
state: ArrowNavigationState | ||
emit: EventEmitter['emit'] | ||
} | ||
|
||
export default function updateElementHandler ({ | ||
state, | ||
emit | ||
}: UpdateElementHandlerProps) { | ||
return ( | ||
id: string, | ||
options: FocusableElementOptions | ||
) => { | ||
const existentElement = state.elements.get(id) | ||
|
||
if (!existentElement) { | ||
return | ||
} | ||
|
||
const updatedFocusableElement: FocusableElement = { | ||
...existentElement, | ||
...options | ||
} | ||
|
||
state.elements.set(id, updatedFocusableElement) | ||
emit(EVENTS.ELEMENTS_CHANGED, state.elements) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import getViewNavigationStateMock from '@/__mocks__/viewNavigationState.mock' | ||
import { ArrowNavigationState } from '@/types' | ||
import createEventEmitter, { EventEmitter } from '@/utils/createEventEmitter' | ||
import updateGroupHandler from './updateGroupHandler' | ||
|
||
describe('updateGroupHandler', () => { | ||
let state: ArrowNavigationState | ||
let emitter: EventEmitter | ||
|
||
beforeEach(() => { | ||
state = getViewNavigationStateMock() | ||
emitter = createEventEmitter() | ||
}) | ||
|
||
it('should update the group', () => { | ||
const updateGroup = updateGroupHandler({ | ||
state, | ||
emit: emitter.emit | ||
}) | ||
|
||
const group0 = state.groupsConfig.get('group-0') | ||
|
||
expect(group0).toBeDefined() | ||
expect(group0?.id).toBe('group-0') | ||
|
||
const handlerFocus = jest.fn() | ||
|
||
updateGroup('group-0', { onFocus: handlerFocus }) | ||
|
||
const updatedGroup0 = state.groupsConfig.get('group-0') | ||
|
||
expect(updatedGroup0).toBeDefined() | ||
expect(updatedGroup0?.id).toBe('group-0') | ||
expect(updatedGroup0?.onFocus).toBe(handlerFocus) | ||
}) | ||
|
||
it('should not update the group if it does not exist', () => { | ||
const updateGroup = updateGroupHandler({ | ||
state, | ||
emit: emitter.emit | ||
}) | ||
|
||
const nonExistentGroup = state.groups.get('non-existent-group') | ||
|
||
expect(nonExistentGroup).toBeUndefined() | ||
|
||
const handlerFocus = jest.fn() | ||
|
||
updateGroup('non-existent-group', { onFocus: handlerFocus }) | ||
|
||
const updatedNonExistentGroup = state.groups.get('non-existent-group') | ||
|
||
expect(updatedNonExistentGroup).toBeUndefined() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* eslint-disable no-param-reassign */ | ||
import EVENTS from '@/config/events' | ||
import type { ArrowNavigationState, FocusableGroupConfig } from '@/types' | ||
import type { EventEmitter } from '@/utils/createEventEmitter' | ||
|
||
interface UpdateElementHandlerProps { | ||
state: ArrowNavigationState | ||
emit: EventEmitter['emit'] | ||
} | ||
|
||
export default function updateElementHandler ({ | ||
state, | ||
emit | ||
}: UpdateElementHandlerProps) { | ||
return ( | ||
id: string, | ||
config: Omit<FocusableGroupConfig, 'id'> | ||
) => { | ||
const existentGroupConfig = state.groupsConfig.get(id) | ||
|
||
if (!existentGroupConfig) { | ||
return | ||
} | ||
|
||
const updatedFocusableGroup: FocusableGroupConfig = { | ||
...existentGroupConfig, | ||
...config | ||
} | ||
|
||
state.groupsConfig.set(id, updatedFocusableGroup) | ||
emit(EVENTS.GROUPS_CHANGED, state.elements) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters