この Gistからのいくつかのアイデアに従って、コンポーネント テストを期待どおりに動作させることができません。
応答からのprops
値が入力されないことを除いて、すべてが機能しているようです。data
これが期待どおりに機能しない理由は何ですか?
これは、既存のコンポーネントに基づいて作成できる最も単純な実際の例です。
// test-component.js
import React from 'react';
import { graphql } from 'react-apollo';
import userQuery from './userQuery.graphql';
function TestComponent (props) {
console.log('props from component', props); // eslint-disable-line
return <div>The component...</div>;
}
export default graphql(userQuery)(TestComponent);
// userQuery.graphql
query userQuery {
user {
items {
firstName
id
lastName
}
}
}
// test-component-test.js
import React from 'react';
import { MockedProvider } from 'react-apollo/lib/test-utils';
import { mount } from 'enzyme';
import toJson from 'enzyme-to-json';
import userQuery from '../userQuery.graphql';
import { TestComponent } from '../';
const mockedData = {
user: {
items: [
{
firstName: 'userF',
id: 'hhhh-gggg-iiii',
lastName: 'userL',
}
]
}
};
describe('<TestComponent />', () => {
describe('Markup should stay consistent', () => {
const component = (
<MockedProvider
mocks={[
{
request: {
query: userQuery
},
result: { data: mockedData }
},
]}
store={{
getState: () => {},
dispatch: () => {},
subscribe: () => {},
}}
>
<TestComponent />
</MockedProvider>
);
const mountedComponent = mount(component);
it('should not have any changes without a new snapshot', () => {
const tree = toJson(mountedComponent);
expect(tree).toMatchSnapshot();
});
});
});
// console.log output during lifecycle
props from component { data:
{ variables: { offset: null, limit: null },
refetch: [Function: bound ],
fetchMore: [Function: bound ],
updateQuery: [Function: bound ],
startPolling: [Function: bound ],
stopPolling: [Function: bound ],
subscribeToMore: [Function: bound ],
loading: true,
networkStatus: 1,
error: [Getter] } }
props from component { data:
{ variables: { offset: null, limit: null },
refetch: [Function: bound ],
fetchMore: [Function: bound ],
updateQuery: [Function: bound ],
startPolling: [Function: bound ],
stopPolling: [Function: bound ],
subscribeToMore: [Function: bound ],
loading: false,
networkStatus: 8,
error: [Getter] } }