gl_field_errors_spec.js 4.09 KB
Newer Older
1
/* eslint-disable arrow-body-style */
2

3
import $ from 'jquery';
4
import GlFieldErrors from '~/gl_field_errors';
5

6
describe('GL Style Field Errors', function() {
7
  preloadFixtures('static/gl_field_errors.html.raw');
8

9
  beforeEach(function() {
10
    loadFixtures('static/gl_field_errors.html.raw');
11 12 13
    const $form = $('form.gl-show-field-errors');

    this.$form = $form;
14 15
    this.fieldErrors = new GlFieldErrors($form);
  });
16

17 18 19 20
  it('should select the correct input elements', function() {
    expect(this.$form).toBeDefined();
    expect(this.$form.length).toBe(1);
    expect(this.fieldErrors).toBeDefined();
21
    const { inputs } = this.fieldErrors.state;
22

23 24
    expect(inputs.length).toBe(4);
  });
25

26 27 28
  it('should ignore elements with custom error handling', function() {
    const customErrorFlag = 'gl-field-error-ignore';
    const customErrorElem = $(`.${customErrorFlag}`);
29

30
    expect(customErrorElem.length).toBe(1);
31

Mike Greiling's avatar
Mike Greiling committed
32
    const customErrors = this.fieldErrors.state.inputs.filter(input => {
33
      return input.inputElement.hasClass(customErrorFlag);
34
    });
35

36 37
    expect(customErrors.length).toBe(0);
  });
38

39
  it('should not show any errors before submit attempt', function() {
Mike Greiling's avatar
Mike Greiling committed
40 41 42 43 44 45 46 47 48 49 50 51
    this.$form
      .find('.email')
      .val('not-a-valid-email')
      .keyup();
    this.$form
      .find('.text-required')
      .val('')
      .keyup();
    this.$form
      .find('.alphanumberic')
      .val('?---*')
      .keyup();
52

53
    const errorsShown = this.$form.find('.gl-field-error-outline');
54

55 56
    expect(errorsShown.length).toBe(0);
  });
57

58
  it('should show errors when input valid is submitted', function() {
Mike Greiling's avatar
Mike Greiling committed
59 60 61 62 63 64 65 66 67 68 69 70
    this.$form
      .find('.email')
      .val('not-a-valid-email')
      .keyup();
    this.$form
      .find('.text-required')
      .val('')
      .keyup();
    this.$form
      .find('.alphanumberic')
      .val('?---*')
      .keyup();
71

72
    this.$form.submit();
73

74
    const errorsShown = this.$form.find('.gl-field-error-outline');
75

76 77
    expect(errorsShown.length).toBe(4);
  });
78

79 80 81 82 83 84 85 86 87 88 89 90 91 92
  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();
93

94 95 96 97 98 99
    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();
100

101 102 103 104 105 106
    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();
107

108 109 110 111 112 113
    expect(emailInputElement).toHaveClass('gl-field-error-outline');
    expect(fieldState.empty).toBe(false);
    expect(fieldState.valid).toBe(false);

    // Then empty input
    emailInputElement.val('').keyup();
114

115 116 117 118 119 120
    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();
121

122 123 124 125
    expect(emailInputElement).not.toHaveClass('gl-field-error-outline');
    expect(fieldState.empty).toBe(false);
    expect(fieldState.valid).toBe(true);
  });
126

127 128 129 130 131 132 133
  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');
134

135 136
    expect(noTitleErrorElem.text()).toBe('This field is required.');
    expect(hasTitleErrorElem.text()).toBe('Please provide a valid email address.');
137
  });
138
});