preactjs / enzyme-adapter-preact-pure

Preact adapter for the Enzyme UI testing library
MIT License
67 stars 17 forks source link

Cannot test Legacy Context API #63

Open pabloai opened 5 years ago

pabloai commented 5 years ago

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 commented 5 years ago

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

robertknight commented 5 years ago

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.