Skip to content

Commit b07d212

Browse files
committed
Refactored data-disable test suite
1 parent 69fe896 commit b07d212

File tree

1 file changed

+59
-91
lines changed

1 file changed

+59
-91
lines changed

test/public/test/data-disable.js

Lines changed: 59 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -24,107 +24,107 @@ module('data-disable', {
2424
}
2525
});
2626

27+
function getVal(el) {
28+
return el.is('input,textarea,select') ? el.val() : el.text();
29+
}
30+
31+
function disabled(el) {
32+
return el.is('input,textarea,select,button') ? el.is(':disabled') : el.data('ujs:enable-with');
33+
}
34+
35+
function checkEnabledState(el, text) {
36+
ok(!disabled(el), el.get(0).tagName + ' should not be disabled');
37+
equal(getVal(el), text, el.get(0).tagName + ' text should be original value');
38+
}
39+
40+
function checkDisabledState(el, text) {
41+
ok(disabled(el), el.get(0).tagName + ' should be disabled');
42+
equal(getVal(el), text, el.get(0).tagName + ' text should be disabled value');
43+
}
44+
2745
asyncTest('form input field with "data-disable-with" attribute', 7, function() {
2846
var form = $('form[data-remote]'), input = form.find('input[type=text]');
2947

30-
function checkOriginalState() {
31-
ok(!input.is(':disabled'), 'input field should not be disabled');
32-
equal(input.val(), 'john', 'input field should have value given to it');
33-
}
34-
checkOriginalState();
48+
checkEnabledState(input, 'john');
3549

3650
form.bind('ajax:success', function(e, data) {
3751
setTimeout(function() {
38-
checkOriginalState();
52+
checkEnabledState(input, 'john');
3953
equal(data.params.user_name, 'john');
4054
start();
4155
}, 13)
4256
})
4357
form.trigger('submit');
4458

45-
ok(input.is(':disabled'), 'input field should be disabled');
46-
equal(input.val(), 'processing ...', 'input field should have disabled value given to it');
59+
checkDisabledState(input, 'processing ...');
4760
});
4861

4962
asyncTest('form button with "data-disable-with" attribute', 6, function() {
5063
var form = $('form[data-remote]'), button = $('<button data-disable-with="submitting ..." name="submit2">Submit</button>');
5164
form.append(button);
5265

53-
function checkOriginalState() {
54-
ok(!button.is(':disabled'), 'button should not be disabled');
55-
equal(button.text(), 'Submit', 'button should have original value');
56-
}
57-
checkOriginalState();
66+
checkEnabledState(button, 'Submit');
5867

5968
form.bind('ajax:success', function(e, data) {
6069
setTimeout(function() {
61-
checkOriginalState();
70+
checkEnabledState(button, 'Submit');
6271
start();
6372
}, 13)
6473
})
6574
form.trigger('submit');
6675

67-
ok(button.is(':disabled'), 'button should be disabled');
68-
equal(button.text(), 'submitting ...', 'button should have disabled value');
76+
checkDisabledState(button, 'submitting ...');
6977
});
7078

71-
asyncTest('form submit button with "data-disable-with" attribute', 6, function(){
79+
asyncTest('form input[type=submit][data-disable-with] disables', 6, function(){
7280
var form = $('form:not([data-remote])'), input = form.find('input[type=submit]');
7381

74-
ok(!input.is(':disabled'), 'input field should not be disabled');
75-
equal(input.val(), 'Submit', 'input field should have value given to it');
76-
77-
function checkDisabledState() {
78-
ok(input.is(':disabled'), 'input field should be disabled');
79-
equal(input.val(), 'submitting ...');
80-
}
82+
checkEnabledState(input, 'Submit');
8183

8284
// WEEIRDD: attaching this handler makes the test work in IE7
8385
form.bind('iframe:loading', function(e, form) {});
8486

8587
form.bind('iframe:loaded', function(e, data) {
8688
setTimeout(function() {
87-
checkDisabledState();
89+
checkDisabledState(input, 'submitting ...');
8890
start();
8991
}, 30);
9092
}).trigger('submit');
9193

92-
setTimeout(checkDisabledState, 30);
94+
setTimeout(function() {
95+
checkDisabledState(input, 'submitting ...');
96+
}, 30);
9397
});
9498

95-
asyncTest('form with input[type=submit][data-disable-with] is replaced in ajax callback', 2, function(){
99+
asyncTest('form[data-remote] input[type=submit][data-disable-with] is replaced in ajax callback', 2, function(){
96100
var form = $('form:not([data-remote])').attr('data-remote', 'true'), origFormContents = form.html();
97101

98102
form.bind('ajax:success', function(){
99103
form.html(origFormContents);
100104

101105
setTimeout(function(){
102106
var input = form.find('input[type=submit]');
103-
ok(!input.is(':disabled'), 'new input field should not be disabled');
104-
equal(input.val(), 'Submit', 'new input field should not have value replaced by "enable" function');
105-
107+
checkEnabledState(input, 'Submit');
106108
start();
107109
}, 30);
108110
}).trigger('submit');
109111
});
110112

111-
asyncTest('form with input[data-disable-with] is replaced with disabled field in ajax callback', 2, function(){
113+
asyncTest('form[data-remote] input[data-disable-with] is replaced with disabled field in ajax callback', 2, function(){
112114
var form = $('form:not([data-remote])').attr('data-remote', 'true'), input = form.find('input[type=submit]'),
113115
newDisabledInput = input.clone().attr('disabled', 'disabled');
114116

115117
form.bind('ajax:success', function(){
116118
input.replaceWith(newDisabledInput);
117119

118120
setTimeout(function(){
119-
ok(!newDisabledInput.is(':disabled'), 'new input field should not be disabled');
120-
equal(newDisabledInput.val(), 'Submit', 'new input field should not have value replaced if "ujs:enable-with" is blank');
121-
121+
checkEnabledState(newDisabledInput, 'Submit');
122122
start();
123123
}, 30);
124124
}).trigger('submit');
125125
});
126126

127-
asyncTest('form textarea with "data-disable-with" attribute', 3, function() {
127+
asyncTest('form[data-remote] textarea[data-disable-with] attribute', 3, function() {
128128
var form = $('form[data-remote]'),
129129
textarea = $('<textarea data-disable-with="processing ..." name="user_bio">born, lived, died.</textarea>').appendTo(form);
130130

@@ -136,99 +136,67 @@ asyncTest('form textarea with "data-disable-with" attribute', 3, function() {
136136
})
137137
form.trigger('submit');
138138

139-
ok(textarea.is(':disabled'), 'textarea should be disabled');
140-
equal(textarea.val(), 'processing ...', 'textarea should have disabled value given to it');
139+
checkDisabledState(textarea, 'processing ...');
141140
});
142141

143-
asyncTest('link with "data-disable-with" attribute disables', 4, function() {
142+
asyncTest('a[data-disable-with] disables', 4, function() {
144143
var link = $('a[data-disable-with]');
145144

146-
ok(!link.data('ujs:enable-with'), 'link should not be disabled');
147-
equal(link.html(), 'Click me', 'link should have value given to it');
148-
149-
function checkDisabledLink() {
150-
ok(link.data('ujs:enable-with'), 'link should be disabled');
151-
equal(link.html(), 'clicking...');
152-
}
145+
checkEnabledState(link, 'Click me');
153146

154147
link.trigger('click');
155-
checkDisabledLink();
148+
checkDisabledState(link, 'clicking...');
156149
start();
157150
});
158151

159-
asyncTest('remote link with "data-disable-with" attribute disables and re-enables', 6, function() {
152+
asyncTest('a[data-remote][data-disable-with] disables and re-enables', 6, function() {
160153
var link = $('a[data-disable-with]').attr('data-remote', true);
161154

162-
function checkEnabledLink() {
163-
ok(!link.data('ujs:enable-with'), 'link should not be disabled');
164-
equal(link.html(), 'Click me', 'link should have value given to it');
165-
}
166-
checkEnabledLink();
167-
168-
function checkDisabledLink() {
169-
ok(link.data('ujs:enable-with'), 'link should be disabled');
170-
equal(link.html(), 'clicking...');
171-
}
155+
checkEnabledState(link, 'Click me');
172156

173157
link
174158
.bind('ajax:beforeSend', function() {
175-
checkDisabledLink();
159+
checkDisabledState(link, 'clicking...');
176160
})
177161
.live('ajax:complete', function() {
178-
checkEnabledLink();
162+
checkEnabledState(link, 'Click me');
179163
start();
180164
})
181165
.trigger('click');
182166
});
183167

184-
asyncTest('remote link with "data-disable-with" attribute disables and re-enables when ajax:before event is cancelled', 6, function() {
168+
asyncTest('a[data-remote][data-disable-with] re-enables when `ajax:before` event is cancelled', 6, function() {
185169
var link = $('a[data-disable-with]').attr('data-remote', true);
186170

187-
function checkEnabledLink() {
188-
ok(!link.data('ujs:enable-with'), 'link should not be disabled');
189-
equal(link.html(), 'Click me', 'link should have value given to it');
190-
}
191-
checkEnabledLink();
192-
193-
function checkDisabledLink() {
194-
ok(link.data('ujs:enable-with'), 'link should be disabled');
195-
equal(link.html(), 'clicking...');
196-
}
171+
checkEnabledState(link, 'Click me');
197172

198173
link
199174
.bind('ajax:before', function() {
200-
checkDisabledLink();
175+
checkDisabledState(link, 'clicking...');
201176
return false;
202177
})
203178
.trigger('click');
204-
setTimeout(function() {
205-
checkEnabledLink();
206-
start();
207-
}, 30);
179+
180+
setTimeout(function() {
181+
checkEnabledState(link, 'Click me');
182+
start();
183+
}, 30);
208184
});
209185

210-
asyncTest('remote link with "data-disable-with" attribute disables and re-enables when ajax:beforeSend event is cancelled', 6, function() {
186+
asyncTest('a[data-remote][data-disable-with] re-enables when `ajax:beforeSend` event is cancelled', 6, function() {
211187
var link = $('a[data-disable-with]').attr('data-remote', true);
212188

213-
function checkEnabledLink() {
214-
ok(!link.data('ujs:enable-with'), 'link should not be disabled');
215-
equal(link.html(), 'Click me', 'link should have value given to it');
216-
}
217-
checkEnabledLink();
218-
219-
function checkDisabledLink() {
220-
ok(link.data('ujs:enable-with'), 'link should be disabled');
221-
equal(link.html(), 'clicking...');
222-
}
189+
checkEnabledState(link, 'Click me');
223190

224191
link
225192
.bind('ajax:beforeSend', function() {
226-
checkDisabledLink();
193+
checkDisabledState(link, 'clicking...');
227194
return false;
228195
})
229196
.trigger('click');
230-
setTimeout(function() {
231-
checkEnabledLink();
232-
start();
233-
}, 30);
197+
198+
setTimeout(function() {
199+
checkEnabledState(link, 'Click me');
200+
start();
201+
}, 30);
234202
});

0 commit comments

Comments
 (0)