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

Cannot test Legacy Context API #63

Open
pabloai opened this issue Jul 19, 2019 · 2 comments
Open

Cannot test Legacy Context API #63

pabloai opened this issue Jul 19, 2019 · 2 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@pabloai
Copy link

pabloai commented Jul 19, 2019

Hi,

I'm trying to test a component using Legacy Context API but the context doesn't seems to be reaching the test file.

The app:

// App.js
export default class App extends Component {
  getChildContext() {
    return { name: "Pipo" };
  }
  render() {
    return <div><Hello /></div>;
  }
}

// Hello.js
export default class Hello extends Component {
  render() {
    return <h1>Hello {this.context.name}!</h1>;
  }
}

The test:

import Enzyme, { shallow } from "enzyme";
import Adapter from "enzyme-adapter-preact-pure";
import Hello from "./Hello";

Enzyme.configure({ adapter: new Adapter() });

it("works", () => {
  const wrapper = shallow(<Hello />, {
    context: { name: "Pepe" }
  });
  expect(wrapper.context("name")).toEqual("Pepe");
});

The test fails with:

Expected value to equal:
  "Pepe"
Received:
  undefined

It seems the context parameter is not being used. Or am I missing something?

You can see a (not) working example here.

Thanks.

@pabloai pabloai changed the title Cannot test Legacy Context API. Cannot test Legacy Context API Jul 19, 2019
@pabloai
Copy link
Author

pabloai commented Jul 24, 2019

You also can see a working example with React and the enzyme-adapter-react-16 here.

@robertknight
Copy link
Member

Thanks for the bug report. Support + tests for legacy context indeed hasn't been implemented yet, as the initial use cases were for new code using the new context API.

@robertknight robertknight added the enhancement New feature or request label Jul 26, 2019
@robertknight robertknight added the help wanted Extra attention is needed label Jul 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants