BigW Consortium Gitlab

gl_field_errors_spec.js 4.16 KB
Newer Older
1
/* eslint-disable space-before-function-paren, arrow-body-style */
2

3
import '~/gl_field_errors';
4 5

((global) => {
6
  preloadFixtures('static/gl_field_errors.html.raw');
7 8 9

  describe('GL Style Field Errors', function() {
    beforeEach(function() {
10
      loadFixtures('static/gl_field_errors.html.raw');
11
      const $form = this.$form = $('form.gl-show-field-errors');
12 13 14
      this.fieldErrors = new global.GlFieldErrors($form);
    });

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

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

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

      const customErrors = this.fieldErrors.state.inputs.filter((input) => {
30
        return input.inputElement.hasClass(customErrorFlag);
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
      });
      expect(customErrors.length).toBe(0);
    });

    it('should not show any errors before submit attempt', function() {
      this.$form.find('.email').val('not-a-valid-email').keyup();
      this.$form.find('.text-required').val('').keyup();
      this.$form.find('.alphanumberic').val('?---*').keyup();

      const errorsShown = this.$form.find('.gl-field-error-outline');
      expect(errorsShown.length).toBe(0);
    });

    it('should show errors when input valid is submitted', function() {
      this.$form.find('.email').val('not-a-valid-email').keyup();
      this.$form.find('.text-required').val('').keyup();
      this.$form.find('.alphanumberic').val('?---*').keyup();

      this.$form.submit();

      const errorsShown = this.$form.find('.gl-field-error-outline');
      expect(errorsShown.length).toBe(4);
    });

    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();
      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();
      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();
      expect(emailInputElement).toHaveClass('gl-field-error-outline');
      expect(fieldState.empty).toBe(false);
      expect(fieldState.valid).toBe(false);

      // Then empty input
      emailInputElement.val('').keyup();
      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();
      expect(emailInputElement).not.toHaveClass('gl-field-error-outline');
      expect(fieldState.empty).toBe(false);
      expect(fieldState.valid).toBe(true);
    });

    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');

      expect(noTitleErrorElem.text()).toBe('This field is required.');
      expect(hasTitleErrorElem.text()).toBe('Please provide a valid email address.');
    });
  });
})(window.gl || (window.gl = {}));