Commit c6974623 authored by Dhiraj Bodicherla's avatar Dhiraj Bodicherla

Updated custom metrics form specs

parent 0e3ff210
...@@ -4,6 +4,8 @@ import MockAdapter from 'axios-mock-adapter'; ...@@ -4,6 +4,8 @@ import MockAdapter from 'axios-mock-adapter';
import { TEST_HOST } from 'helpers/test_constants'; import { TEST_HOST } from 'helpers/test_constants';
import CustomMetricsFormFields from 'ee/custom_metrics/components/custom_metrics_form_fields.vue'; import CustomMetricsFormFields from 'ee/custom_metrics/components/custom_metrics_form_fields.vue';
const { CancelToken } = axios;
describe('custom metrics form fields component', () => { describe('custom metrics form fields component', () => {
let component; let component;
let mockAxios; let mockAxios;
...@@ -121,9 +123,14 @@ describe('custom metrics form fields component', () => { ...@@ -121,9 +123,14 @@ describe('custom metrics form fields component', () => {
it('receives and validates a persisted value', () => { it('receives and validates a persisted value', () => {
const query = 'persistedQuery'; const query = 'persistedQuery';
const axiosPost = jest.spyOn(axios, 'post'); const axiosPost = jest.spyOn(axios, 'post');
const source = CancelToken.source();
mountComponent({ metricPersisted: true, ...makeFormData({ query }) }); mountComponent({ metricPersisted: true, ...makeFormData({ query }) });
expect(axiosPost).toHaveBeenCalledWith(validateQueryPath, { query }); expect(axiosPost).toHaveBeenCalledWith(
validateQueryPath,
{ query },
{ cancelToken: source.token },
);
expect(getNamedInput(queryInputName).value).toBe(query); expect(getNamedInput(queryInputName).value).toBe(query);
jest.runAllTimers(); jest.runAllTimers();
}); });
...@@ -144,16 +151,19 @@ describe('custom metrics form fields component', () => { ...@@ -144,16 +151,19 @@ describe('custom metrics form fields component', () => {
}); });
describe('when query validation is in flight', () => { describe('when query validation is in flight', () => {
jest.useFakeTimers();
beforeEach(() => { beforeEach(() => {
jest.useFakeTimers();
mountComponent( mountComponent(
{ metricPersisted: true, ...makeFormData({ query: 'validQuery' }) }, { metricPersisted: true, ...makeFormData({ query: 'validQuery' }) },
{ {
requestValidation: jest requestValidation: jest.fn().mockImplementation(
.fn() () =>
.mockImplementation( new Promise(resolve =>
() => new Promise(resolve => setTimeout(() => resolve(validQueryResponse))), setTimeout(() => {
), resolve(validQueryResponse);
}, 4000),
),
),
}, },
); );
}); });
...@@ -162,28 +172,45 @@ describe('custom metrics form fields component', () => { ...@@ -162,28 +172,45 @@ describe('custom metrics form fields component', () => {
jest.clearAllTimers(); jest.clearAllTimers();
}); });
it('expect queryValidateInFlight is in flight', () => { it('expect queryValidateInFlight is in flight', done => {
const queryInput = component.find(`input[name="${queryInputName}"]`); const queryInput = component.find(`input[name="${queryInputName}"]`);
queryInput.setValue('query'); queryInput.setValue('query');
queryInput.trigger('input'); queryInput.trigger('input');
component.vm.$nextTick(() => { component.vm.$nextTick(() => {
expect(component.vm.queryValidateInFlight).toBe(true); expect(component.vm.queryValidateInFlight).toBe(true);
jest.runAllTimers(); jest.runOnlyPendingTimers();
setTimeout(() => { component.vm.$nextTick(() => {
expect(component.vm.queryValidateInFlight).toBe(false); expect(component.vm.queryValidateInFlight).toBe(false);
expect(component.vm.queryIsValid).toBe(true); expect(component.vm.queryIsValid).toBe(true);
done();
}); });
}); });
}); });
it('expect loading message to display', () => { it('expect loading message to display', done => {
const queryInput = component.find(`input[name="${queryInputName}"]`); const queryInput = component.find(`input[name="${queryInputName}"]`);
queryInput.setValue('query'); queryInput.setValue('query');
queryInput.trigger('input'); queryInput.trigger('input');
component.vm.$nextTick(() => { component.vm.$nextTick(() => {
expect(component.text()).toContain('Validating query'); expect(component.text()).toContain('Validating query');
jest.runAllTimers(); jest.runOnlyPendingTimers();
done();
});
});
it('expect loading message to disappear', done => {
const queryInput = component.find(`input[name="${queryInputName}"]`);
queryInput.setValue('query');
queryInput.trigger('input');
component.vm.$nextTick(() => {
jest.runOnlyPendingTimers();
component.vm.$nextTick(() => {
expect(component.vm.queryValidateInFlight).toBe(false);
expect(component.vm.queryIsValid).toBe(true);
expect(component.vm.errorMessage).toBe('');
done();
});
}); });
}); });
}); });
......
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