Skip to content

Commit

Permalink
Allow id to be null
Browse files Browse the repository at this point in the history
  • Loading branch information
envex committed Feb 28, 2024
1 parent 0291809 commit bcf9e6a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/polaris-viz/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ and adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

- Fixed the positioning of the `<ZeroLine />` for the Stacked Horizontal Chart when all values are negative.

### Changed

- Allow `useWatchActiveSeries` to accept `null` as an `id` to disable attaching events.

## [10.5.2] - 2024-02-22

### Added
Expand Down
44 changes: 44 additions & 0 deletions packages/polaris-viz/src/hooks/tests/useWatchActiveSeries.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import {mount} from '@shopify/react-testing';

import {useWatchActiveSeries} from '../useWatchActiveSeries';

describe('useWatchActiveSeries', () => {
const addEventListenerSpy = jest.fn((_) => {});

beforeEach(() => {
jest
.spyOn(window, 'addEventListener')
.mockImplementation(addEventListenerSpy);
});

afterEach(() => {
addEventListenerSpy.mockReset();
});

it('attaches event listeners to window when id is passed', () => {
function MockComponent() {
useWatchActiveSeries('id', () => {});

return null;
}

mount(<MockComponent />);

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

it('does not attach event listeners to window when id is null', () => {
function MockComponent() {
useWatchActiveSeries(null, () => {});

return null;
}

mount(<MockComponent />);

expect(addEventListenerSpy).not.toHaveBeenCalled();
});
});
6 changes: 5 additions & 1 deletion packages/polaris-viz/src/hooks/useWatchActiveSeries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ import {getEventName} from './ColorVisionA11y';
import {useCallbackRef} from './useCallbackRef';

export function useWatchActiveSeries(
id: string,
id: string | null,
onIndexChange: (event: ColorVisionEventReturn) => void,
) {
const onIndexChangeCallback = useCallbackRef(onIndexChange);

useEffect(() => {
if (id == null) {
return;
}

const eventName = getEventName(id, COLOR_VISION_SINGLE_ITEM);

window.addEventListener(eventName, onIndexChangeCallback);
Expand Down

0 comments on commit bcf9e6a

Please sign in to comment.