enzyme-adapter-preact-pure icon indicating copy to clipboard operation
enzyme-adapter-preact-pure copied to clipboard

Cannot test Legacy Context API

Open pabloai opened this issue 5 years ago • 2 comments

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 avatar Jul 19 '19 10:07 pabloai

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

pabloai avatar Jul 24 '19 09:07 pabloai

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 avatar Jul 26 '19 09:07 robertknight