forked from noelboss/featherlight
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeatherlight_gallery_test.js
More file actions
221 lines (204 loc) · 8.67 KB
/
Copy pathfeatherlight_gallery_test.js
File metadata and controls
221 lines (204 loc) · 8.67 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
var expect = chai.expect;
(function($) {
var $htmlFixtures = null;
var resetFixtures = function(){
if (!$htmlFixtures) $htmlFixtures = $('#fixtures').detach();
$('body >:not(#mocha)').remove()
$('body').append($htmlFixtures.clone(true));
};
var triggerKeyCode = function(keyCode) {
$(window).trigger($.Event("keyup", { keyCode: keyCode }));
};
$.fx.off = true;
$.fn.toJSON = function() { return 'jQuery: ' + this.selector + ' (' + this.length + ')' };
describe('Featherlight Gallery', function() {
beforeEach(resetFixtures);
after(resetFixtures);
it ('works as expected', function(done) {
$('#basic-test a').featherlightGallery();
$('#basic-test a').eq(2).click();
patiently(done, [function() {
expect($('.featherlight img')).to.have.attr('src').match(/photo_large.jpg\?2/);
$('.featherlight').trigger('next');
}, function() {
expect($('.featherlight img')).to.have.attr('src').match(/photo_large.jpg\?3/);
$('.featherlight-next').click();
}, function() {
expect($('.featherlight img')).to.have.attr('src').match(/photo_large.jpg\?0/);
$('.featherlight').trigger('previous');
}, function() {
expect($('.featherlight img')).to.have.attr('src').match(/photo_large.jpg\?3/);
}]);
});
it ('can be navigated using arrow keys', function(done) {
$('#basic-test a').featherlightGallery();
$('#basic-test a').eq(2).click();
patiently(done, [function() {
expect($('.featherlight img')).to.have.attr('src').match(/photo_large.jpg\?2/);
triggerKeyCode(39);
}, function() {
expect($('.featherlight img')).to.have.attr('src').match(/photo_large.jpg\?3/);
triggerKeyCode(39);
}, function() {
expect($('.featherlight img')).to.have.attr('src').match(/photo_large.jpg\?0/);
triggerKeyCode(37);
}, function() {
expect($('.featherlight img')).to.have.attr('src').match(/photo_large.jpg\?3/);
}]);
});
it ('will keep modified background intact', function(done) {
$('#basic-test a').featherlightGallery({
afterOpen: function() {
this.$instance.find('.featherlight-content').append('<div class="something"/>');
}
});
$('#basic-test a').eq(2).click();
patiently(done, [function() {
expect($('.featherlight img')).to.have.attr('src').match(/photo_large.jpg\?2/);
expect($('.featherlight-content *:last-child')).to.have.class('something');
$('.featherlight').trigger('next');
}, function() {
expect($('.featherlight img')).to.have.attr('src').match(/photo_large.jpg\?3/);
expect($('.featherlight-content *:last-child')).to.have.class('something');
}]);
});
it ('can be setup without JS', function(done) {
$('#full-auto a:last').click();
patiently(done, [function() {
expect($('.featherlight img')).to.have.attr('src').match(/photo_large.jpg\?auto_1/);
$('.featherlight').trigger('next');
}, function() {
expect($('.featherlight img')).to.have.attr('src').match(/photo_large.jpg\?auto_0/);
}]);
});
it ('is chainable', function() {
var $anchors = $('#basic-test a');
expect($anchors.featherlight()).to.equal($anchors);
});
it ('triggers afterContent after each slide', function(done) {
var lastCurrent = null;
$('#basic-test a').featherlightGallery({
afterContent: function() { lastCurrent = this.$currentTarget; }
});
$('#basic-test a').eq(2).click();
patiently(done, [function(){
expect(lastCurrent).to.not.be.null
expect(lastCurrent).to.have.attr('href').match(/photo_large.jpg\?2/);
$('.featherlight').trigger('next');
}, function() {
expect(lastCurrent).to.have.attr('href').match(/photo_large.jpg\?3/);
}]);
});
describe('$.featherlightGallery', function() {
it ('can be called directly', function(done) {
var lastCurrent = null;
var imgs = '<img src="fixtures/photo.jpeg?direct_1"/>' +
'<img src="fixtures/photo.jpeg?direct_2"/>' +
'<img src="fixtures/photo.jpeg?direct_3"/>';
$.featherlightGallery($(imgs), {
targetAttr: 'src',
afterContent: function(){ lastCurrent = this.$currentTarget }
});
patiently(done, [function(){
expect($('.featherlight img')).to.have.attr('src').match(/direct_1$/);
$('.featherlight').trigger('previous');
}, function() {
expect(lastCurrent).to.have.attr('src').match(/direct_3$/);
}]);
});
});
describe('$.featherlightGallery', function() {
it ('does not get confused with same images', function(done) {
var imgs = '<img src="fixtures/photo.jpeg"/>' +
'<img src="fixtures/photo.jpeg"/>' +
'<img src="fixtures/photo.jpeg?direct_3"/>';
$.featherlightGallery($(imgs), { targetAttr: 'src' });
$.fx.off = false
patiently(done, [function(){
expect($('.featherlight img')).to.have.attr('src').match(/photo.jpeg$/);
$('.featherlight').trigger('next');
},
function(){
expect($('.featherlight img')).to.have.css('opacity').lessThan(1);
},
function(){
expect($('.featherlight img')).to.have.css('opacity').equal('1');
$('.featherlight').trigger('next');
},
function() {
expect($('.featherlight img')).to.have.attr('src').match(/direct_3$/);
$.fx.off = true;
}]);
});
});
it ('accepts config as data-attributes', function(done) {
var $cur;
$('#extra-test').featherlightGallery();
$('#extra-test a:first').click();
patiently(done, [function(){
$cur = $.featherlightGallery.current()
expect($cur).to.be.an.instanceof($.featherlightGallery)
expect($cur).to.have.property('called', true);
$('.featherlight').trigger('next');
}, function(){
expect($('.featherlight')).to.contain('Hello');
expect($('.featherlight img')).to.not.be.visible;
}]);
});
it('can specify callbacks to track the progress of the dialog', function(done) {
var callbacks = ['beforeOpen', 'beforeContent', 'afterContent', 'afterOpen', 'beforeClose', 'afterClose'],
lastCallback = undefined,
options = {targetAttr: 'src'};
$.each(callbacks, function(i, cb){
options[cb] = function() {
expect(lastCallback).to.equal(callbacks[i-1]);
lastCallback = cb;
}
});
$.featherlightGallery($('<img src="fixtures/photo.jpeg?direct_1"/>'), options);
patiently(done, [function(){
expect(lastCallback).to.equal('afterOpen');
$.featherlight.current().close();
}, function(){
expect(lastCallback).to.equal('afterClose');
}]);
});
it ('sets last-slide and first-slide classes', function(done) {
$('#basic-test a').featherlightGallery();
$('#basic-test a').eq(3).click();
patiently(done, [function() {
expect($('.featherlight')).to.not.have.class('featherlight-first-slide');
expect($('.featherlight')).to.have.class('featherlight-last-slide');
$('.featherlight').trigger('next');
}, function() {
expect($('.featherlight')).to.have.class('featherlight-first-slide');
expect($('.featherlight')).to.not.have.class('featherlight-last-slide');
$('.featherlight-next').click();
}, function() {
expect($('.featherlight')).to.not.have.class('featherlight-first-slide');
expect($('.featherlight')).to.not.have.class('featherlight-last-slide');
$('.featherlight').trigger('previous');
}, function() {
expect($('.featherlight')).to.have.class('featherlight-first-slide');
expect($('.featherlight')).to.not.have.class('featherlight-last-slide');
}]);
});
it ("supports 'close:anywhere'", function(done) {
var gall = $('#basic-test').featherlightGallery({filter: 'a', closeOnClick: 'anywhere'});
$('#basic-test a').eq(2).click();
patiently(done, [function() {
expect($('.featherlight img')).to.have.attr('src').match(/photo_large.jpg\?2/);
$('.featherlight-next').click();
}, function() {
expect($('.featherlight img')).to.have.attr('src').match(/photo_large.jpg\?3/);
}]);
});
describe('.current', function() {
it ('only returns actual featherlight gallery instances', function() {
$.featherlight('<p>This is a test<p>');
expect($.featherlight.current()).to.not.be.null;
expect($.featherlightGallery.current()).to.be.null;
});
});
});
}(jQuery));