按照这个 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] } }