2018-03-09 15:18:59 -05:00
|
|
|
import $ from 'jquery';
|
2022-05-13 08:08:49 -04:00
|
|
|
import { loadHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
|
2017-10-10 04:09:01 -04:00
|
|
|
import GlFieldErrors from '~/gl_field_errors';
|
2016-09-28 07:01:03 -04:00
|
|
|
|
2019-12-20 10:07:34 -05:00
|
|
|
describe('GL Style Field Errors', () => {
|
|
|
|
let testContext;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
testContext = {};
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2022-05-13 08:08:49 -04:00
|
|
|
loadHTMLFixture('static/gl_field_errors.html');
|
2018-06-14 02:18:05 -04:00
|
|
|
const $form = $('form.gl-show-field-errors');
|
|
|
|
|
2019-12-20 10:07:34 -05:00
|
|
|
testContext.$form = $form;
|
|
|
|
testContext.fieldErrors = new GlFieldErrors($form);
|
2017-10-10 04:09:01 -04:00
|
|
|
});
|
2016-09-28 07:01:03 -04:00
|
|
|
|
2022-05-13 08:08:49 -04:00
|
|
|
afterEach(() => {
|
|
|
|
resetHTMLFixture();
|
|
|
|
});
|
|
|
|
|
2019-12-20 10:07:34 -05:00
|
|
|
it('should select the correct input elements', () => {
|
|
|
|
expect(testContext.$form).toBeDefined();
|
|
|
|
expect(testContext.$form.length).toBe(1);
|
|
|
|
expect(testContext.fieldErrors).toBeDefined();
|
|
|
|
const { inputs } = testContext.fieldErrors.state;
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2017-10-10 04:09:01 -04:00
|
|
|
expect(inputs.length).toBe(4);
|
|
|
|
});
|
2016-09-28 07:01:03 -04:00
|
|
|
|
2019-12-20 10:07:34 -05:00
|
|
|
it('should ignore elements with custom error handling', () => {
|
2017-10-10 04:09:01 -04:00
|
|
|
const customErrorFlag = 'gl-field-error-ignore';
|
|
|
|
const customErrorElem = $(`.${customErrorFlag}`);
|
2016-09-28 07:01:03 -04:00
|
|
|
|
2017-10-10 04:09:01 -04:00
|
|
|
expect(customErrorElem.length).toBe(1);
|
2016-09-28 07:01:03 -04:00
|
|
|
|
2020-12-23 16:10:24 -05:00
|
|
|
const customErrors = testContext.fieldErrors.state.inputs.filter((input) => {
|
2017-10-10 04:09:01 -04:00
|
|
|
return input.inputElement.hasClass(customErrorFlag);
|
2016-09-28 07:01:03 -04:00
|
|
|
});
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2017-10-10 04:09:01 -04:00
|
|
|
expect(customErrors.length).toBe(0);
|
|
|
|
});
|
2016-09-28 07:01:03 -04:00
|
|
|
|
2019-12-20 10:07:34 -05:00
|
|
|
it('should not show any errors before submit attempt', () => {
|
2020-12-23 07:10:26 -05:00
|
|
|
testContext.$form.find('.email').val('not-a-valid-email').keyup();
|
|
|
|
testContext.$form.find('.text-required').val('').keyup();
|
|
|
|
testContext.$form.find('.alphanumberic').val('?---*').keyup();
|
2016-09-28 07:01:03 -04:00
|
|
|
|
2019-12-20 10:07:34 -05:00
|
|
|
const errorsShown = testContext.$form.find('.gl-field-error-outline');
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2017-10-10 04:09:01 -04:00
|
|
|
expect(errorsShown.length).toBe(0);
|
|
|
|
});
|
2016-09-28 07:01:03 -04:00
|
|
|
|
2019-12-20 10:07:34 -05:00
|
|
|
it('should show errors when input valid is submitted', () => {
|
2020-12-23 07:10:26 -05:00
|
|
|
testContext.$form.find('.email').val('not-a-valid-email').keyup();
|
|
|
|
testContext.$form.find('.text-required').val('').keyup();
|
|
|
|
testContext.$form.find('.alphanumberic').val('?---*').keyup();
|
2016-09-28 07:01:03 -04:00
|
|
|
|
2019-12-20 10:07:34 -05:00
|
|
|
testContext.$form.submit();
|
2016-09-28 07:01:03 -04:00
|
|
|
|
2019-12-20 10:07:34 -05:00
|
|
|
const errorsShown = testContext.$form.find('.gl-field-error-outline');
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2017-10-10 04:09:01 -04:00
|
|
|
expect(errorsShown.length).toBe(4);
|
|
|
|
});
|
2016-09-28 07:01:03 -04:00
|
|
|
|
2019-12-20 10:07:34 -05:00
|
|
|
it('should properly track validity state on input after invalid submission attempt', () => {
|
|
|
|
testContext.$form.submit();
|
2017-10-10 04:09:01 -04:00
|
|
|
|
2019-12-20 10:07:34 -05:00
|
|
|
const emailInputModel = testContext.fieldErrors.state.inputs[1];
|
2017-10-10 04:09:01 -04:00
|
|
|
const fieldState = emailInputModel.state;
|
|
|
|
const emailInputElement = emailInputModel.inputElement;
|
|
|
|
|
|
|
|
// No input
|
|
|
|
expect(emailInputElement).toHaveClass('gl-field-error-outline');
|
|
|
|
expect(fieldState.empty).toBe(true);
|
|
|
|
expect(fieldState.valid).toBe(false);
|
|
|
|
|
|
|
|
// Then invalid input
|
|
|
|
emailInputElement.val('not-a-valid-email').keyup();
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2017-10-10 04:09:01 -04:00
|
|
|
expect(emailInputElement).toHaveClass('gl-field-error-outline');
|
|
|
|
expect(fieldState.empty).toBe(false);
|
|
|
|
expect(fieldState.valid).toBe(false);
|
|
|
|
|
|
|
|
// Then valid input
|
|
|
|
emailInputElement.val('email@gitlab.com').keyup();
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2017-10-10 04:09:01 -04:00
|
|
|
expect(emailInputElement).not.toHaveClass('gl-field-error-outline');
|
|
|
|
expect(fieldState.empty).toBe(false);
|
|
|
|
expect(fieldState.valid).toBe(true);
|
|
|
|
|
|
|
|
// Then invalid input
|
|
|
|
emailInputElement.val('not-a-valid-email').keyup();
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2017-10-10 04:09:01 -04:00
|
|
|
expect(emailInputElement).toHaveClass('gl-field-error-outline');
|
|
|
|
expect(fieldState.empty).toBe(false);
|
|
|
|
expect(fieldState.valid).toBe(false);
|
|
|
|
|
|
|
|
// Then empty input
|
|
|
|
emailInputElement.val('').keyup();
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2017-10-10 04:09:01 -04:00
|
|
|
expect(emailInputElement).toHaveClass('gl-field-error-outline');
|
|
|
|
expect(fieldState.empty).toBe(true);
|
|
|
|
expect(fieldState.valid).toBe(false);
|
|
|
|
|
|
|
|
// Then valid input
|
|
|
|
emailInputElement.val('email@gitlab.com').keyup();
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2017-10-10 04:09:01 -04:00
|
|
|
expect(emailInputElement).not.toHaveClass('gl-field-error-outline');
|
|
|
|
expect(fieldState.empty).toBe(false);
|
|
|
|
expect(fieldState.valid).toBe(true);
|
|
|
|
});
|
2016-09-28 07:01:03 -04:00
|
|
|
|
2019-12-20 10:07:34 -05:00
|
|
|
it('should properly infer error messages', () => {
|
|
|
|
testContext.$form.submit();
|
|
|
|
const trackedInputs = testContext.fieldErrors.state.inputs;
|
2017-10-10 04:09:01 -04:00
|
|
|
const inputHasTitle = trackedInputs[1];
|
|
|
|
const hasTitleErrorElem = inputHasTitle.inputElement.siblings('.gl-field-error');
|
|
|
|
const inputNoTitle = trackedInputs[2];
|
|
|
|
const noTitleErrorElem = inputNoTitle.inputElement.siblings('.gl-field-error');
|
2016-09-28 07:01:03 -04:00
|
|
|
|
2017-10-10 04:09:01 -04:00
|
|
|
expect(noTitleErrorElem.text()).toBe('This field is required.');
|
|
|
|
expect(hasTitleErrorElem.text()).toBe('Please provide a valid email address.');
|
2016-09-28 07:01:03 -04:00
|
|
|
});
|
2017-10-10 04:09:01 -04:00
|
|
|
});
|