2018-11-08 19:23:39 +05:30
|
|
|
/* eslint-disable arrow-body-style */
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
import $ from 'jquery';
|
2018-03-17 18:26:18 +05:30
|
|
|
import GlFieldErrors from '~/gl_field_errors';
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
describe('GL Style Field Errors', function() {
|
2019-07-07 11:18:12 +05:30
|
|
|
preloadFixtures('static/gl_field_errors.html');
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
beforeEach(function() {
|
2019-07-07 11:18:12 +05:30
|
|
|
loadFixtures('static/gl_field_errors.html');
|
2018-11-08 19:23:39 +05:30
|
|
|
const $form = $('form.gl-show-field-errors');
|
|
|
|
|
|
|
|
this.$form = $form;
|
2018-03-17 18:26:18 +05:30
|
|
|
this.fieldErrors = new GlFieldErrors($form);
|
|
|
|
});
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
it('should select the correct input elements', function() {
|
|
|
|
expect(this.$form).toBeDefined();
|
|
|
|
expect(this.$form.length).toBe(1);
|
|
|
|
expect(this.fieldErrors).toBeDefined();
|
2018-11-08 19:23:39 +05:30
|
|
|
const { inputs } = this.fieldErrors.state;
|
2018-12-13 13:39:08 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(inputs.length).toBe(4);
|
|
|
|
});
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
it('should ignore elements with custom error handling', function() {
|
|
|
|
const customErrorFlag = 'gl-field-error-ignore';
|
|
|
|
const customErrorElem = $(`.${customErrorFlag}`);
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(customErrorElem.length).toBe(1);
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
const customErrors = this.fieldErrors.state.inputs.filter(input => {
|
2018-03-17 18:26:18 +05:30
|
|
|
return input.inputElement.hasClass(customErrorFlag);
|
2016-11-03 12:29:30 +05:30
|
|
|
});
|
2018-12-13 13:39:08 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(customErrors.length).toBe(0);
|
|
|
|
});
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
it('should not show any errors before submit attempt', function() {
|
2018-12-13 13:39:08 +05:30
|
|
|
this.$form
|
|
|
|
.find('.email')
|
|
|
|
.val('not-a-valid-email')
|
|
|
|
.keyup();
|
|
|
|
this.$form
|
|
|
|
.find('.text-required')
|
|
|
|
.val('')
|
|
|
|
.keyup();
|
|
|
|
this.$form
|
|
|
|
.find('.alphanumberic')
|
|
|
|
.val('?---*')
|
|
|
|
.keyup();
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
const errorsShown = this.$form.find('.gl-field-error-outline');
|
2018-12-13 13:39:08 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(errorsShown.length).toBe(0);
|
|
|
|
});
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
it('should show errors when input valid is submitted', function() {
|
2018-12-13 13:39:08 +05:30
|
|
|
this.$form
|
|
|
|
.find('.email')
|
|
|
|
.val('not-a-valid-email')
|
|
|
|
.keyup();
|
|
|
|
this.$form
|
|
|
|
.find('.text-required')
|
|
|
|
.val('')
|
|
|
|
.keyup();
|
|
|
|
this.$form
|
|
|
|
.find('.alphanumberic')
|
|
|
|
.val('?---*')
|
|
|
|
.keyup();
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
this.$form.submit();
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
const errorsShown = this.$form.find('.gl-field-error-outline');
|
2018-12-13 13:39:08 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(errorsShown.length).toBe(4);
|
|
|
|
});
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
it('should properly track validity state on input after invalid submission attempt', function() {
|
|
|
|
this.$form.submit();
|
|
|
|
|
|
|
|
const emailInputModel = this.fieldErrors.state.inputs[1];
|
|
|
|
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-12-13 13:39:08 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
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-12-13 13:39:08 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
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-12-13 13:39:08 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(emailInputElement).toHaveClass('gl-field-error-outline');
|
|
|
|
expect(fieldState.empty).toBe(false);
|
|
|
|
expect(fieldState.valid).toBe(false);
|
|
|
|
|
|
|
|
// Then empty input
|
|
|
|
emailInputElement.val('').keyup();
|
2018-12-13 13:39:08 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
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-12-13 13:39:08 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(emailInputElement).not.toHaveClass('gl-field-error-outline');
|
|
|
|
expect(fieldState.empty).toBe(false);
|
|
|
|
expect(fieldState.valid).toBe(true);
|
|
|
|
});
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
it('should properly infer error messages', function() {
|
|
|
|
this.$form.submit();
|
|
|
|
const trackedInputs = this.fieldErrors.state.inputs;
|
|
|
|
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-11-03 12:29:30 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(noTitleErrorElem.text()).toBe('This field is required.');
|
|
|
|
expect(hasTitleErrorElem.text()).toBe('Please provide a valid email address.');
|
2016-11-03 12:29:30 +05:30
|
|
|
});
|
2018-03-17 18:26:18 +05:30
|
|
|
});
|