-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
125 additions
and
7 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
94 changes: 94 additions & 0 deletions
94
packages/polaris-viz/src/hooks/tests/useWatchActiveSeries.test.tsx
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,94 @@ | ||
import {mount} from '@shopify/react-testing'; | ||
import {COLOR_VISION_SINGLE_ITEM} from '@shopify/polaris-viz-core'; | ||
|
||
import { | ||
useWatchActiveSeries, | ||
setActiveSeriesListener, | ||
} from '../useWatchActiveSeries'; | ||
import {getEventName} from '../ColorVisionA11y'; | ||
import {useCallbackRef} from '../useCallbackRef'; | ||
|
||
jest.mock('../useCallbackRef'); | ||
|
||
const mockUseCallbackRef = useCallbackRef as jest.MockedFunction< | ||
typeof useCallbackRef | ||
>; | ||
|
||
const onIndexChangeCallback = jest.fn(); | ||
|
||
describe('useWatchActiveSeries()', () => { | ||
beforeEach(() => { | ||
mockUseCallbackRef.mockReturnValue(onIndexChangeCallback); | ||
}); | ||
|
||
afterEach(() => { | ||
mockUseCallbackRef.mockReset(); | ||
}); | ||
|
||
describe('useWatchActiveSeries()', () => { | ||
it('attaches event listener to window when id is passed', () => { | ||
const addEventListenerSpy = jest.spyOn(window, 'addEventListener'); | ||
|
||
function MockComponent() { | ||
useWatchActiveSeries('id', onIndexChangeCallback); | ||
|
||
return null; | ||
} | ||
|
||
mount(<MockComponent />); | ||
|
||
expect(addEventListenerSpy).toHaveBeenCalledWith( | ||
'id:color-vision-event:singleItem', | ||
onIndexChangeCallback, | ||
); | ||
}); | ||
|
||
it('removes event listener from window when component is unmounted', () => { | ||
const onIndexChangeCallback = jest.fn(); | ||
const removeEventListenerSpy = jest.spyOn(window, 'removeEventListener'); | ||
|
||
mockUseCallbackRef.mockReturnValue(onIndexChangeCallback); | ||
|
||
function MockComponent() { | ||
useWatchActiveSeries('id', onIndexChangeCallback); | ||
|
||
return null; | ||
} | ||
|
||
const component = mount(<MockComponent />); | ||
|
||
component.unmount(); | ||
|
||
expect(removeEventListenerSpy).toHaveBeenCalledWith( | ||
'id:color-vision-event:singleItem', | ||
onIndexChangeCallback, | ||
); | ||
}); | ||
}); | ||
|
||
describe('setActiveSeriesListener()', () => { | ||
it('adds an event listener using an event name generated using the given ID', () => { | ||
const id = getEventName('someChartId', COLOR_VISION_SINGLE_ITEM); | ||
const addEventListenerSpy = jest.spyOn(window, 'addEventListener'); | ||
|
||
setActiveSeriesListener('someChartId', onIndexChangeCallback); | ||
|
||
expect(addEventListenerSpy).toHaveBeenCalledWith( | ||
id, | ||
onIndexChangeCallback, | ||
); | ||
}); | ||
|
||
it('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', onIndexChangeCallback); | ||
|
||
expect(addEventListenerSpy).toHaveBeenCalledWith( | ||
id, | ||
onIndexChangeCallback, | ||
); | ||
}); | ||
}); | ||
}); |
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