Commit a32000a3 authored by Natalia Tepluhina's avatar Natalia Tepluhina Committed by Illya Klymov

Abstract mocking Apollo client to helper method

parent 7e0e3508
import { shallowMount, createLocalVue } from '@vue/test-utils'; import { shallowMount, createLocalVue } from '@vue/test-utils';
import { createMockClient } from 'mock-apollo-client';
import VueApollo from 'vue-apollo'; import VueApollo from 'vue-apollo';
import VueRouter from 'vue-router'; import VueRouter from 'vue-router';
import VueDraggable from 'vuedraggable'; import VueDraggable from 'vuedraggable';
import { InMemoryCache } from 'apollo-cache-inmemory';
import Design from '~/design_management/components/list/item.vue'; import Design from '~/design_management/components/list/item.vue';
import createRouter from '~/design_management/router'; import createRouter from '~/design_management/router';
import getDesignListQuery from '~/design_management/graphql/queries/get_design_list.query.graphql'; import getDesignListQuery from '~/design_management/graphql/queries/get_design_list.query.graphql';
import permissionsQuery from '~/design_management/graphql/queries/design_permissions.query.graphql'; import permissionsQuery from '~/design_management/graphql/queries/design_permissions.query.graphql';
import moveDesignMutation from '~/design_management/graphql/mutations/move_design.mutation.graphql'; import moveDesignMutation from '~/design_management/graphql/mutations/move_design.mutation.graphql';
import { deprecatedCreateFlash as createFlash } from '~/flash'; import { deprecatedCreateFlash as createFlash } from '~/flash';
import createMockApollo from '../../helpers/mock_apollo_helper';
import Index from '~/design_management/pages/index.vue'; import Index from '~/design_management/pages/index.vue';
import { import {
designListQueryResponse, designListQueryResponse,
...@@ -39,8 +38,7 @@ const designToMove = { ...@@ -39,8 +38,7 @@ const designToMove = {
describe('Design management index page with Apollo mock', () => { describe('Design management index page with Apollo mock', () => {
let wrapper; let wrapper;
let mockClient; let fakeApollo;
let apolloProvider;
let moveDesignHandler; let moveDesignHandler;
async function moveDesigns(localWrapper) { async function moveDesigns(localWrapper) {
...@@ -56,41 +54,23 @@ describe('Design management index page with Apollo mock', () => { ...@@ -56,41 +54,23 @@ describe('Design management index page with Apollo mock', () => {
}); });
} }
const fragmentMatcher = { match: () => true };
const cache = new InMemoryCache({
fragmentMatcher,
addTypename: false,
});
const findDesigns = () => wrapper.findAll(Design); const findDesigns = () => wrapper.findAll(Design);
function createComponent({ function createComponent({
moveHandler = jest.fn().mockResolvedValue(moveDesignMutationResponse), moveHandler = jest.fn().mockResolvedValue(moveDesignMutationResponse),
}) { }) {
mockClient = createMockClient({ cache });
mockClient.setRequestHandler(
getDesignListQuery,
jest.fn().mockResolvedValue(designListQueryResponse),
);
mockClient.setRequestHandler(
permissionsQuery,
jest.fn().mockResolvedValue(permissionsQueryResponse),
);
moveDesignHandler = moveHandler; moveDesignHandler = moveHandler;
mockClient.setRequestHandler(moveDesignMutation, moveDesignHandler); const requestHandlers = [
[getDesignListQuery, jest.fn().mockResolvedValue(designListQueryResponse)],
apolloProvider = new VueApollo({ [permissionsQuery, jest.fn().mockResolvedValue(permissionsQueryResponse)],
defaultClient: mockClient, [moveDesignMutation, moveDesignHandler],
}); ];
fakeApollo = createMockApollo(requestHandlers);
wrapper = shallowMount(Index, { wrapper = shallowMount(Index, {
localVue, localVue,
apolloProvider, apolloProvider: fakeApollo,
router, router,
stubs: { VueDraggable }, stubs: { VueDraggable },
}); });
...@@ -99,8 +79,6 @@ describe('Design management index page with Apollo mock', () => { ...@@ -99,8 +79,6 @@ describe('Design management index page with Apollo mock', () => {
afterEach(() => { afterEach(() => {
wrapper.destroy(); wrapper.destroy();
wrapper = null; wrapper = null;
mockClient = null;
apolloProvider = null;
}); });
it('has a design with id 1 as a first one', async () => { it('has a design with id 1 as a first one', async () => {
...@@ -152,8 +130,9 @@ describe('Design management index page with Apollo mock', () => { ...@@ -152,8 +130,9 @@ describe('Design management index page with Apollo mock', () => {
await moveDesigns(wrapper); await moveDesigns(wrapper);
await jest.runOnlyPendingTimers(); await wrapper.vm.$nextTick(); // kick off the DOM update
await wrapper.vm.$nextTick(); await jest.runOnlyPendingTimers(); // kick off the mocked GQL stuff (promises)
await wrapper.vm.$nextTick(); // kick off the DOM update for flash
expect(createFlash).toHaveBeenCalledWith( expect(createFlash).toHaveBeenCalledWith(
'Something went wrong when reordering designs. Please try again', 'Something went wrong when reordering designs. Please try again',
......
import { InMemoryCache } from 'apollo-cache-inmemory';
import { createMockClient } from 'mock-apollo-client';
import VueApollo from 'vue-apollo';
export default (handlers = []) => {
const fragmentMatcher = { match: () => true };
const cache = new InMemoryCache({
fragmentMatcher,
addTypename: false,
});
const mockClient = createMockClient({ cache });
if (Array.isArray(handlers)) {
handlers.forEach(([query, value]) => mockClient.setRequestHandler(query, value));
} else {
throw new Error('You should pass an array of handlers to mock Apollo client');
}
const apolloProvider = new VueApollo({ defaultClient: mockClient });
return apolloProvider;
};
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment