1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
|
import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
import LengthValidator, { isAboveMaxLength, isBelowMinLength } from '~/validators/length_validator';
describe('length_validator', () => {
describe('isAboveMaxLength', () => {
it('should return true if the string is longer than the maximum length', () => {
expect(isAboveMaxLength('123456', '5')).toBe(true);
});
it('should return false if the string is shorter than the maximum length', () => {
expect(isAboveMaxLength('1234', '5')).toBe(false);
});
});
describe('isBelowMinLength', () => {
it('should return true if the string is shorter than the minimum length and not empty', () => {
expect(isBelowMinLength('1234', '5', 'false')).toBe(true);
});
it('should return false if the string is longer than the minimum length', () => {
expect(isBelowMinLength('123456', '5', 'false')).toBe(false);
});
it('should return false if the string is empty and allowed to be empty', () => {
expect(isBelowMinLength('', '5', 'true')).toBe(false);
});
it('should return true if the string is empty and not allowed to be empty', () => {
expect(isBelowMinLength('', '5', 'false')).toBe(true);
});
});
describe('LengthValidator', () => {
let input;
let validator;
beforeEach(() => {
setHTMLFixture(
'<div class="container"><input class="js-validate-length" /><span class="gl-field-error"></span></div>',
);
input = document.querySelector('input');
input.dataset.minLength = '3';
input.dataset.maxLength = '5';
input.dataset.minLengthMessage = 'Too short';
input.dataset.maxLengthMessage = 'Too long';
validator = new LengthValidator({ container: '.container' });
});
afterEach(() => {
resetHTMLFixture();
});
it('sets error message for input with value longer than max length', () => {
input.value = '123456';
input.dispatchEvent(new Event('input'));
expect(validator.errorMessage).toBe('Too long');
});
it('sets error message for input with value shorter than min length', () => {
input.value = '12';
input.dispatchEvent(new Event('input'));
expect(validator.errorMessage).toBe('Too short');
});
it('does not set error message for input with valid length', () => {
input.value = '123';
input.dispatchEvent(new Event('input'));
expect(validator.errorMessage).toBeNull();
});
it('does not set error message for empty input if allowEmpty is true', () => {
input.dataset.allowEmpty = 'true';
input.value = '';
input.dispatchEvent(new Event('input'));
expect(validator.errorMessage).toBeNull();
});
it('sets error message for empty input if allowEmpty is false', () => {
input.dataset.allowEmpty = 'false';
input.value = '';
input.dispatchEvent(new Event('input'));
expect(validator.errorMessage).toBe('Too short');
});
it('sets error message for empty input if allowEmpty is not defined', () => {
input.value = '';
input.dispatchEvent(new Event('input'));
expect(validator.errorMessage).toBe('Too short');
});
});
});
|