judge 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/judge/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Judge
2
- VERSION = "1.2.0"
2
+ VERSION = "1.3.0"
3
3
  end
@@ -11,61 +11,69 @@ describe('judge', function() {
11
11
  });
12
12
 
13
13
  it('validates a single element', function() {
14
- var e = document.getElementById('foo_one'),
15
- v = judge.validate(e);
16
- expect(v.length).toEqual(1);
14
+ var elements = document.getElementById('foo_one'),
15
+ results = judge.validate(elements);
16
+ expect(results.length).toEqual(1);
17
17
  });
18
18
 
19
19
  it('validates a collection of elements', function() {
20
- var e = document.querySelectorAll('input[type=radio]'),
21
- v = judge.validate(e);
22
- expect(v.length).toEqual(1);
20
+ var elements = document.querySelectorAll('input[type=radio]'),
21
+ results = judge.validate(elements);
22
+ expect(results.length).toEqual(1);
23
+ });
24
+
25
+ describe('callbacks', function() {
26
+ it('calls callback correct number of times', function() {
27
+ var elements = document.getElementsByTagName('textarea');
28
+ callback = jasmine.createSpy(),
29
+ length = elements.length;
30
+ judge.validate(elements, callback);
31
+ expect(callback.callCount).toBe(length);
32
+ });
23
33
  });
24
34
 
25
35
  });
26
36
 
27
37
  describe('judge.Watcher', function() {
28
38
 
29
- var j;
39
+ var watcher;
30
40
 
31
41
  beforeEach(function() {
32
42
  loadFixtures('spec/javascripts/fixtures/form.html');
33
- j = new judge.Watcher(document.getElementById('foo_one'));
43
+ watcher = new judge.Watcher(document.getElementById('foo_one'));
34
44
  });
35
45
 
36
46
  it('returns new instance of judge', function() {
37
- expect(typeof j).toEqual('object');
38
- expect(j.constructor).toEqual(judge.Watcher);
47
+ expect(watcher.constructor).toEqual(judge.Watcher);
39
48
  });
40
49
 
41
50
  it('associates with element', function() {
42
- expect(j.element).toEqual(document.getElementById('foo_one'));
51
+ expect(watcher.element).toEqual(document.getElementById('foo_one'));
43
52
  });
44
53
 
45
54
  it('holds validators', function() {
46
- expect(_(j.validators).isArray()).toEqual(true);
47
- expect(_(j.validators).isEmpty()).toEqual(false);
55
+ expect(_(watcher.validators).isArray()).toEqual(true);
56
+ expect(_(watcher.validators).isEmpty()).toEqual(false);
48
57
  });
49
58
 
50
59
  it('holds messages inside validators', function() {
51
- expect(_(j.validators).first().hasOwnProperty('messages')).toBe(true);
52
- expect(_(j.validators).first().messages).toBeInstanceOf(Object);
60
+ expect(_(watcher.validators).first().messages).toBeInstanceOf(Object);
53
61
  });
54
62
 
55
63
  it('has validation methods in prototype', function() {
56
- expect(j.validates()).not.toBeEmpty();
57
- expect(_(j.validates()).keys()).toContain('presence');
64
+ expect(watcher.validates()).not.toBeEmpty();
65
+ expect(_(watcher.validates()).keys()).toContain('presence');
58
66
  });
59
67
 
60
68
  it('has custom validation methods when defined by user', function() {
61
69
  judge.customValidators.phatness = function() {};
62
- expect(_(j.validates()).keys()).toContain('phatness');
70
+ expect(_(watcher.validates()).keys()).toContain('phatness');
63
71
  });
64
72
 
65
73
  it('throws error if element has no data-validate attribute', function() {
66
- var e = document.createElement('input');
67
- e.type = 'text';
68
- expect(function() { new judge.Watcher(e); }).toThrow();
74
+ var input = document.createElement('input');
75
+ input.type = 'text';
76
+ expect(function() { new judge.Watcher(input); }).toThrow();
69
77
  });
70
78
 
71
79
  it('throws error if no element is passed', function() {
@@ -73,125 +81,6 @@ describe('judge', function() {
73
81
  });
74
82
 
75
83
  });
76
-
77
- describe('judge.store', function() {
78
-
79
- var e;
80
-
81
- beforeEach(function() {
82
- loadFixtures('spec/javascripts/fixtures/form.html');
83
- judge.store.clear();
84
- e = document.getElementById('foo_one');
85
- });
86
-
87
- describe('save / get', function() {
88
-
89
- it('saves Watcher against key', function() {
90
- judge.store.save('mykey', e);
91
- expect(_(judge.store.get('mykey')).first().constructor).toEqual(judge.Watcher);
92
- expect(_(judge.store.get('mykey')).first().element).toBe(e);
93
- });
94
-
95
- it('does not save Watcher if element has already been stored against same key', function() {
96
- judge.store.save('mykey', e);
97
- judge.store.save('mykey', e);
98
- expect(judge.store.get('mykey').length).toEqual(1);
99
- });
100
-
101
- it('does save Watcher again if key is different', function() {
102
- judge.store.save('mykey', e);
103
- judge.store.save('mykey2', e);
104
- expect(judge.store.get('mykey').length).toEqual(1);
105
- expect(judge.store.get('mykey2').length).toEqual(1);
106
- });
107
-
108
- });
109
-
110
- describe('getDOM', function() {
111
-
112
- it('returns DOM elements from stored Watchers', function() {
113
- judge.store.save('mykey', e);
114
- judge.store.save('mykey', document.getElementById('foo_two_foobar'));
115
- var d = judge.store.getDOM('mykey');
116
- expect(d.length).toEqual(2);
117
- expect(Object.prototype.toString.call(d[0])).toEqual('[object HTMLSelectElement]');
118
- });
119
-
120
- it('returns store object with watchers converted to elements if no key given', function() {
121
- judge.store.save('mykey', e);
122
- judge.store.save('mykey2', document.getElementById('foo_two_foobar'));
123
- judge.store.save('mykey2', document.getElementById('foo_three'));
124
- var d = judge.store.getDOM();
125
- expect(d.mykey.length).toEqual(1);
126
- expect(d.mykey2.length).toEqual(2);
127
- expect(Object.prototype.toString.call(d.mykey[0])).toEqual('[object HTMLSelectElement]');
128
- });
129
-
130
- it('returns null if key not found', function() {
131
- expect(judge.store.getDOM('notakey')).toEqual(null);
132
- });
133
-
134
- });
135
-
136
- describe('validate', function() {
137
-
138
- it('validates all elements stored against key', function() {
139
- judge.store.save('mykey', e);
140
- var results = judge.store.validate('mykey');
141
- expect(_(results).first()).toBeInstanceOf(Object);
142
- expect(_(results).first().element).toEqual(e);
143
- });
144
-
145
- it('returns null if no elements found', function() {
146
- var results = judge.store.validate('mykey');
147
- expect(results).toBe(null);
148
- });
149
-
150
- it('returns null if key is not passed', function() {
151
- var results = judge.store.validate();
152
- expect(results).toBe(null);
153
- });
154
-
155
- });
156
-
157
- describe('remove', function() {
158
-
159
- it('removes Watcher from store', function() {
160
- judge.store.save('mykey', e);
161
- expect(_(judge.store.remove('mykey', e)).isUndefined()).toEqual(true);
162
- expect(judge.store.get('mykey')).toBe(null);
163
- });
164
-
165
- it('returns null if key not found', function() {
166
- judge.store.save('mykey', e);
167
- expect(judge.store.remove('notakey', e)).toEqual(null);
168
- });
169
-
170
- });
171
-
172
- describe('clear', function() {
173
-
174
- it('clears entire store if no key is passed', function() {
175
- judge.store.save('mykey', e);
176
- judge.store.clear();
177
- expect(judge.store.get()).toEqual({});
178
- });
179
-
180
- it('clears all Watchers against key', function() {
181
- judge.store.save('mykey', e);
182
- judge.store.save('mykey2', e);
183
- judge.store.clear('mykey');
184
- expect(judge.store.get('mykey')).toBe(null);
185
- expect(judge.store.get('mykey2').length).toEqual(1);
186
- });
187
-
188
- it('returns null if key not found', function() {
189
- expect(judge.store.clear('notakey')).toBe(null);
190
- });
191
-
192
- });
193
-
194
- });
195
84
 
196
85
  describe('judge.Watcher instance methods', function() {
197
86
 
@@ -201,179 +90,174 @@ describe('judge', function() {
201
90
 
202
91
  describe('validate method', function() {
203
92
 
204
- var j, r;
93
+ var watcher, result;
205
94
 
206
95
  beforeEach(function() {
207
- j = new judge.Watcher(document.getElementById('foo_one'));
208
- r = j.validate();
96
+ watcher = new judge.Watcher(document.getElementById('foo_one'));
97
+ result = watcher.validate();
209
98
  });
210
99
 
211
100
  it('returns element', function() {
212
- expect(r.element).toBeInstanceOf(Object);
101
+ expect(result.element).toBeInstanceOf(Object);
213
102
  });
214
103
 
215
104
  it('returns validity', function() {
216
- expect(typeof r.valid).toEqual('boolean');
105
+ expect(_.isBoolean(result.valid)).toBe(true);
217
106
  });
218
107
 
219
108
  it('returns messages', function() {
220
- expect(r.messages).toBeInstanceOf(Array);
109
+ expect(result.messages).toBeInstanceOf(Array);
110
+ });
111
+
112
+ describe('callback', function() {
113
+
114
+ var callback, args;
115
+
116
+ beforeEach(function() {
117
+ callback = jasmine.createSpy();
118
+ watcher.validate(callback);
119
+ args = callback.argsForCall[0];
120
+ });
121
+
122
+ it('is called when given', function() {
123
+ expect(callback).toHaveBeenCalled();
124
+ });
125
+ it('receives correct args', function() {
126
+ expect(_.isBoolean(args[0])).toBe(true);
127
+ expect(_.isArray(args[1])).toBe(true);
128
+ expect(_.isElement(args[2])).toBe(true);
129
+ });
130
+
221
131
  });
222
132
 
223
133
  });
224
134
 
225
135
  describe('presence', function() {
226
136
 
227
- var j;
137
+ var watcher;
228
138
 
229
139
  beforeEach(function() {
230
- j = new judge.Watcher(document.getElementById('foo_one'));
140
+ watcher = new judge.Watcher(document.getElementById('foo_one'));
231
141
  });
232
142
 
233
143
  it('invalidates empty input', function() {
234
- expect(j.validate().valid).toEqual(false);
235
- });
236
-
237
- it('returns message', function() {
238
- expect(j.validate().messages).not.toBeEmpty();
144
+ expect(watcher.validate().valid).toEqual(false);
239
145
  });
240
146
 
241
147
  it('validates non-empty input', function() {
242
- j.element.children[1].selected = true;
243
- expect(j.validate().valid).toEqual(true);
148
+ watcher.element.children[1].selected = true;
149
+ expect(watcher.validate().valid).toEqual(true);
244
150
  });
245
151
 
246
152
  });
247
153
 
248
154
  describe('length', function() {
249
155
 
250
- var j;
156
+ var watcher;
251
157
 
252
158
  beforeEach(function() {
253
- j = new judge.Watcher(document.getElementById('foo_two_foobar'));
159
+ watcher = new judge.Watcher(document.getElementById('foo_two_foobar'));
254
160
  });
255
161
 
256
162
  it('validates valid input', function() {
257
- j.element.value = 'abcdef';
258
- expect(j.validate().valid).toEqual(true);
163
+ watcher.element.value = 'abcdef';
164
+ expect(watcher.validate().valid).toEqual(true);
259
165
  });
260
166
 
261
167
  it('validates allow_blank', function() {
262
- j.element.value = '';
263
- expect(j.validate().valid).toEqual(true);
264
- });
265
-
266
- it('returns message', function() {
267
- j.element.value = 'abc';
268
- expect(j.validate().messages).not.toBeEmpty();
168
+ watcher.element.value = '';
169
+ expect(watcher.validate().valid).toEqual(true);
269
170
  });
270
171
 
271
172
  it('invalidates when value is under minimum', function() {
272
- j.element.value = 'abc';
273
- expect(j.validate().valid).toEqual(false);
173
+ watcher.element.value = 'abc';
174
+ expect(watcher.validate().valid).toEqual(false);
274
175
  });
275
176
 
276
177
  it('invalidates when value is over maximum', function() {
277
- j.element.value = 'abcdefghijkl';
278
- expect(j.validate().valid).toEqual(false);
178
+ watcher.element.value = 'abcdefghijkl';
179
+ expect(watcher.validate().valid).toEqual(false);
279
180
  });
280
-
281
- //it('invalidates when value is not equal to is', function() {
282
- // jIs.element.value = 'abc';
283
- // expect(jIs.validate().valid).toEqual(false);
284
- //});
285
181
  });
286
182
 
287
183
  describe('exclusion', function() {
288
184
 
289
- var j;
185
+ var watcher;
290
186
 
291
187
  beforeEach(function() {
292
- j = new judge.Watcher(document.getElementById('foo_three'));
188
+ watcher = new judge.Watcher(document.getElementById('foo_three'));
293
189
  });
294
190
 
295
191
  it('validates when value is not in array', function() {
296
- expect(j.validate().valid).toEqual(true);
192
+ expect(watcher.validate().valid).toEqual(true);
297
193
  });
298
194
 
299
195
  it('invalidates when value is in array', function() {
300
- j.element.children[1].selected = true;
301
- expect(j.validate().valid).toEqual(false);
302
- });
303
-
304
- it('returns message', function() {
305
- expect(j.validate().messages).not.toBeEmpty();
196
+ watcher.element.children[1].selected = true;
197
+ expect(watcher.validate().valid).toEqual(false);
306
198
  });
307
199
 
308
200
  });
309
201
 
310
202
  describe('inclusion', function() {
311
203
 
312
- var j;
204
+ var watcher;
313
205
 
314
206
  beforeEach(function() {
315
- j = new judge.Watcher(document.getElementById('foo_three_inc'));
207
+ watcher = new judge.Watcher(document.getElementById('foo_three_inc'));
316
208
  });
317
209
 
318
210
  it('validates when value is in array', function() {
319
- j.element.children[1].selected = true;
320
- expect(j.validate().valid).toEqual(true);
211
+ watcher.element.children[1].selected = true;
212
+ expect(watcher.validate().valid).toEqual(true);
321
213
  });
322
214
 
323
215
  it('invalidates when value is not in array', function() {
324
- expect(j.validate().valid).toEqual(false);
325
- });
326
-
327
- it('returns message', function() {
328
- expect(j.validate().messages).not.toBeEmpty();
216
+ expect(watcher.validate().valid).toEqual(false);
329
217
  });
330
218
 
331
219
  });
332
220
 
333
221
  describe('numericality', function() {
334
222
 
335
- var j, jEven, jGt, jLt;
223
+ var watcher, watcherEven, watcherGt, watcherLt;
336
224
 
337
225
  beforeEach(function() {
338
- j = new judge.Watcher(document.getElementById('foo_four'));
339
- jEven = new judge.Watcher(document.getElementById('foo_four_even'));
340
- jGt = new judge.Watcher(document.getElementById('foo_four_gt'));
341
- jLt = new judge.Watcher(document.getElementById('foo_four_lt'));
226
+ watcher = new judge.Watcher(document.getElementById('foo_four'));
227
+ watcherEven = new judge.Watcher(document.getElementById('foo_four_even'));
228
+ watcherGt = new judge.Watcher(document.getElementById('foo_four_gt'));
229
+ watcherLt = new judge.Watcher(document.getElementById('foo_four_lt'));
342
230
  });
343
231
 
344
232
  it('invalidates when value is not a number', function() {
345
- j.element.value = 'foo bar';
346
- expect(j.validate().valid).toEqual(false);
347
- expect(j.validate().messages).not.toBeEmpty();
233
+ watcher.element.value = 'foo bar';
234
+ expect(watcher.validate().valid).toEqual(false);
348
235
  });
349
236
 
350
237
  it('validates odd / invalidates not odd', function() {
351
- j.element.value = '2';
352
- expect(j.validate().valid).toEqual(false);
353
- expect(j.validate().messages).not.toBeEmpty();
354
- j.element.value = '1';
355
- expect(j.validate().valid).toEqual(true);
238
+ watcher.element.value = '2';
239
+ expect(watcher.validate().valid).toEqual(false);
240
+ watcher.element.value = '1';
241
+ expect(watcher.validate().valid).toEqual(true);
356
242
  });
357
243
 
358
244
  it('validates even / invalidates not even', function() {
359
- jEven.element.value = '1';
360
- expect(jEven.validate().valid).toEqual(false);
361
- expect(jEven.validate().messages).not.toBeEmpty();
362
- jEven.element.value = '2';
363
- expect(jEven.validate().valid).toEqual(true);
245
+ watcherEven.element.value = '1';
246
+ expect(watcherEven.validate().valid).toEqual(false);
247
+ watcherEven.element.value = '2';
248
+ expect(watcherEven.validate().valid).toEqual(true);
364
249
  });
365
250
 
366
251
  describe('integer', function() {
367
252
 
368
253
  it('validates int', function() {
369
- j.element.value = '1';
370
- expect(j.validate().valid).toEqual(true);
254
+ watcher.element.value = '1';
255
+ expect(watcher.validate().valid).toEqual(true);
371
256
  });
372
257
 
373
258
  it('invalidates float', function() {
374
- j.element.value = '1.1';
375
- expect(j.validate().valid).toEqual(false);
376
- expect(j.validate().messages).not.toBeEmpty();
259
+ watcher.element.value = '1.1';
260
+ expect(watcher.validate().valid).toEqual(false);
377
261
  });
378
262
 
379
263
  });
@@ -381,17 +265,15 @@ describe('judge', function() {
381
265
  describe('greater than', function() {
382
266
 
383
267
  it('invalidates not greater than', function() {
384
- jGt.element.value = '6';
385
- expect(jGt.validate().valid).toEqual(false);
386
- expect(jGt.validate().messages).not.toBeEmpty();
387
- jGt.element.value = '7';
388
- expect(jGt.validate().valid).toEqual(false);
389
- expect(jGt.validate().messages).not.toBeEmpty();
268
+ watcherGt.element.value = '6';
269
+ expect(watcherGt.validate().valid).toEqual(false);
270
+ watcherGt.element.value = '7';
271
+ expect(watcherGt.validate().valid).toEqual(false);
390
272
  });
391
273
 
392
274
  it('validates greater than', function() {
393
- jGt.element.value = '8';
394
- expect(jGt.validate().valid).toEqual(true);
275
+ watcherGt.element.value = '8';
276
+ expect(watcherGt.validate().valid).toEqual(true);
395
277
  });
396
278
 
397
279
  });
@@ -399,84 +281,55 @@ describe('judge', function() {
399
281
  describe('less than', function() {
400
282
 
401
283
  it('invalidates not less than', function() {
402
- jLt.element.value = '8';
403
- expect(jLt.validate().valid).toEqual(false);
404
- jLt.element.value = '7';
405
- expect(jLt.validate().valid).toEqual(false);
284
+ watcherLt.element.value = '8';
285
+ expect(watcherLt.validate().valid).toEqual(false);
286
+ watcherLt.element.value = '7';
287
+ expect(watcherLt.validate().valid).toEqual(false);
406
288
  });
407
289
 
408
290
  it('validates less than', function() {
409
- jLt.element.value = '6';
410
- expect(jLt.validate().valid).toEqual(true);
291
+ watcherLt.element.value = '6';
292
+ expect(watcherLt.validate().valid).toEqual(true);
411
293
  });
412
-
413
294
  });
414
-
415
- //it('validates equal to', function() {
416
- // jLt.element.value = '5';
417
- // expect(jLt.validate().valid).toEqual(false);
418
- // jLt.element.value = '6';
419
- // expect(jLt.validate().valid).toEqual(true);
420
- //});
421
-
422
- //it('validates less than or equal to', function() {
423
- // j.element.value = '5';
424
- // expect(j.validate().valid).toEqual(true);
425
- // j.element.value = '7';
426
- // expect(j.validate().valid).toEqual(true);
427
- // j.element.value = '9';
428
- // expect(j.validate().valid).toEqual(false);
429
- //});
430
-
431
- //it('validates greater than or equal to', function() {
432
- // jEven.element.value = '20';
433
- // expect(jEven.validate().valid).toEqual(true);
434
- // jEven.element.value = '2';
435
- // expect(jEven.validate().valid).toEqual(true);
436
- // jEven.element.value = '1';
437
- // expect(jEven.validate().valid).toEqual(false);
438
- //});
439
-
440
295
  });
441
296
 
442
297
  describe('format', function() {
443
298
 
444
299
  describe('with', function() {
445
300
 
446
- var j;
301
+ var watcher;
447
302
 
448
303
  beforeEach(function() {
449
- j = new judge.Watcher(document.getElementById('foo_five_wi'));
304
+ watcher = new judge.Watcher(document.getElementById('foo_five_wi'));
450
305
  });
451
306
 
452
307
  it('invalidates value matching with', function() {
453
- expect(j.validate().valid).toEqual(false);
454
- expect(j.validate().messages).not.toBeEmpty();
308
+ expect(watcher.validate().valid).toEqual(false);
455
309
  });
456
310
 
457
311
  it('invalidates value not matching with', function() {
458
- j.element.children[1].selected = true;
459
- expect(j.validate().valid).toEqual(true);
312
+ watcher.element.children[1].selected = true;
313
+ expect(watcher.validate().valid).toEqual(true);
460
314
  });
461
315
 
462
316
  });
463
317
 
464
318
  describe('without', function() {
465
319
 
466
- var j;
320
+ var watcher;
467
321
 
468
322
  beforeEach(function() {
469
- j = new judge.Watcher(document.getElementById('foo_five_wo'));
323
+ watcher = new judge.Watcher(document.getElementById('foo_five_wo'));
470
324
  });
471
325
 
472
326
  it('validates value not matching with', function() {
473
- expect(j.validate().valid).toEqual(true);
327
+ expect(watcher.validate().valid).toEqual(true);
474
328
  });
475
329
 
476
330
  it('invalidates value matching with', function() {
477
- j.element.children[1].selected = true;
478
- expect(j.validate().valid).toEqual(false);
479
- expect(j.validate().messages).not.toBeEmpty();
331
+ watcher.element.children[1].selected = true;
332
+ expect(watcher.validate().valid).toEqual(false);
480
333
  });
481
334
 
482
335
  });
@@ -485,178 +338,161 @@ describe('judge', function() {
485
338
 
486
339
  describe('acceptance', function() {
487
340
 
488
- var j;
341
+ var watcher;
489
342
 
490
343
  beforeEach(function() {
491
- j = new judge.Watcher(document.getElementById('foo_six'));
344
+ watcher = new judge.Watcher(document.getElementById('foo_six'));
492
345
  });
493
346
 
494
347
  it('validates when element is checked', function() {
495
- j.element.checked = true;
496
- expect(j.validate().valid).toEqual(true);
348
+ watcher.element.checked = true;
349
+ expect(watcher.validate().valid).toEqual(true);
497
350
  });
498
351
 
499
352
  it('invalidates when element is not checked', function() {
500
- expect(j.validate().valid).toEqual(false);
353
+ expect(watcher.validate().valid).toEqual(false);
501
354
  });
502
355
 
503
356
  });
504
357
 
505
358
  describe('confirmation', function() {
506
359
 
507
- var j, c;
360
+ var watcher, conf;
508
361
 
509
362
  beforeEach(function() {
510
- j = new judge.Watcher(document.getElementById('foo_seven'));
511
- c = document.getElementById('foo_seven_confirmation');
363
+ watcher = new judge.Watcher(document.getElementById('foo_seven'));
364
+ conf = document.getElementById('foo_seven_confirmation');
512
365
  });
513
366
 
514
367
  it('validates when confirmed', function() {
515
- j.element.value = 'password';
516
- c.value = 'password';
517
- expect(j.validate().valid).toEqual(true);
368
+ watcher.element.value = 'password';
369
+ conf.value = 'password';
370
+ expect(watcher.validate().valid).toEqual(true);
518
371
  });
519
372
 
520
373
  it('invalidates when not confirmed', function() {
521
- j.element.value = 'password';
522
- c.value = 'wrongpassword';
523
- expect(j.validate().valid).toEqual(false);
374
+ watcher.element.value = 'password';
375
+ conf.value = 'wrongpassword';
376
+ expect(watcher.validate().valid).toEqual(false);
524
377
  });
525
378
 
526
379
  });
527
380
 
528
381
  });
529
382
 
530
- describe('utils', function() {
383
+ describe('judge.store', function() {
531
384
 
532
- describe('isCollection', function() {
533
-
534
- beforeEach(function() {
535
- loadFixtures('spec/javascripts/fixtures/form.html');
385
+ var element;
386
+
387
+ beforeEach(function() {
388
+ loadFixtures('spec/javascripts/fixtures/form.html');
389
+ judge.store.clear();
390
+ element = document.getElementById('foo_one');
391
+ });
392
+
393
+ describe('save / get', function() {
394
+
395
+ it('saves Watcher against key', function() {
396
+ judge.store.save('mykey', element);
397
+ expect(_(judge.store.get('mykey')).first().constructor).toEqual(judge.Watcher);
398
+ expect(_(judge.store.get('mykey')).first().element).toBe(element);
536
399
  });
537
400
 
538
- it('returns true if judge can treat object as collection', function() {
539
- var a = [],
540
- n = document.getElementsByTagName('input');
541
- expect(judge.utils.isCollection(a)).toEqual(true);
542
- expect(judge.utils.isCollection(n)).toEqual(true);
401
+ it('does not save Watcher if element has already been stored against same key', function() {
402
+ judge.store.save('mykey', element);
403
+ judge.store.save('mykey', element);
404
+ expect(judge.store.get('mykey').length).toEqual(1);
543
405
  });
544
406
 
545
- it('returns false otherwise', function() {
546
- var o = { a:1, b:2 };
547
- expect(judge.utils.isCollection(o)).toEqual(false);
407
+ it('does save Watcher again if key is different', function() {
408
+ judge.store.save('mykey', element);
409
+ judge.store.save('mykey2', element);
410
+ expect(judge.store.get('mykey').length).toEqual(1);
411
+ expect(judge.store.get('mykey2').length).toEqual(1);
548
412
  });
549
413
 
550
414
  });
551
415
 
552
- describe('getObjectString', function() {
416
+ describe('getDOM', function() {
553
417
 
554
- it('returns type as represented in Object.prototype.toString', function() {
555
- var i = document.createElement('input'),
556
- s = document.createElement('select');
557
- expect(judge.utils.getObjectString(i)).toEqual('HTMLInputElement');
558
- expect(judge.utils.getObjectString(s)).toEqual('HTMLSelectElement');
418
+ it('returns DOM elements from stored Watchers', function() {
419
+ judge.store.save('mykey', element);
420
+ judge.store.save('mykey', document.getElementById('foo_two_foobar'));
421
+ var storedElements = judge.store.getDOM('mykey');
422
+ expect(storedElements.length).toEqual(2);
423
+ expect(Object.prototype.toString.call(storedElements[0])).toEqual('[object HTMLSelectElement]');
559
424
  });
560
425
 
561
- });
562
-
563
- describe('isInt', function() {
564
-
565
- it('returns true when int', function() {
566
- expect(judge.utils.isInt(1)).toEqual(true);
567
- expect(judge.utils.isInt(1.)).toEqual(true);
568
- expect(judge.utils.isInt(1.0)).toEqual(true);
569
- expect(judge.utils.isInt(0)).toEqual(true);
570
- expect(judge.utils.isInt(-1)).toEqual(true);
426
+ it('returns store object with watchers converted to elements if no key given', function() {
427
+ judge.store.save('mykey', element);
428
+ judge.store.save('mykey2', document.getElementById('foo_two_foobar'));
429
+ judge.store.save('mykey2', document.getElementById('foo_three'));
430
+ var storedElements = judge.store.getDOM();
431
+ expect(storedElements.mykey.length).toEqual(1);
432
+ expect(storedElements.mykey2.length).toEqual(2);
433
+ expect(Object.prototype.toString.call(storedElements.mykey[0])).toEqual('[object HTMLSelectElement]');
571
434
  });
572
435
 
573
- it('returns false when not int', function() {
574
- expect(judge.utils.isInt(1.1)).toEqual(false);
575
- expect(judge.utils.isInt(-1.1)).toEqual(false);
436
+ it('returns null if key not found', function() {
437
+ expect(judge.store.getDOM('notakey')).toEqual(null);
576
438
  });
577
439
 
578
440
  });
579
441
 
580
- describe('isFloat', function() {
581
-
582
- it('returns true when float', function() {
583
- expect(judge.utils.isFloat(1.1)).toEqual(true);
584
- expect(judge.utils.isFloat(-1.1)).toEqual(true);
585
- });
442
+ describe('validate', function() {
586
443
 
587
- it('returns false when not float', function() {
588
- expect(judge.utils.isFloat(1)).toEqual(false);
589
- expect(judge.utils.isFloat(1.)).toEqual(false);
590
- expect(judge.utils.isFloat(1.0)).toEqual(false);
591
- expect(judge.utils.isFloat(0)).toEqual(false);
592
- expect(judge.utils.isFloat(-1)).toEqual(false);
444
+ it('validates all elements stored against key', function() {
445
+ judge.store.save('mykey', element);
446
+ var results = judge.store.validate('mykey');
447
+ expect(_(results).first()).toBeInstanceOf(Object);
448
+ expect(_(results).first().element).toEqual(element);
593
449
  });
594
450
 
595
- });
596
-
597
- describe('isEven', function() {
598
-
599
- it('returns true when even', function() {
600
- expect(judge.utils.isEven(2)).toEqual(true);
601
- expect(judge.utils.isEven(0)).toEqual(true);
602
- expect(judge.utils.isEven(-2)).toEqual(true);
451
+ it('returns null if no elements found', function() {
452
+ var results = judge.store.validate('mykey');
453
+ expect(results).toBe(null);
603
454
  });
604
-
605
- it('returns false when odd', function() {
606
- expect(judge.utils.isEven(1)).toEqual(false);
607
- expect(judge.utils.isEven(-1)).toEqual(false);
455
+
456
+ it('returns null if key is not passed', function() {
457
+ var results = judge.store.validate();
458
+ expect(results).toBe(null);
608
459
  });
609
460
 
610
461
  });
611
462
 
612
- describe('isOdd', function() {
613
-
614
- it('returns true when odd', function() {
615
- expect(judge.utils.isOdd(1)).toEqual(true);
616
- expect(judge.utils.isOdd(-1)).toEqual(true);
617
- });
463
+ describe('remove', function() {
618
464
 
619
- it('returns false when even', function() {
620
- expect(judge.utils.isOdd(2)).toEqual(false);
621
- expect(judge.utils.isOdd(0)).toEqual(false);
622
- expect(judge.utils.isOdd(-2)).toEqual(false);
465
+ it('removes Watcher from store', function() {
466
+ judge.store.save('mykey', element);
467
+ expect(_(judge.store.remove('mykey', element)).isUndefined()).toEqual(true);
468
+ expect(judge.store.get('mykey')).toBe(null);
623
469
  });
624
470
 
625
- });
626
-
627
- describe('operate', function() {
628
-
629
- it('evaluates and returns true or false', function() {
630
- expect(judge.utils.operate(1, '<', 4)).toEqual(true);
631
- expect(judge.utils.operate(1, '==', 1)).toEqual(true);
632
- expect(judge.utils.operate(1, '>=', 4)).toEqual(false);
471
+ it('returns null if key not found', function() {
472
+ judge.store.save('mykey', element);
473
+ expect(judge.store.remove('notakey', element)).toEqual(null);
633
474
  });
634
475
 
635
476
  });
636
477
 
637
- describe('convertRegExp', function() {
478
+ describe('clear', function() {
638
479
 
639
- it('converts string format options-first ruby regexp into RegExp object', function() {
640
- var re = judge.utils.convertRegExp('(?mix:[A-Z0-9]\.)');
641
- expect(re).toBeInstanceOf(RegExp);
642
- expect(re.source).toEqual('[A-Z0-9]\.');
643
- expect(re.multiline).toEqual(true);
644
- expect(re.global).toEqual(false);
480
+ it('clears entire store if no key is passed', function() {
481
+ judge.store.save('mykey', element);
482
+ judge.store.clear();
483
+ expect(judge.store.get()).toEqual({});
645
484
  });
646
485
 
647
- });
648
-
649
- describe('convertFlags', function() {
650
-
651
- it('returns m if present in options string without negation', function() {
652
- expect(judge.utils.convertFlags('mix')).toEqual('m');
653
- expect(judge.utils.convertFlags('m-ix')).toEqual('m');
654
- expect(judge.utils.convertFlags('mx-i')).toEqual('m');
486
+ it('clears all Watchers against key', function() {
487
+ judge.store.save('mykey', element);
488
+ judge.store.save('mykey2', element);
489
+ judge.store.clear('mykey');
490
+ expect(judge.store.get('mykey')).toBe(null);
491
+ expect(judge.store.get('mykey2').length).toEqual(1);
655
492
  });
656
493
 
657
- it('returns empty string otherwise', function() {
658
- expect(judge.utils.convertFlags('ix-m')).toEqual('');
659
- expect(judge.utils.convertFlags('x-mi')).toEqual('');
494
+ it('returns null if key not found', function() {
495
+ expect(judge.store.clear('notakey')).toBe(null);
660
496
  });
661
497
 
662
498
  });