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

Allow id to be null #1633

Merged
merged 1 commit into from
Mar 6, 2024
Merged

Allow id to be null #1633

merged 1 commit into from
Mar 6, 2024

Conversation

envex
Copy link
Collaborator

@envex envex commented Feb 28, 2024

What does this implement/fix?

Allows a consumer to conditionally watch for events. If for some reason the id prop is not available, we won't attach the events to window.

Copy link

github-actions bot commented Feb 28, 2024

size-limit report 📦

Path Size Loading time (3g) Running time (snapdragon) Total time
polaris-viz-core-cjs 61.36 KB (0%) 1.3 s (0%) 985 ms (+5.26% 🔺) 2.3 s
polaris-viz-cjs 215.95 KB (+0.02% 🔺) 4.4 s (+0.02% 🔺) 1.6 s (-4.49% 🔽) 6 s
polaris-viz-esm 175.93 KB (+0.02% 🔺) 3.6 s (+0.02% 🔺) 1.4 s (+13.28% 🔺) 4.9 s
polaris-viz-css 4.78 KB (0%) 96 ms (0%) 275 ms (-10.95% 🔽) 371 ms
polaris-viz-esnext 181.41 KB (+0.02% 🔺) 3.7 s (+0.02% 🔺) 1.2 s (-3.91% 🔽) 4.8 s

Copy link
Member

@kvendrik kvendrik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If ID is null it won't do anything which makes it weird that you'd call it in the first place. How about we create a second export that makes all of this available as a non-hook? Based on the logic that seems to be possible and easy enough to do?

Alternatively you could export getEventName() and let consumers implement custom logic that does the same thing but outside of a hook

@envex envex force-pushed the envex/use-watch-null-id branch from bcf9e6a to 2fd3dd6 Compare February 28, 2024 18:52
@envex envex requested a review from kvendrik February 28, 2024 18:52
@@ -7,18 +7,34 @@ import {getEventName} from './ColorVisionA11y';
import {useCallbackRef} from './useCallbackRef';

export function useWatchActiveSeries(
id: string,
id: string | null,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The point of adding the separate setActiveSeriesListener() is so we don't need to make this optional right? So let's remove that logic?

@envex envex force-pushed the envex/use-watch-null-id branch from 2fd3dd6 to db39619 Compare February 29, 2024 14:57
@envex envex requested a review from kvendrik February 29, 2024 14:58
Comment on lines 10 to 94
describe('useWatchActiveSeries()', () => {
describe('useWatchActiveSeries()', () => {
it('attaches event listeners to window when id is passed', () => {
const addEventListenerSpy = jest.spyOn(window, 'addEventListener');

function MockComponent() {
useWatchActiveSeries('id', () => {});

return null;
}

mount(<MockComponent />);

expect(addEventListenerSpy).toHaveBeenCalledWith(
'id:color-vision-event:singleItem',
expect.any(Function),
);
});
});

describe('setActiveSeriesListener()', () => {
it('generates an event name using the given ID', () => {
const id = getEventName('someChartId', COLOR_VISION_SINGLE_ITEM);
const addEventListenerSpy = jest.spyOn(window, 'addEventListener');

setActiveSeriesListener('someChartId', () => {});

expect(addEventListenerSpy).toHaveBeenCalledWith(
id,
expect.any(Function),
);
});
});
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There seem to be tests missing here. E.g. tests that make sure the listener gets removed on unmount

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kvendrik Added a test for removeEventListener, let me know if you think it makes sense to add one to ensure setActiveSeriesListener returns a function as well.

@envex envex force-pushed the envex/use-watch-null-id branch from db39619 to bd09112 Compare March 6, 2024 15:34
@envex envex requested a review from kvendrik March 6, 2024 15:35
Copy link
Member

@kvendrik kvendrik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! 🎉

});

describe('setActiveSeriesListener()', () => {
it('generates an event name using the given ID', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it('generates an event name using the given ID', () => {
it('adds en event listener using an event name generated using the given ID', () => {

id,
expect.any(Function),
);
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing test: returns a method that removes the previously set event listener

const id = getEventName('someChartId', COLOR_VISION_SINGLE_ITEM);
const addEventListenerSpy = jest.spyOn(window, 'addEventListener');

setActiveSeriesListener('someChartId', () => {});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test should also make sure that the given callback is the one that's being used for the addEventListener call

@envex envex force-pushed the envex/use-watch-null-id branch from bd09112 to ade1ce1 Compare March 6, 2024 16:13
@envex envex force-pushed the envex/use-watch-null-id branch from ade1ce1 to 7f38db2 Compare March 6, 2024 16:24
@envex envex merged commit 3ac06d0 into main Mar 6, 2024
6 checks passed
@envex envex deleted the envex/use-watch-null-id branch September 12, 2024 18:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants