enzyme-adapter-preact-pure
enzyme-adapter-preact-pure copied to clipboard
Cannot test Legacy Context API
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.
You also can see a working example with React and the enzyme-adapter-react-16
here.
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.