Skip to content
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

New React context API adds new tag types #1509

Closed
2 of 9 tasks
Tracked by #1553
lourd opened this issue Feb 3, 2018 · 46 comments
Closed
2 of 9 tasks
Tracked by #1553

New React context API adds new tag types #1509

lourd opened this issue Feb 3, 2018 · 46 comments

Comments

@lourd
Copy link

lourd commented Feb 3, 2018

The new API for context, publicly released just a few hours ago in the React 16.3 alpha, adds new tag types for the Provider and Consumer components that are returned from the createContext function.

When updating my module to use the new API, I get an unknown node error from the ReactSixteenAdapter.js:

Enzyme Internal Error: unknown node with tag 13

I went in and took a stab at updating the adapter, and found that the Consumer prop has another new tag type, 12.

I added the following lines:

+ var ContextProvider = 13
+ var ContextConsumer = 12

and tried adding cases for both of those in the module's switch statement, but none of the existing case handling options worked to get my tests passing again.

Environment

API

  • shallow
  • mount
  • render

Version

library version
Enzyme 3.3.0
React 16.3.0-alpha.0

Adapter

  • enzyme-adapter-react-16
  • enzyme-adapter-react-15
  • enzyme-adapter-react-15.4
  • enzyme-adapter-react-14
  • enzyme-adapter-react-13
  • enzyme-adapter-react-helper
lourd added a commit to lourd/react-google-api that referenced this issue Feb 3, 2018
Tests are failing because of new node types within enzyme. Filed an
issue here enzymejs/enzyme#1509
@lourd
Copy link
Author

lourd commented Feb 3, 2018

Here's the relevant build output from my test and the source that its covering

@Anonyfox
Copy link

any news on this?

@ljharb
Copy link
Member

ljharb commented Mar 16, 2018

Nobody's put up a PR yet - so no news yet.

@aweary aweary closed this as completed in e21b7b5 Mar 30, 2018
aweary added a commit that referenced this issue Mar 30, 2018
Add support for react context element types, fixes #1509
@x3mka
Copy link

x3mka commented Mar 31, 2018

Guys, publish a new version with this fix, please. No way for npm install from git monorepo sub dir drives me crazy: npm/npm#2974

@diegohaz
Copy link

diegohaz commented Mar 31, 2018

@x3mka in the meanwhile, I've copied the ReactSixteenAdapter contents and put it on my setup file: https://github.com/diegohaz/constate/blob/93b7b5b469be4521784b51380f49e6589c3e56b9/test/setup.js

The only difference from the file on master is the ensureKeyOrUndefined function that I needed to add to the file:
https://github.com/diegohaz/constate/blob/93b7b5b469be4521784b51380f49e6589c3e56b9/test/ReactSixteenAdapter.js

@x3mka
Copy link

x3mka commented Apr 1, 2018

@diegohaz Thanks!

@South-Paw
Copy link

Hi, just hit this issue with a repo we're working on, thanks to @diegohaz's solution though I've been able to solve it and continue working on things. I assume until #1553 is complete, this is the best way to solve the issue..?

Side note; thanks, for all the hard work you guys do! 👍❤️

@ryandrewjohnson
Copy link

If you're using jest I found that you can get decent results mocking out context with a utility function like so:

// ensure you're resetting modules before each test
beforeEach(() => {
  jest.resetModules();
});

const getComponentWithContext = context => {
  // mock out the context you're using in the component
  jest.doMock('./YourContext', () => {
    return {
      YourContext: {
        Consumer: (props) => props.children(context)
      }
    }
  });
  
  // you need to re-require after calling jest.doMock.
  // return the updated Component module that now includes the mocked context
  return require('./Component');
};

// then use it like so...
it('should return stuff', () => {
    const Component = getComponentWithContext({ data: 'here is some context data for test' });
    const wrapper = mount(<Component />);
    // now do your assertions
  });

For a more detailed explanation I did a quick write up Unit testing components using React’s new Context API

@icfantv
Copy link

icfantv commented Apr 25, 2018

@ryandrewjohnson Thanks for the example! I'm curious as to what the the Producer key looks like in the mocked context. I was unable to figure out the right syntax to make Jest happy.

@tipbruley
Copy link

@icfantv , I think it would be Provider:(props)=>props.children

@kysedate
Copy link

kysedate commented May 9, 2018

Following up with @ryandrewjohnson's example, this can be done with shallow as well if you add .dive() at the end of the call. i.e. const wrapper = shallow(<Component />).dive(); I ran into a scenario where I'm adding context to controls that already have extensive unit tests, and this was the only way to avoid major breakage.

@art-in
Copy link

art-in commented May 14, 2018

So issue was fixed, but not published on npm yet (for 1.5 months)?

@shahkashani
Copy link

Please publish this, y'all.

@ChuckJonas
Copy link

Still no update on this one?

@ljharb
Copy link
Member

ljharb commented Jul 24, 2018

Not yet, things are in progress. Please be patient.

@mspoerer
Copy link

Any news on this topic?

@ljharb
Copy link
Member

ljharb commented Aug 8, 2018

The following packages are now released:

dsamojlenko pushed a commit to cds-snc/ircc-rescheduler that referenced this issue Sep 1, 2018
This is the general pattern we're going to see as we add withContext
to components.
In short, we have to export a wrapped version of the component
as well as a basic version of it for testing.

In our tests, we can pass in the hardcoded context object that
the PropTypes expect and we're all good.

//

Worth pointing out that there's an enzyme problem whereby the new
React Provider and Consumer nodes aren't recognized as legitimate
nodes, so any tests that rely on them will fail.
This should be addressed in the near future but for right now
we can't do a lot about it.
Source:
- enzymejs/enzyme#1509

Since the LanguageSwitcher is part of the FederalBanner (and
the LanguageSwitcher is wrapped in withContext), this means that
we can't `mount` our FederalBanner component until enzyme is updated.
dsamojlenko pushed a commit to cds-snc/ircc-rescheduler that referenced this issue Sep 1, 2018
Enzyme throws an error whenever it's used to try and render a
React Provider or Consumer.
Source:
- enzymejs/enzyme#1509

They've promised an update in the next week or so, but until
then we can't `mount` any of our pages.

Worth pointing out that these tests are pretty useless anyway.
rvsia added a commit to rvsia/patternfly-react that referenced this issue Oct 1, 2018
* see enzymejs/enzyme#1509 for enzyme-adapter update, it throws an error in tests where is context used
* updated Patternfly to newer version, the old one do not contains all required styles
rvsia added a commit to rvsia/patternfly-react that referenced this issue Oct 1, 2018
* see enzymejs/enzyme#1509 for enzyme-adapter update, it throws an error in tests where context is used
* updated Patternfly to newer version, the old one do not contains all required styles
rvsia added a commit to rvsia/patternfly-react that referenced this issue Oct 1, 2018
* see enzymejs/enzyme#1509 for enzyme-adapter update, it throws an error in tests where context is used
* updated Patternfly to newer version, the old one do not contains all required styles
rvsia added a commit to rvsia/patternfly-react that referenced this issue Oct 1, 2018
* see enzymejs/enzyme#1509 for enzyme-adapter update, it throws an error in tests where context is used
* updated Patternfly to newer version, the old one do not contains all required styles
rvsia added a commit to rvsia/patternfly-react that referenced this issue Oct 2, 2018
* see enzymejs/enzyme#1509 for enzyme-adapter update, it throws an error in tests where context is used
* updated Patternfly to newer version, the old one do not contains all required styles
rvsia added a commit to rvsia/patternfly-react that referenced this issue Oct 2, 2018
* see enzymejs/enzyme#1509 for enzyme-adapter update, it throws an error in tests where context is used
* updated Patternfly to newer version, the old one do not contains all required styles
rvsia added a commit to rvsia/patternfly-react that referenced this issue Oct 3, 2018
* see enzymejs/enzyme#1509 for enzyme-adapter update, it throws an error in tests where context is used
rvsia added a commit to rvsia/patternfly-react that referenced this issue Oct 3, 2018
* see enzymejs/enzyme#1509 for enzyme-adapter update, it throws an error in tests where context is used
rvsia added a commit to rvsia/patternfly-react that referenced this issue Oct 3, 2018
* see enzymejs/enzyme#1509 for enzyme-adapter update, it throws an error in tests where context is used
* deleted patch package (new included version do not need it)
rvsia added a commit to rvsia/patternfly-react that referenced this issue Oct 3, 2018
* see enzymejs/enzyme#1509 for enzyme-adapter update, it throws an error in tests where context is used
* deleted patch package (new included version does not need it)
rvsia added a commit to rvsia/patternfly-react that referenced this issue Oct 3, 2018
* see enzymejs/enzyme#1509 for enzyme-adapter update, it throws an error in tests where context is used
* deleted patch package (new included version does not need it)
rvsia added a commit to rvsia/patternfly-react that referenced this issue Oct 4, 2018
* see enzymejs/enzyme#1509 for enzyme-adapter update, it throws an error in tests where context is used
* deleted patch package (new included version does not need it)
rvsia added a commit to rvsia/patternfly-react that referenced this issue Oct 4, 2018
* see enzymejs/enzyme#1509 for enzyme-adapter update, it throws an error in tests where context is used
* deleted patch package (new included version does not need it)
rvsia added a commit to rvsia/patternfly-react that referenced this issue Oct 4, 2018
* see enzymejs/enzyme#1509 for enzyme-adapter update, it throws an error in tests where context is used
* deleted patch package (new included version does not need it)
rvsia added a commit to rvsia/patternfly-react that referenced this issue Oct 5, 2018
* see enzymejs/enzyme#1509 for enzyme-adapter update, it throws an error in tests where context is used
* deleted patch package (new included version does not need it)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests