Commit a7a4b853 authored by Kushal Pandya's avatar Kushal Pandya

Merge branch 'ps-simplify-apollo-in-base-dast-profile-spec' into 'master'

Simplify setting up apollo provider in spec

See merge request gitlab-org/gitlab!68229
parents e7135c97 b1015553
import { GlAlert, GlForm, GlModal } from '@gitlab/ui'; import { GlAlert, GlForm, GlModal } from '@gitlab/ui';
import { createLocalVue } from '@vue/test-utils'; import { createLocalVue } from '@vue/test-utils';
import merge from 'lodash/merge'; import merge from 'lodash/merge';
import { createMockClient } from 'mock-apollo-client';
import VueApollo from 'vue-apollo'; import VueApollo from 'vue-apollo';
import BaseDastProfileForm from 'ee/security_configuration/dast_profiles/components/base_dast_profile_form.vue'; import BaseDastProfileForm from 'ee/security_configuration/dast_profiles/components/base_dast_profile_form.vue';
import dastSiteProfileCreateMutation from 'ee/security_configuration/dast_profiles/dast_site_profiles/graphql/dast_site_profile_create.mutation.graphql'; import dastSiteProfileCreateMutation from 'ee/security_configuration/dast_profiles/dast_site_profiles/graphql/dast_site_profile_create.mutation.graphql';
import { dastSiteProfileCreate } from 'ee_jest/security_configuration/dast_profiles/dast_site_profiles/mock_data/apollo_mock'; import { dastSiteProfileCreate } from 'ee_jest/security_configuration/dast_profiles/dast_site_profiles/mock_data/apollo_mock';
import createMockApollo from 'helpers/mock_apollo_helper';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper'; import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import waitForPromises from 'helpers/wait_for_promises'; import waitForPromises from 'helpers/wait_for_promises';
...@@ -23,11 +23,8 @@ const defaultProps = { ...@@ -23,11 +23,8 @@ const defaultProps = {
modalProps: {}, modalProps: {},
}; };
const defaultRequestHandler = jest.fn().mockResolvedValue(dastSiteProfileCreate());
describe('BaseDastProfileForm', () => { describe('BaseDastProfileForm', () => {
let wrapper; let wrapper;
let apolloProvider;
let requestHandler; let requestHandler;
// Finders // Finders
...@@ -45,22 +42,8 @@ describe('BaseDastProfileForm', () => { ...@@ -45,22 +42,8 @@ describe('BaseDastProfileForm', () => {
// Assertions // Assertions
const expectSubmitNotLoading = () => expect(findSubmitButton().props('loading')).toBe(false); const expectSubmitNotLoading = () => expect(findSubmitButton().props('loading')).toBe(false);
const mockClientFactory = (handler = defaultRequestHandler) => {
const mockClient = createMockClient();
mockClient.setRequestHandler(dastSiteProfileCreateMutation, handler);
requestHandler = handler;
return mockClient;
};
const withHandler = (handler) => {
apolloProvider.defaultClient = mockClientFactory(handler);
};
const createComponent = (options) => { const createComponent = (options) => {
apolloProvider = new VueApollo({ const apolloProvider = createMockApollo([[dastSiteProfileCreateMutation, requestHandler]]);
defaultClient: mockClientFactory(),
});
const mountOpts = merge( const mountOpts = merge(
{}, {},
...@@ -75,13 +58,15 @@ describe('BaseDastProfileForm', () => { ...@@ -75,13 +58,15 @@ describe('BaseDastProfileForm', () => {
); );
wrapper = shallowMountExtended(BaseDastProfileForm, mountOpts); wrapper = shallowMountExtended(BaseDastProfileForm, mountOpts);
return { withHandler };
}; };
beforeEach(() => {
// default request handler
requestHandler = jest.fn().mockResolvedValue(dastSiteProfileCreate());
});
afterEach(() => { afterEach(() => {
wrapper.destroy(); wrapper.destroy();
apolloProvider = null;
}); });
it('renders default slot', () => { it('renders default slot', () => {
...@@ -206,11 +191,12 @@ describe('BaseDastProfileForm', () => { ...@@ -206,11 +191,12 @@ describe('BaseDastProfileForm', () => {
const defaultErrorMessage = 'Default error message'; const defaultErrorMessage = 'Default error message';
beforeEach(async () => { beforeEach(async () => {
requestHandler.mockRejectedValue(new Error('GraphQL Network Error'));
createComponent({ createComponent({
slots: { slots: {
'error-message': defaultErrorMessage, 'error-message': defaultErrorMessage,
}, },
}).withHandler(jest.fn().mockRejectedValue(new Error('GraphQL Network Error'))); });
submitForm(); submitForm();
await waitForPromises(); await waitForPromises();
}); });
...@@ -229,9 +215,10 @@ describe('BaseDastProfileForm', () => { ...@@ -229,9 +215,10 @@ describe('BaseDastProfileForm', () => {
const errors = ['error#1', 'error#2', 'error#3']; const errors = ['error#1', 'error#2', 'error#3'];
beforeEach(async () => { beforeEach(async () => {
requestHandler.mockResolvedValue(dastSiteProfileCreate(errors));
createComponent({ createComponent({
stubs: { GlAlert }, stubs: { GlAlert },
}).withHandler(jest.fn().mockResolvedValue(dastSiteProfileCreate(errors))); });
submitForm(); submitForm();
await waitForPromises(); await waitForPromises();
}); });
......
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