Skip to content

Commit cc96ebe

Browse files
author
Marcin Szczepanski
committed
Add full screen tests
1 parent e19bc32 commit cc96ebe

File tree

2 files changed

+73
-47
lines changed

2 files changed

+73
-47
lines changed

src/gallery.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,13 @@
2323
};
2424
};
2525

26-
Gallery.start = function (elem) {
27-
var galleryNode = elem.querySelector('.gallery');
28-
if (!galleryNode) return; // early exit if no gallery
29-
30-
26+
Gallery.start = function (galleryNode) {
3127
galleryMode = galleryNode.dataset.mode || 'normal';
3228

3329
if (galleryMode === 'full-screen') {
30+
// FIXME remove depenency on Reveal, have a callback? function
31+
// that will get a root node to move full screen slides to (ie. slidesNode)
32+
3433
// for full screen mode we need to:
3534
// - take the gallery out of the flow and insert it before "slides"
3635
// - hide slides
@@ -41,7 +40,9 @@
4140
galleryNode.parentNode.replaceChild(placeholder, galleryNode);
4241

4342
var slidesNode = document.querySelector(".slides");
44-
slidesNode.parentNode.insertBefore(galleryNode, slidesNode);
43+
if (slidesNode) {
44+
slidesNode.parentNode.insertBefore(galleryNode, slidesNode);
45+
}
4546
}
4647

4748
var items = Array.prototype.slice.apply(galleryNode.querySelectorAll("li"));
@@ -73,6 +74,7 @@
7374
galleryTimer = setInterval(Gallery.step(items, iterations), interval);
7475
};
7576

77+
// FIXME Gallery.stop should take elem and root nodes as well
7678
Gallery.stop = function () {
7779
clearInterval(galleryTimer);
7880

tests/test.gallery.js

Lines changed: 65 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,55 +3,62 @@
33
describe('Gallery', function () {
44
var expect = chai.expect;
55

6-
describe('#start', function () {
7-
var itemElem1, itemElem2, items, galleryElem;
8-
var MockItem = function (hasLabel, attributes) {
9-
this.hasLabel = hasLabel;
10-
this.attributes = attributes;
11-
return this;
6+
var itemElem1, itemElem2, items, galleryElem;
7+
var MockItem = function (hasLabel, attributes) {
8+
this.hasLabel = hasLabel;
9+
this.attributes = attributes;
10+
this.style = {};
11+
this.img = {
12+
style: {},
13+
attributes: this.attributes,
14+
src: "SRC"
1215
};
13-
MockItem.prototype.querySelector = function (selector) {
14-
if (selector === "label") {
15-
return this.hasLabel;
16-
} else if (selector === "img") {
17-
return {
18-
style: {},
19-
attributes: this.attributes
20-
};
16+
return this;
17+
};
18+
MockItem.prototype.querySelector = function (selector) {
19+
if (selector === "label") {
20+
return this.hasLabel;
21+
} else if (selector === "img") {
22+
return this.img;
23+
}
24+
};
25+
MockItem.prototype.appendChild = function () {};
26+
var intervalStub, stepSpy;
27+
28+
beforeEach(function () {
29+
/* Mock Item elements */
30+
itemElem1 = new MockItem(true, {});
31+
itemElem2 = new MockItem(false, {alt: { value: "ALT"}});
32+
items = [itemElem1, itemElem2];
33+
/* Mock DOM Gallery element */
34+
galleryElem = {
35+
dataset: {},
36+
// return SOMETHING!
37+
querySelector: function (selector) {
38+
return this;
39+
},
40+
querySelectorAll: function (selector) {
41+
return items;
42+
},
43+
parentNode: {
44+
replaceChild: function () {}
2145
}
2246
};
23-
MockItem.prototype.appendChild = function () {};
24-
var intervalStub, stepSpy;
25-
26-
beforeEach(function () {
27-
/* Mock Item elements */
28-
itemElem1 = new MockItem(true, {});
29-
itemElem2 = new MockItem(false, {alt: { value: "ALT"}});
30-
items = [itemElem1, itemElem2];
31-
/* Mock DOM Gallery element */
32-
galleryElem = {
33-
dataset: {},
34-
// return SOMETHING!
35-
querySelector: function (selector) {
36-
return this;
37-
},
38-
querySelectorAll: function (selector) {
39-
return items;
40-
}
41-
};
42-
intervalStub = sinon.stub(window, "setInterval");
43-
stepSpy = sinon.spy(Gallery, "step");
44-
});
45-
46-
afterEach(function () {
47-
intervalStub.restore();
48-
stepSpy.restore();
49-
});
47+
intervalStub = sinon.stub(window, "setInterval");
48+
stepSpy = sinon.spy(Gallery, "step");
49+
});
50+
afterEach(function () {
51+
intervalStub.restore();
52+
stepSpy.restore();
53+
});
5054

55+
describe('#start', function () {
5156
it("initialises the gallery with defaults", function () {
5257
Gallery.start(galleryElem);
5358
expect(stepSpy.calledWith(items, 1)).to.be.true;
5459
expect(intervalStub.calledWith(sinon.match.any, 1000)).to.be.true;
60+
expect(itemElem1.style.backgroundImage).to.be.undefined;
61+
expect(itemElem1.img.style.display).to.equal("");
5562
});
5663

5764
it("initialises the gallery with custom iterations", function () {
@@ -82,6 +89,23 @@ describe('Gallery', function () {
8289
expect(appendChildSpy2.called).to.be.false;
8390
appendChildSpy2.restore();
8491
});
92+
it("handles a full screen gallery", function () {
93+
galleryElem.dataset.mode = "full-screen";
94+
Gallery.start(galleryElem);
95+
expect(itemElem1.img.style.display).to.equal("none");
96+
expect(itemElem2.img.style.display).to.equal("none");
97+
expect(itemElem1.style.backgroundImage).to.equal("url(SRC)");
98+
});
99+
});
100+
describe('#stop', function () {
101+
it("restores a full screen gallery", function () {
102+
galleryElem.dataset.mode = "full-screen";
103+
Gallery.start(galleryElem);
104+
Gallery.stop();
105+
expect(itemElem1.img.style.display).to.equal("");
106+
expect(itemElem2.img.style.display).to.equal("");
107+
expect(itemElem1.style.backgroundImage).to.equal("");
108+
});
85109
});
86110
describe('#step', function () {
87111
var items;

0 commit comments

Comments
 (0)