source: branches/2.4/prototype/plugins/jq-raty/test/spec.js @ 6754

Revision 6754, 85.3 KB checked in by niltonneto, 12 years ago (diff)

Ticket #0000 - Copiadas as alterações do Trunk. Versão final da 2.4.1.

  • Property svn:executable set to *
Line 
1describe('Using ID', function() {
2
3        beforeEach(function() {
4                $('body').append('<div id="star"></div>');
5        });
6
7        afterEach(function() {
8                $('#star').remove();
9        });
10
11        it ('should chainable', function() {
12                // given
13                var star                = $('#star'),
14                        className       = 'my-class';
15
16                // when
17                star.raty().addClass(className);
18
19                // then
20            expect(star).toHaveClass(className);
21        });
22
23        it ('should change the path', function() {
24                // given
25                var star = $('#star');
26
27                // when
28                star.raty({ path: '../img' });
29
30                var imgs = star.children('img');
31
32                // then
33            expect(imgs.eq(0)).toHaveAttr('src', '../img/star-off.png');
34            expect(imgs.eq(1)).toHaveAttr('src', '../img/star-off.png');
35            expect(imgs.eq(2)).toHaveAttr('src', '../img/star-off.png');
36            expect(imgs.eq(3)).toHaveAttr('src', '../img/star-off.png');
37            expect(imgs.eq(4)).toHaveAttr('src', '../img/star-off.png');
38        });
39
40        it ('should create the default stars', function() {
41                // given
42                var star = $('#star');
43
44                // when
45                star.raty();
46
47                var imgs = star.children('img');
48
49                // then
50            expect(imgs.eq(0)).toHaveAttr('title', 'bad');
51            expect(imgs.eq(1)).toHaveAttr('title', 'poor');
52            expect(imgs.eq(2)).toHaveAttr('title', 'regular');
53            expect(imgs.eq(3)).toHaveAttr('title', 'good');
54            expect(imgs.eq(4)).toHaveAttr('title', 'gorgeous');
55
56            expect(imgs.eq(0)).toHaveAttr('src', 'img/star-off.png');
57            expect(imgs.eq(1)).toHaveAttr('src', 'img/star-off.png');
58            expect(imgs.eq(2)).toHaveAttr('src', 'img/star-off.png');
59            expect(imgs.eq(3)).toHaveAttr('src', 'img/star-off.png');
60            expect(imgs.eq(4)).toHaveAttr('src', 'img/star-off.png');
61
62            expect(star.children('input').val()).toEqual('');
63        });
64
65        it ('should create 7 stars', function() {
66                // given
67                var star = $('#star');
68
69                // when
70                star.raty({ number: 7 });
71
72                var imgs = star.children('img');
73
74                // then
75            expect(imgs.eq(0)).toHaveAttr('title', 'bad');
76            expect(imgs.eq(1)).toHaveAttr('title', 'poor');
77            expect(imgs.eq(2)).toHaveAttr('title', 'regular');
78            expect(imgs.eq(3)).toHaveAttr('title', 'good');
79            expect(imgs.eq(4)).toHaveAttr('title', 'gorgeous');
80            expect(imgs.eq(5)).toHaveAttr('title', '6');
81            expect(imgs.eq(6)).toHaveAttr('title', '7');
82
83            expect(imgs.eq(0)).toHaveAttr('src', 'img/star-off.png');
84            expect(imgs.eq(1)).toHaveAttr('src', 'img/star-off.png');
85            expect(imgs.eq(2)).toHaveAttr('src', 'img/star-off.png');
86            expect(imgs.eq(3)).toHaveAttr('src', 'img/star-off.png');
87            expect(imgs.eq(4)).toHaveAttr('src', 'img/star-off.png');
88            expect(imgs.eq(5)).toHaveAttr('src', 'img/star-off.png');
89            expect(imgs.eq(6)).toHaveAttr('src', 'img/star-off.png');
90        });
91
92        it ('should create the score field', function() {
93                // given
94                var star = $('#star');
95
96                // when
97                star.raty();
98
99                var $score = star.children('input');
100
101                // then
102            expect($score).toHaveAttr('type', 'hidden');
103            expect($score).toHaveAttr('name', 'score');
104            expect($score).toHaveValue(0);
105        });
106
107        it ('should create a custom score name field', function() {
108                // given
109                var star = $('#star');
110
111                // when
112                star.raty({ scoreName: 'entity.score' });
113
114                var $score = star.children('input');
115
116                // then
117                expect($score).toHaveAttr('type', 'hidden');
118                expect($score).toHaveAttr('name', 'entity.score');
119                expect($score).toHaveValue(0);
120        });
121
122        it ('should start readOnly with not rated yet message', function() {
123                // given
124                var star = $('#star');
125
126                // when
127                star.raty({ readOnly: true });
128
129                var imgs = star.children('img');
130
131                // then
132            expect(imgs.eq(0)).toHaveAttr('title', 'not rated yet');
133            expect(imgs.eq(1)).toHaveAttr('title', 'not rated yet');
134            expect(imgs.eq(2)).toHaveAttr('title', 'not rated yet');
135            expect(imgs.eq(3)).toHaveAttr('title', 'not rated yet');
136            expect(imgs.eq(4)).toHaveAttr('title', 'not rated yet');
137        });
138
139        it ('should be started with 3 stars', function() {
140                // given
141                var star = $('#star');
142
143                // when
144                star.raty({ score: 3 });
145
146                var imgs = star.children('img');
147
148                // then
149                expect(imgs.eq(0)).toHaveAttr('src', 'img/star-on.png');
150                expect(imgs.eq(1)).toHaveAttr('src', 'img/star-on.png');
151                expect(imgs.eq(2)).toHaveAttr('src', 'img/star-on.png');
152                expect(imgs.eq(3)).toHaveAttr('src', 'img/star-off.png');
153                expect(imgs.eq(4)).toHaveAttr('src', 'img/star-off.png');
154        });
155
156        it ('should be started with 3 stars and readOnly', function() {
157                // given
158                var star = $('#star');
159
160                // when
161                star.raty({ readOnly: true, score: 3});
162
163                var imgs = star.children('img');
164
165                // then
166                expect(imgs.eq(0)).toHaveAttr('title', 'regular');
167            expect(imgs.eq(1)).toHaveAttr('title', 'regular');
168            expect(imgs.eq(2)).toHaveAttr('title', 'regular');
169            expect(imgs.eq(3)).toHaveAttr('title', 'regular');
170            expect(imgs.eq(4)).toHaveAttr('title', 'regular');
171
172                expect(imgs.eq(0)).toHaveAttr('src', 'img/star-on.png');
173                expect(imgs.eq(1)).toHaveAttr('src', 'img/star-on.png');
174                expect(imgs.eq(2)).toHaveAttr('src', 'img/star-on.png');
175                expect(imgs.eq(3)).toHaveAttr('src', 'img/star-off.png');
176                expect(imgs.eq(4)).toHaveAttr('src', 'img/star-off.png');
177        });
178
179        it ('should be hovered 2 stars', function() {
180                // given
181                var star = $('#star');
182
183                // when
184                star.raty();
185
186                var imgs = star.children('img');
187
188                imgs.eq(1).mouseover();
189
190                // then
191                expect(imgs.eq(0)).toHaveAttr('src', 'img/star-on.png');
192                expect(imgs.eq(1)).toHaveAttr('src', 'img/star-on.png');
193                expect(imgs.eq(2)).toHaveAttr('src', 'img/star-off.png');
194                expect(imgs.eq(3)).toHaveAttr('src', 'img/star-off.png');
195                expect(imgs.eq(4)).toHaveAttr('src', 'img/star-off.png');
196        });
197
198        it ('should be clicked 2 stars', function() {
199                // given
200                var star = $('#star');
201
202                // when
203                star.raty();
204
205                var imgs = star.children('img');
206
207                imgs.eq(1).mouseover().click().mouseleave();
208
209                // then
210                expect(imgs.eq(0)).toHaveAttr('src', 'img/star-on.png');
211                expect(imgs.eq(1)).toHaveAttr('src', 'img/star-on.png');
212                expect(imgs.eq(2)).toHaveAttr('src', 'img/star-off.png');
213                expect(imgs.eq(3)).toHaveAttr('src', 'img/star-off.png');
214                expect(imgs.eq(4)).toHaveAttr('src', 'img/star-off.png');
215
216                expect(star.children('input')).toHaveValue(2);
217        });
218
219        it ('should not be hovered with readOnly', function() {
220                // given
221                var star = $('#star');
222
223                // when
224                star.raty({ readOnly: true });
225
226                var imgs = star.children('img');
227
228                imgs.eq(1).mouseover();
229
230                // then
231                expect(imgs.eq(0)).toHaveAttr('src', 'img/star-off.png');
232                expect(imgs.eq(1)).toHaveAttr('src', 'img/star-off.png');
233                expect(imgs.eq(2)).toHaveAttr('src', 'img/star-off.png');
234                expect(imgs.eq(3)).toHaveAttr('src', 'img/star-off.png');
235                expect(imgs.eq(4)).toHaveAttr('src', 'img/star-off.png');
236        });
237
238        it ('should not be clicked with readOnly', function() {
239                // given
240                var star = $('#star');
241
242                // when
243                star.raty({ readOnly: true });
244
245                var imgs = star.children('img');
246
247                imgs.eq(1).mouseover().click().mouseleave();
248
249                // then
250                expect(imgs.eq(0)).toHaveAttr('src', 'img/star-off.png');
251                expect(imgs.eq(1)).toHaveAttr('src', 'img/star-off.png');
252                expect(imgs.eq(2)).toHaveAttr('src', 'img/star-off.png');
253                expect(imgs.eq(3)).toHaveAttr('src', 'img/star-off.png');
254                expect(imgs.eq(4)).toHaveAttr('src', 'img/star-off.png');
255
256                expect(star.children('input').val()).toEqual('');
257        });
258
259        it ('should execute click callback', function() {
260                // given
261                var star = $('#star');
262
263                // when
264                star.raty({
265                        click: function(score, evt) {
266                                $(this).attr('title', score);
267                        }
268                });
269
270                star.children('img').eq(3).mouseover().click().mouseleave();
271
272                // then
273                expect(star).toHaveAttr('title', 4);
274        });
275
276        it ('should have this as self element', function() {
277                // given
278                var star = $('#star');
279
280                // when
281                star.raty({
282                        click: function(score, evt) {
283                                $(this).attr('title', score);
284                        }
285                });
286
287                star.children('img').eq(3).mouseover().click().mouseleave();
288
289                // then
290                expect(star).toHaveAttr('title', 4);
291        });
292
293        it ('should create default cancel button', function() {
294                // given
295                var star = $('#star');
296
297                // when
298                star.raty({ cancel: true });
299
300                var cancel = star.children('img:first');
301               
302                // then
303                expect(cancel).toHaveClass('raty-cancel');
304                expect(cancel).toHaveAttr('title', 'cancel this rating!');
305                expect(cancel).toHaveAttr('alt', 'x');
306                expect(cancel).toHaveAttr('src', 'img/cancel-off.png');
307        });
308
309        it ('should change cancel off button', function() {
310                // given
311                var star = $('#star');
312
313                // when
314                star.raty({ cancel: true, cancelOff: 'new-cancel-off.png' });
315
316                var cancel = star.children('img:first');
317
318                // then
319                expect(cancel).toHaveClass('raty-cancel');
320                expect(cancel).toHaveAttr('title', 'cancel this rating!');
321                expect(cancel).toHaveAttr('alt', 'x');
322                expect(cancel).toHaveAttr('src', 'img/new-cancel-off.png');
323        });
324
325        it ('should change cancel on button', function() {
326                // given
327                var star = $('#star');
328
329                // when
330                star.raty({ cancel: true, cancelOn: 'new-cancel-on.png' });
331
332                var cancel = star.children('img:first').mouseenter();
333
334                // then
335                expect(cancel).toHaveClass('raty-cancel');
336                expect(cancel).toHaveAttr('title', 'cancel this rating!');
337                expect(cancel).toHaveAttr('alt', 'x');
338                expect(cancel).toHaveAttr('src', 'img/new-cancel-on.png');
339        });
340
341        it ('should change cancel hint', function() {
342                // given
343                var star = $('#star');
344
345                // when
346                star.raty({ cancel: true, cancelHint: 'my-hint' });
347
348                var cancel = star.children('img:first').mouseenter();
349
350                // then
351                expect(cancel).toHaveAttr('title', 'my-hint');
352        });
353
354        it ('should turn off the stars on mouseover on cancel button', function() {
355                // given
356                var star        = $('#star').raty({ score: 3, cancel: true }),
357                        imgs    = star.children('img');
358               
359                // when
360                star.children('img:first').mouseenter();
361
362                // then
363                expect(imgs.eq(1)).toHaveAttr('src', 'img/star-off.png');
364                expect(imgs.eq(2)).toHaveAttr('src', 'img/star-off.png');
365                expect(imgs.eq(3)).toHaveAttr('src', 'img/star-off.png');
366                expect(imgs.eq(4)).toHaveAttr('src', 'img/star-off.png');
367                expect(imgs.eq(5)).toHaveAttr('src', 'img/star-off.png');
368        });
369
370        it ('should display noRatedMsg when readOnly', function() {
371                // given
372                var star = $('#star');
373
374                // when
375                star.raty({ readOnly: true });
376
377                // then
378                var imgs = star.children('img');
379
380                expect(imgs.eq(0)).toHaveAttr('title', 'not rated yet');
381                expect(imgs.eq(1)).toHaveAttr('title', 'not rated yet');
382                expect(imgs.eq(2)).toHaveAttr('title', 'not rated yet');
383                expect(imgs.eq(3)).toHaveAttr('title', 'not rated yet');
384                expect(imgs.eq(4)).toHaveAttr('title', 'not rated yet');
385        });
386
387        it ('should cancel the rating', function() {
388                // given
389                var star        = $('#star').raty({ score: 5, cancel: true }),
390                        imgs    = star.children('img');
391
392                // when
393                star.children('img:first').mouseover().click().mouseleave();
394
395                // then
396                expect(imgs.eq(1)).toHaveAttr('src', 'img/star-off.png');
397                expect(imgs.eq(2)).toHaveAttr('src', 'img/star-off.png');
398                expect(imgs.eq(3)).toHaveAttr('src', 'img/star-off.png');
399                expect(imgs.eq(4)).toHaveAttr('src', 'img/star-off.png');
400                expect(imgs.eq(5)).toHaveAttr('src', 'img/star-off.png');
401        });
402
403        it ('should create right cancel button', function() {
404                // given
405                var star = $('#star');
406
407                // when
408                star.raty({ cancel: true, cancelPlace: 'right' });
409
410                var cancel = star.children('img:last');
411
412                // then
413                expect(cancel).toHaveClass('raty-cancel');
414                expect(cancel).toHaveAttr('title', 'cancel this rating!');
415                expect(cancel).toHaveAttr('alt', 'x');
416                expect(cancel).toHaveAttr('src', 'img/cancel-off.png');
417        });
418
419        it ('should set hint for cancel button', function() {
420                // given
421                var star        = $('#star'),
422                        hint    = 'remove my rating!';
423
424                // when
425                star.raty({ cancel: true, cancelHint: hint });
426
427                var cancel = star.children('img:first');
428
429                // then
430                expect(cancel).toHaveClass('raty-cancel');
431                expect(cancel).toHaveAttr('title', hint);
432                expect(cancel).toHaveAttr('alt', 'x');
433                expect(cancel).toHaveAttr('src', 'img/cancel-off.png');
434        });
435
436        it ('should execute cancel click callback', function() {
437                // given
438                var star = $('#star').raty({ cancel: true, click: function(score, evt) { $(this).attr('title', (score === null)); } });
439
440                // when
441                star.children('img:first').mouseover().click().mouseleave();
442
443                // then
444                expect(star).toHaveAttr('title', 'true');
445                expect(star.children('input').val()).toEqual('');
446        });
447
448        it ('should round down on max limit down with no half and halfShow', function() {
449                // given
450                var star = $('#star');
451
452                // when
453                star.raty({ half: false, halfShow: false, score: 1.5 });
454
455                var imgs = star.children('img');
456
457                // then
458                expect(imgs.eq(0)).toHaveAttr('src', 'img/star-on.png');
459            expect(imgs.eq(1)).toHaveAttr('src', 'img/star-off.png');
460            expect(imgs.eq(2)).toHaveAttr('src', 'img/star-off.png');
461            expect(imgs.eq(3)).toHaveAttr('src', 'img/star-off.png');
462            expect(imgs.eq(4)).toHaveAttr('src', 'img/star-off.png');
463        });
464
465        it ('should round up on min limit up with no half and halfShow', function() {
466                // given
467                var star = $('#star');
468
469                // when
470                star.raty({ half: false, halfShow: false, score: 1.6 });
471
472                var imgs = star.children('img');
473
474                // then
475                expect(imgs.eq(0)).toHaveAttr('src', 'img/star-on.png');
476            expect(imgs.eq(1)).toHaveAttr('src', 'img/star-on.png');
477            expect(imgs.eq(2)).toHaveAttr('src', 'img/star-off.png');
478            expect(imgs.eq(3)).toHaveAttr('src', 'img/star-off.png');
479            expect(imgs.eq(4)).toHaveAttr('src', 'img/star-off.png');
480        });
481
482        it ('should round down on max limit down with half and halfShow', function() {
483                // given
484                var star = $('#star');
485               
486                // when
487                star.raty({ half: true, halfShow: true, score: 1.25 });
488               
489                var imgs = star.children('img');
490               
491                // then
492                expect(imgs.eq(0)).toHaveAttr('src', 'img/star-on.png');
493                expect(imgs.eq(1)).toHaveAttr('src', 'img/star-off.png');
494                expect(imgs.eq(2)).toHaveAttr('src', 'img/star-off.png');
495                expect(imgs.eq(3)).toHaveAttr('src', 'img/star-off.png');
496                expect(imgs.eq(4)).toHaveAttr('src', 'img/star-off.png');
497        });
498
499        it ('should show half on min limit half with half and halfShow', function() {
500                // given
501                var star = $('#star');
502               
503                // when
504                star.raty({ half: true, halfShow: true, score: 1.26 });
505               
506                var imgs = star.children('img');
507               
508                // then
509                expect(imgs.eq(0)).toHaveAttr('src', 'img/star-on.png');
510                expect(imgs.eq(1)).toHaveAttr('src', 'img/star-half.png');
511                expect(imgs.eq(2)).toHaveAttr('src', 'img/star-off.png');
512                expect(imgs.eq(3)).toHaveAttr('src', 'img/star-off.png');
513                expect(imgs.eq(4)).toHaveAttr('src', 'img/star-off.png');
514        });
515
516        it ('should show half on max limit half with half and halfShow', function() {
517                // given
518                var star = $('#star');
519               
520                // when
521                star.raty({ half: true, halfShow: true, score: 1.75 });
522               
523                var imgs = star.children('img');
524               
525                // then
526                expect(imgs.eq(0)).toHaveAttr('src', 'img/star-on.png');
527                expect(imgs.eq(1)).toHaveAttr('src', 'img/star-half.png');
528                expect(imgs.eq(2)).toHaveAttr('src', 'img/star-off.png');
529                expect(imgs.eq(3)).toHaveAttr('src', 'img/star-off.png');
530                expect(imgs.eq(4)).toHaveAttr('src', 'img/star-off.png');
531        });
532
533        it ('should show round up on min limit up with half and halfShow', function() {
534                // given
535                var star = $('#star');
536               
537                // when
538                star.raty({ half: true, halfShow: true, score: 1.76 });
539               
540                var imgs = star.children('img');
541               
542                // then
543                expect(imgs.eq(0)).toHaveAttr('src', 'img/star-on.png');
544                expect(imgs.eq(1)).toHaveAttr('src', 'img/star-on.png');
545                expect(imgs.eq(2)).toHaveAttr('src', 'img/star-off.png');
546                expect(imgs.eq(3)).toHaveAttr('src', 'img/star-off.png');
547                expect(imgs.eq(4)).toHaveAttr('src', 'img/star-off.png');
548        });
549
550        it ('should round down on max limit down with half and no halfShow', function() {
551                // given
552                var star = $('#star');
553
554                // when
555                star.raty({ half: true, halfShow: false, score: 1.25 });
556
557                var imgs = star.children('img');
558
559                // then
560                expect(imgs.eq(0)).toHaveAttr('src', 'img/star-on.png');
561                expect(imgs.eq(1)).toHaveAttr('src', 'img/star-off.png');
562                expect(imgs.eq(2)).toHaveAttr('src', 'img/star-off.png');
563                expect(imgs.eq(3)).toHaveAttr('src', 'img/star-off.png');
564                expect(imgs.eq(4)).toHaveAttr('src', 'img/star-off.png');
565        });
566
567        it ('should round down on min limit half with half and no halfShow', function() {
568                // given
569                var star = $('#star');
570
571                // when
572                star.raty({ half: true, halfShow: false, score: 1.26 });
573
574                var imgs = star.children('img');
575
576                // then
577                expect(imgs.eq(0)).toHaveAttr('src', 'img/star-on.png');
578                expect(imgs.eq(1)).toHaveAttr('src', 'img/star-off.png');
579                expect(imgs.eq(2)).toHaveAttr('src', 'img/star-off.png');
580                expect(imgs.eq(3)).toHaveAttr('src', 'img/star-off.png');
581                expect(imgs.eq(4)).toHaveAttr('src', 'img/star-off.png');
582        });
583
584        it ('should round up on max limit half with half and no halfShow', function() {
585                // given
586                var star = $('#star');
587               
588                // when
589                star.raty({ half: true, halfShow: false, score: 1.75 });
590               
591                var imgs = star.children('img');
592               
593                // then
594                expect(imgs.eq(0)).toHaveAttr('src', 'img/star-on.png');
595                expect(imgs.eq(1)).toHaveAttr('src', 'img/star-on.png');
596                expect(imgs.eq(2)).toHaveAttr('src', 'img/star-off.png');
597                expect(imgs.eq(3)).toHaveAttr('src', 'img/star-off.png');
598                expect(imgs.eq(4)).toHaveAttr('src', 'img/star-off.png');
599        });
600
601        it ('should round up on min limit up with half and no halfShow', function() {
602                // given
603                var star = $('#star');
604               
605                // when
606                star.raty({ half: true, halfShow: false, score: 1.76 });
607               
608                var imgs = star.children('img');
609               
610                // then
611                expect(imgs.eq(0)).toHaveAttr('src', 'img/star-on.png');
612                expect(imgs.eq(1)).toHaveAttr('src', 'img/star-on.png');
613                expect(imgs.eq(2)).toHaveAttr('src', 'img/star-off.png');
614                expect(imgs.eq(3)).toHaveAttr('src', 'img/star-off.png');
615                expect(imgs.eq(4)).toHaveAttr('src', 'img/star-off.png');
616        });
617
618        it ('should round down on max limit down with no half and halfShow', function() {
619                // given
620                var star = $('#star');
621
622                // when
623                star.raty({ half: false, halfShow: true, score: 1.25 });
624
625                var imgs = star.children('img');
626
627                // then
628                expect(imgs.eq(0)).toHaveAttr('src', 'img/star-on.png');
629                expect(imgs.eq(1)).toHaveAttr('src', 'img/star-off.png');
630                expect(imgs.eq(2)).toHaveAttr('src', 'img/star-off.png');
631                expect(imgs.eq(3)).toHaveAttr('src', 'img/star-off.png');
632                expect(imgs.eq(4)).toHaveAttr('src', 'img/star-off.png');
633        });
634
635        it ('should show half on min limit half with no half and halfShow', function() {
636                // given
637                var star = $('#star');
638               
639                // when
640                star.raty({ half: false, halfShow: true, score: 1.26 });
641               
642                var imgs = star.children('img');
643               
644                // then
645                expect(imgs.eq(0)).toHaveAttr('src', 'img/star-on.png');
646                expect(imgs.eq(1)).toHaveAttr('src', 'img/star-half.png');
647                expect(imgs.eq(2)).toHaveAttr('src', 'img/star-off.png');
648                expect(imgs.eq(3)).toHaveAttr('src', 'img/star-off.png');
649                expect(imgs.eq(4)).toHaveAttr('src', 'img/star-off.png');
650        });
651
652        it ('should show half on max limit half with no half and halfShow', function() {
653                // given
654                var star = $('#star');
655               
656                // when
657                star.raty({ half: false, halfShow: true, score: 1.75 });
658               
659                var imgs = star.children('img');
660               
661                // then
662                expect(imgs.eq(0)).toHaveAttr('src', 'img/star-on.png');
663                expect(imgs.eq(1)).toHaveAttr('src', 'img/star-half.png');
664                expect(imgs.eq(2)).toHaveAttr('src', 'img/star-off.png');
665                expect(imgs.eq(3)).toHaveAttr('src', 'img/star-off.png');
666                expect(imgs.eq(4)).toHaveAttr('src', 'img/star-off.png');
667        });
668
669        it ('should show round up on min limit up with no half and halfShow', function() {
670                // given
671                var star = $('#star');
672               
673                // when
674                star.raty({ half: false, halfShow: true, score: 1.76 });
675               
676                var imgs = star.children('img');
677               
678                // then
679                expect(imgs.eq(0)).toHaveAttr('src', 'img/star-on.png');
680                expect(imgs.eq(1)).toHaveAttr('src', 'img/star-on.png');
681                expect(imgs.eq(2)).toHaveAttr('src', 'img/star-off.png');
682                expect(imgs.eq(3)).toHaveAttr('src', 'img/star-off.png');
683                expect(imgs.eq(4)).toHaveAttr('src', 'img/star-off.png');
684        });
685
686        it ('should set custom hints', function() {
687                // given
688                var star = $('#star');
689
690                // when
691                star.raty({ hints: ['a', '', null, 'd', '5'] });
692
693                var imgs = star.children('img');
694
695                // then
696            expect(imgs.eq(0)).toHaveAttr('title', 'a');
697            expect(imgs.eq(1)).toHaveAttr('title', '');
698            expect(imgs.eq(2)).toHaveAttr('title', 3);
699            expect(imgs.eq(3)).toHaveAttr('title', 'd');
700            expect(imgs.eq(4)).toHaveAttr('title', '5');
701        });
702
703        it ('should change the star off', function() {
704                // given
705                var star = $('#star');
706
707                // when
708                star.raty({ starOff: 'medal-off.png' });
709
710                var imgs = star.children('img');
711
712                // then
713            expect(imgs.eq(0)).toHaveAttr('src', 'img/medal-off.png');
714            expect(imgs.eq(1)).toHaveAttr('src', 'img/medal-off.png');
715            expect(imgs.eq(2)).toHaveAttr('src', 'img/medal-off.png');
716            expect(imgs.eq(3)).toHaveAttr('src', 'img/medal-off.png');
717            expect(imgs.eq(4)).toHaveAttr('src', 'img/medal-off.png');
718        });
719
720        it ('should change the star on', function() {
721                // given
722                var star = $('#star');
723
724                // when
725                star.raty({ starOn: 'medal-on.png' });
726
727                var imgs = star.children('img');
728
729                imgs.eq(3).mouseover();
730
731                // then
732            expect(imgs.eq(0)).toHaveAttr('src', 'img/medal-on.png');
733            expect(imgs.eq(1)).toHaveAttr('src', 'img/medal-on.png');
734            expect(imgs.eq(2)).toHaveAttr('src', 'img/medal-on.png');
735            expect(imgs.eq(3)).toHaveAttr('src', 'img/medal-on.png');
736            expect(imgs.eq(4)).toHaveAttr('src', 'img/star-off.png');
737        });
738
739        it ('should use range icons', function() {
740                // given
741                var star = $('#star');
742
743                // when
744                star.raty({
745                        iconRange: [
746                    { range: 2, on: 'face-a.png', off: 'face-a-off.png' },
747                    { range: 3, on: 'face-b.png', off: 'face-b-off.png' },
748                    { range: 4, on: 'face-c.png', off: 'face-c-off.png' },
749                    { range: 5, on: 'face-d.png', off: 'face-d-off.png' }
750                        ]
751                });
752
753                var imgs = star.children('img');
754
755                // then
756            expect(imgs.eq(0)).toHaveAttr('src', 'img/face-a-off.png');
757            expect(imgs.eq(1)).toHaveAttr('src', 'img/face-a-off.png');
758            expect(imgs.eq(2)).toHaveAttr('src', 'img/face-b-off.png');
759            expect(imgs.eq(3)).toHaveAttr('src', 'img/face-c-off.png');
760            expect(imgs.eq(4)).toHaveAttr('src', 'img/face-d-off.png');
761        });
762
763        it ('should use range icons and apply not found icons default', function() {
764                // given
765                var star = $('#star');
766
767                // when
768                star.raty({
769                        iconRange: [
770                    { range: 2, off: 'face-a-off.png' },
771                    { range: 3, off: 'face-b-off.png' },
772                    { range: 4, on: 'face-c.png', off: 'face-c-off.png' },
773                    { range: 5 }
774                        ]
775                });
776
777                var imgs = star.children('img');
778
779                imgs.eq(3).mouseover();
780
781                // then
782            expect(imgs.eq(0)).toHaveAttr('src', 'img/star-on.png');
783            expect(imgs.eq(1)).toHaveAttr('src', 'img/star-on.png');
784            expect(imgs.eq(2)).toHaveAttr('src', 'img/star-on.png');
785            expect(imgs.eq(3)).toHaveAttr('src', 'img/face-c.png');
786            expect(imgs.eq(4)).toHaveAttr('src', 'img/star-off.png');
787        });
788
789        it ('should hover range icons', function() {
790                // given
791                var star = $('#star');
792
793                // when
794                star.raty({
795                        iconRange: [
796                                { range: 2, on: 'face-a.png', off: 'face-a-off.png' },
797                                { range: 3, on: 'face-b.png', off: 'face-b-off.png' },
798                                { range: 4, on: 'face-c.png', off: 'face-c-off.png' },
799                                { range: 5, on: 'face-d.png', off: 'face-d-off.png' }
800                        ],
801            starOff: 'face-off.png'
802                });
803
804                var imgs = star.children('img');
805
806                imgs.eq(4).mouseover();
807
808                // then
809                expect(imgs.eq(0)).toHaveAttr('src', 'img/face-a.png');
810                expect(imgs.eq(1)).toHaveAttr('src', 'img/face-a.png');
811                expect(imgs.eq(2)).toHaveAttr('src', 'img/face-b.png');
812                expect(imgs.eq(3)).toHaveAttr('src', 'img/face-c.png');
813                expect(imgs.eq(4)).toHaveAttr('src', 'img/face-d.png');
814        });
815
816        it ('should set off range icons on mouseleave', function() {
817                // given
818                var star = $('#star');
819
820                // when
821                star.raty({
822                        iconRange: [
823                                { range: 2, on: 'face-a.png', off: 'face-a-off.png' },
824                                { range: 3, on: 'face-b.png', off: 'face-b-off.png' },
825                                { range: 4, on: 'face-c.png', off: 'face-c-off.png' },
826                                { range: 5, on: 'face-d.png', off: 'face-d-off.png' },
827                        ],
828            starOff: 'face-off.png'
829                });
830
831                var imgs = star.children('img');
832
833                imgs.eq(4).mouseover();
834                star.mouseleave();
835
836                // then
837                expect(imgs.eq(0)).toHaveAttr('src', 'img/face-a-off.png');
838                expect(imgs.eq(1)).toHaveAttr('src', 'img/face-a-off.png');
839                expect(imgs.eq(2)).toHaveAttr('src', 'img/face-b-off.png');
840                expect(imgs.eq(3)).toHaveAttr('src', 'img/face-c-off.png');
841                expect(imgs.eq(4)).toHaveAttr('src', 'img/face-d-off.png');
842        });
843
844        it ('should keep the score value range icons on mouseleave', function() {
845                // given
846                var star = $('#star');
847
848                // when
849                star.raty({
850                        iconRange: [
851                                { range: 2, on: 'face-a.png', off: 'face-a-off.png' },
852                                { range: 3, on: 'face-b.png', off: 'face-b-off.png' },
853                                { range: 4, on: 'face-c.png', off: 'face-c-off.png' },
854                                { range: 5, on: 'face-d.png', off: 'face-d-off.png' }
855                        ],
856            starOff: 'face-off.png',
857            score: 1
858                });
859
860                var imgs = star.children('img');
861
862                imgs.eq(4).mouseover();
863                star.mouseleave();
864
865                // then
866                expect(imgs.eq(0)).toHaveAttr('src', 'img/face-a.png');
867                expect(imgs.eq(1)).toHaveAttr('src', 'img/face-a-off.png');
868                expect(imgs.eq(2)).toHaveAttr('src', 'img/face-b-off.png');
869                expect(imgs.eq(3)).toHaveAttr('src', 'img/face-c-off.png');
870                expect(imgs.eq(4)).toHaveAttr('src', 'img/face-d-off.png');
871        });
872
873        it ('should calculate the right icon size', function() {
874                // given
875                var star        = $('#star'),
876                        size    = 24,
877                        number  = 5;
878
879                // when
880                star.raty({ size: size });
881
882                // then
883                expect(star.width()).toEqual(number * size + number * 4);
884        });
885
886        it ('should set custom width', function() {
887                // given
888                var star        = $('#star'),
889                        size    = 24;
890
891                // when
892                star.raty({ size: size, width: 200 });
893
894                // then
895                expect(star.width()).toEqual(200);
896        });
897
898        it ('should set custom width when readOnly', function() {
899                // given
900                var star        = $('#star'),
901                        size    = 24;
902
903                // when
904                star.raty({ readOnly: true, size: size, width: 200 });
905
906                // then
907                expect(star.width()).toEqual(200);
908        });
909
910        it ('should calculate the right icon size with cancel button', function() {
911                // given
912                var star        = $('#star'),
913                        size    = 24,
914                        number  = 5;
915
916                // when
917                star.raty({ cancel: true, size: size });
918
919                // then
920                expect(star.width()).toEqual(number * size + number * 4 + (size + 4));
921        });
922
923        it ('should set a target on div with mouseover', function() {
924                $('body').append('<div id="hint"></div>');
925
926                // given
927                var hint = $('#hint'),
928                        star = $('#star').raty({ target: '#hint' });
929
930                // when
931                star.children('img').eq(3).mouseover();
932
933                // then
934                expect(hint).toHaveHtml('good');
935
936                hint.remove();
937        });
938
939        it ('should set a target on div with mouseover but take off when mouseleave', function() {
940                $('body').append('<div id="hint"></div>');
941
942                // given
943                var hint = $('#hint'),
944                        star = $('#star').raty({ target: '#hint' });
945
946                // when
947                star.children('img').eq(3).mouseover().mouseleave();
948
949                // then
950                expect(hint).toBeEmpty();
951
952                hint.remove();
953        });
954
955        it ('should change the cancel hint using target', function() {
956                $('body').append('<div id="hint"></div>');
957
958                // given
959                var hint = $('#hint'),
960                        star = $('#star').raty({
961                        cancel:     true,
962                        cancelHint: 'none',
963                        target:     '#hint',
964                });
965
966                // when
967                star.children('img:first').mouseenter();
968
969                // then
970                expect(hint).toHaveHtml('none');
971
972                hint.remove();
973        });
974
975        it ('should set hint on a combobox element', function() {
976                $('body').append('<select id="hint"><option value="">--</option><option value="good">good</option></select>');
977
978                // given
979                var hint = $('#hint'),
980                        star = $('#star').raty({ target: '#hint' });
981
982                // when
983                star.children('img').eq(3).mouseover();
984
985                // then
986                expect(hint.val()).toEqual('good');
987
988                hint.remove();
989        });
990
991        it ('should set hint on a text element', function() {
992                $('body').append('<input id="hint" type="text" />');
993
994                // given
995                var hint = $('#hint'),
996                        star = $('#star').raty({ target: '#hint' });
997
998                // when
999                star.children('img').eq(3).mouseover();
1000
1001                // then
1002                expect(hint.val()).toEqual('good');
1003
1004                hint.remove();
1005        });
1006
1007        it ('should set hint on a textarea element', function() {
1008                $('body').append('<textarea id="hint"></textarea>');
1009
1010                // given
1011                var hint = $('#hint'),
1012                        star = $('#star').raty({ target: '#hint' });
1013
1014                // when
1015                star.children('img').eq(3).mouseover();
1016
1017                // then
1018                expect(hint.val()).toEqual('good');
1019
1020                hint.remove();
1021        });
1022
1023        it ('should set number hint on a combobox element', function() {
1024                $('body').append('<select id="hint"><option value="">--</option><option value="4">4</option></select>');
1025
1026                // given
1027                var hint = $('#hint'),
1028                        star = $('#star').raty({ target: '#hint', targetType: 'number' });
1029
1030                // when
1031                star.children('img').eq(3).mouseover();
1032
1033                // then
1034                expect(hint.val()).toEqual('4');
1035
1036                hint.remove();
1037        });
1038
1039        it ('should set number hint and keep it on mouseleave', function() {
1040                $('body').append('<select id="hint"><option value="">--</option><option value="4">4</option></select>');
1041
1042                // given
1043                var hint = $('#hint'),
1044                        star = $('#star').raty({ target: '#hint', targetType: 'number', targetKeep: true });
1045
1046                // when
1047                star.children('img').eq(3).mouseover().click().mouseleave();
1048
1049                // then
1050                expect(hint.val()).toEqual('4');
1051
1052                hint.remove();
1053        });
1054
1055        it ('should create stars without space', function() {
1056                // given
1057                var star        = $('#star');
1058                        size    = 16,
1059                        number  = 5;
1060
1061                // when
1062                star.raty({ space: false });
1063
1064                // then
1065                expect(star.width()).toEqual(number * size + number * (4 * 0));
1066        });
1067
1068        it ('should create stars with cancel without space', function() {
1069                // given
1070                var star        = $('#star');
1071                        size    = 16,
1072                        number  = 5;
1073
1074                // when
1075                star.raty({ cancel: true, space: false });
1076
1077                // then
1078                expect(star.width()).toEqual(number * size + number * (4 * 0) + 16 + (4 * 0));
1079        });
1080
1081        it ('should not set target with targetText when has started score and targetKeep is true with mouseout', function() {
1082                $('body').append('<div id="hint"></div>');
1083
1084                // given
1085                var hint        = $('#hint'),
1086                        star    = $('#star').raty({ score: 3, target: '#hint', targetKeep: true, targetText: 'my-hint-message' });
1087
1088                // when
1089                star.children('img').eq(3).mouseover().mouseleave();
1090
1091                // then
1092                expect(hint).toHaveHtml('regular');
1093
1094                hint.remove();
1095        });
1096
1097        it ('should not set target with targetText when has clicked score and targetKeep is true with mouseout', function() {
1098                $('body').append('<div id="hint"></div>');
1099
1100                // given
1101                var hint        = $('#hint'),
1102                        star    = $('#star').raty({ target: '#hint', targetKeep: true, targetText: 'my-hint-message' });
1103
1104                // when
1105                star.children('img').eq(3).mouseover().click().mouseleave();
1106
1107                // then
1108                expect(hint).toHaveHtml('good');
1109
1110                hint.remove();
1111        });
1112
1113        it ('should set target with targetText when has scoreed score and targetKeep is false with mouseout', function() {
1114                $('body').append('<div id="hint"></div>');
1115
1116                // given
1117                var message     = 'my-hint-message',
1118                        hint    = $('#hint'),
1119                        star    = $('#star').raty({ score: 3, target: '#hint', targetKeep: false, targetText: message });
1120
1121                // when
1122                star.children('img').eq(3).mouseover().mouseleave();
1123
1124                // then
1125                expect(hint).toHaveHtml(message);
1126
1127                hint.remove();
1128        });
1129
1130        it ('should set target with targetText when has clicked score and targetKeep false with mouseout', function() {
1131                $('body').append('<div id="hint"></div>');
1132
1133                // given
1134                var message     = 'my-hint-message',
1135                        hint    = $('#hint'),
1136                        star    = $('#star').raty({ target: '#hint', targetKeep: false, targetText: message });
1137
1138                // when
1139                star.children('img').eq(3).mouseover().click().mouseleave();
1140
1141                // then
1142                expect(hint).toHaveHtml(message);
1143
1144                hint.remove();
1145        });
1146
1147        it ('should set target with targetText when has started score and targetKeep is false without mouseout', function() {
1148                $('body').append('<div id="hint"></div>');
1149
1150                // given
1151                var message     = 'my-hint-message',
1152                        hint    = $('#hint'),
1153                        star    = $('#star');
1154
1155                // when
1156                star.raty({ score: 3, target: '#hint', targetKeep: false, targetText: message });
1157
1158                // then
1159                expect(hint).toHaveHtml(message);
1160
1161                hint.remove();
1162        });
1163
1164        it ('should set score as readonly when start with readOnly', function() {
1165                // given
1166                var star = $('#star');
1167
1168                // when
1169                star.raty({ readOnly: true });
1170
1171                // then
1172                expect(star.children('input')).toHaveAttr('readonly', 'readonly');
1173        });
1174
1175        it ('should be down with round option', function() {
1176                // given
1177                var star = $('#star');
1178
1179                // when
1180                star.raty({ halfShow: true, score: 1.9, round: { down: .9, full: .98, up: .99 } });
1181
1182                var imgs = star.children('img');
1183
1184                // then
1185                expect(imgs.eq(0)).toHaveAttr('src', 'img/star-on.png');
1186            expect(imgs.eq(1)).toHaveAttr('src', 'img/star-off.png');
1187            expect(imgs.eq(2)).toHaveAttr('src', 'img/star-off.png');
1188            expect(imgs.eq(3)).toHaveAttr('src', 'img/star-off.png');
1189            expect(imgs.eq(4)).toHaveAttr('src', 'img/star-off.png');
1190        });
1191
1192        it ('should be half with round option', function() {
1193                // given
1194                var star = $('#star');
1195
1196                // when
1197                star.raty({ halfShow: true, score: 1.91, round: { down: .9, full: .98, up: .99 } });
1198
1199                var imgs = star.children('img');
1200
1201                // then
1202                expect(imgs.eq(0)).toHaveAttr('src', 'img/star-on.png');
1203            expect(imgs.eq(1)).toHaveAttr('src', 'img/star-half.png');
1204            expect(imgs.eq(2)).toHaveAttr('src', 'img/star-off.png');
1205            expect(imgs.eq(3)).toHaveAttr('src', 'img/star-off.png');
1206            expect(imgs.eq(4)).toHaveAttr('src', 'img/star-off.png');
1207        });
1208
1209        it ('should be up with round option', function() {
1210                // given
1211                var star = $('#star');
1212
1213                // when
1214                star.raty({ halfShow: true, score: 1.99, round: { down: .9, full: .98, up: .99 } });
1215
1216                var imgs = star.children('img');
1217
1218                // then
1219                expect(imgs.eq(0)).toHaveAttr('src', 'img/star-on.png');
1220            expect(imgs.eq(1)).toHaveAttr('src', 'img/star-on.png');
1221            expect(imgs.eq(2)).toHaveAttr('src', 'img/star-off.png');
1222            expect(imgs.eq(3)).toHaveAttr('src', 'img/star-off.png');
1223            expect(imgs.eq(4)).toHaveAttr('src', 'img/star-off.png');
1224        });
1225
1226        it ('should be full with round option and halfShow off', function() {
1227                // given
1228                var star = $('#star');
1229
1230                // when
1231                star.raty({ halfShow: false, score: 1.98, round: { down: .9, full: .98, up: .99 } });
1232
1233                var imgs = star.children('img');
1234
1235                // then
1236                expect(imgs.eq(0)).toHaveAttr('src', 'img/star-on.png');
1237            expect(imgs.eq(1)).toHaveAttr('src', 'img/star-on.png');
1238            expect(imgs.eq(2)).toHaveAttr('src', 'img/star-off.png');
1239            expect(imgs.eq(3)).toHaveAttr('src', 'img/star-off.png');
1240            expect(imgs.eq(4)).toHaveAttr('src', 'img/star-off.png');
1241        });
1242
1243        it ('should be down with round option and others options default', function() {
1244                // given
1245                var star = $('#star');
1246
1247                // when
1248                star.raty({ halfShow: true, score: 1.9, round: { down: .9 } });
1249
1250                var imgs = star.children('img');
1251
1252                // then
1253                expect(imgs.eq(0)).toHaveAttr('src', 'img/star-on.png');
1254            expect(imgs.eq(1)).toHaveAttr('src', 'img/star-off.png');
1255            expect(imgs.eq(2)).toHaveAttr('src', 'img/star-off.png');
1256            expect(imgs.eq(3)).toHaveAttr('src', 'img/star-off.png');
1257            expect(imgs.eq(4)).toHaveAttr('src', 'img/star-off.png');
1258        });
1259
1260        it ('should be half with round option and others options default', function() {
1261                // given
1262                var star = $('#star');
1263
1264                // when
1265                star.raty({ halfShow: true, score: 1.26, round: { } });
1266
1267                var imgs = star.children('img');
1268
1269                // then
1270                expect(imgs.eq(0)).toHaveAttr('src', 'img/star-on.png');
1271            expect(imgs.eq(1)).toHaveAttr('src', 'img/star-half.png');
1272            expect(imgs.eq(2)).toHaveAttr('src', 'img/star-off.png');
1273            expect(imgs.eq(3)).toHaveAttr('src', 'img/star-off.png');
1274            expect(imgs.eq(4)).toHaveAttr('src', 'img/star-off.png');
1275        });
1276
1277        it ('should be up with round option and others options default', function() {
1278                // given
1279                var star = $('#star');
1280
1281                // when
1282                star.raty({ halfShow: true, score: 1.99, round: { up: .99 } });
1283
1284                var imgs = star.children('img');
1285
1286                // then
1287                expect(imgs.eq(0)).toHaveAttr('src', 'img/star-on.png');
1288            expect(imgs.eq(1)).toHaveAttr('src', 'img/star-on.png');
1289            expect(imgs.eq(2)).toHaveAttr('src', 'img/star-off.png');
1290            expect(imgs.eq(3)).toHaveAttr('src', 'img/star-off.png');
1291            expect(imgs.eq(4)).toHaveAttr('src', 'img/star-off.png');
1292        });
1293
1294        it ('should be full with round option and halfShow off and others options default', function() {
1295                // given
1296                var star = $('#star');
1297
1298                // when
1299                star.raty({ halfShow: false, score: 1.98, round: { full: .98 } });
1300
1301                var imgs = star.children('img');
1302
1303                // then
1304                expect(imgs.eq(0)).toHaveAttr('src', 'img/star-on.png');
1305            expect(imgs.eq(1)).toHaveAttr('src', 'img/star-on.png');
1306            expect(imgs.eq(2)).toHaveAttr('src', 'img/star-off.png');
1307            expect(imgs.eq(3)).toHaveAttr('src', 'img/star-off.png');
1308            expect(imgs.eq(4)).toHaveAttr('src', 'img/star-off.png');
1309        });
1310
1311        it ('should set hint target with start option and targetKeep true', function() {
1312                $('body').append('<div id="hint"></div>');
1313
1314                // given
1315                var hint        = $('#hint'),
1316                        star    = $('#star');
1317
1318                // when
1319                star.raty({ target: '#hint', precision: true, score: 3, targetKeep: true });
1320
1321                // then
1322                expect(hint).toHaveHtml('regular');
1323
1324                hint.remove();
1325        });
1326
1327        it ('should set precision number target with just one fractional number', function() {
1328                $('body').append('<div id="hint"></div>');
1329
1330                // given
1331                var hint        = $('#hint'),
1332                        star    = $('#star');
1333
1334                // when
1335                star.raty({ target: '#hint', precision: true, targetType: 'number', score: 1.2333, targetKeep: true });
1336
1337                // then
1338                expect(hint).toHaveHtml('1.2');
1339
1340                hint.remove();
1341        });
1342
1343        it ('should set precision number target with start options and targetKeep true', function() {
1344                $('body').append('<div id="hint"></div>');
1345
1346                // given
1347                var hint        = $('#hint'),
1348                        star    = $('#star');
1349
1350                // when
1351                star.raty({ target: '#hint', precision: true, targetType: 'number', score: 1.2, targetKeep: true });
1352
1353                // then
1354                expect(hint).toHaveHtml('1.2');
1355
1356                hint.remove();
1357        });
1358
1359        it ('should set target with format on mouseover', function() {
1360                $('body').append('<div id="hint"></div>');
1361
1362                // given
1363                var hint        = $('#hint'),
1364                        star    = $('#star').raty({ target: '#hint', targetFormat: 'score: {score}', precision: true });
1365
1366                // when
1367                star.children('img:first').mouseover();
1368
1369                // then
1370                expect(hint).toHaveHtml('score: bad');
1371
1372                hint.remove();
1373        });
1374
1375        it ('should set score with precision and half disabled', function() {
1376                $('body').append('<div id="hint"></div>');
1377
1378                // given
1379                var hint        = $('#hint'),
1380                        star    = $('#star').raty({ target: '#hint', targetKeep: true, precision: true });
1381
1382                // when
1383                star.children('img:first').mouseover().click().mouseleave();
1384
1385                // then
1386                expect(hint).toHaveHtml('bad');
1387
1388                hint.remove();
1389        });
1390
1391        it ('should set and keep target with format on click and mouseleave', function() {
1392                $('body').append('<div id="hint"></div>');
1393
1394                // given
1395                var hint        = $('#hint'),
1396                        star    = $('#star').raty({ target: '#hint', targetFormat: 'score: {score}', targetKeep: true });
1397
1398                // when
1399                star.children('img:first').mouseover().click().mouseleave();
1400
1401                // then
1402                expect(hint).toHaveHtml('score: bad');
1403
1404                hint.remove();
1405        });
1406
1407        it ('should set target with format and precision', function() {
1408                $('body').append('<div id="hint"></div>');
1409
1410                // given
1411                var hint        = $('#hint'),
1412                        star    = $('#star');
1413
1414                // when
1415                star.raty({ target: '#hint', targetFormat: 'score: {score}', targetKeep: true, targetType: 'number', precision: true, score: 1.2 });
1416
1417                // then
1418                expect(hint).toHaveHtml('score: 1.2');
1419
1420                hint.remove();
1421        });
1422
1423        it ('should set target with none value', function() {
1424                $('body').append('<div id="hint"></div>');
1425
1426                // given
1427                var hint        = $('#hint'),
1428                        star    = $('#star');
1429
1430                // when
1431                star.raty({ target: '#hint', targetFormat: 'score: {score}', targetKeep: true, targetType: 'number', targetText: 'none' });
1432
1433                // then
1434                expect(hint).toHaveHtml('score: none');
1435
1436                hint.remove();
1437        });
1438
1439        it ('should not to use format template on cancel mouseover', function() {
1440                $('body').append('<div id="hint"></div>');
1441
1442                // given
1443                var hint        = $('#hint'),
1444                        star    = $('#star').raty({ cancel: true, target: '#hint', targetFormat: 'score: {score}', targetKeep: true });
1445
1446                // when
1447                star.children('img:first').mouseenter();
1448
1449                // then
1450                expect(hint).toHaveHtml('cancel this rating!');
1451
1452                hint.remove();
1453        });
1454
1455        it ('should show single icon selection on mouseover', function() {
1456                // given
1457                var star = $('#star');
1458
1459                // when
1460                star.raty({ single: true });
1461
1462                var imgs = star.children('img');
1463
1464                imgs.eq(2).mouseover();
1465
1466                // then
1467                expect(imgs.eq(0)).toHaveAttr('src', 'img/star-off.png');
1468            expect(imgs.eq(1)).toHaveAttr('src', 'img/star-off.png');
1469            expect(imgs.eq(2)).toHaveAttr('src', 'img/star-on.png');
1470            expect(imgs.eq(3)).toHaveAttr('src', 'img/star-off.png');
1471            expect(imgs.eq(4)).toHaveAttr('src', 'img/star-off.png');
1472        });
1473
1474        it ('should show single icon selection on click', function() {
1475                // given
1476                var star = $('#star');
1477
1478                // when
1479                star.raty({ single: true });
1480
1481                var imgs = star.children('img');
1482
1483                imgs.eq(2).mouseover().click().mouseleave();
1484
1485                // then
1486                expect(imgs.eq(0)).toHaveAttr('src', 'img/star-off.png');
1487            expect(imgs.eq(1)).toHaveAttr('src', 'img/star-off.png');
1488            expect(imgs.eq(2)).toHaveAttr('src', 'img/star-on.png');
1489            expect(imgs.eq(3)).toHaveAttr('src', 'img/star-off.png');
1490            expect(imgs.eq(4)).toHaveAttr('src', 'img/star-off.png');
1491        });
1492
1493        it ('should show single icon selection with iconRange enabled on mouseover', function() {
1494                // given
1495                var star = $('#star');
1496
1497                // when
1498                star.raty({
1499                        iconRange: [
1500                                { range: 2, on: 'face-a.png', off: 'face-a-off.png' },
1501                                { range: 3, on: 'face-b.png', off: 'face-b-off.png' },
1502                                { range: 4, on: 'face-c.png', off: 'face-c-off.png' },
1503                                { range: 5, on: 'face-d.png', off: 'face-d-off.png' }
1504                        ],
1505                        single: true
1506                });
1507
1508                var imgs = star.children('img');
1509
1510                imgs.eq(3).mouseover();
1511
1512                // then
1513                expect(imgs.eq(0)).toHaveAttr('src', 'img/face-a-off.png');
1514            expect(imgs.eq(1)).toHaveAttr('src', 'img/face-a-off.png');
1515            expect(imgs.eq(2)).toHaveAttr('src', 'img/face-b-off.png');
1516            expect(imgs.eq(3)).toHaveAttr('src', 'img/face-c.png');
1517            expect(imgs.eq(4)).toHaveAttr('src', 'img/face-d-off.png');
1518        });
1519
1520        it ('should show single icon selection with iconRange enabled on click', function() {
1521                // given
1522                var star = $('#star');
1523
1524                // when
1525                star.raty({
1526                        iconRange: [
1527                                { range: 2, on: 'face-a.png', off: 'face-a-off.png' },
1528                                { range: 3, on: 'face-b.png', off: 'face-b-off.png' },
1529                                { range: 4, on: 'face-c.png', off: 'face-c-off.png' },
1530                                { range: 5, on: 'face-d.png', off: 'face-d-off.png' }
1531                        ],
1532                        single: true
1533                });
1534
1535                var imgs = star.children('img');
1536
1537                imgs.eq(3).mouseover().click().mouseleave();
1538
1539                // then
1540                expect(imgs.eq(0)).toHaveAttr('src', 'img/face-a-off.png');
1541            expect(imgs.eq(1)).toHaveAttr('src', 'img/face-a-off.png');
1542            expect(imgs.eq(2)).toHaveAttr('src', 'img/face-b-off.png');
1543            expect(imgs.eq(3)).toHaveAttr('src', 'img/face-c.png');
1544            expect(imgs.eq(4)).toHaveAttr('src', 'img/face-d-off.png');
1545        });
1546
1547        it ('score should get it as int', function() {
1548                // given
1549                var star = $('#star').raty({ score: 1 });
1550
1551                // when
1552                var score = star.raty('score');
1553
1554                // then
1555                expect(score).toEqual(1);
1556        });
1557
1558        it ('score should get it as float', function() {
1559                // given
1560                var star = $('#star').raty({ score: 1.5 });
1561
1562                // when
1563                var score = star.raty('score');
1564
1565                // then
1566                expect(score).toEqual(1.5);
1567        });
1568
1569        it ('[score] should not have score when it is setted to 0', function() {
1570                // given
1571                var star = $('#star').raty({ score: 0 });
1572
1573                // when
1574                var score = star.raty('score');
1575
1576                // then
1577                expect(score).toEqual(null);
1578        });
1579
1580        it ('[score] should have 20 as max', function() {
1581                // given
1582                var star = $('#star').raty({ number: 30, score: 30 });
1583
1584                // when
1585                var score = star.raty('score');
1586
1587                // then
1588                expect(score).toEqual(20);
1589        });
1590
1591        it ('[score] should have 0 as min', function() {
1592                // given
1593                var star = $('#star').raty({ score: -30 });
1594
1595                // when
1596                var score = star.raty('score');
1597
1598                // then
1599                expect(score).toEqual(null);
1600        });
1601
1602        it ('[set] should be chainable', function() {
1603                // given
1604                var star        = $('#star').raty(),
1605                        clazz   = 'some-class';
1606
1607                // when
1608                var clone = star.raty('set', { readOnly: true }).addClass(clazz);
1609
1610                // then
1611                expect(clone).toHaveClass(clazz);
1612        });
1613
1614        it ('[set] should change just the passed parameters', function() {
1615                // given
1616                var star = $('#star').raty({ number: 6 });
1617
1618                // when
1619                star.raty('set', { scoreName: 'changed' });
1620
1621                // then
1622                var clone = $('#star');
1623
1624                expect(clone.children('input')).toHaveAttr('name', 'changed');
1625                expect(clone.children('img').length).toEqual(6);
1626        });
1627
1628        it ('[reload] should be chainable', function() {
1629                // given
1630                var star        = $('#star').raty(),
1631                        clazz   = 'some-class';
1632
1633                // when
1634                var clone = star.raty('reload').addClass(clazz);
1635
1636                // then
1637                expect(clone).toHaveClass(clazz);
1638        });
1639
1640        it ('[reload] should reload with same configuration', function() {
1641                // given
1642                var star = $('#star').raty({ number: 6 });
1643
1644                // when
1645                star.raty('reload');
1646
1647                // then
1648                expect($('#star').children('img').length).toEqual(6);
1649        });
1650
1651        it ('[mouseover] should display the value', function() {
1652                $('body').append('<div id="hint"></div>');
1653
1654                // given
1655                var hint        = $('#hint'),
1656                        star    = $('#star').raty({
1657                                mouseover: function(score, evt) {
1658                                        hint.html(score);
1659                                }
1660                        });
1661
1662                // when
1663                star.children('img:eq(1)').mouseover();
1664
1665                // then
1666                expect(hint).toHaveHtml(2);
1667
1668                hint.remove();
1669        });
1670
1671        it ('[mouseover] should display the cancel value', function() {
1672                $('body').append('<div id="hint"></div>');
1673
1674                // given
1675                var hint        = $('#hint'),
1676                        star    = $('#star').raty({
1677                                cancel          : true,
1678                                mouseover       : function(score, evt) {
1679                                        hint.html(score === null ? 'null' : score);
1680                                }
1681                        });
1682
1683                // when
1684                star.children('.raty-cancel').mouseover();
1685
1686                // then
1687                expect(hint).toHaveHtml('null');
1688
1689                hint.remove();
1690        });
1691
1692        it ('[mouseover] should pass the original value on cancel mouseout', function() {
1693                $('body').append('<div id="hint"></div>');
1694
1695                // given
1696                var hint        = $('#hint'),
1697                        star    = $('#star').raty({
1698                                cancel          : true,
1699                                score           : 1,
1700                                mouseover       : function(score, evt) {
1701                                        hint.html(score === null ? 'null' : score);
1702                                }
1703                        });
1704
1705                // when
1706                star.children('.raty-cancel').mouseenter().mouseleave();
1707
1708                // then
1709                expect(hint).toHaveHtml(1);
1710
1711                hint.remove();
1712        });
1713
1714        it ('[mouseover] should pass the original value on star mouseout', function() {
1715                $('body').append('<div id="hint"></div>');
1716
1717                // given
1718                var hint        = $('#hint'),
1719                        star    = $('#star').raty({
1720                                score           : 1,
1721                                cancel          : true,
1722                                mouseover       : function(score, evt) {
1723                                        hint.html(score);
1724                                }
1725                        });
1726
1727                // when
1728                star.children('img:eq(4)').mouseover().mouseleave();
1729
1730                // then
1731                expect(hint).toHaveHtml(1);
1732
1733                hint.remove();
1734        });
1735
1736        it ('[score] should accept int string', function() {
1737                // given
1738                var star = $('#star');
1739
1740                // when
1741                star.raty({ number: 10, score: '10' });
1742
1743                // then
1744                expect(star.children('input')).toHaveValue(10);
1745        });
1746
1747        it ('[score] should accept float string', function() {
1748                // given
1749                var star = $('#star');
1750
1751                // when
1752                star.raty({ number: 11, score: '10.5' });
1753
1754                // then
1755                expect(star.children('input')).toHaveValue(10.5);
1756        });
1757
1758        it ('[number] should accept string', function() {
1759                // given
1760                var star = $('#star');
1761
1762                // when
1763                star.raty({ number: '10' });
1764
1765                // then
1766                expect(star.children('img').length).toEqual(10);
1767        });
1768
1769        it ('[mouseover] handle null on cancel over', function() {
1770                var isOver = true;
1771
1772                // given
1773                var star = $('#star').raty({
1774                        cancel          : true,
1775                        mouseover       : function(score, evt) {
1776                                if (isOver) {
1777                                        // then
1778                                        expect(score === null).toBeTruthy();
1779
1780                                        isOver = false;
1781                                }
1782                        }
1783                });
1784
1785                // when
1786                star.children('.raty-cancel').mouseover();
1787        });
1788
1789        it ('[mouseover] handle undefined on mouse out of cancel', function() {
1790                var nextOut = false;
1791
1792                // given
1793                var star = $('#star').raty({
1794                        cancel          : true,
1795                        mouseover       : function(score, evt) {
1796                                if (nextOut) {
1797                                        // then
1798                                        expect(score === undefined).toBeTruthy();
1799                                }
1800
1801                                nextOut = true;
1802                        }
1803                });
1804
1805                // when
1806                star.children('.raty-cancel').mouseleave();
1807        });
1808
1809        it ('[mouseover] handle undefined on star out', function() {
1810                var nextOut = false;
1811
1812                // given
1813                var star = $('#star').raty({
1814                        mouseover       : function(score, evt) {
1815                                if (nextOut) {
1816                                        // then
1817                                        expect(score === undefined).toBeTruthy();
1818                                }
1819
1820                                nextOut = true;
1821                        }
1822                });
1823
1824                // when
1825                star.children('img:first').mouseleave();
1826        });
1827
1828        it ('[mouseover] handle 1 on star out', function() {
1829                var nextOut = false;
1830
1831                // given
1832                var star = $('#star').raty({
1833                        score           : 1,
1834                        mouseover       : function(score, evt) {
1835                                if (nextOut) {
1836                                        // then
1837                                        expect(score).toEqual(1);
1838                                }
1839
1840                                nextOut = true;
1841                        }
1842                });
1843
1844                // when
1845                star.children('img:first').mouseleave();
1846        });
1847
1848        it ('[mouseover] handle 1 on cancel out', function() {
1849                var nextOut = false;
1850
1851                // given
1852                var star = $('#star').raty({
1853                        cancel          : true,
1854                        score           : 1,
1855                        mouseover       : function(score, evt) {
1856                                if (nextOut) {
1857                                        // then
1858                                        expect(parseInt(score)).toEqual(1);
1859                                }
1860
1861                                nextOut = true;
1862                        }
1863                });
1864
1865                // when
1866                star.children('.raty-cancel').mouseleave();
1867        });
1868
1869});
1870
1871describe('Using class', function() {
1872
1873        beforeEach(function() {
1874                $('body').append('<div class="star"></div><div class="star"></div><div class="star"></div>');
1875        });
1876
1877        afterEach(function() {
1878                $('.star').remove();
1879        });
1880
1881        it ('should create the default stars', function() {
1882                // given
1883                var stars = $('.star');
1884
1885                // when
1886                stars.raty();
1887
1888                // then
1889                var imgs1 = stars.eq(0).children('img'),
1890                        imgs2 = stars.eq(1).children('img'),
1891                        imgs3 = stars.eq(2).children('img');
1892
1893            expect(imgs1.eq(0)).toHaveAttr('title', 'bad');
1894            expect(imgs1.eq(1)).toHaveAttr('title', 'poor');
1895            expect(imgs1.eq(2)).toHaveAttr('title', 'regular');
1896            expect(imgs1.eq(3)).toHaveAttr('title', 'good');
1897            expect(imgs1.eq(4)).toHaveAttr('title', 'gorgeous');
1898
1899            expect(imgs1.eq(0)).toHaveAttr('src', 'img/star-off.png');
1900            expect(imgs1.eq(1)).toHaveAttr('src', 'img/star-off.png');
1901            expect(imgs1.eq(2)).toHaveAttr('src', 'img/star-off.png');
1902            expect(imgs1.eq(3)).toHaveAttr('src', 'img/star-off.png');
1903            expect(imgs1.eq(4)).toHaveAttr('src', 'img/star-off.png');
1904
1905            expect(imgs2.eq(0)).toHaveAttr('title', 'bad');
1906            expect(imgs2.eq(1)).toHaveAttr('title', 'poor');
1907            expect(imgs2.eq(2)).toHaveAttr('title', 'regular');
1908            expect(imgs2.eq(3)).toHaveAttr('title', 'good');
1909            expect(imgs2.eq(4)).toHaveAttr('title', 'gorgeous');
1910
1911            expect(imgs2.eq(0)).toHaveAttr('src', 'img/star-off.png');
1912            expect(imgs2.eq(1)).toHaveAttr('src', 'img/star-off.png');
1913            expect(imgs2.eq(2)).toHaveAttr('src', 'img/star-off.png');
1914            expect(imgs2.eq(3)).toHaveAttr('src', 'img/star-off.png');
1915            expect(imgs2.eq(4)).toHaveAttr('src', 'img/star-off.png');
1916
1917            expect(imgs3.eq(0)).toHaveAttr('title', 'bad');
1918            expect(imgs3.eq(1)).toHaveAttr('title', 'poor');
1919            expect(imgs3.eq(2)).toHaveAttr('title', 'regular');
1920            expect(imgs3.eq(3)).toHaveAttr('title', 'good');
1921            expect(imgs3.eq(4)).toHaveAttr('title', 'gorgeous');
1922
1923            expect(imgs3.eq(0)).toHaveAttr('src', 'img/star-off.png');
1924            expect(imgs3.eq(1)).toHaveAttr('src', 'img/star-off.png');
1925            expect(imgs3.eq(2)).toHaveAttr('src', 'img/star-off.png');
1926            expect(imgs3.eq(3)).toHaveAttr('src', 'img/star-off.png');
1927            expect(imgs3.eq(4)).toHaveAttr('src', 'img/star-off.png');
1928        });
1929
1930        it ('should select first raty', function() {
1931                // given
1932                var stars = $('.star').raty(),
1933                        imgs1 = stars.eq(0).children('img'),
1934                        imgs2 = stars.eq(1).children('img'),
1935                        imgs3 = stars.eq(2).children('img');
1936
1937                // when
1938                imgs1.eq(3).mouseover();
1939
1940                // then
1941            expect(imgs1.eq(0)).toHaveAttr('src', 'img/star-on.png');
1942            expect(imgs1.eq(1)).toHaveAttr('src', 'img/star-on.png');
1943            expect(imgs1.eq(2)).toHaveAttr('src', 'img/star-on.png');
1944            expect(imgs1.eq(3)).toHaveAttr('src', 'img/star-on.png');
1945            expect(imgs1.eq(4)).toHaveAttr('src', 'img/star-off.png');
1946
1947            expect(imgs2.eq(0)).toHaveAttr('src', 'img/star-off.png');
1948            expect(imgs2.eq(1)).toHaveAttr('src', 'img/star-off.png');
1949            expect(imgs2.eq(2)).toHaveAttr('src', 'img/star-off.png');
1950            expect(imgs2.eq(3)).toHaveAttr('src', 'img/star-off.png');
1951            expect(imgs2.eq(4)).toHaveAttr('src', 'img/star-off.png');
1952
1953            expect(imgs3.eq(0)).toHaveAttr('src', 'img/star-off.png');
1954            expect(imgs3.eq(1)).toHaveAttr('src', 'img/star-off.png');
1955            expect(imgs3.eq(2)).toHaveAttr('src', 'img/star-off.png');
1956            expect(imgs3.eq(3)).toHaveAttr('src', 'img/star-off.png');
1957            expect(imgs3.eq(4)).toHaveAttr('src', 'img/star-off.png');
1958        });
1959
1960        it ('should click second raty', function() {
1961                // given
1962                var stars = $('.star').raty(),
1963                        imgs1 = stars.eq(0).children('img'),
1964                        imgs2 = stars.eq(1).children('img'),
1965                        imgs3 = stars.eq(2).children('img');
1966
1967                // when
1968
1969                imgs2.eq(3).mouseover().click().mouseleave();
1970
1971                // then
1972            expect(imgs1.eq(0)).toHaveAttr('src', 'img/star-off.png');
1973            expect(imgs1.eq(1)).toHaveAttr('src', 'img/star-off.png');
1974            expect(imgs1.eq(2)).toHaveAttr('src', 'img/star-off.png');
1975            expect(imgs1.eq(3)).toHaveAttr('src', 'img/star-off.png');
1976            expect(imgs1.eq(4)).toHaveAttr('src', 'img/star-off.png');
1977
1978            expect(imgs2.eq(0)).toHaveAttr('src', 'img/star-on.png');
1979            expect(imgs2.eq(1)).toHaveAttr('src', 'img/star-on.png');
1980            expect(imgs2.eq(2)).toHaveAttr('src', 'img/star-on.png');
1981            expect(imgs2.eq(3)).toHaveAttr('src', 'img/star-on.png');
1982            expect(imgs2.eq(4)).toHaveAttr('src', 'img/star-off.png');
1983
1984            expect(imgs3.eq(0)).toHaveAttr('src', 'img/star-off.png');
1985            expect(imgs3.eq(1)).toHaveAttr('src', 'img/star-off.png');
1986            expect(imgs3.eq(2)).toHaveAttr('src', 'img/star-off.png');
1987            expect(imgs3.eq(3)).toHaveAttr('src', 'img/star-off.png');
1988            expect(imgs3.eq(4)).toHaveAttr('src', 'img/star-off.png');
1989        });
1990
1991        it ('should click on first and third raty', function() {
1992                // given
1993                var stars = $('.star').raty(),
1994                        imgs1 = stars.eq(0).children('img'),
1995                        imgs2 = stars.eq(1).children('img'),
1996                        imgs3 = stars.eq(2).children('img');
1997
1998                // when
1999                imgs1.eq(3).mouseover().click().mouseleave();
2000                imgs3.eq(3).mouseover().click().mouseleave();
2001
2002                // then
2003                expect(imgs1.eq(0)).toHaveAttr('src', 'img/star-on.png');
2004                expect(imgs1.eq(1)).toHaveAttr('src', 'img/star-on.png');
2005                expect(imgs1.eq(2)).toHaveAttr('src', 'img/star-on.png');
2006                expect(imgs1.eq(3)).toHaveAttr('src', 'img/star-on.png');
2007                expect(imgs1.eq(4)).toHaveAttr('src', 'img/star-off.png');
2008
2009            expect(imgs2.eq(0)).toHaveAttr('src', 'img/star-off.png');
2010            expect(imgs2.eq(1)).toHaveAttr('src', 'img/star-off.png');
2011            expect(imgs2.eq(2)).toHaveAttr('src', 'img/star-off.png');
2012            expect(imgs2.eq(3)).toHaveAttr('src', 'img/star-off.png');
2013            expect(imgs2.eq(4)).toHaveAttr('src', 'img/star-off.png');
2014
2015                expect(imgs3.eq(0)).toHaveAttr('src', 'img/star-on.png');
2016                expect(imgs3.eq(1)).toHaveAttr('src', 'img/star-on.png');
2017                expect(imgs3.eq(2)).toHaveAttr('src', 'img/star-on.png');
2018                expect(imgs3.eq(3)).toHaveAttr('src', 'img/star-on.png');
2019                expect(imgs3.eq(4)).toHaveAttr('src', 'img/star-off.png');
2020        });
2021
2022        it ('should hover all and out', function() {
2023                // given
2024                var stars = $('.star').raty(),
2025                        imgs1 = stars.eq(0).children('img'),
2026                        imgs2 = stars.eq(1).children('img'),
2027                        imgs3 = stars.eq(2).children('img');
2028
2029                // when
2030                imgs1.eq(1).mouseover().mouseleave();
2031                imgs2.eq(2).mouseover().mouseleave();
2032                imgs3.eq(3).mouseover().mouseleave();
2033
2034                // then
2035                expect(imgs1.eq(0)).toHaveAttr('src', 'img/star-off.png');
2036                expect(imgs1.eq(1)).toHaveAttr('src', 'img/star-off.png');
2037                expect(imgs1.eq(2)).toHaveAttr('src', 'img/star-off.png');
2038                expect(imgs1.eq(3)).toHaveAttr('src', 'img/star-off.png');
2039                expect(imgs1.eq(4)).toHaveAttr('src', 'img/star-off.png');
2040
2041            expect(imgs2.eq(0)).toHaveAttr('src', 'img/star-off.png');
2042            expect(imgs2.eq(1)).toHaveAttr('src', 'img/star-off.png');
2043            expect(imgs2.eq(2)).toHaveAttr('src', 'img/star-off.png');
2044            expect(imgs2.eq(3)).toHaveAttr('src', 'img/star-off.png');
2045            expect(imgs2.eq(4)).toHaveAttr('src', 'img/star-off.png');
2046
2047                expect(imgs3.eq(0)).toHaveAttr('src', 'img/star-off.png');
2048                expect(imgs3.eq(1)).toHaveAttr('src', 'img/star-off.png');
2049                expect(imgs3.eq(2)).toHaveAttr('src', 'img/star-off.png');
2050                expect(imgs3.eq(3)).toHaveAttr('src', 'img/star-off.png');
2051                expect(imgs3.eq(4)).toHaveAttr('src', 'img/star-off.png');
2052        });
2053
2054        it ('should set score as readonly when start with readOnly', function() {
2055                // given
2056                var stars = $('.star');
2057
2058                // when
2059                stars.eq(0).raty({ readOnly: true });
2060                stars.eq(1).raty({ readOnly: true });
2061                stars.eq(2).raty({ readOnly: true });
2062
2063                // then
2064                expect(stars.eq(0).children('input')).toHaveAttr('readonly', 'readonly');
2065                expect(stars.eq(1).children('input')).toHaveAttr('readonly', 'readonly');
2066                expect(stars.eq(2).children('input')).toHaveAttr('readonly', 'readonly');
2067        });
2068
2069        it ('score should get it as int', function() {
2070                // given
2071                var stars = $('.star');;
2072
2073                stars.eq(0).raty({ score: 1 });
2074                stars.eq(1).raty({ score: 2 });
2075                stars.eq(2).raty({ score: 3 });
2076
2077                // when
2078                var score1 = stars.eq(0).raty('score'),
2079                        score2 = stars.eq(1).raty('score'),
2080                        score3 = stars.eq(2).raty('score');
2081
2082                // then
2083                expect(score1).toEqual(1);
2084                expect(score2).toEqual(2);
2085                expect(score3).toEqual(3);
2086        });
2087
2088        it ('score should get it as float', function() {
2089                // given
2090                var stars = $('.star');;
2091
2092                stars.eq(0).raty({ score: 1.1 });
2093                stars.eq(1).raty({ score: 2.2 });
2094                stars.eq(2).raty({ score: 3.3 });
2095
2096                // when
2097                var score1 = stars.eq(0).raty('score'),
2098                        score2 = stars.eq(1).raty('score'),
2099                        score3 = stars.eq(2).raty('score');
2100
2101                // then
2102                expect(score1).toEqual(1.1);
2103                expect(score2).toEqual(2.2);
2104                expect(score3).toEqual(3.3);
2105        });
2106
2107});
2108
2109describe('Using function with id', function() {
2110
2111        beforeEach(function() {
2112                $('body').append('<div id="star"></div>');
2113        });
2114
2115        afterEach(function() {
2116                $('#star').remove();
2117        });
2118
2119        it ('should start the start with 3 stars', function() {
2120                // given
2121                var star = $('#star').raty();
2122
2123                // when
2124                star.raty('score', 3);
2125
2126                // then
2127                var imgs = star.children('img');
2128
2129            expect(imgs.eq(0)).toHaveAttr('src', 'img/star-on.png');
2130            expect(imgs.eq(1)).toHaveAttr('src', 'img/star-on.png');
2131            expect(imgs.eq(2)).toHaveAttr('src', 'img/star-on.png');
2132            expect(imgs.eq(3)).toHaveAttr('src', 'img/star-off.png');
2133            expect(imgs.eq(4)).toHaveAttr('src', 'img/star-off.png');
2134        });
2135
2136        it ('should not call click callback when start function run without true option', function() {
2137                // given
2138                var star = $('#star').raty({ click: function(score, evt) { $(this).attr('title', score); }});
2139
2140                // when
2141                star.raty('score', 3);
2142
2143                // then
2144                var imgs = star.children('img');
2145
2146            expect(imgs.eq(0)).toHaveAttr('src', 'img/star-on.png');
2147            expect(imgs.eq(1)).toHaveAttr('src', 'img/star-on.png');
2148            expect(imgs.eq(2)).toHaveAttr('src', 'img/star-on.png');
2149            expect(imgs.eq(3)).toHaveAttr('src', 'img/star-off.png');
2150            expect(imgs.eq(4)).toHaveAttr('src', 'img/star-off.png');
2151
2152            expect(star.attr('title')).toEqual(undefined);
2153        });
2154
2155        it ('should set readOnly and fix the hints', function() {
2156                // given
2157                var star = $('#star').raty({ score: 1 });
2158
2159                // when
2160                star.raty('readOnly', true);
2161
2162                var imgs = star.children('img');
2163
2164                imgs.eq(3).mouseover().click();
2165
2166                // then
2167            expect(imgs.eq(0)).toHaveAttr('src', 'img/star-on.png');
2168            expect(imgs.eq(1)).toHaveAttr('src', 'img/star-off.png');
2169            expect(imgs.eq(2)).toHaveAttr('src', 'img/star-off.png');
2170            expect(imgs.eq(3)).toHaveAttr('src', 'img/star-off.png');
2171            expect(imgs.eq(4)).toHaveAttr('src', 'img/star-off.png');
2172
2173            expect(imgs.eq(0)).toHaveAttr('title', 'bad');
2174            expect(imgs.eq(1)).toHaveAttr('title', 'bad');
2175            expect(imgs.eq(2)).toHaveAttr('title', 'bad');
2176            expect(imgs.eq(3)).toHaveAttr('title', 'bad');
2177            expect(imgs.eq(4)).toHaveAttr('title', 'bad');
2178        });
2179
2180        it ('should set readOnly and hide cancel button', function() {
2181                // given
2182                var star = $('#star').raty({ cancel: true, path: '../img' });
2183
2184                // when
2185                star.raty('readOnly', true);
2186
2187                // then
2188            expect(star.children('.raty-cancel')).not.toBeVisible();
2189        });
2190
2191        it ('should unset readOnly and show cancel button', function() {
2192                // given
2193                var star = $('#star').raty({ cancel: true, readOnly: true, path: '../img' });
2194
2195                // when
2196                star.raty('readOnly', false);
2197
2198                // then
2199            expect(star.children('.raty-cancel')).toBeVisible();
2200        });
2201
2202        it ('should unset readOnly and put back the titles', function() {
2203                // given
2204                var star = $('#star').raty();
2205
2206                // when
2207                star.raty('readOnly', true).raty('readOnly', false);
2208
2209                var imgs = star.children('img');
2210
2211                imgs.eq(1).mouseover().click();
2212
2213                // then
2214            expect(imgs.eq(0)).toHaveAttr('src', 'img/star-on.png');
2215            expect(imgs.eq(1)).toHaveAttr('src', 'img/star-on.png');
2216            expect(imgs.eq(2)).toHaveAttr('src', 'img/star-off.png');
2217            expect(imgs.eq(3)).toHaveAttr('src', 'img/star-off.png');
2218            expect(imgs.eq(4)).toHaveAttr('src', 'img/star-off.png');
2219
2220            expect(imgs.eq(0)).toHaveAttr('title', 'bad');
2221            expect(imgs.eq(1)).toHaveAttr('title', 'poor');
2222            expect(imgs.eq(2)).toHaveAttr('title', 'regular');
2223            expect(imgs.eq(3)).toHaveAttr('title', 'good');
2224            expect(imgs.eq(4)).toHaveAttr('title', 'gorgeous');
2225        });
2226
2227        it ('should cancel without click', function() {
2228                // given
2229                var star = $('#star').raty({ score: 1 });
2230
2231                // when
2232                star.raty('cancel');
2233
2234                var imgs = star.children('img');
2235
2236                // then
2237            expect(imgs.eq(0)).toHaveAttr('src', 'img/star-off.png');
2238            expect(imgs.eq(1)).toHaveAttr('src', 'img/star-off.png');
2239            expect(imgs.eq(2)).toHaveAttr('src', 'img/star-off.png');
2240            expect(imgs.eq(3)).toHaveAttr('src', 'img/star-off.png');
2241            expect(imgs.eq(4)).toHaveAttr('src', 'img/star-off.png');
2242
2243            expect(star.children('input').val()).toEqual('');
2244        });
2245
2246        it ('should cancel with click', function() {
2247                // given
2248                var star = $('#star').raty({
2249                        click: function(score, evt) {
2250                                $(this).attr('title', (score === null) ? 'null' : score);
2251                        },
2252                        score: 1
2253                });
2254
2255                // when
2256                star.raty('cancel', true);
2257
2258                var imgs = star.children('img');
2259
2260                // then
2261            expect(imgs.eq(0)).toHaveAttr('src', 'img/star-off.png');
2262            expect(imgs.eq(1)).toHaveAttr('src', 'img/star-off.png');
2263            expect(imgs.eq(2)).toHaveAttr('src', 'img/star-off.png');
2264            expect(imgs.eq(3)).toHaveAttr('src', 'img/star-off.png');
2265            expect(imgs.eq(4)).toHaveAttr('src', 'img/star-off.png');
2266
2267            expect(star).toHaveAttr('title', 'null');
2268            expect(star.children('input').val()).toEqual('');
2269        });
2270
2271        it ('should click the star 3', function() {
2272                // given
2273                var star = $('#star').raty({
2274                        click: function(score, evt) {
2275                                $(this).attr('title', score);
2276                        }
2277                });
2278
2279                // when
2280                star.raty('click', 3);
2281
2282                // then
2283                var imgs = star.children('img');
2284
2285                expect(imgs.eq(0)).toHaveAttr('src', 'img/star-on.png');
2286                expect(imgs.eq(1)).toHaveAttr('src', 'img/star-on.png');
2287                expect(imgs.eq(2)).toHaveAttr('src', 'img/star-on.png');
2288                expect(imgs.eq(3)).toHaveAttr('src', 'img/star-off.png');
2289                expect(imgs.eq(4)).toHaveAttr('src', 'img/star-off.png');
2290                expect(star).toHaveAttr('title', 3);
2291        });
2292
2293        it ('should set a target on div with mouseover', function() {
2294                $('body').append('<div id="hint"></div>');
2295
2296                // given
2297                var hint = $('#hint'),
2298                        star = $('#star').raty({ target: '#hint', targetKeep: true, click: function(score, evt) { } });
2299
2300                // when
2301                star.raty('click', 4);
2302
2303                // then
2304                expect(hint).toHaveHtml('good');
2305
2306                hint.remove();
2307        });
2308
2309        it ('should set a target on div with mouseover', function() {
2310                $('body').append('<div id="hint"></div>');
2311
2312                // given
2313                var hint = $('#hint'),
2314                        star = $('#star').raty({ target: '#hint', targetKeep: true });
2315
2316                // when
2317                star.raty('score', 4);
2318
2319                // then
2320                expect(hint).toHaveHtml('good');
2321
2322                hint.remove();
2323        });
2324
2325        it ('should set a target on div with cancel', function() {
2326                $('body').append('<div id="hint"></div>');
2327
2328                // given
2329                var hint = $('#hint'),
2330                        star = $('#star').raty({ cancel: true, target: '#hint', targetKeep: true });
2331
2332                // when
2333                star.raty('cancel').mouseout();
2334
2335                // then
2336                expect(hint).toBeEmpty();
2337
2338                hint.remove();
2339        });
2340
2341        it ('[score / readOnly] should not set score when it is read-only', function() {
2342                // given
2343                var star = $('#star').raty({ readOnly: true });
2344
2345                // when
2346                star.raty('score', 1);
2347
2348                // then
2349                var imgs = star.children('img');
2350
2351                expect(imgs.eq(0)).toHaveAttr('src', 'img/star-off.png');
2352                expect(imgs.eq(1)).toHaveAttr('src', 'img/star-off.png');
2353                expect(imgs.eq(2)).toHaveAttr('src', 'img/star-off.png');
2354                expect(imgs.eq(3)).toHaveAttr('src', 'img/star-off.png');
2355                expect(imgs.eq(4)).toHaveAttr('src', 'img/star-off.png');
2356        });
2357
2358        it ('should not click when it is readOnly', function() {
2359                // given
2360                var star = $('#star').raty({ readOnly: true, click: function(score, evt) { } });
2361
2362                // when
2363                star.raty('click', 1);
2364
2365                // then
2366                var imgs = star.children('img');
2367
2368                expect(imgs.eq(0)).toHaveAttr('src', 'img/star-off.png');
2369                expect(imgs.eq(1)).toHaveAttr('src', 'img/star-off.png');
2370                expect(imgs.eq(2)).toHaveAttr('src', 'img/star-off.png');
2371                expect(imgs.eq(3)).toHaveAttr('src', 'img/star-off.png');
2372                expect(imgs.eq(4)).toHaveAttr('src', 'img/star-off.png');
2373        });
2374
2375        it ('should not cancel when it is readOnly', function() {
2376                // given
2377                var star = $('#star').raty({ readOnly: true, score: 1 });
2378
2379                // when
2380                star.raty('cancel');
2381
2382                // then
2383                var imgs = star.children('img');
2384
2385                expect(imgs.eq(0)).toHaveAttr('src', 'img/star-on.png');
2386                expect(imgs.eq(1)).toHaveAttr('src', 'img/star-off.png');
2387                expect(imgs.eq(2)).toHaveAttr('src', 'img/star-off.png');
2388                expect(imgs.eq(3)).toHaveAttr('src', 'img/star-off.png');
2389                expect(imgs.eq(4)).toHaveAttr('src', 'img/star-off.png');
2390                expect(star.children('input').val()).toEqual('1');
2391        });
2392
2393        it ('should set score as readonly when start with readOnly', function() {
2394                // given
2395                var star = $('#star').raty();
2396
2397                // when
2398                star.raty('readOnly', true);
2399
2400                // then
2401                expect(star.children('input')).toHaveAttr('readonly', 'readonly');
2402        });
2403
2404        it ('should set the cancel hint on target', function() {
2405                $('body').append('<div id="hint"></div>');
2406
2407                // given
2408                var hint        = $('#hint'),
2409                        star    = $('#star').raty({ cancel: true, target: '#hint', targetFormat: 'score: {score}', targetKeep: true });
2410
2411                // when
2412                star.raty('cancel').mouseleave();
2413
2414                // then
2415                expect(hint).toHaveHtml('score: ');
2416
2417                hint.remove();
2418        });
2419
2420        it ('should rebind the cancel button after readOnly be enabled', function() {
2421                // given
2422                var star = $('#star').raty({ cancel: true });
2423
2424                // when
2425                star.raty('readOnly', true).raty('readOnly', false).children('.raty-cancel').mouseenter();
2426
2427                var imgs = star.children('img').not('.raty-cancel');
2428
2429                // then
2430                expect(imgs.eq(0)).toHaveAttr('src', 'img/star-off.png');
2431                expect(imgs.eq(1)).toHaveAttr('src', 'img/star-off.png');
2432                expect(imgs.eq(2)).toHaveAttr('src', 'img/star-off.png');
2433                expect(imgs.eq(3)).toHaveAttr('src', 'img/star-off.png');
2434                expect(imgs.eq(4)).toHaveAttr('src', 'img/star-off.png');
2435        });
2436
2437        it ('[cancel] should cancel after a read-only false', function() {
2438                // given
2439                var star = $('#star').raty({ score: 1 }).raty('readOnly', true).raty('readOnly', false);
2440
2441                // when
2442                star.raty('cancel');
2443
2444                var imgs = star.children('img');
2445
2446                // then
2447            expect(imgs.eq(0)).toHaveAttr('src', 'img/star-off.png');
2448            expect(imgs.eq(1)).toHaveAttr('src', 'img/star-off.png');
2449            expect(imgs.eq(2)).toHaveAttr('src', 'img/star-off.png');
2450            expect(imgs.eq(3)).toHaveAttr('src', 'img/star-off.png');
2451            expect(imgs.eq(4)).toHaveAttr('src', 'img/star-off.png');
2452
2453            expect(star.children('input').val()).toEqual('');
2454        });
2455
2456});
2457
2458describe('Using function with class', function() {
2459       
2460        beforeEach(function() {
2461                $('body').append('<div class="star"></div><div class="star"></div><div class="star"></div>');
2462        });
2463
2464        afterEach(function() {
2465                $('.star').remove();
2466        });
2467
2468        it ('should start the starts with 3 stars', function() {
2469                // given
2470                var stars = $('.star').raty();
2471
2472                // when
2473                stars.raty('score', 3);
2474
2475                // then
2476                var imgs1 = stars.eq(0).children('img'),
2477                        imgs2 = stars.eq(1).children('img'),
2478                        imgs3 = stars.eq(2).children('img');
2479
2480                expect(imgs1.eq(0)).toHaveAttr('src', 'img/star-on.png');
2481                expect(imgs1.eq(1)).toHaveAttr('src', 'img/star-on.png');
2482                expect(imgs1.eq(2)).toHaveAttr('src', 'img/star-on.png');
2483                expect(imgs1.eq(3)).toHaveAttr('src', 'img/star-off.png');
2484                expect(imgs1.eq(4)).toHaveAttr('src', 'img/star-off.png');
2485
2486                expect(imgs2.eq(0)).toHaveAttr('src', 'img/star-on.png');
2487                expect(imgs2.eq(1)).toHaveAttr('src', 'img/star-on.png');
2488                expect(imgs2.eq(2)).toHaveAttr('src', 'img/star-on.png');
2489                expect(imgs2.eq(3)).toHaveAttr('src', 'img/star-off.png');
2490                expect(imgs2.eq(4)).toHaveAttr('src', 'img/star-off.png');
2491
2492                expect(imgs3.eq(0)).toHaveAttr('src', 'img/star-on.png');
2493                expect(imgs3.eq(1)).toHaveAttr('src', 'img/star-on.png');
2494                expect(imgs3.eq(2)).toHaveAttr('src', 'img/star-on.png');
2495                expect(imgs3.eq(3)).toHaveAttr('src', 'img/star-off.png');
2496                expect(imgs3.eq(4)).toHaveAttr('src', 'img/star-off.png');
2497        });
2498       
2499        it ('should set readOnly and fix the hint', function() {
2500                // given
2501                var stars = $('.star').raty();
2502
2503                // when
2504                stars.raty('readOnly', true);
2505
2506                var imgs1 = stars.eq(0).children('img'),
2507                        imgs2 = stars.eq(1).children('img'),
2508                        imgs3 = stars.eq(2).children('img');
2509       
2510                imgs1.eq(3).mouseover().click();
2511                imgs2.eq(3).mouseover().click();
2512                imgs3.eq(3).mouseover().click();
2513
2514                // then
2515                expect(imgs1.eq(0)).toHaveAttr('src', 'img/star-off.png');
2516                expect(imgs1.eq(1)).toHaveAttr('src', 'img/star-off.png');
2517                expect(imgs1.eq(2)).toHaveAttr('src', 'img/star-off.png');
2518                expect(imgs1.eq(3)).toHaveAttr('src', 'img/star-off.png');
2519                expect(imgs1.eq(4)).toHaveAttr('src', 'img/star-off.png');
2520
2521                expect(imgs1.eq(0)).toHaveAttr('title', 'not rated yet');
2522            expect(imgs1.eq(1)).toHaveAttr('title', 'not rated yet');
2523            expect(imgs1.eq(2)).toHaveAttr('title', 'not rated yet');
2524            expect(imgs1.eq(3)).toHaveAttr('title', 'not rated yet');
2525            expect(imgs1.eq(4)).toHaveAttr('title', 'not rated yet');
2526
2527                expect(imgs2.eq(0)).toHaveAttr('src', 'img/star-off.png');
2528                expect(imgs2.eq(1)).toHaveAttr('src', 'img/star-off.png');
2529                expect(imgs2.eq(2)).toHaveAttr('src', 'img/star-off.png');
2530                expect(imgs2.eq(3)).toHaveAttr('src', 'img/star-off.png');
2531                expect(imgs2.eq(4)).toHaveAttr('src', 'img/star-off.png');
2532
2533                expect(imgs2.eq(0)).toHaveAttr('title', 'not rated yet');
2534            expect(imgs2.eq(1)).toHaveAttr('title', 'not rated yet');
2535            expect(imgs2.eq(2)).toHaveAttr('title', 'not rated yet');
2536            expect(imgs2.eq(3)).toHaveAttr('title', 'not rated yet');
2537            expect(imgs2.eq(4)).toHaveAttr('title', 'not rated yet');
2538
2539                expect(imgs3.eq(0)).toHaveAttr('src', 'img/star-off.png');
2540                expect(imgs3.eq(1)).toHaveAttr('src', 'img/star-off.png');
2541                expect(imgs3.eq(2)).toHaveAttr('src', 'img/star-off.png');
2542                expect(imgs3.eq(3)).toHaveAttr('src', 'img/star-off.png');
2543                expect(imgs3.eq(4)).toHaveAttr('src', 'img/star-off.png');
2544
2545                expect(imgs3.eq(0)).toHaveAttr('title', 'not rated yet');
2546            expect(imgs3.eq(1)).toHaveAttr('title', 'not rated yet');
2547            expect(imgs3.eq(2)).toHaveAttr('title', 'not rated yet');
2548            expect(imgs3.eq(3)).toHaveAttr('title', 'not rated yet');
2549            expect(imgs3.eq(4)).toHaveAttr('title', 'not rated yet');
2550        });
2551
2552        it ('should unset readOnly and put back the titles', function() {
2553                // given
2554                var stars = $('.star').raty();
2555
2556                // when
2557                stars.raty('readOnly', true).raty('readOnly', false);
2558
2559                var imgs1 = stars.eq(0).children('img'),
2560                        imgs2 = stars.eq(1).children('img'),
2561                        imgs3 = stars.eq(2).children('img');
2562
2563                imgs1.eq(3).mouseover().click();
2564                imgs2.eq(3).mouseover().click();
2565                imgs3.eq(3).mouseover().click();
2566
2567                // then
2568                expect(imgs1.eq(0)).toHaveAttr('src', 'img/star-on.png');
2569                expect(imgs1.eq(1)).toHaveAttr('src', 'img/star-on.png');
2570                expect(imgs1.eq(2)).toHaveAttr('src', 'img/star-on.png');
2571                expect(imgs1.eq(3)).toHaveAttr('src', 'img/star-on.png');
2572                expect(imgs1.eq(4)).toHaveAttr('src', 'img/star-off.png');
2573
2574                expect(imgs1.eq(0)).toHaveAttr('title', 'bad');
2575            expect(imgs1.eq(1)).toHaveAttr('title', 'poor');
2576            expect(imgs1.eq(2)).toHaveAttr('title', 'regular');
2577            expect(imgs1.eq(3)).toHaveAttr('title', 'good');
2578            expect(imgs1.eq(4)).toHaveAttr('title', 'gorgeous');
2579
2580                expect(imgs2.eq(0)).toHaveAttr('src', 'img/star-on.png');
2581                expect(imgs2.eq(1)).toHaveAttr('src', 'img/star-on.png');
2582                expect(imgs2.eq(2)).toHaveAttr('src', 'img/star-on.png');
2583                expect(imgs2.eq(3)).toHaveAttr('src', 'img/star-on.png');
2584                expect(imgs2.eq(4)).toHaveAttr('src', 'img/star-off.png');
2585
2586                expect(imgs2.eq(0)).toHaveAttr('title', 'bad');
2587            expect(imgs2.eq(1)).toHaveAttr('title', 'poor');
2588            expect(imgs2.eq(2)).toHaveAttr('title', 'regular');
2589            expect(imgs2.eq(3)).toHaveAttr('title', 'good');
2590            expect(imgs2.eq(4)).toHaveAttr('title', 'gorgeous');
2591
2592                expect(imgs3.eq(0)).toHaveAttr('src', 'img/star-on.png');
2593                expect(imgs3.eq(1)).toHaveAttr('src', 'img/star-on.png');
2594                expect(imgs3.eq(2)).toHaveAttr('src', 'img/star-on.png');
2595                expect(imgs3.eq(3)).toHaveAttr('src', 'img/star-on.png');
2596                expect(imgs3.eq(4)).toHaveAttr('src', 'img/star-off.png');
2597
2598                expect(imgs3.eq(0)).toHaveAttr('title', 'bad');
2599            expect(imgs3.eq(1)).toHaveAttr('title', 'poor');
2600            expect(imgs3.eq(2)).toHaveAttr('title', 'regular');
2601            expect(imgs3.eq(3)).toHaveAttr('title', 'good');
2602            expect(imgs3.eq(4)).toHaveAttr('title', 'gorgeous');
2603        });
2604       
2605        it ('should cancel without click', function() {
2606                // given
2607                var stars = $('.star').raty({ score: 1 });
2608
2609                // when
2610                stars.raty('cancel');
2611
2612                // then
2613                var imgs1 = stars.eq(0).children('img'),
2614                        imgs2 = stars.eq(1).children('img'),
2615                        imgs3 = stars.eq(2).children('img');
2616
2617                expect(imgs1.eq(0)).toHaveAttr('src', 'img/star-off.png');
2618                expect(imgs1.eq(1)).toHaveAttr('src', 'img/star-off.png');
2619                expect(imgs1.eq(2)).toHaveAttr('src', 'img/star-off.png');
2620                expect(imgs1.eq(3)).toHaveAttr('src', 'img/star-off.png');
2621                expect(imgs1.eq(4)).toHaveAttr('src', 'img/star-off.png');
2622                expect(stars.eq(0).children('input').val()).toEqual('');
2623
2624                expect(imgs2.eq(0)).toHaveAttr('src', 'img/star-off.png');
2625                expect(imgs2.eq(1)).toHaveAttr('src', 'img/star-off.png');
2626                expect(imgs2.eq(2)).toHaveAttr('src', 'img/star-off.png');
2627                expect(imgs2.eq(3)).toHaveAttr('src', 'img/star-off.png');
2628                expect(imgs2.eq(4)).toHaveAttr('src', 'img/star-off.png');
2629                expect(stars.eq(1).children('input').val()).toEqual('');
2630
2631                expect(imgs3.eq(0)).toHaveAttr('src', 'img/star-off.png');
2632                expect(imgs3.eq(1)).toHaveAttr('src', 'img/star-off.png');
2633                expect(imgs3.eq(2)).toHaveAttr('src', 'img/star-off.png');
2634                expect(imgs3.eq(3)).toHaveAttr('src', 'img/star-off.png');
2635                expect(imgs3.eq(4)).toHaveAttr('src', 'img/star-off.png');
2636                expect(stars.eq(2).children('input').val()).toEqual('');
2637        });
2638       
2639        it ('should cancel with click', function() {
2640                // given
2641                var stars = $('.star').raty({
2642                        click: function(score, evt) {
2643                                $(this).attr('title', (score === null) ? 'null' : score);
2644                        },
2645                        score: 1
2646                });
2647
2648                // when
2649                stars.raty('cancel', true);
2650
2651                // then
2652                var imgs1 = stars.eq(0).children('img'),
2653                        imgs2 = stars.eq(1).children('img'),
2654                        imgs3 = stars.eq(2).children('img');
2655       
2656                expect(imgs1.eq(0)).toHaveAttr('src', 'img/star-off.png');
2657                expect(imgs1.eq(1)).toHaveAttr('src', 'img/star-off.png');
2658                expect(imgs1.eq(2)).toHaveAttr('src', 'img/star-off.png');
2659                expect(imgs1.eq(3)).toHaveAttr('src', 'img/star-off.png');
2660                expect(imgs1.eq(4)).toHaveAttr('src', 'img/star-off.png');
2661                expect(stars.eq(0)).toHaveAttr('title', 'null');
2662                expect(stars.eq(0).children('input').val()).toEqual('');
2663
2664                expect(imgs2.eq(0)).toHaveAttr('src', 'img/star-off.png');
2665                expect(imgs2.eq(1)).toHaveAttr('src', 'img/star-off.png');
2666                expect(imgs2.eq(2)).toHaveAttr('src', 'img/star-off.png');
2667                expect(imgs2.eq(3)).toHaveAttr('src', 'img/star-off.png');
2668                expect(imgs2.eq(4)).toHaveAttr('src', 'img/star-off.png');
2669                expect(stars.eq(1)).toHaveAttr('title', 'null');
2670                expect(stars.eq(1).children('input').val()).toEqual('');
2671
2672                expect(imgs3.eq(0)).toHaveAttr('src', 'img/star-off.png');
2673                expect(imgs3.eq(1)).toHaveAttr('src', 'img/star-off.png');
2674                expect(imgs3.eq(2)).toHaveAttr('src', 'img/star-off.png');
2675                expect(imgs3.eq(3)).toHaveAttr('src', 'img/star-off.png');
2676                expect(imgs3.eq(4)).toHaveAttr('src', 'img/star-off.png');
2677                expect(stars.eq(1)).toHaveAttr('title', 'null');
2678                expect(stars.eq(2).children('input').val()).toEqual('');
2679        });
2680
2681        it ('should click the star 3', function() {
2682                // given
2683                var stars = $('.star').raty({
2684                        click: function(score, evt) {
2685                                $(this).attr('title', score);
2686                        }
2687                });
2688
2689                // when
2690                stars.raty('click', 3);
2691
2692                // then
2693                var imgs1 = stars.eq(0).children('img'),
2694                        imgs2 = stars.eq(1).children('img'),
2695                        imgs3 = stars.eq(2).children('img');
2696       
2697                expect(imgs1.eq(0)).toHaveAttr('src', 'img/star-on.png');
2698                expect(imgs1.eq(1)).toHaveAttr('src', 'img/star-on.png');
2699                expect(imgs1.eq(2)).toHaveAttr('src', 'img/star-on.png');
2700                expect(imgs1.eq(3)).toHaveAttr('src', 'img/star-off.png');
2701                expect(imgs1.eq(4)).toHaveAttr('src', 'img/star-off.png');
2702                expect(stars.eq(0)).toHaveAttr('title', 3);
2703
2704                expect(imgs2.eq(0)).toHaveAttr('src', 'img/star-on.png');
2705                expect(imgs2.eq(1)).toHaveAttr('src', 'img/star-on.png');
2706                expect(imgs2.eq(2)).toHaveAttr('src', 'img/star-on.png');
2707                expect(imgs2.eq(3)).toHaveAttr('src', 'img/star-off.png');
2708                expect(imgs2.eq(4)).toHaveAttr('src', 'img/star-off.png');
2709                expect(stars.eq(1)).toHaveAttr('title', 3);
2710
2711                expect(imgs3.eq(0)).toHaveAttr('src', 'img/star-on.png');
2712                expect(imgs3.eq(1)).toHaveAttr('src', 'img/star-on.png');
2713                expect(imgs3.eq(2)).toHaveAttr('src', 'img/star-on.png');
2714                expect(imgs3.eq(3)).toHaveAttr('src', 'img/star-off.png');
2715                expect(imgs3.eq(4)).toHaveAttr('src', 'img/star-off.png');
2716                expect(stars.eq(2)).toHaveAttr('title', 3);
2717        });
2718       
2719        it ('should set a target on div with click', function() {
2720                $('body').append('<div id="hint1"></div><div id="hint2"></div><div id="hint3"></div>');
2721
2722                // given
2723                var hint1       = $('#hint1'),
2724                        hint2   = $('#hint2'),
2725                        hint3   = $('#hint3'),
2726                        stars   = $('.star');
2727
2728                stars.eq(0).raty({ target: '#hint1', targetKeep: true, click: function(score, evt) { } });
2729                stars.eq(1).raty({ target: '#hint2', targetKeep: true, click: function(score, evt) { } });
2730                stars.eq(2).raty({ target: '#hint3', targetKeep: true, click: function(score, evt) { } });
2731
2732                // when
2733                stars.raty('click', 4);
2734
2735                // then
2736                expect(hint1).toHaveHtml('good');
2737                expect(hint2).toHaveHtml('good');
2738                expect(hint3).toHaveHtml('good');
2739
2740                hint1.remove();
2741                hint2.remove();
2742                hint3.remove();
2743        });
2744       
2745        it ('should set a target on div with start', function() {
2746                $('body').append('<div id="hint1"></div><div id="hint2"></div><div id="hint3"></div>');
2747
2748                // given
2749                var hint1       = $('#hint1'),
2750                        hint2   = $('#hint2'),
2751                        hint3   = $('#hint3'),
2752                        stars   = $('.star');
2753       
2754                stars.eq(0).raty({ target: '#hint1', targetKeep: true, click: function(score, evt) { } });
2755                stars.eq(1).raty({ target: '#hint2', targetKeep: true, click: function(score, evt) { } });
2756                stars.eq(2).raty({ target: '#hint3', targetKeep: true, click: function(score, evt) { } });
2757
2758                // when
2759                stars.raty('score', 4);
2760
2761                // then
2762                expect(hint1).toHaveHtml('good');
2763                expect(hint2).toHaveHtml('good');
2764                expect(hint3).toHaveHtml('good');
2765
2766                hint1.remove();
2767                hint2.remove();
2768                hint3.remove();
2769        });
2770
2771        it ('should set a target on div with cancel', function() {
2772                $('body').append('<div id="hint1"></div><div id="hint2"></div><div id="hint3"></div>');
2773
2774                // given
2775                var hint1       = $('#hint1'),
2776                        hint2   = $('#hint2'),
2777                        hint3   = $('#hint3'),
2778                        stars   = $('.star');
2779       
2780                stars.eq(0).raty({ cancel: true, target: '#hint1', targetKeep: true, click: function(score, evt) { } });
2781                stars.eq(1).raty({ cancel: true, target: '#hint2', targetKeep: true, click: function(score, evt) { } });
2782                stars.eq(2).raty({ cancel: true, target: '#hint3', targetKeep: true, click: function(score, evt) { } });
2783
2784                // when
2785                stars.raty('cancel').mouseout();
2786
2787                // then
2788                expect(hint1).toBeEmpty();
2789                expect(hint2).toBeEmpty();
2790                expect(hint3).toBeEmpty();
2791
2792                hint1.remove();
2793                hint2.remove();
2794                hint3.remove();
2795        });
2796
2797        it ('should not start when it is readOnly', function() {
2798                // given
2799                var stars = $('.star').raty({ readOnly: true });
2800
2801                // when
2802                stars.eq(0).raty('score', 1);
2803                stars.eq(1).raty('score', 1);
2804                stars.eq(2).raty('score', 1);
2805
2806                // then
2807                var imgs1 = stars.eq(0).children('img'),
2808                        imgs2 = stars.eq(1).children('img'),
2809                        imgs3 = stars.eq(2).children('img');
2810
2811                expect(imgs1.eq(0)).toHaveAttr('src', 'img/star-off.png');
2812                expect(imgs1.eq(1)).toHaveAttr('src', 'img/star-off.png');
2813                expect(imgs1.eq(2)).toHaveAttr('src', 'img/star-off.png');
2814                expect(imgs1.eq(3)).toHaveAttr('src', 'img/star-off.png');
2815                expect(imgs1.eq(4)).toHaveAttr('src', 'img/star-off.png');
2816
2817                expect(imgs2.eq(0)).toHaveAttr('src', 'img/star-off.png');
2818                expect(imgs2.eq(1)).toHaveAttr('src', 'img/star-off.png');
2819                expect(imgs2.eq(2)).toHaveAttr('src', 'img/star-off.png');
2820                expect(imgs2.eq(3)).toHaveAttr('src', 'img/star-off.png');
2821                expect(imgs2.eq(4)).toHaveAttr('src', 'img/star-off.png');
2822
2823                expect(imgs3.eq(0)).toHaveAttr('src', 'img/star-off.png');
2824                expect(imgs3.eq(1)).toHaveAttr('src', 'img/star-off.png');
2825                expect(imgs3.eq(2)).toHaveAttr('src', 'img/star-off.png');
2826                expect(imgs3.eq(3)).toHaveAttr('src', 'img/star-off.png');
2827                expect(imgs3.eq(4)).toHaveAttr('src', 'img/star-off.png');
2828        });
2829
2830        it ('should not click when it is readOnly', function() {
2831                // given
2832                var stars = $('.star').raty({ readOnly: true, click: function(score, evt) { } });
2833
2834                // when
2835                stars.eq(0).raty('click', 1);
2836                stars.eq(1).raty('click', 1);
2837                stars.eq(2).raty('click', 1);
2838
2839                // then
2840                var imgs1 = stars.eq(0).children('img'),
2841                        imgs2 = stars.eq(1).children('img'),
2842                        imgs3 = stars.eq(2).children('img');
2843
2844                expect(imgs1.eq(0)).toHaveAttr('src', 'img/star-off.png');
2845                expect(imgs1.eq(1)).toHaveAttr('src', 'img/star-off.png');
2846                expect(imgs1.eq(2)).toHaveAttr('src', 'img/star-off.png');
2847                expect(imgs1.eq(3)).toHaveAttr('src', 'img/star-off.png');
2848                expect(imgs1.eq(4)).toHaveAttr('src', 'img/star-off.png');
2849
2850                expect(imgs2.eq(0)).toHaveAttr('src', 'img/star-off.png');
2851                expect(imgs2.eq(1)).toHaveAttr('src', 'img/star-off.png');
2852                expect(imgs2.eq(2)).toHaveAttr('src', 'img/star-off.png');
2853                expect(imgs2.eq(3)).toHaveAttr('src', 'img/star-off.png');
2854                expect(imgs2.eq(4)).toHaveAttr('src', 'img/star-off.png');
2855
2856                expect(imgs3.eq(0)).toHaveAttr('src', 'img/star-off.png');
2857                expect(imgs3.eq(1)).toHaveAttr('src', 'img/star-off.png');
2858                expect(imgs3.eq(2)).toHaveAttr('src', 'img/star-off.png');
2859                expect(imgs3.eq(3)).toHaveAttr('src', 'img/star-off.png');
2860                expect(imgs3.eq(4)).toHaveAttr('src', 'img/star-off.png');
2861        });
2862
2863        it ('should not cancel when it is readOnly', function() {
2864                // given
2865                var stars = $('.star').raty({ readOnly: true, score: 1 });
2866
2867                // when
2868                stars.eq(0).raty('cancel');
2869                stars.eq(1).raty('cancel');
2870                stars.eq(2).raty('cancel');
2871
2872                // then
2873                var imgs1 = stars.eq(0).children('img'),
2874                        imgs2 = stars.eq(1).children('img'),
2875                        imgs3 = stars.eq(2).children('img');
2876
2877                expect(imgs1.eq(0)).toHaveAttr('src', 'img/star-on.png');
2878                expect(imgs1.eq(1)).toHaveAttr('src', 'img/star-off.png');
2879                expect(imgs1.eq(2)).toHaveAttr('src', 'img/star-off.png');
2880                expect(imgs1.eq(3)).toHaveAttr('src', 'img/star-off.png');
2881                expect(imgs1.eq(4)).toHaveAttr('src', 'img/star-off.png');
2882                expect(stars.eq(0).children('input').val()).toEqual('1');
2883
2884                expect(imgs2.eq(0)).toHaveAttr('src', 'img/star-on.png');
2885                expect(imgs2.eq(1)).toHaveAttr('src', 'img/star-off.png');
2886                expect(imgs2.eq(2)).toHaveAttr('src', 'img/star-off.png');
2887                expect(imgs2.eq(3)).toHaveAttr('src', 'img/star-off.png');
2888                expect(imgs2.eq(4)).toHaveAttr('src', 'img/star-off.png');
2889                expect(stars.eq(1).children('input').val()).toEqual('1');
2890
2891                expect(imgs3.eq(0)).toHaveAttr('src', 'img/star-on.png');
2892                expect(imgs3.eq(1)).toHaveAttr('src', 'img/star-off.png');
2893                expect(imgs3.eq(2)).toHaveAttr('src', 'img/star-off.png');
2894                expect(imgs3.eq(3)).toHaveAttr('src', 'img/star-off.png');
2895                expect(imgs3.eq(4)).toHaveAttr('src', 'img/star-off.png');
2896                expect(stars.eq(2).children('input').val()).toEqual('1');
2897        });
2898
2899        it ('should set score as readonly when start with readOnly', function() {
2900                // given
2901                var stars = $('.star').raty();
2902
2903                // when
2904                stars.eq(0).raty('readOnly', true);
2905                stars.eq(1).raty('readOnly', true);
2906                stars.eq(2).raty('readOnly', true);
2907
2908                // then
2909                expect(stars.eq(0).children('input')).toHaveAttr('readonly', 'readonly');
2910                expect(stars.eq(1).children('input')).toHaveAttr('readonly', 'readonly');
2911                expect(stars.eq(2).children('input')).toHaveAttr('readonly', 'readonly');
2912        });
2913
2914        it ('should rebind the cancel button after readOnly be enabled', function() {
2915                // given
2916                var stars = $('.star').raty({ cancel: true });
2917
2918                // when
2919                stars.raty('readOnly', true).raty('readOnly', false).children('.raty-cancel').mouseenter();
2920
2921                // then
2922                var imgs1 = stars.eq(0).children('img').not('.raty-cancel'),
2923                        imgs2 = stars.eq(1).children('img').not('.raty-cancel'),
2924                        imgs3 = stars.eq(2).children('img').not('.raty-cancel');
2925
2926                expect(imgs1.eq(0)).toHaveAttr('src', 'img/star-off.png');
2927                expect(imgs1.eq(1)).toHaveAttr('src', 'img/star-off.png');
2928                expect(imgs1.eq(2)).toHaveAttr('src', 'img/star-off.png');
2929                expect(imgs1.eq(3)).toHaveAttr('src', 'img/star-off.png');
2930                expect(imgs1.eq(4)).toHaveAttr('src', 'img/star-off.png');
2931       
2932                expect(imgs2.eq(0)).toHaveAttr('src', 'img/star-off.png');
2933                expect(imgs2.eq(1)).toHaveAttr('src', 'img/star-off.png');
2934                expect(imgs2.eq(2)).toHaveAttr('src', 'img/star-off.png');
2935                expect(imgs2.eq(3)).toHaveAttr('src', 'img/star-off.png');
2936                expect(imgs2.eq(4)).toHaveAttr('src', 'img/star-off.png');
2937       
2938                expect(imgs3.eq(0)).toHaveAttr('src', 'img/star-off.png');
2939                expect(imgs3.eq(1)).toHaveAttr('src', 'img/star-off.png');
2940                expect(imgs3.eq(2)).toHaveAttr('src', 'img/star-off.png');
2941                expect(imgs3.eq(3)).toHaveAttr('src', 'img/star-off.png');
2942                expect(imgs3.eq(4)).toHaveAttr('src', 'img/star-off.png');
2943        });
2944
2945        it ('[score] should set score on rating that is not read-only', function() {
2946                // given
2947                var stars = $('.star');
2948
2949                stars.raty();
2950                stars.eq(1).raty('readOnly', true);
2951
2952                // when
2953                stars.raty('score', 2);
2954
2955                // then
2956                expect(stars.eq(0).children('input')).toHaveValue('2');
2957            expect(stars.eq(1).children('input').val()).toEqual('');
2958            expect(stars.eq(2).children('input')).toHaveValue('2');
2959        });
2960
2961        it ('[click] should click on rating that is not read-only', function() {
2962                // given
2963                var stars = $('.star');
2964
2965                stars.raty({ click: function() { } });
2966                stars.eq(1).raty('readOnly', true);
2967
2968                // when
2969                stars.raty('click', 2);
2970
2971                // then
2972                expect(stars.eq(0).children('input')).toHaveValue('2');
2973            expect(stars.eq(1).children('input').val()).toEqual('');
2974            expect(stars.eq(2).children('input')).toHaveValue('2');
2975        });
2976
2977        it ('[cancel] should cancel all rating that is not read-only', function() {
2978                // given
2979                var stars = $('.star');
2980
2981                stars.raty({ score: 2 });
2982                stars.eq(1).raty('readOnly', true);
2983
2984                // when
2985                stars.raty('cancel');
2986
2987                // then
2988                expect(stars.eq(0).children('input').val()).toEqual('');
2989            expect(stars.eq(1).children('input')).toHaveValue('2');
2990            expect(stars.eq(2).children('input').val()).toEqual('');
2991        });
2992
2993});
Note: See TracBrowser for help on using the repository browser.