@@ -602,6 +602,174 @@ it('calls mutation on submitting form ', () => {
});
```
### Testing with mocked Apollo Client
To test the logic of Apollo cache updates, we might want to mock an Apollo Client in our unit tests. To separate tests with mocked client from 'usual' unit tests, it's recommended to create an additional component factory. This way we only create Apollo Client instance when it's necessary:
```javascript
functioncreateComponent(){...}
functioncreateComponentWithApollo(){...}
```
We use [`mock-apollo-client`](https://www.npmjs.com/package/mock-apollo-client) library to mock Apollo client in tests.
```javascript
import{createMockClient}from'mock-apollo-client';
```
Then we need to inject `VueApollo` to Vue local instance (`localVue.use()` can also be called within `createComponentWithApollo()`)
```javascript
importVueApollofrom'vue-apollo';
import{createLocalVue}from'@vue/test-utils';
constlocalVue=createLocalVue();
localVue.use(VueApollo);
```
After this, on the global `describe`, we should create a variable for `fakeApollo`:
```javascript
describe('Some component with Apollo mock',()=>{
letwrapper;
letfakeApollo
})
```
Within component factory, we need to define an array of _handlers_ for every query or mutation:
When mocking resolved values, make sure the structure of the response is the same as actual API response: i.e. root property should be `data` for example
When testing queries, please keep in mind they are promises, so they need to be _resolved_ to render a result. Without resolving, we can check the `loading` state of the query: