Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: DH-14630 Added support for null in useTableListener #1227

Merged
merged 1 commit into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions packages/jsapi-components/src/useTableListener.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { renderHook } from '@testing-library/react-hooks';
import { Evented } from '@deephaven/jsapi-types';
import useTableListener from './useTableListener';

const eventName = 'mock.event';

const addEventListener = jest.fn();
const handler = jest.fn();
const unsubscribe = jest.fn();

const evented = ({
addEventListener,
} as unknown) as Evented;

beforeEach(() => {
jest.clearAllMocks();
addEventListener.mockReturnValue(unsubscribe);
});

it('should register an event listener', () => {
renderHook(() => useTableListener(evented, eventName, handler));
expect(evented.addEventListener).toHaveBeenCalledWith(eventName, handler);
});

it('should unsubscribe on unmount', () => {
const { unmount } = renderHook(() =>
useTableListener(evented, eventName, handler)
);

expect(unsubscribe).not.toHaveBeenCalled();

unmount();

expect(unsubscribe).toHaveBeenCalled();
});

it.each([null, undefined])(
'should handle null or undefined: %s',
nullOrUndefined => {
const { unmount } = renderHook(() =>
useTableListener(nullOrUndefined, eventName, handler)
);

unmount();

expect(unsubscribe).not.toHaveBeenCalled();
}
);
4 changes: 2 additions & 2 deletions packages/jsapi-components/src/useTableListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import Log from '@deephaven/log';
const log = Log.module('useTableListener');

export const useTableListener = (
eventEmitter: Evented | undefined,
eventEmitter: Evented | undefined | null,
eventName: string,
callback: EventListener
): void =>
useEffect(
function initEventEmitter() {
if (eventEmitter === undefined) {
if (eventEmitter == null) {
log.debug2('Emitter undefined, skipping addEventListener', eventName);
return;
}
Expand Down