forked from stevenmills/bootstrapvalidator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoFocus.js
90 lines (80 loc) · 3.08 KB
/
autoFocus.js
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
describe('autoFocus', function() {
beforeEach(function() {
$([
'<form class="form-horizontal" id="autoFocusForm">',
'<div class="form-group">',
'<input type="text" name="username" required />',
'</div>',
'<div class="form-group">',
'<input type="text" name="email" required data-bv-emailaddress />',
'</div>',
'<div class="form-group">',
'<button type="submit" id="submitButton">Submit</button>',
'</div>',
'</form>'
].join('')).appendTo('body');
this.bv = $('#autoFocusForm')
.bootstrapValidator()
.submit(function(e) {
e.preventDefault();
})
.data('bootstrapValidator');
this.$username = this.bv.getFieldElements('username');
this.$email = this.bv.getFieldElements('email');
});
afterEach(function() {
$('#autoFocusForm').bootstrapValidator('destroy').remove();
});
it('default option (autoFocus=true)', function() {
$('#submitButton').click();
expect(this.$username.is(':focus')).toBeTruthy();
expect($(document.activeElement).attr('name')).toEqual('username');
this.bv.resetForm();
this.$username.val('user_name');
this.$email.val('');
$('#submitButton').click();
expect(this.$email.is(':focus')).toBeTruthy();
expect($(document.activeElement).attr('name')).toEqual('email');
});
it('set autoFocus=false for form', function() {
$('#autoFocusForm')
.bootstrapValidator('destroy')
.bootstrapValidator({
autoFocus: false
});
this.$username.val('');
this.$email.val('invalid#email');
$('#submitButton').click();
expect(this.$username.is(':focus')).toBeFalsy();
expect(this.$email.is(':focus')).toBeFalsy();
});
it('set autoFocus=false for all fields', function() {
this.bv
.addField('username', {
autoFocus: false
})
.addField('email', {
autoFocus: false
});
this.$username.val('user_name');
this.$email.val('invalid#email');
$('#submitButton').click();
expect(this.$username.is(':focus')).toBeFalsy();
expect(this.$email.is(':focus')).toBeFalsy();
});
it('set different autoFocus value for fields', function() {
this.bv
.addField('username', {
autoFocus: false
})
.addField('email', {
autoFocus: true
});
this.$username.val('');
this.$email.val('invalid_email');
$('#submitButton').click();
expect(this.$username.is(':focus')).toBeFalsy();
expect(this.$email.is(':focus')).toBeTruthy();
expect($(document.activeElement).attr('name')).toEqual('email');
});
});