Commit b1015553 authored by Paul Slaughter's avatar Paul Slaughter
parent 4f012d00
import { GlAlert, GlForm, GlModal } from '@gitlab/ui';
import { createLocalVue } from '@vue/test-utils';
import merge from 'lodash/merge';
import { createMockClient } from 'mock-apollo-client';
import VueApollo from 'vue-apollo';
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 { 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 waitForPromises from 'helpers/wait_for_promises';
......@@ -23,11 +23,8 @@ const defaultProps = {
modalProps: {},
};
const defaultRequestHandler = jest.fn().mockResolvedValue(dastSiteProfileCreate());
describe('BaseDastProfileForm', () => {
let wrapper;
let apolloProvider;
let requestHandler;
// Finders
......@@ -45,22 +42,8 @@ describe('BaseDastProfileForm', () => {
// Assertions
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) => {
apolloProvider = new VueApollo({
defaultClient: mockClientFactory(),
});
const apolloProvider = createMockApollo([[dastSiteProfileCreateMutation, requestHandler]]);
const mountOpts = merge(
{},
......@@ -75,13 +58,15 @@ describe('BaseDastProfileForm', () => {
);
wrapper = shallowMountExtended(BaseDastProfileForm, mountOpts);
return { withHandler };
};
beforeEach(() => {
// default request handler
requestHandler = jest.fn().mockResolvedValue(dastSiteProfileCreate());
});
afterEach(() => {
wrapper.destroy();
apolloProvider = null;
});
it('renders default slot', () => {
......@@ -206,11 +191,12 @@ describe('BaseDastProfileForm', () => {
const defaultErrorMessage = 'Default error message';
beforeEach(async () => {
requestHandler.mockRejectedValue(new Error('GraphQL Network Error'));
createComponent({
slots: {
'error-message': defaultErrorMessage,
},
}).withHandler(jest.fn().mockRejectedValue(new Error('GraphQL Network Error')));
});
submitForm();
await waitForPromises();
});
......@@ -229,9 +215,10 @@ describe('BaseDastProfileForm', () => {
const errors = ['error#1', 'error#2', 'error#3'];
beforeEach(async () => {
requestHandler.mockResolvedValue(dastSiteProfileCreate(errors));
createComponent({
stubs: { GlAlert },
}).withHandler(jest.fn().mockResolvedValue(dastSiteProfileCreate(errors)));
});
submitForm();
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