Я получаю сообщение об ошибке при тестировании относительно Apollo GraphQL:
Ожидается проанализированный документ GraphQL.Возможно, вам нужно обернуть строку запроса в тег «gql»?
Это тест, который у меня есть:
import React from 'react';
import { Provider as ReduxProvider } from 'react-redux';
import { MockedProvider as ApolloProvider } from 'react-apollo/test-utils';
import { mount } from 'enzyme';
import { CouriersSelect } from '../../CouriersSelect';
import GetAllCouriers from '../../containers/GetAllCouriers';
import store from '../../../../redux/store';
import mocks from '../__fixtures__/couriers-mock';
jest.mock('react-i18next', () => ({
// this mock makes sure any components using the translate HoC receive the t function as a prop
translate: () => Component => {
Component.defaultProps = { ...Component.defaultProps, t: key => key }; // eslint-disable-line
return Component;
},
}));
describe('CouriersSelect test', () => {
let props;
beforeEach(() => {
props = {
t: k => k,
handleCouriers: jest.fn(),
};
});
it('correctly renders CouriersSelect component', () => {
const wrapper = mount(
<ApolloProvider mocks={mocks}>
<ReduxProvider store={store}>
<GetAllCouriers>
<CouriersSelect {...props} />
</GetAllCouriers>
</ReduxProvider>
</ApolloProvider>,
);
console.log(wrapper.debug());
expect(wrapper.find('SelectSkeleton')).toHaveLength(1);
});
});
И это компонент, который я тестирую:
import { Select, SelectItem } from 'carbon-components-react';
import React from 'react';
import { translate } from 'react-i18next';
import PropTypes from 'prop-types';
import GetAllCouriers from './containers/GetAllCouriers';
export const CouriersSelect = ({ t, handleCouriers }) => (
<GetAllCouriers>
{({ data }) => (
<Select
id="select-2"
onChange={handleCouriers}
>
{data.map(carriers => (
<SelectItem
key={carriers.name}
value={carriers.name}
text={carriers.name}
/>
))}
</Select>
)}
</GetAllCouriers>
);
CouriersSelect.propTypes = {
t: PropTypes.func.isRequired,
handleCouriers: PropTypes.func.isRequired,
};
export default translate()(CouriersSelect);
Так чего же мне не хватать?
РЕДАКТИРОВАТЬ:
Код Аполлона:
Запрос:
export const GET_ALL_COURIERS = gql`
query GetAllCouriers {
GetAllCouriers {
id
url
keyName
name
}
}
`;
Звоните:
import React from 'react';
import PropTypes from 'prop-types';
import { Query } from 'react-apollo';
import { connect } from 'react-redux';
import { compose } from 'redux';
import { translate } from 'react-i18next';
import { SelectSkeleton } from 'carbon-components-react';
import { GET_ALL_COURIERS } from './GraphQLQuery';
import ErrorState from '../../../global/components/ErrorState';
const GetAllCouriers = ({ t, children, softlayerAccountId }) => (
<Query
query={GET_ALL_COURIERS}
context={{ uri: `/graphql?ims_account=${softlayerAccountId}` }}
>
{({ loading, error, data, refetch }) => {
let getAllCouriers;
if (error) {
return (
<div className="empty-state-table">
<ErrorState description={t('shipments.errorMessage')} />
</div>
);
}
if (loading) return <SelectSkeleton />;
if (data && data.GetAllCouriers) {
getAllCouriers = data.GetAllCouriers;
console.log(JSON.stringify(data, null, 2));
}
return children({
data: getAllCouriers,
error,
loading,
refetch,
softlayerAccountId,
});
}}
</Query>
);
GetAllCouriers.propTypes = {
t: PropTypes.func.isRequired,
children: PropTypes.func.isRequired,
softlayerAccountId: PropTypes.string.isRequired,
};
export default compose(
connect(store => ({
softlayerAccountId: store.global.softlayerAccountId,
})),
translate(),
)(GetAllCouriers);