-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Dive shallow in a Context Consumer #1908
Comments
Indeed; this is because you're shallow rendering your arrow function, which means your render prop is never invoked. You can try: |
Hi @ljharb, thanks for your answer. It's not better with
But you made me realize that my use case is not accurate : in fact, I met this error while diving into a HOC that adds |
Ah - that’s the problem then. enzyme does not yet support new-style Context. |
Oh OK ! So I guess I have to wait for the checkbox "New context APIs in 16.3" to be checked in #1553 ? If so, I'll just close my ticket and follow the latter for updates. |
Hello, any workaround ? |
The workaround is, continue to use the regular context APIs and avoid createContext for now. |
@ljharb do you have an example on how to do that? |
@thousight there's tons of examples all over the internet on how to use the only form of context that existed prior to React v16.3 :-) |
Enzyme is the final hurdle we have to complete the react-redux v6 migration (which uses the React v16.3 new context API). |
@thousight @WNemencha I've done some workaround for it, below is my example. If someone knows better solution I'm eager to see it 😈 In my example I can test whether the DataListPagination.test.tsx const mockChangePageByOffset = jest.fn().mockReturnValue('mock changePageByOffset');
jest.mock('../../providers/DataPaginationProvider', () => ({
DataPaginationContext: {
// tslint:disable-next-line:no-any
Consumer: (props: any) =>
(
<div id="DataPaginationContext.Consumer" {...props}>
{props.children({
limit: 'limit',
offset: 'offset',
totalCount: 'totalCount',
changePageByOffset: mockChangePageByOffset,
})}
</div>
),
},
}));
describe('DataListPagination', () => {
it('should handle offsetChange event correctly', () => {
const component = enzyme.shallow<DataListPagination, {}, DataListPagination>(
(<DataListPagination />));
// call shallow() method to invoke render prop function
// and return component inside it
const deepComponent = component.shallow();
// find component and simulate onChange event
deepComponent.find('OffsetPagination').simulate('change', 'test_value');
expect(mockChangePageByOffset).toHaveBeenCalledTimes(1);
expect(mockChangePageByOffset).toHaveBeenCalledWith('test_value');
});
}); Tested component DataListPagination.tsx export class DataListPagination extends Component<{}> {
handleOffsetChange = (changePageByOffset: (n: number) => void) =>
(newOffset: number) => changePageByOffset(newOffset)
renderChildren = ({ limit, offset, totalCount, changePageByOffset }: DataPaginationContextType) => {
return !!totalCount && (
<OffsetPagination
limit={limit}
offset={offset}
maxItems={totalCount}
floated="right"
onChange={this.handleOffsetChange(changePageByOffset)}
/>
);
}
render() {
return (
<DataPaginationContext.Consumer>
{this.renderChildren}
</DataPaginationContext.Consumer>
);
}
} |
I have the same problem, but return the following error:
|
From Enzyme |
Looks like this issue is not fully fixed. This will pass
However this will fail
|
Actually, I am wrong. Just need to dive deeper to get actual Component rendered. |
I created module for workaround https://www.npmjs.com/package/shallow-with-context. The module works well in our projects. |
@mjancarik if the workaround fixes the problem, filing it as a PR would be helpful - but you may want to look into the |
what do you mean by dive deeper, i'm facing same issue |
@vishalc129 you need to call |
im wrapping my component as when i tried to debug using console.log(component.debug()); then o/p is --
and when i use - component.dive() it says - |
@vishalc129 please file a new issue and i'll help you debug it. |
For all who came here for a solution. I just did componentWrapper = shallow(<SomeComponent {...props} />).dive() ,which worked for a component wrapped by a context consumer. |
Hi,
Current behavior
I'm trying to dive into a Component wrapped with a HOC that adds a Context Consumer but I get an error
TypeError: ShallowWrapper::dive() can only be called on components
. Here is my test case to reproduce :I'm not familiar with React Contexts, but it seems related to the "function as a child", which is however the regular way to use contexts (cf Context.Consumer in React doc).
Expected behavior
In my test case, the
dive
should succeed and return a wrapper around<div>dark</div>
I guess.Your environment
My
package.json
:API
Version
Adapter
Am I getting something wrong ? Is there anything particular to do when "shallow diving" in a Context Consumer ? I can't see where's my error on this case so any help would be appreciated !
The text was updated successfully, but these errors were encountered: