Commit d1d09af7 authored by NataliaTepluhina's avatar NataliaTepluhina Committed by Denys Mishunov

Fixed fakeApollo to accept resolvers

parent 4dbb018e
...@@ -2,14 +2,14 @@ import { InMemoryCache } from 'apollo-cache-inmemory'; ...@@ -2,14 +2,14 @@ import { InMemoryCache } from 'apollo-cache-inmemory';
import { createMockClient } from 'mock-apollo-client'; import { createMockClient } from 'mock-apollo-client';
import VueApollo from 'vue-apollo'; import VueApollo from 'vue-apollo';
export default (handlers = []) => { export default (handlers = [], resolvers = {}) => {
const fragmentMatcher = { match: () => true }; const fragmentMatcher = { match: () => true };
const cache = new InMemoryCache({ const cache = new InMemoryCache({
fragmentMatcher, fragmentMatcher,
addTypename: false, addTypename: false,
}); });
const mockClient = createMockClient({ cache }); const mockClient = createMockClient({ cache, resolvers });
if (Array.isArray(handlers)) { if (Array.isArray(handlers)) {
handlers.forEach(([query, value]) => mockClient.setRequestHandler(query, value)); handlers.forEach(([query, value]) => mockClient.setRequestHandler(query, value));
......
import { ApolloMutation } from 'vue-apollo'; import VueApollo, { ApolloMutation } from 'vue-apollo';
import { GlLoadingIcon } from '@gitlab/ui'; import { GlLoadingIcon } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils'; import { shallowMount, createLocalVue } from '@vue/test-utils';
import waitForPromises from 'helpers/wait_for_promises'; import waitForPromises from 'helpers/wait_for_promises';
import createMockApollo from 'jest/helpers/mock_apollo_helper';
import GetSnippetQuery from 'shared_queries/snippet/snippet.query.graphql';
import { deprecatedCreateFlash as Flash } from '~/flash'; import { deprecatedCreateFlash as Flash } from '~/flash';
import * as urlUtils from '~/lib/utils/url_utility'; import * as urlUtils from '~/lib/utils/url_utility';
import SnippetEditApp from '~/snippets/components/edit.vue'; import SnippetEditApp from '~/snippets/components/edit.vue';
...@@ -13,6 +15,7 @@ import FormFooterActions from '~/vue_shared/components/form/form_footer_actions. ...@@ -13,6 +15,7 @@ import FormFooterActions from '~/vue_shared/components/form/form_footer_actions.
import { SNIPPET_VISIBILITY_PRIVATE } from '~/snippets/constants'; import { SNIPPET_VISIBILITY_PRIVATE } from '~/snippets/constants';
import UpdateSnippetMutation from '~/snippets/mutations/updateSnippet.mutation.graphql'; import UpdateSnippetMutation from '~/snippets/mutations/updateSnippet.mutation.graphql';
import CreateSnippetMutation from '~/snippets/mutations/createSnippet.mutation.graphql'; import CreateSnippetMutation from '~/snippets/mutations/createSnippet.mutation.graphql';
import defaultVisibilityQuery from '~/snippets/queries/snippet_visibility.query.graphql';
import { testEntries } from '../test_utils'; import { testEntries } from '../test_utils';
jest.mock('~/flash'); jest.mock('~/flash');
...@@ -47,8 +50,10 @@ const createTestSnippet = () => ({ ...@@ -47,8 +50,10 @@ const createTestSnippet = () => ({
describe('Snippet Edit app', () => { describe('Snippet Edit app', () => {
let wrapper; let wrapper;
let fakeApollo;
const relativeUrlRoot = '/foo/'; const relativeUrlRoot = '/foo/';
const originalRelativeUrlRoot = gon.relative_url_root; const originalRelativeUrlRoot = gon.relative_url_root;
const GetSnippetQuerySpy = jest.fn().mockResolvedValue(createTestSnippet());
const mutationTypes = { const mutationTypes = {
RESOLVE: jest.fn().mockResolvedValue({ RESOLVE: jest.fn().mockResolvedValue({
...@@ -78,12 +83,9 @@ describe('Snippet Edit app', () => { ...@@ -78,12 +83,9 @@ describe('Snippet Edit app', () => {
props = {}, props = {},
loading = false, loading = false,
mutationRes = mutationTypes.RESOLVE, mutationRes = mutationTypes.RESOLVE,
withApollo = false,
} = {}) { } = {}) {
if (wrapper) { let componentData = {
throw new Error('wrapper already exists');
}
wrapper = shallowMount(SnippetEditApp, {
mocks: { mocks: {
$apollo: { $apollo: {
queries: { queries: {
...@@ -92,6 +94,36 @@ describe('Snippet Edit app', () => { ...@@ -92,6 +94,36 @@ describe('Snippet Edit app', () => {
mutate: mutationRes, mutate: mutationRes,
}, },
}, },
};
if (wrapper) {
throw new Error('wrapper already exists');
}
if (withApollo) {
const localVue = createLocalVue();
localVue.use(VueApollo);
const requestHandlers = [
[GetSnippetQuery, GetSnippetQuerySpy],
[
defaultVisibilityQuery,
jest.fn().mockResolvedValue({
visibilityLevels: expect.anything(),
selectedLevel: SNIPPET_VISIBILITY_PRIVATE,
multipleLevelsRestricted: expect.anything(),
}),
],
];
fakeApollo = createMockApollo(requestHandlers);
componentData = {
localVue,
apolloProvider: fakeApollo,
};
}
wrapper = shallowMount(SnippetEditApp, {
...componentData,
stubs: { stubs: {
ApolloMutation, ApolloMutation,
FormFooterActions, FormFooterActions,
...@@ -152,16 +184,13 @@ describe('Snippet Edit app', () => { ...@@ -152,16 +184,13 @@ describe('Snippet Edit app', () => {
if (nodes.length) { if (nodes.length) {
wrapper.setData({ wrapper.setData({
snippet: nodes[0], snippet: nodes[0],
newSnippet: false,
});
} else {
wrapper.setData({
newSnippet: true,
}); });
} }
wrapper.vm.onSnippetFetch({
data: {
snippets: {
nodes,
},
},
});
}; };
describe('rendering', () => { describe('rendering', () => {
...@@ -183,6 +212,15 @@ describe('Snippet Edit app', () => { ...@@ -183,6 +212,15 @@ describe('Snippet Edit app', () => {
}, },
); );
it('does not make the query to fetch snippet on create', async () => {
createComponent({ props: { snippetGid: '' }, withApollo: true });
jest.runOnlyPendingTimers();
await wrapper.vm.$nextTick();
expect(GetSnippetQuerySpy).not.toHaveBeenCalled();
});
it.each` it.each`
title | actions | shouldDisable title | actions | shouldDisable
${''} | ${[]} | ${true} ${''} | ${[]} | ${true}
......
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