forked from akiran/react-slick
-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathjQSlickUtils.js
More file actions
129 lines (123 loc) · 3.38 KB
/
jQSlickUtils.js
File metadata and controls
129 lines (123 loc) · 3.38 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
// this is for fetching details after initializing react and jquery slicks
// and compare those details to see if things are going different
import {
createSliderReact,
createJQuerySliderChildren,
activeSlideInLastTransition
} from "./testUtils";
import $ from "jquery";
import * as slickCarousel from "slick-carousel";
import util from "util";
import js_beautify, { html as html_beautify } from "js-beautify";
// simulates actions from given actions object
// takes document from the scope from where it's called
function simulateActions(actions) {
if (actions.clickNext) {
for (let click = 0; click < actions.clickNext; click++) {
$(".slick-next").click();
}
}
if (actions.clickPrev) {
for (let click = 0; click < actions.clickPrev; click++) {
$(".slick-prev").click();
}
}
if (actions.clickSequence) {
for (let click of actions.clickSequence) {
if (click === "n") {
$(".slick-next").click();
} else if (click === "p") {
$(".slick-prev").click();
} else {
// that's right, you can't even write n/p properly
}
}
}
}
// takes an object of keys and returns those details
/* Possible keys can be one of the following
currentSlide(index and value), activeSlides(index and value),
allSlides(index and value), clonedSlides(index and value)
*/
function fetchDetails(keys) {
let details = {};
let currentSlide = null,
activeSlides = [],
allSlides = [],
clonedSlides = [],
visibleSlides = [];
for (let slide of $("div.slick-slide")) {
const slideObj = {
index: $(slide).attr("data-slick-index"),
value: $(slide).find("div").find("div").find("h3").text()
};
allSlides.push(slideObj);
if ($(slide).hasClass("slick-current")) {
currentSlide = slideObj.index;
}
if ($(slide).hasClass("slick-active")) {
activeSlides.push(slideObj);
}
if ($(slide).hasClass("slick-cloned")) {
clonedSlides.push(slideObj);
}
if ($(slide).attr("aria-hidden") == "false") {
visibleSlides.push(slideObj);
}
}
if (keys.currentSlide) {
details.currentSlide = currentSlide;
}
if (keys.activeSlides) {
details.activeSlides = activeSlides;
}
if (keys.allSlides) {
details.allSlides = allSlides;
}
if (keys.clonedSlides) {
details.clonedSlides = clonedSlides;
}
if (keys.visibleSlides) {
details.visibleSlides = visibleSlides;
}
return details;
}
// creates a jQuery slick with given settings and
// performs the given actions
// returns the given keys
export function getJQuerySlickDetails(settings, actions, keys) {
// create new slider
document.body.innerHTML = `
<section class="regular slider">
${createJQuerySliderChildren(settings.noOfSlides)}
</section>
`;
$(".regular.slider").slick({
...settings
});
simulateActions(actions);
// console.log(html_beautify($('.regular.slider').html()))
return fetchDetails(keys);
}
const settings = {
infinite: true,
noOfSlides: 5,
slidesToShow: 3,
slidesToScroll: 2,
useCSS: false,
speed: 0
};
const actions = {
clickNext: 2,
clickPrev: 1,
clickSequence: ["n", "p", "n"]
};
const keys = {
activeSlides: true,
visibleSlides: true,
allSlides: true
};
test("testing getJQuerySlickDetails utility", () => {
const details = getJQuerySlickDetails(settings, actions, keys);
expect(details.activeSlides).toEqual(details.visibleSlides);
});