cancel_spec.js
7.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
describe('#cancel', function() {
beforeEach(function() {
$.fn.raty.defaults.path = '../lib/images';
this.el = Helper.create('#el');
});
afterEach(function() {
Helper.clear();
});
it ('creates the element', function() {
// given
// when
this.el.raty({ cancel: true });
// then
var cancel = this.el.children('.raty-cancel');
expect(cancel).toHaveClass('raty-cancel');
expect(cancel).toHaveAttr('title', 'Cancel this rating!');
expect(cancel).toHaveAttr('alt', 'x');
expect(cancel).toHaveAttr('src', '../lib/images/cancel-off.png');
});
context('on mouseover', function() {
it ('turns on', function() {
// given
this.el.raty({ cancel: true });
var cancel = this.el.children('.raty-cancel');
// when
cancel.trigger('mouseover');
// then
expect(cancel).toHaveAttr('src', '../lib/images/cancel-on.png');
});
it ('keeps the :cancelClass', function() {
// given
this.el.raty({ cancel: true });
var cancel = this.el.children('.raty-cancel');
// when
cancel.trigger('mouseover');
// then
expect(cancel).toHaveClass(this.el[0].opt.cancelClass);
});
context('with stars on', function() {
it ('turns off the stars', function() {
// given
this.el.raty({ score: 3, cancel: true });
var
cancel = this.el.children('.raty-cancel'),
stars = this.el.children('img:not(.raty-cancel)');
// when
cancel.trigger('mouseover');
// then
expect(stars).toHaveAttr('src', '../lib/images/star-off.png');
});
});
context('with :starType', function() {
it ('uses the given element', function() {
// given
this.el.raty({ cancel: true, starType: 'i' });
var cancel = this.el.children('.raty-cancel');
// when
cancel.trigger('mouseover');
// then
expect(cancel[0].tagName).toEqual('I');
});
it ('keeps the :cancelClass', function() {
// given
this.el.raty({ cancel: true, starType: 'i' });
var cancel = this.el.children('.raty-cancel');
// when
cancel.trigger('mouseover');
// then
expect(cancel).toHaveClass(this.el[0].opt.cancelClass);
});
it ('sets class replacing dot to hiphen', function() {
// given
this.el.raty({ cancel: true, starType: 'i' });
var cancel = this.el.children('.raty-cancel');
// when
cancel.trigger('mouseover');
// then
expect(cancel).toHaveClass('cancel-on-png');
});
it ('does not set "src" attribute', function() {
// given
this.el.raty({ cancel: true, starType: 'i' });
var cancel = this.el.children('.raty-cancel');
// when
cancel.trigger('mouseover');
// then
expect(cancel).not.toHaveAttr('src');
});
it ('sets "data-alt" attribute', function() {
// given
this.el.raty({ cancel: true, starType: 'i' });
var cancel = this.el.children('.raty-cancel');
// when
cancel.trigger('mouseover');
// then
expect(cancel).toHaveAttr('data-alt');
});
it ('does not set "alt" attribute', function() {
// given
this.el.raty({ cancel: true, starType: 'i' });
var cancel = this.el.children('.raty-cancel');
// when
cancel.trigger('mouseover');
// then
expect(cancel).not.toHaveAttr('alt');
});
});
});
context('on mouseleave', function() {
it ('turns off', function() {
// given
this.el.raty({ cancel: true });
var cancel = this.el.children('.raty-cancel');
// when
cancel.trigger('mouseleave');
// then
expect(cancel).toHaveAttr('src', '../lib/images/cancel-off.png');
});
it ('keeps the :cancelClass', function() {
// given
this.el.raty({ cancel: true });
var cancel = this.el.children('.raty-cancel');
// when
cancel.trigger('mouseleave');
// then
expect(cancel).toHaveClass(this.el[0].opt.cancelClass);
});
context('with stars turned on', function() {
it ('turns on the star again', function() {
// given
this.el.raty({ score: 5, cancel: true });
var
cancel = this.el.children('.raty-cancel'),
stars = this.el.children('img:not(.raty-cancel)');
// when
cancel.trigger('mouseleave');
// then
expect(stars).toHaveAttr('src', '../lib/images/star-on.png');
});
});
});
context('on click', function() {
it ('cancel the rating', function() {
// given
this.el.raty({ cancel: true, score: 1 });
var
cancel = this.el.children('.raty-cancel'),
input = this.el.children('input'),
stars = this.el.children('img:not(.raty-cancel)');
// when
cancel.trigger('click').trigger('mouseleave');
// then
expect(stars).toHaveAttr('src', '../lib/images/star-off.png');
expect(input).toHaveValue('');
});
});
context('when starts :readOnly', function() {
it ('starts hidden', function() {
// given
this.el.raty({ cancel: true, readOnly: true });
var cancel = this.el.children('.raty-cancel');
// when
this.el.raty('readOnly', true);
// then
expect(cancel).toBeHidden();
});
context('on click', function() {
it ('does not cancel the rating', function() {
// given
this.el.raty({ cancel: true, readOnly: true, score: 5 });
var
cancel = this.el.children('.raty-cancel'),
input = this.el.children('input'),
stars = this.el.children('img:not(.raty-cancel)');
// when
cancel.trigger('click').trigger('mouseleave');
// then
expect(stars).toHaveAttr('src', '../lib/images/star-on.png');
expect(input).toHaveValue('5');
});
});
});
context('when become :readOnly', function() {
it ('becomes hidden', function() {
// given
this.el.raty({ cancel: true });
// when
this.el.raty('readOnly', true);
// then
expect(this.el.children('.raty-cancel')).toBeHidden();
});
});
context('with :starType', function() {
it ('uses the given element', function() {
// given
// when
this.el.raty({ cancel: true, starType: 'i' });
// then
expect(this.el.children('.raty-cancel')[0].tagName).toEqual('I');
});
it ('keeps the :cancelClass', function() {
// given
this.el.raty({ cancel: true, starType: 'i' });
var cancel = this.el.children('.raty-cancel');
// when
cancel.trigger('mouseleave');
// then
expect(cancel).toHaveClass(this.el[0].opt.cancelClass);
});
it ('sets class replacing dot to hiphen', function() {
// given
// when
this.el.raty({ cancel: true, starType: 'i' });
// then
expect(this.el.children('.raty-cancel')).toHaveClass('cancel-off-png');
});
it ('does not set "src" attribute', function() {
// given
// when
this.el.raty({ cancel: true, starType: 'i' });
// then
expect(this.el.children('.raty-cancel')).not.toHaveProp('src');
});
it ('sets "data-alt" attribute', function() {
// given
// when
this.el.raty({ cancel: true, starType: 'i' });
// then
expect(this.el.children('.raty-cancel')).toHaveAttr('data-alt', 'x');
});
it ('does not set "alt" attribute', function() {
// given
// when
this.el.raty({ cancel: true, starType: 'i' });
// then
expect(this.el.children('.raty-cancel')).not.toHaveProp('alt');
});
});
});