Closed NotoriousWebmaster closed 5 years ago
What testing framework are you using, i.e. where is .state()
coming from ?
We're using Jest and Enzyme. http://airbnb.io/enzyme/docs/api/ShallowWrapper/state.html
You need to use the .dive() method
import React from "react";
import { shallowWithIntl } from "enzyme-react-intl";
import Hello from "./helloworld";
describe("Hello component", () => {
const hello = shallowWithIntl(<Hello />).dive();
it("should get the hello's state", () => {
expect(hello.state().amount).toBe(5);
});
});
You should have your initial state this way.
Thats how I achieve the things:
import React from 'react';
import StandardFilterIntl, {StandardFilter} from 'bundles/components/Filter/StandardFilter';
import {mountWithIntl} from 'enzyme-react-intl';
const FilterComponent = mountWithIntl(<StandardFilterIntl {...standardFilterProps} />);
FilterComponent.find(StandardFilter).state()
We start with a component using react-intl:
And here's the test:
And the output includes:
FAIL src\app\components\common\currency-input\helloworld.test.js ● Hello component › should get the hello's state
If we removed the intl calls in the component, the test would pass (but with shallow rather than shallowWithIntl).
Thanks for your help.