-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
1 parent
d93559c
commit 9ad816a
Showing
7 changed files
with
139 additions
and
18 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
61 changes: 61 additions & 0 deletions
61
src/components/MediaQueryProvider/tests/MediaQueryProvider.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,61 @@ | ||
import React from 'react'; | ||
import {matchMedia} from '@shopify/jest-dom-mocks'; | ||
import {act} from 'react-dom/test-utils'; | ||
import {mountWithApp} from 'test-utilities'; | ||
import {EventListener} from 'components'; | ||
import {MediaQueryProvider} from '../MediaQueryProvider'; | ||
import {useMediaQuery} from '../../../utilities/media-query'; | ||
|
||
describe('MediaQueryProvider', () => { | ||
beforeEach(() => { | ||
matchMedia.mock(); | ||
}); | ||
|
||
afterEach(() => { | ||
matchMedia.restore(); | ||
}); | ||
|
||
it('renders EventListener with the resize event', () => { | ||
const mediaQueryProvider = mountWithApp(<MediaQueryProvider />); | ||
expect(mediaQueryProvider).toContainReactComponentTimes(EventListener, 1, { | ||
event: 'resize', | ||
}); | ||
}); | ||
|
||
it('passes isNavigationCollapsed to MediaQueryContext.Provider', () => { | ||
function Component() { | ||
const mediaQuery = useMediaQuery(); | ||
// eslint-disable-next-line shopify/jest/no-if | ||
return mediaQuery !== undefined ? <div /> : null; | ||
} | ||
|
||
const mediaQueryProvider = mountWithApp( | ||
<MediaQueryProvider> | ||
<Component /> | ||
</MediaQueryProvider>, | ||
); | ||
expect(mediaQueryProvider).toContainReactComponentTimes('div', 1); | ||
}); | ||
|
||
it('sets isNavigationCollapsed when resize occurs', () => { | ||
function Component() { | ||
const {isNavigationCollapsed} = useMediaQuery(); | ||
// eslint-disable-next-line shopify/jest/no-if | ||
return isNavigationCollapsed ? <div>content</div> : null; | ||
} | ||
const mediaQueryProvider = mountWithApp( | ||
<MediaQueryProvider> | ||
<Component /> | ||
</MediaQueryProvider>, | ||
); | ||
|
||
matchMedia.setMedia(() => ({matches: true})); | ||
|
||
act(() => { | ||
window.dispatchEvent(new Event('resize')); | ||
}); | ||
|
||
mediaQueryProvider.forceUpdate(); | ||
expect(mediaQueryProvider).toContainReactComponentTimes('div', 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
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,32 @@ | ||
import React, {useContext} from 'react'; | ||
import {mount, mountWithApp} from 'test-utilities'; | ||
import {useMediaQuery} from '../hooks'; | ||
import {MediaQueryContext} from '../context'; | ||
|
||
let consoleErrorSpy: jest.SpyInstance; | ||
|
||
function Component() { | ||
return useMediaQuery() === useContext(MediaQueryContext) ? <div /> : null; | ||
} | ||
|
||
describe('useMediaQuery', () => { | ||
beforeEach(() => { | ||
consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); | ||
}); | ||
|
||
afterEach(() => { | ||
consoleErrorSpy.mockRestore(); | ||
}); | ||
|
||
it('returns context', () => { | ||
const component = mountWithApp(<Component />); | ||
expect(component).toContainReactComponent('div'); | ||
}); | ||
|
||
it('throws an error if context is not set', () => { | ||
const attemptMount = () => mount(<Component />); | ||
expect(attemptMount).toThrow( | ||
'No mediaQuery was provided. Your application must be wrapped in an <AppProvider> component. See https://polaris.shopify.com/components/structure/app-provider for implementation instructions.', | ||
); | ||
}); | ||
}); |