在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):MadRabbit/graphql-mock开源软件地址(OpenSource Url):https://github.com/MadRabbit/graphql-mock开源编程语言(OpenSource Language):TypeScript 100.0%开源软件介绍(OpenSource Introduction):GraphQL Client Side MockingThis is a library that helps with the apollo graphql projects testing.
Essentially it provides for
Quick Exampleimport { mount } from 'enzyme';
import { ApolloProvider } from 'react-apollo';
import { TodoList } from './components';
import { graphqlMock } from './helper';
const query = `
query Todos {
items {
id
name
}
}
`;
const render = () => mount(
<ApolloProvider client={graphqlMock.client}>
<TodoList />
</ApolloProvider>
);
describe('<TodoList />', () => {
it('renders what server returns', () => {
graphqlMock.expect(query).reply([
items: [
{ id: '1', name: 'one' },
{ id: '2', name: 'two' }
]
]);
expect(render().html()).toEqual(
'<ul><li>one</li><li>two</li></ul>'
);
});
it('responds to failures gracefuly', () => {
graphqlMock.expect(query).fail('everything is terrible');
expect(render().html()).toEqual('<div>everything is terrible</div>');
});
it('shows the loading state too', () => {
graphqlMock.expect(query).loading(true);
expect(render().html()).toEqual('<div>Loading...</div>');
});
}); Yes, it supports mutations too! Full Documentationreact-apollo-hooksTL;DR maybe just use
Firstly it uses internal memoisation for the queries, so you will need a new client with every
render/test. // use this
<ApolloProvider client={graphqlMock.client}>
// ...
</ApolloProvider>
// NOT THIS
const { client } = graphqlMock;
<ApolloProvider client={client}>
// ...
</ApolloProvider> Secondly graphqlMock.expect(mutation).reply({
createItem: { id: 1, name: 'new item' }
});
const wrapper = render();
wrapper.find('button').simulate('click');
// you need to add those two
await new Promise(r => setTimeout(r, 10));
wrapper.update();
expect(wrapper.html()).toEqual('<div id="id">new item</div>'); Copyright & LicenseAll code in this library released under the terms of the MIT license Copyright (C) 2018-2019 Nikolay Nemshilov |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论