|
| 1 | +// this is for fetching details after initializing react and jquery slicks |
| 2 | +// and compare those details to see if things are going different |
| 3 | + |
| 4 | +import {createSliderReact, createJQuerySliderChildren, activeSlideInLastTransition} from './testUtils' |
| 5 | +import $ from 'jquery' |
| 6 | +import * as slickCarousel from 'slick-carousel' |
| 7 | +import util from 'util' |
| 8 | +import js_beautify, {html as html_beautify} from 'js-beautify' |
| 9 | + |
| 10 | +// simulates actions from given actions object |
| 11 | +// takes document from the scope from where it's called |
| 12 | +function simulateActions(actions){ |
| 13 | + if(actions.clickNext){ |
| 14 | + for(let click = 0; click < actions.clickNext; click++){ |
| 15 | + $('.slick-next').click() |
| 16 | + } |
| 17 | + } |
| 18 | + if(actions.clickPrev){ |
| 19 | + for(let click = 0; click < actions.clickPrev; click++){ |
| 20 | + $('.slick-prev').click() |
| 21 | + } |
| 22 | + } |
| 23 | + if(actions.clickSequence){ |
| 24 | + for(let click of actions.clickSequence){ |
| 25 | + if(click === 'n'){ |
| 26 | + $('.slick-next').click() |
| 27 | + } else if (click === 'p') { |
| 28 | + $('.slick-prev').click() |
| 29 | + } else { |
| 30 | + // that's right, you can't even write n/p properly |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +// takes an object of keys and returns those details |
| 37 | +/* Possible keys can be one of the following |
| 38 | +currentSlide(index and value), activeSlides(index and value), |
| 39 | +allSlides(index and value), clonedSlides(index and value) |
| 40 | +*/ |
| 41 | +function fetchDetails(keys){ |
| 42 | + let details = {} |
| 43 | + let currentSlide = null, |
| 44 | + activeSlides = [], |
| 45 | + allSlides = [], |
| 46 | + clonedSlides = [], |
| 47 | + visibleSlides = [] |
| 48 | + for (let slide of $('div.slick-slide')){ |
| 49 | + const slideObj = { |
| 50 | + index: $(slide).attr('data-slick-index'), |
| 51 | + value: $(slide).find('div').find('div').find('h3').text() |
| 52 | + } |
| 53 | + allSlides.push(slideObj) |
| 54 | + if($(slide).hasClass('slick-current')){ currentSlide = slideObj.index } |
| 55 | + if($(slide).hasClass('slick-active')){ activeSlides.push(slideObj) } |
| 56 | + if($(slide).hasClass('slick-cloned')){ clonedSlides.push(slideObj) } |
| 57 | + if($(slide).attr('aria-hidden') == 'false'){ visibleSlides.push(slideObj) } |
| 58 | + } |
| 59 | + if(keys.currentSlide) { details.currentSlide = currentSlide } |
| 60 | + if(keys.activeSlides) { details.activeSlides = activeSlides } |
| 61 | + if(keys.allSlides) { details.allSlides = allSlides } |
| 62 | + if(keys.clonedSlides) { details.clonedSlides = clonedSlides } |
| 63 | + if(keys.visibleSlides) { details.visibleSlides = visibleSlides } |
| 64 | + return details |
| 65 | +} |
| 66 | + |
| 67 | +// creates a jQuery slick with given settings and |
| 68 | +// performs the given actions |
| 69 | +// returns the given keys |
| 70 | +export function getJQuerySlickDetails(settings, actions, keys){ |
| 71 | + // create new slider |
| 72 | + document.body.innerHTML = ` |
| 73 | + <section class="regular slider"> |
| 74 | + ${createJQuerySliderChildren(settings.noOfSlides)} |
| 75 | + </section> |
| 76 | + ` |
| 77 | + $('.regular.slider').slick({ |
| 78 | + ...settings |
| 79 | + }) |
| 80 | + simulateActions(actions) |
| 81 | + // console.log(html_beautify($('.regular.slider').html())) |
| 82 | + return fetchDetails(keys) |
| 83 | +} |
| 84 | + |
| 85 | +const settings = { |
| 86 | + infinite: true, |
| 87 | + noOfSlides: 5, |
| 88 | + slidesToShow: 3, |
| 89 | + slidesToScroll: 2, |
| 90 | + useCSS: false, |
| 91 | + speed: 0, |
| 92 | +} |
| 93 | +const actions = { |
| 94 | + clickNext: 2, |
| 95 | + clickPrev: 1, |
| 96 | + clickSequence: ['n', 'p', 'n'] |
| 97 | +} |
| 98 | +const keys = { |
| 99 | + activeSlides: true, |
| 100 | + visibleSlides: true, |
| 101 | + allSlides: true, |
| 102 | +} |
| 103 | + |
| 104 | +test('testing getJQuerySlickDetails utility', () => { |
| 105 | + const details = getJQuerySlickDetails(settings, actions, keys) |
| 106 | + expect(details.activeSlides).toEqual(details.visibleSlides) |
| 107 | +}) |
0 commit comments