-
-
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
useContext hook not working with shallow #2176
Comments
enzyme's context options only work with "legacy" context; I believe useContext uses a different mechanism. React, and react's shallow renderer, don't seem to provide any way for us to hook into hooks. |
I also am having this problem, and would MEGA appreciate any workarounds or fixes that might come :) |
for the sake of explicitness, here's an example of directly using a context provider and calling https://codesandbox.io/s/elastic-zhukovsky-w5pjc import React, { useContext } from "react";
import Enzyme, { shallow } from "enzyme";
import Adapter from "enzyme-adapter-react-16";
Enzyme.configure({ adapter: new Adapter() });
const MyContext = React.createContext({});
export const MyComponent = () => {
const { myVal } = useContext(MyContext);
return <div data-test="my-component">{myVal}</div>;
};
it("renders the correct text", () => {
const wrapper = shallow(
<MyContext.Provider value={{ myVal: "foobar" }}>
<MyComponent />
</MyContext.Provider>
).dive();
expect(wrapper.text()).toEqual("foobar"); // expected "foobar" received ""
}); @ljharb is the reason why this variation also doesn't work the same as you mentioned above? |
It'd be because it("renders the correct text", () => {
const wrapper = shallow(
<MyComponent />,
{
wrappingComponent: MyContext.Provider,
wrappingComponentProps: { value: { myVal: 'foobar' }
}
);
expect(wrapper.text()).toEqual("foobar"); // expected "foobar" received ""
}); |
Unfortunately this also results in the context value being undefined. https://codesandbox.io/s/amazing-noether-bv6u7 import React, { useContext } from "react";
import Enzyme, { shallow } from "enzyme";
import Adapter from "enzyme-adapter-react-16";
Enzyme.configure({ adapter: new Adapter() });
const MyContext = React.createContext({});
export const MyComponent = () => {
const { myVal } = useContext(MyContext);
return <div data-test="my-component">{myVal}</div>;
};
it("renders the correct text", () => {
const wrapper = shallow(<MyComponent />, {
wrappingComponent: MyContext.Provider,
wrappingComponentProps: { value: { myVal: "foobar" } }
});
console.log(wrapper.debug()); // <div data-test="my-component" />
console.log(wrapper.text()); // ""
}); |
The evidence is that if you use snapshot test, you will not find |
createShallowRenderer(options = {}) {
const adapter = this;
const renderer = new ShallowRenderer();
...
return {
render(el, unmaskedContext, {
providerValues = new Map(),
} = {}) {
if (typeof el.type === 'string') {
isDOM = true;
} else if (isContextProvider(el)) {
providerValues.set(el.type, el.props.value);
const MockProvider = Object.assign(
props => props.children,
el.type,
);
return withSetStateAllowed(() => renderer.render({ ...el, type: MockProvider }));
}
...
},
...
}
} Maybe |
The workaround I'm using for now is the one described in this article; wrap https://codesandbox.io/s/crimson-mountain-vo7sk
import React, { useContext } from "react";
const MyContext = React.createContext({});
export default MyContext;
export const useMyContext = () => useContext(MyContext);
import React from "react";
import Enzyme, { shallow } from "enzyme";
import Adapter from "enzyme-adapter-react-16";
import MyContext, { useMyContext } from "./MyContext";
import * as MyContextModule from "./MyContext";
Enzyme.configure({ adapter: new Adapter() });
export const MyComponent = () => {
const { myVal } = useMyContext(); // instead of useContext(MyContext)
return <div data-test="my-component">{myVal}</div>;
};
it("renders the correct text", () => {
jest.spyOn(MyContextModule, "useMyContext").mockImplementation(() => ({
myVal: "foobar"
}));
const wrapper = shallow(
<MyContext.Provider>
<MyComponent />
</MyContext.Provider>
).dive();
expect(wrapper.text()).toEqual("foobar");
}); |
I'm having same issue with using react-redux 7.1, but I can't change the way how react-redux use the useContext(). Is there any other workaround?
|
I have the same problem. Did you solve it? |
This is broken until facebook/react#15589 gets though. As hooks do not really work in shallow without it. |
that react PR is only for getting useEffect to work. It doesn't address context, though something similar may be doable. |
…omponent, use full mount React Redux 7.x no longer supports `store` prop for `Provider`, and uses the new `React.createContext()` instead of the legacy context. That requires an update of the unit tests. - use `wrappingComponent` option to activate a mock Redux provider - switch to full mount, as `wrappingComponent` doesn't work with `useContext` in Enzyme (enzymejs/enzyme#2176)
…omponent, use full mount React Redux 7.x no longer supports `store` prop for `Provider`, and uses the new `React.createContext()` instead of the legacy context. That requires an update of the unit tests. - use `wrappingComponent` option to activate a mock Redux provider - switch to full mount, as `wrappingComponent` doesn't work with `useContext` in Enzyme (enzymejs/enzyme#2176)
I have the same problem with using styled-components@next (v5) :'( |
Same problem (I think?) here with |
@lensbart all custom hooks are built on top of builtin hooks; useIntl presumably uses useContext. |
Yes it does |
@ljharb Is there something more complex preventing this? Or is it just nobody has stepped up to do the work? Would something similar to facebook/react#16168 fix this? If you could point me in the right direction, I'm happy to make the change. I am not well acquainted with React shallow renderer internals though, so any advice would be appreciated. |
I found some workarounds for shallow rendering with React.useContext, Material UI's makeStyles, and react-redux v7.x.x. See below for some high level notes and my codesandbox for examples. Components that consume context using React.useContext()
Components that consume context using <Context.Consumer />
Material UI's makeStyles
Redux - Redux has some fancy logic for using context. It looks like you can optionally pass the store or a custom context into a connected component.
It's pretty disappointing that shallow rendering's been broken for so long and that our options are limited to switching to mount or finding brittle workarounds like these. |
@Alphy11 yes - the change either needs to be made directly in react's shallow renderer, or, enzyme would need to rebuild from scratch react's entire hooks system. Certainly someone submitting a PR to either react or enzyme would be great, but it's not simple, fast, or straightforward. |
For me, |
…omponent, use full mount React Redux 7.x no longer supports `store` prop for `Provider`, and uses the new `React.createContext()` instead of the legacy context. That requires an update of the unit tests. - use `wrappingComponent` option to activate a mock Redux provider - switch to full mount, as `wrappingComponent` doesn't work with `useContext` in Enzyme (enzymejs/enzyme#2176)
* fix(deps): update dependency redux-form to v8 * fix(deps): update dependency react-redux to v7 * ImageEditorCanvas: change react-redux withRef to forwardRef, modernize the component React Redux ref API has changed in 7.0.0. Modernization includes: - use modern refs - use arrow functions for bound methods - every variable has its own `const` decl * Zoninator Search Autocomplete: change react-redux withRef to forwardRef * Notifications: use react-redux forwardRef to get list element * Get Redux store from new React context in TinyMCE wrappers * WebPaymentBox unit test: pass mock Redux store by wrappingComponent enzyme option Instead of using the legacy React context, which is an implementation detail of particular React Redux version, use the Enzyme's `wrappingComponent` option to render a mock Redux provider above the tested component. * WithContactDetailsValidation Unit Test: pass Redux store as wrappingComponent, use full mount React Redux 7.x no longer supports `store` prop for `Provider`, and uses the new `React.createContext()` instead of the legacy context. That requires an update of the unit tests. - use `wrappingComponent` option to activate a mock Redux provider - switch to full mount, as `wrappingComponent` doesn't work with `useContext` in Enzyme (enzymejs/enzyme#2176) * Jetpack Connect Help Button: convert to hooked functional component with useDispatch * PluginsList: rewrite tests to use Enzyme * DomainManagement.List: rewrite tests to use Enzyme
I spent a long time trying a bunch of things because the docs for shallow made it sound like The docs for shallow say:
In an app context, rendering a Is there a better way the docs could describe what providing |
|
@ljharb yes, you know that from looking at React internals, and I know that because you've said it in this issue. But the React docs for useContext don't imply that at all; in fact, they imply the opposite:
But since it is different in a way that matters to Enzyme, what can we add to the documentation to make other people know it, too? If this issue was archived tomorrow and the discussion disappeared, what guardrails would be in place to stop someone else from opening a new issue to report the apparent bug? (And thereby wasting the maintainer's time, as well as their own.) |
A coworker pointed out the code example in the docs on getWrappingComponent also doesn't work now, since it uses react-redux, which now also uses I added a test to my code sandbox to demonstrate this. |
I'd be happy to review a PR that clarifies the docs (including the unfortunately-now-incorrect react-redux example). |
I could update the docs to remove the incorrect example and add caveats about |
It allows you to wrap a component in a (legacy) context provider, but directly be wrapping the actual component, so you don't have to dive down to the thing you're actually trying to test. |
@twharmon jest.mock("react", () => ({
...jest.requireActual("react"),
useContext: () => 'context_value'
})); instead of jest.spyOn(React, 'useContext').mockImplementation(() => 'context_value') |
@splybon Weird, this one does not work for me but the version with
And in tests I do
For some reason it's still using the context from |
I was able to workaround it the following way:
and its usage:
You can pass context value via |
Does this work? Because I love it. I mean, for me it is a better workaround than having to modify the code you are testing to use |
That indeed only works if your real code always live-looks-up |
I think it is exactly the opposite. He seems to be proposing a solution to that problem |
That solution requires relying on very specific babel interop implementation details. |
Ok, that is something I didn't knew |
The tests with context were failing with the error: "Invariant Violation: Hooks can only be called inside the body of a function component". The root cause is somwhere between styled-components v5+ which started using React hooks and enzyme renderer. The cleanest workaround I have found is enzymejs/enzyme#2176 (comment)
…nzyme support of useContext 'enzymejs/enzyme#2176'
* context experiments * basic context setup * tidy up debugging * uncommen event listener for expected closing behaviour * make debug clearer * clean up console logging for clarity * move globalnavcontext provider up to header component * GN-117 add useCallback to clickOutside hook to maintain reference to id of open dropdown * GN-117 Refactor Nav component to class. Add custom hook and context for open dropdown state and nav area click outside detection * GN-117 Begin conversion of Account class component to functional component * GN-117 Remove commented out lines * GN-117 Reinstate propTypes for Naccount placeholder component * Side effect sideline (#150) * side effect shenanigans * Magical useRef() * update key event to use e.key rather than e.keycode for better cross platform * Move myAccount isExpanded state into GlobalNavContext * Remove redundant event binding * Make megamenu click detection more 'reacty' * Manage context of opened menus * Rename Naccount component to Account Co-authored-by: John Davey <[email protected]> * Tests needed * GN-117 Tidy up redundant arguments and commented out logic for useClickOutside hook * GN-117 Change directory structure for hooks and react context. Make useClickOutside re-useable. * GN-117 Import GlobalNavContextProvider from new location for Account tests * GN-117 Fixing broken unit tests * GN-117 Fixing broken tests * GN-117 Fixing broken tests * Replace simulate click * GN-117 Revert some tests to shallow rendering after adding defaults to context * GN-117 Revert to shallow mounting for a previously failing unit test * GN-117 Convert unit test from shallow to mount due to issues around enzyme support of useContext 'enzymejs/enzyme#2176' * GN-117 Rename GlobalNavContext to HeaderContext * GN-117 remove unused dependencies from account test * GN-117 Tidy up unit tests * testing custom hook useClickOutside * basic setup * Custom hook test * Update test * Rename test Co-authored-by: w@rren <[email protected]>
* GN-95 Move NavLinks to own component * GN-95 Get button in if dropdown * GN-95 Skip links and click behaviour * GN-95 Skip links * GN-95 Styling * GN-95 Styling, aria controls, chevron * GN-95 Active text on button * GN-95 Refactoring styling * Ignore netlify file * GN-95 Chevrons and revised services list * GN-95 Link styling * GN-95 More services dropdown and styling * GN-95 Missed commit with dropdownComponent * Comment out test to get build * Attempt fix for $RefreshReg$ issue See https://github.com/pmmmwh/react-refresh-webpack-plugin/blob/main/docs/TROUBLESHOOTING.md#usage-with-indirection-like-workers-and-js-templates * Focus trap plugin * GN-95 Focus trap around all of nav when active * Remove tests for alpha * GN-95 Focus trap no initial focus * GN-95 Z-index for pathways and guidance fix * GN-95 Escape by clicking search or acccount * GN-95 Replacing with nds components * GN-95 Config fix for grid css issue * GN-101 Issues (#122) * Scrim * GN-101 Move focus to search when clicked * GN-101 Close MyAccount Dropdown on meganav click * GN-101 Close MyAccount Dropdown on meganav click * GN-95 Now click scrim to close * GN-101 Fix list styling variation * GN-101 Code review fixes Co-authored-by: w@rren <[email protected]> Co-authored-by: w@rren <[email protected]> * GN-102 Bnf content (#125) * Scrim * GN-101 Move focus to search when clicked * GN-101 Close MyAccount Dropdown on meganav click * GN-101 Close MyAccount Dropdown on meganav click * GN-95 Now click scrim to close * GN-101 Fix list styling variation * GN-102 Add BNF content * GN-102 BNFc content * GN-102 BNF content update * GN-101 Code review fixes Co-authored-by: w@rren <[email protected]> * GN-102 Nitpicking css * GN-102 Pull url from services * GN-102 Spacing * GN-101 Fix failing tests * GN-102 Update snapshot * GN-102 Fix letters and dancing button Co-authored-by: w@rren <[email protected]> * GN-103 Bnfc content (#126) * Scrim * GN-101 Move focus to search when clicked * GN-101 Close MyAccount Dropdown on meganav click * GN-101 Close MyAccount Dropdown on meganav click * GN-95 Now click scrim to close * GN-101 Fix list styling variation * GN-102 Add BNF content * GN-102 BNFc content * GN-102 BNF content update * GN-103 BNFC content * GN-103 Revert nav text to abbreviation * GN-103 Use var for url * GN-103 Spacing * GN-101 Fix failing tests * GN-103 Update snapshot * GN-103 Button fix and letter link fix * GN-103 Button dancing fix * GN-103 Another dancing button fix attempt Co-authored-by: w@rren <[email protected]> * GN-105 Standards and indicators content (#128) * GN-105 Content for standards and indicators * GN-105 Update links * GN-105 Remove standards and indicators from more * GN-101 Fix failing tests * GN-105 Update snapshot * GN-105 Amend S&I base link Co-authored-by: w@rren <[email protected]> Co-authored-by: John Davey <[email protected]> * GN-106 About us content (#130) * GN-101 Move focus to search when clicked * GN-101 Close MyAccount Dropdown on meganav click * GN-101 Close MyAccount Dropdown on meganav click * GN-101 Styling issues across applications * GN-101 Use bundled design system css * GN-101 Loose grid * GN-98 Initial guidance content * GN-98 Content update * GN-106 About us content * GN-101 Fix failing tests * GN-106 Update snapshot * GN-98 Snapshot updated * GN-106 Content link issue * Start guidance content * AW/21 Colour Scheme * GN-98 Guidance content * GN-106 About us content update * GN-108 Remove e.preventDefault on mega menu click handler to enable links to work * GN-108 Fix scrim * GN-108 Add canUseDOM state to control display of dropdown menus in main nav * Prettier * Reset root about us URL Co-authored-by: w@rren <[email protected]> Co-authored-by: John Davey <[email protected]> Co-authored-by: John Davey <[email protected]> * GN-98 Guidance content (#129) * GN-101 Move focus to search when clicked * GN-101 Close MyAccount Dropdown on meganav click * GN-101 Close MyAccount Dropdown on meganav click * GN-101 Styling issues across applications * GN-101 Use bundled design system css * GN-101 Loose grid * GN-98 Initial guidance content * GN-98 Content update * GN-101 Fix failing tests * GN-98 Snapshot updated * Start guidance content * AW/21 Colour Scheme * GN-98 Guidance content * GN-108 Remove e.preventDefault on mega menu click handler to enable links to work * GN-108 Fix scrim * GN-108 Add canUseDOM state to control display of dropdown menus in main nav * Url updates * Prettier Co-authored-by: w@rren <[email protected]> Co-authored-by: John Davey <[email protected]> * GN-104 Cks content (#127) * Scrim * GN-101 Move focus to search when clicked * GN-101 Close MyAccount Dropdown on meganav click * GN-101 Close MyAccount Dropdown on meganav click * GN-101 Styling issues across applications * GN-101 Use bundled design system css * GN-101 Loose grid * GN-98 Initial guidance content * GN-95 Now click scrim to close * GN-101 Fix list styling variation * GN-102 Add BNF content * GN-102 BNFc content * GN-102 BNF content update * GN-103 BNFC content * GN-104 CKS content * GN-104 Revert nav text to abbreviation * GN-103 Revert nav text to abbreviation * GN-101 Code review fixes Co-authored-by: w@rren <[email protected]> * GN-102 Nitpicking css * GN-104 Rename var * GN-102 Pull url from services * GN-103 Use var for url * GN-98 Content update * demo stuff * GN-104 Url var * GN-102 Spacing * GN-103 Spacing * GN-105 Content for standards and indicators * GN-106 About us content * GN-105 Update links * GN-105 Remove standards and indicators from more * GN-101 Fix failing tests * GN-102 Update snapshot * GN-103 Update snapshot * GN-105 Update snapshot * GN-106 Update snapshot * GN-104 Update snapshot * GN-98 Snapshot updated * GN-000 Alpha services. May want renaming for UR. * GN-000 Renamed for UR. DO NOT DEPLOY TO LIVE. * GN-000 Snapshot updated * GN-000 Links for UR. DO NOT DEPLOY TO LIVE * GN-000 Prettier issue * GN-102 Fix letters and dancing button * GN-103 Button fix and letter link fix * GN-103 Button dancing fix * GN-103 Another dancing button fix attempt * GN-106 Content link issue * GN-105 Amend S&I base link * Start guidance content * AW/21 Colour Scheme * GN-98 Guidance content * GN-106 About us content update * GN-109 More content * GN-107 Pathways content * GN-108 Remove e.preventDefault on mega menu click handler to enable links to work * GN-108 Fix scrim * GN-108 Add canUseDOM state to control display of dropdown menus in main nav * Duplicate css removed * Remove duplicate CSS * Remove duplicate css * Remove duplicate export * Links to live * Tidy up * GN-104 Snapshot update * Snapshot update Co-authored-by: w@rren <[email protected]> Co-authored-by: John Davey <[email protected]> * GN-109 More dropdown content (#133) * Scrim * GN-101 Move focus to search when clicked * GN-101 Close MyAccount Dropdown on meganav click * GN-101 Close MyAccount Dropdown on meganav click * GN-101 Styling issues across applications * GN-101 Use bundled design system css * GN-101 Loose grid * GN-98 Initial guidance content * GN-95 Now click scrim to close * GN-101 Fix list styling variation * GN-102 Add BNF content * GN-102 BNFc content * GN-102 BNF content update * GN-103 BNFC content * GN-104 CKS content * GN-104 Revert nav text to abbreviation * GN-103 Revert nav text to abbreviation * GN-101 Code review fixes Co-authored-by: w@rren <[email protected]> * GN-102 Nitpicking css * GN-104 Rename var * GN-102 Pull url from services * GN-103 Use var for url * GN-98 Content update * demo stuff * GN-104 Url var * GN-102 Spacing * GN-103 Spacing * GN-105 Content for standards and indicators * GN-106 About us content * GN-105 Update links * GN-105 Remove standards and indicators from more * GN-101 Fix failing tests * GN-102 Update snapshot * GN-103 Update snapshot * GN-105 Update snapshot * GN-106 Update snapshot * GN-104 Update snapshot * GN-98 Snapshot updated * GN-000 Alpha services. May want renaming for UR. * GN-000 Renamed for UR. DO NOT DEPLOY TO LIVE. * GN-000 Snapshot updated * GN-000 Links for UR. DO NOT DEPLOY TO LIVE * GN-000 Prettier issue * GN-102 Fix letters and dancing button * GN-103 Button fix and letter link fix * GN-103 Button dancing fix * GN-103 Another dancing button fix attempt * GN-106 Content link issue * GN-105 Amend S&I base link * Start guidance content * AW/21 Colour Scheme * GN-98 Guidance content * GN-106 About us content update * GN-109 More content * GN-107 Pathways content * GN-108 Remove e.preventDefault on mega menu click handler to enable links to work * GN-108 Fix scrim * GN-108 Add canUseDOM state to control display of dropdown menus in main nav * Duplicate css removed * Remove duplicate CSS * Remove duplicate css * Remove duplicate export * Links to live * Tidy up * Snapshot update Co-authored-by: Eleanor Mollett <[email protected]> Co-authored-by: John Davey <[email protected]> * GN-110 Life sciences content (#137) * Scrim * GN-101 Move focus to search when clicked * GN-101 Close MyAccount Dropdown on meganav click * GN-101 Close MyAccount Dropdown on meganav click * GN-101 Styling issues across applications * GN-101 Use bundled design system css * GN-101 Loose grid * GN-98 Initial guidance content * GN-95 Now click scrim to close * GN-101 Fix list styling variation * GN-102 Add BNF content * GN-102 BNFc content * GN-102 BNF content update * GN-103 BNFC content * GN-104 CKS content * GN-104 Revert nav text to abbreviation * GN-103 Revert nav text to abbreviation * GN-101 Code review fixes Co-authored-by: w@rren <[email protected]> * GN-102 Nitpicking css * GN-104 Rename var * GN-102 Pull url from services * GN-103 Use var for url * GN-98 Content update * demo stuff * GN-104 Url var * GN-102 Spacing * GN-103 Spacing * GN-105 Content for standards and indicators * GN-106 About us content * GN-105 Update links * GN-105 Remove standards and indicators from more * GN-101 Fix failing tests * GN-102 Update snapshot * GN-103 Update snapshot * GN-105 Update snapshot * GN-106 Update snapshot * GN-104 Update snapshot * GN-98 Snapshot updated * GN-000 Alpha services. May want renaming for UR. * GN-000 Renamed for UR. DO NOT DEPLOY TO LIVE. * GN-000 Snapshot updated * GN-000 Links for UR. DO NOT DEPLOY TO LIVE * GN-000 Prettier issue * GN-102 Fix letters and dancing button * GN-103 Button fix and letter link fix * GN-103 Button dancing fix * GN-103 Another dancing button fix attempt * GN-106 Content link issue * GN-105 Amend S&I base link * Start guidance content * AW/21 Colour Scheme * GN-98 Guidance content * GN-106 About us content update * GN-109 More content * GN-107 Pathways content * GN-108 Remove e.preventDefault on mega menu click handler to enable links to work * GN-108 Fix scrim * GN-108 Add canUseDOM state to control display of dropdown menus in main nav * Duplicate css removed * Remove duplicate CSS * Remove duplicate css * Remove duplicate export * Links to live * Tidy up * GN-110 Life Sciences dropdown content * Snapshot update Co-authored-by: Eleanor Mollett <[email protected]> Co-authored-by: John Davey <[email protected]> * GN-107 Pathways content (#134) * Scrim * GN-101 Move focus to search when clicked * GN-101 Close MyAccount Dropdown on meganav click * GN-101 Close MyAccount Dropdown on meganav click * GN-101 Styling issues across applications * GN-101 Use bundled design system css * GN-101 Loose grid * GN-98 Initial guidance content * GN-95 Now click scrim to close * GN-101 Fix list styling variation * GN-102 Add BNF content * GN-102 BNFc content * GN-102 BNF content update * GN-103 BNFC content * GN-104 CKS content * GN-104 Revert nav text to abbreviation * GN-103 Revert nav text to abbreviation * GN-101 Code review fixes Co-authored-by: w@rren <[email protected]> * GN-102 Nitpicking css * GN-104 Rename var * GN-102 Pull url from services * GN-103 Use var for url * GN-98 Content update * demo stuff * GN-104 Url var * GN-102 Spacing * GN-103 Spacing * GN-105 Content for standards and indicators * GN-106 About us content * GN-105 Update links * GN-105 Remove standards and indicators from more * GN-101 Fix failing tests * GN-102 Update snapshot * GN-103 Update snapshot * GN-105 Update snapshot * GN-106 Update snapshot * GN-104 Update snapshot * GN-98 Snapshot updated * GN-000 Alpha services. May want renaming for UR. * GN-000 Renamed for UR. DO NOT DEPLOY TO LIVE. * GN-000 Snapshot updated * GN-000 Links for UR. DO NOT DEPLOY TO LIVE * GN-000 Prettier issue * GN-102 Fix letters and dancing button * GN-103 Button fix and letter link fix * GN-103 Button dancing fix * GN-103 Another dancing button fix attempt * GN-106 Content link issue * GN-105 Amend S&I base link * Start guidance content * AW/21 Colour Scheme * GN-98 Guidance content * GN-106 About us content update * GN-109 More content * GN-107 Pathways content * GN-108 Remove e.preventDefault on mega menu click handler to enable links to work * GN-108 Fix scrim * GN-108 Add canUseDOM state to control display of dropdown menus in main nav * Duplicate css removed * Remove duplicate CSS * Remove duplicate css * Remove duplicate export * Links to live * Tidy up * Snapshot update Co-authored-by: Eleanor Mollett <[email protected]> Co-authored-by: John Davey <[email protected]> * GN-000 All content (#132) * Scrim * GN-101 Move focus to search when clicked * GN-101 Close MyAccount Dropdown on meganav click * GN-101 Close MyAccount Dropdown on meganav click * GN-101 Styling issues across applications * GN-101 Use bundled design system css * GN-101 Loose grid * GN-98 Initial guidance content * GN-95 Now click scrim to close * GN-101 Fix list styling variation * GN-102 Add BNF content * GN-102 BNFc content * GN-102 BNF content update * GN-103 BNFC content * GN-104 CKS content * GN-104 Revert nav text to abbreviation * GN-103 Revert nav text to abbreviation * GN-101 Code review fixes Co-authored-by: w@rren <[email protected]> * GN-102 Nitpicking css * GN-104 Rename var * GN-102 Pull url from services * GN-103 Use var for url * GN-98 Content update * demo stuff * GN-104 Url var * GN-102 Spacing * GN-103 Spacing * GN-105 Content for standards and indicators * GN-106 About us content * GN-105 Update links * GN-105 Remove standards and indicators from more * GN-101 Fix failing tests * GN-102 Update snapshot * GN-103 Update snapshot * GN-105 Update snapshot * GN-106 Update snapshot * GN-104 Update snapshot * GN-98 Snapshot updated * GN-000 Alpha services. May want renaming for UR. * GN-000 Renamed for UR. DO NOT DEPLOY TO LIVE. * GN-000 Snapshot updated * GN-000 Links for UR. DO NOT DEPLOY TO LIVE * GN-000 Prettier issue * GN-102 Fix letters and dancing button * GN-103 Button fix and letter link fix * GN-103 Button dancing fix * GN-103 Another dancing button fix attempt * GN-106 Content link issue * GN-105 Amend S&I base link * Start guidance content * AW/21 Colour Scheme * GN-98 Guidance content * GN-106 About us content update * GN-109 More content * GN-107 Pathways content * GN-108 Remove e.preventDefault on mega menu click handler to enable links to work * GN-108 Fix scrim * GN-108 Add canUseDOM state to control display of dropdown menus in main nav * Duplicate css removed * Remove duplicate CSS * Remove duplicate css * Remove duplicate export * Links to live * Tidy up * GN-110 Life Sciences dropdown content * Snapshot update Co-authored-by: w@rren <[email protected]> Co-authored-by: John Davey <[email protected]> * GN-108 Snag list (#138) * Scrim * GN-101 Move focus to search when clicked * GN-101 Close MyAccount Dropdown on meganav click * GN-101 Close MyAccount Dropdown on meganav click * GN-101 Styling issues across applications * GN-101 Use bundled design system css * GN-101 Loose grid * GN-98 Initial guidance content * GN-95 Now click scrim to close * GN-101 Fix list styling variation * GN-102 Add BNF content * GN-102 BNFc content * GN-102 BNF content update * GN-103 BNFC content * GN-104 CKS content * GN-104 Revert nav text to abbreviation * GN-103 Revert nav text to abbreviation * GN-101 Code review fixes Co-authored-by: w@rren <[email protected]> * GN-102 Nitpicking css * GN-104 Rename var * GN-102 Pull url from services * GN-103 Use var for url * GN-98 Content update * demo stuff * GN-104 Url var * GN-102 Spacing * GN-103 Spacing * GN-105 Content for standards and indicators * GN-106 About us content * GN-105 Update links * GN-105 Remove standards and indicators from more * GN-101 Fix failing tests * GN-102 Update snapshot * GN-103 Update snapshot * GN-105 Update snapshot * GN-106 Update snapshot * GN-104 Update snapshot * GN-98 Snapshot updated * GN-000 Alpha services. May want renaming for UR. * GN-000 Renamed for UR. DO NOT DEPLOY TO LIVE. * GN-000 Snapshot updated * GN-000 Links for UR. DO NOT DEPLOY TO LIVE * GN-000 Prettier issue * GN-102 Fix letters and dancing button * GN-103 Button fix and letter link fix * GN-103 Button dancing fix * GN-103 Another dancing button fix attempt * GN-106 Content link issue * GN-105 Amend S&I base link * Start guidance content * AW/21 Colour Scheme * GN-98 Guidance content * GN-106 About us content update * GN-109 More content * GN-107 Pathways content * GN-108 Remove e.preventDefault on mega menu click handler to enable links to work * GN-108 Fix scrim * GN-108 Add canUseDOM state to control display of dropdown menus in main nav * Duplicate css removed * Remove duplicate CSS * Remove duplicate css * Remove duplicate export * Links to live * Tidy up * GN-108 Primary to CTA buttons * GN-108 Add column gap * GN-108 Consistent URLs Co-authored-by: Eleanor Mollett <[email protected]> Co-authored-by: John Davey <[email protected]> * GN-112 New simplified pathways content (#139) * GN-112 New simplified pathways content * GN-112 Remove pathways dropdown * GN-113 Alpha snag list (#140) * Settings * GN-113 Alpha snag list * Config typo * GN-000 Merge npm release (#144) * GN-116 Publishing to npm (#142) * GN-116 Pin Node version with Volta * GN-116 Update readme * GN-56 Footer logo and remove IE8 support for SVG fallback (#141) Co-authored-by: Ian Routledge <[email protected]> Co-authored-by: Ian Routledge <[email protected]> Co-authored-by: Ian Routledge <[email protected]> * GN-119 BNF CSS scoping (#146) * GN-138 Shorten nav copy (#147) * GN-140 Standards and indicators content (#151) * GN-116 Publishing to npm (#142) * GN-116 Pin Node version with Volta * GN-116 Update readme * GN-56 Footer logo and remove IE8 support for SVG fallback (#141) Co-authored-by: Ian Routledge <[email protected]> * GN-140 Standards removal Co-authored-by: Ian Routledge <[email protected]> Co-authored-by: Ian Routledge <[email protected]> * GN-118 Add non-JS dropdown (#149) * GN-118 Add non-JS dropdown * GN-118 More link fallback * Removed redundant nesting * GN-117 Remove need to assert on dom (#148) * context experiments * basic context setup * tidy up debugging * uncommen event listener for expected closing behaviour * make debug clearer * clean up console logging for clarity * move globalnavcontext provider up to header component * GN-117 add useCallback to clickOutside hook to maintain reference to id of open dropdown * GN-117 Refactor Nav component to class. Add custom hook and context for open dropdown state and nav area click outside detection * GN-117 Begin conversion of Account class component to functional component * GN-117 Remove commented out lines * GN-117 Reinstate propTypes for Naccount placeholder component * Side effect sideline (#150) * side effect shenanigans * Magical useRef() * update key event to use e.key rather than e.keycode for better cross platform * Move myAccount isExpanded state into GlobalNavContext * Remove redundant event binding * Make megamenu click detection more 'reacty' * Manage context of opened menus * Rename Naccount component to Account Co-authored-by: John Davey <[email protected]> * Tests needed * GN-117 Tidy up redundant arguments and commented out logic for useClickOutside hook * GN-117 Change directory structure for hooks and react context. Make useClickOutside re-useable. * GN-117 Import GlobalNavContextProvider from new location for Account tests * GN-117 Fixing broken unit tests * GN-117 Fixing broken tests * GN-117 Fixing broken tests * Replace simulate click * GN-117 Revert some tests to shallow rendering after adding defaults to context * GN-117 Revert to shallow mounting for a previously failing unit test * GN-117 Convert unit test from shallow to mount due to issues around enzyme support of useContext 'enzymejs/enzyme#2176' * GN-117 Rename GlobalNavContext to HeaderContext * GN-117 remove unused dependencies from account test * GN-117 Tidy up unit tests * testing custom hook useClickOutside * basic setup * Custom hook test * Update test * Rename test Co-authored-by: w@rren <[email protected]> * Set base high z-index (#152) * Snapshot update * Remove nerv * GN-146 Data tracking attributes * GN-146 Data tracking attributes (#153) * Update snapshot * GN-115 Add additional data-tracking attributes * GN-115 Update test snapshot * GN-145 Implement self contained grid system (#155) * Replace find with filter (#154) * GN-148 Fix content overflow * Move focus trap to deps * GN-149 Skip x route (#157) * GN-149 Reinstate ids for top level links to make them focussable upon closing dropdown * GN-149 Refactor Co-authored-by: w@rren <[email protected]> Co-authored-by: w@rren <[email protected]> * GN-130 Fix invisible white close button * Scrim flicker fix Co-authored-by: John Davey <[email protected]> * GN-115 Add IE11 compatible e.key string for escape key * GN-115 Fix bug in copy * GN-115 Add Esc to ESCAPE_KEYS for IE11 support * Communities link update * Fix concatenated links * Correct public involvement link (again) * GN-137 Add tests (#158) * Uncommented tests * GN-137 Add unit tests for dropdown component * Updated browserslist & caniuse * NavLinks test WIP * Trying to test WIP * GN-137 Fix tests for tracked link * GN-137 Update 'Proposed' to 'Awaiting development' * GN-137 Skip failing test - enzyme mocking hooks issue * GN-137 Fix or skip failing tests due to enzyme react hooks testing issues Co-authored-by: w@rren <[email protected]> * GN-154 Button styling Including GN-155 GN-153 * GN-115 Add data-tracking attrs for Standards and Indicators dropdown * Alpha current style override Co-authored-by: John Davey <[email protected]> Co-authored-by: Eleanor Mollett <[email protected]> Co-authored-by: John Davey <[email protected]> Co-authored-by: John Davey <[email protected]> Co-authored-by: Ian Routledge <[email protected]> Co-authored-by: Ian Routledge <[email protected]> Co-authored-by: John Davey <[email protected]>
The
useContext
hook does not work withshallow
rendering and passing incontext
. Are there any workarounds for now?Current behavior
The value returned by
useContext
in a function component appears to be empty when usingshallow
and passing in acontext
argument. (The same problem also occurs when wrapping the component in the appropriate context Provider directly and then calling.dive()
).Here is a minimal test case, also reproduced at https://codesandbox.io/s/nice-blackwell-yz7tn in the
index.spec.js
file.Expected behavior
The text in the example above is expected to be
"foobar"
, but it's actually""
.In general, the value returned from
useContext
appears to be undefined.Note that using
mount
instead ofshallow
causes the test to pass.Also note: the codesandbox above has a second file (
class.spec.js
) in which a hack is employed that makes the test pass, which uses the legacycontextTypes
. But this only appears to work with classes andthis.context
, not withuseContext
.Your environment
enzyme 3.10.0
enzyme-adapter-react-16 1.14.0
react 16.8.6
react-dom 16.8.6
API
Version
Adapter
The text was updated successfully, but these errors were encountered: