-
Notifications
You must be signed in to change notification settings - Fork 1.2k
[Frame] Refactor to use createContext API #803
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
import * as React from 'react'; | ||
import * as PropTypes from 'prop-types'; | ||
import {mountWithAppProvider} from 'test-utilities'; | ||
import {noop} from '../../../utilities/other'; | ||
import ContextualSaveBar from '../ContextualSaveBar'; | ||
import {Provider, createFrameContext} from '../../Frame'; | ||
|
||
describe('<ContextualSaveBar />', () => { | ||
const props = { | ||
|
@@ -12,58 +12,87 @@ describe('<ContextualSaveBar />', () => { | |
}; | ||
|
||
it('calls the contextual save bar on mount with correct values', () => { | ||
const {frame} = mountWithContext(<ContextualSaveBar {...props} />); | ||
expect(frame.setContextualSaveBar).toHaveBeenCalledWith({ | ||
const mockFrameContext = createFrameContext({ | ||
setContextualSaveBar: jest.fn(), | ||
removeContextualSaveBar: jest.fn(), | ||
}); | ||
|
||
mountWithAppProvider( | ||
<Provider value={mockFrameContext}> | ||
<ContextualSaveBar {...props} /> | ||
</Provider>, | ||
); | ||
expect(mockFrameContext.frame.setContextualSaveBar).toHaveBeenCalledWith({ | ||
...props, | ||
}); | ||
}); | ||
|
||
it('removes the contextual save bar on unmount', () => { | ||
const {contextualSaveBar, frame} = mountWithContext( | ||
<ContextualSaveBar {...props} />, | ||
const mockFrameContext = createFrameContext({ | ||
setContextualSaveBar: jest.fn(), | ||
removeContextualSaveBar: jest.fn(), | ||
}); | ||
|
||
const frame = mountWithAppProvider( | ||
<Provider value={mockFrameContext}> | ||
<ContextualSaveBar {...props} /> | ||
</Provider>, | ||
); | ||
expect(frame.removeContextualSaveBar).not.toHaveBeenCalled(); | ||
contextualSaveBar.unmount(); | ||
expect(frame.removeContextualSaveBar).toHaveBeenCalled(); | ||
expect( | ||
mockFrameContext.frame.removeContextualSaveBar, | ||
).not.toHaveBeenCalled(); | ||
frame.unmount(); | ||
expect(mockFrameContext.frame.removeContextualSaveBar).toHaveBeenCalled(); | ||
}); | ||
|
||
it('calls the contextual save bar with correct values if its props change after it mounted', () => { | ||
const {frame, contextualSaveBar} = mountWithContext( | ||
<ContextualSaveBar {...props} />, | ||
const mockFrameContext = createFrameContext({ | ||
setContextualSaveBar: jest.fn(), | ||
removeContextualSaveBar: jest.fn(), | ||
}); | ||
|
||
const frame = mountWithAppProvider( | ||
<Provider value={mockFrameContext}> | ||
<ContextualSaveBar {...props} /> | ||
danrosenthal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
</Provider>, | ||
); | ||
expect(frame.setContextualSaveBar).toHaveBeenCalledTimes(1); | ||
const newProps = { | ||
saveAction: {content: 'Save', onAction: noop, loading: true}, | ||
discardAction: {content: 'Discard', onAction: noop}, | ||
message: 'Unsaved changes', | ||
}; | ||
contextualSaveBar.setProps({...newProps}); | ||
expect(frame.setContextualSaveBar).toHaveBeenCalledWith({ | ||
frame.setProps({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just curious, why do we need to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is an alternative to mounting and unmounting the child component, since we no longer have direct access to the child component. Calling |
||
children: <ContextualSaveBar {...newProps} />, | ||
}); | ||
expect(mockFrameContext.frame.setContextualSaveBar).toHaveBeenCalledWith({ | ||
...newProps, | ||
}); | ||
expect(frame.setContextualSaveBar).toHaveBeenCalledTimes(2); | ||
expect(mockFrameContext.frame.setContextualSaveBar).toHaveBeenCalledTimes( | ||
2, | ||
); | ||
}); | ||
|
||
it('doesnt call the contextual save bar if its props remain unchanged after it mounted', () => { | ||
const {frame, contextualSaveBar} = mountWithContext( | ||
<ContextualSaveBar {...props} />, | ||
const mockFrameContext = createFrameContext({ | ||
setContextualSaveBar: jest.fn(), | ||
removeContextualSaveBar: jest.fn(), | ||
}); | ||
|
||
const frame = mountWithAppProvider( | ||
<Provider value={mockFrameContext}> | ||
<ContextualSaveBar {...props} /> | ||
danrosenthal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
</Provider>, | ||
); | ||
|
||
expect(mockFrameContext.frame.setContextualSaveBar).toHaveBeenCalledTimes( | ||
1, | ||
); | ||
expect(frame.setContextualSaveBar).toHaveBeenCalledTimes(1); | ||
const newProps = {...props}; | ||
contextualSaveBar.setProps({...newProps}); | ||
expect(frame.setContextualSaveBar).toHaveBeenCalledTimes(1); | ||
frame.setProps({ | ||
children: <ContextualSaveBar {...newProps} />, | ||
}); | ||
expect(mockFrameContext.frame.setContextualSaveBar).toHaveBeenCalledTimes( | ||
1, | ||
); | ||
}); | ||
}); | ||
|
||
function mountWithContext(element: React.ReactElement<any>) { | ||
const frame = { | ||
setContextualSaveBar: jest.fn(), | ||
removeContextualSaveBar: jest.fn(), | ||
}; | ||
const contextualSaveBar = mountWithAppProvider(element, { | ||
context: {frame}, | ||
childContextTypes: {frame: PropTypes.any}, | ||
}); | ||
|
||
return {contextualSaveBar, frame}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import * as React from 'react'; | ||
import {noop} from '@shopify/javascript-utilities/other'; | ||
import {FrameContext} from '../../types'; | ||
|
||
const defaultContext: FrameContext = { | ||
frame: { | ||
showToast: noop, | ||
hideToast: noop, | ||
setContextualSaveBar: noop, | ||
removeContextualSaveBar: noop, | ||
startLoading: noop, | ||
stopLoading: noop, | ||
}, | ||
}; | ||
|
||
const {Provider, Consumer} = React.createContext<FrameContext>(defaultContext); | ||
|
||
export {Provider, Consumer}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export {Provider, Consumer} from './Context'; |
Uh oh!
There was an error while loading. Please reload this page.