-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Fix display of multiple tooltips
- Loading branch information
Francesco Longo
committed
Sep 11, 2024
1 parent
63d9ef7
commit 96ccee6
Showing
4 changed files
with
113 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
import { fireEvent } from '@testing-library/react'; | ||
|
||
import { registerTooltip } from '../item/tooltips-registry'; | ||
|
||
describe('registerTooltip', () => { | ||
test('calls previous callbacks when registering new callback', () => { | ||
const callbackOne = jest.fn(); | ||
const callbackTwo = jest.fn(); | ||
const callbackThree = jest.fn(); | ||
registerTooltip(callbackOne); | ||
registerTooltip(callbackTwo); | ||
registerTooltip(callbackThree); | ||
expect(callbackThree).toBeCalledTimes(0); | ||
expect(callbackTwo).toBeCalledTimes(1); | ||
expect(callbackOne).toBeCalledTimes(2); | ||
}); | ||
test('does not call deregistered callbacks', () => { | ||
const callbackOne = jest.fn(); | ||
const callbackTwo = jest.fn(); | ||
const deregisterOne = registerTooltip(callbackOne); | ||
deregisterOne(); | ||
registerTooltip(callbackTwo); | ||
expect(callbackTwo).toBeCalledTimes(0); | ||
expect(callbackOne).toBeCalledTimes(0); | ||
}); | ||
test('calls callbacks upon clicking on ESC', () => { | ||
const callbackOne = jest.fn(); | ||
const deregisterOne = registerTooltip(callbackOne); | ||
fireEvent.keyDown(document, { key: 'Escape' }); | ||
expect(callbackOne).toBeCalledTimes(1); | ||
deregisterOne(); | ||
fireEvent.keyDown(document, { key: 'Escape' }); | ||
expect(callbackOne).toBeCalledTimes(1); | ||
}); | ||
}); |
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,33 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
type OnClose = () => void; | ||
|
||
let callbacks: Array<OnClose> = []; | ||
let listenerRegistered = false; | ||
|
||
const onKeyDown = (event: KeyboardEvent) => { | ||
if (event.key === 'Escape') { | ||
callbacks.forEach(callback => callback()); | ||
} | ||
}; | ||
|
||
export const registerTooltip = (onClose: OnClose) => { | ||
callbacks.forEach(callback => callback()); | ||
callbacks.push(onClose); | ||
if (!listenerRegistered) { | ||
listenerRegistered = true; | ||
document.addEventListener('keydown', onKeyDown); | ||
} | ||
return () => { | ||
deregisterTooltip(onClose); | ||
}; | ||
}; | ||
|
||
const deregisterTooltip = (onClose: OnClose) => { | ||
callbacks = callbacks.filter(callback => callback !== onClose); | ||
if (listenerRegistered && callbacks.length === 0) { | ||
listenerRegistered = false; | ||
document.removeEventListener('keydown', onKeyDown); | ||
} | ||
}; |