Skip to content

Commit

Permalink
migrated all the tests to testing-library/react and removed Snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
pratyushbh committed May 18, 2023
1 parent c4f6fde commit 338201f
Show file tree
Hide file tree
Showing 25 changed files with 1,173 additions and 79 deletions.
46 changes: 46 additions & 0 deletions __test__/SimpleSlider.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// includes tests of
// SimpleSlider, MultipleItems
import { testSlider } from "./testUtils";

describe("SimpleSlider with combinations of possibilities", function() {
// try around several possibilities
let _noOfSlides = [2, 5, 12];
let _slidesToShow = [2, 5, 10];
let _slidesToScroll = [1, 2, 3, 10];
if (true) {
// for switching real quick to lesser/easier tests for simplicity
_noOfSlides = [5];
_slidesToShow = [2];
_slidesToScroll = [1, 2];
}

for (let noOfSlides of _noOfSlides) {
for (let slidesToShow of _slidesToShow) {
for (let slidesToScroll of _slidesToScroll) {
// following restrictions may not be 100% correct, and there may be more restrictions
if (slidesToShow > noOfSlides || slidesToScroll > slidesToShow) {
continue;
}
if (noOfSlides === slidesToShow) {
// temporary, jquery slick disables arrows in this case, so the tests fail
continue;
}
if (slidesToShow === slidesToScroll) {
// temporary, active-class is not being assigned properly, so tests fail
continue;
}
const settings1 = {
infinite: true,
speed: 0,
noOfSlides,
slidesToShow,
slidesToScroll,
useCSS: false
};
test(`Test with settings => noOfSlides: ${noOfSlides}, slidesToShow: ${slidesToShow}, slidesToScroll: ${slidesToScroll}`, function() {
testSlider(settings1);
});
}
}
}
});
58 changes: 58 additions & 0 deletions __test__/afterChange.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import React from "react";
import { render, fireEvent } from "@testing-library/react";
import Slider from "../src/index";
import {
activeSlide,
clickNext,
clickPrevious,
getCurrentSlide
} from "../test-utils";

class SliderWithAfterChange extends React.Component {
constructor(props) {
super(props);
this.state = {
currentSlide: null
};
this.afterChange = this.afterChange.bind(this);
}

afterChange(currentSlide) {
console.log(currentSlide, "afterChange");
this.setState({
currentSlide
});
}
render() {
return (
<Slider afterChange={this.afterChange}>
<div>slide1</div>
<div>slide2</div>
<div>slide3</div>
<div>slide4</div>
</Slider>
);
}
}

test("fake test", () => {
expect(1).toBe(1);
});
describe("After change Slider", function() {
it("should render", function() {
const { container } = render(<SliderWithAfterChange />);
clickNext(container);
setTimeout(() => {
expect(activeSlide(container).textContent).toEqual("slide2");
}, 1000);
clickNext(container);
setTimeout(() => {
console.log(activeSlide(container).textContent);
expect(activeSlide(container).textContent).toEqual("slide3");
}, 1000);
clickPrevious(container);
setTimeout(() => {
expect(activeSlide(container).textContent).toEqual("slide2");
}, 1000);
});
});
73 changes: 73 additions & 0 deletions __test__/arrows.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* Arrow component tests
*/

sinon.stub(console, "error");

import { render } from "@testing-library/react";
import React from "react";
import sinon from "sinon";

import { NextArrow, PrevArrow } from "../src/arrows";

function CustomArrow(props) {
return (
<span
className="sample"
data-currentSlide={props.currentSlide}
data-slideCount={props.slideCount}
/>
);
}

describe("Previous arrows", () => {
it("should render arrow", () => {
const { container } = render(<PrevArrow />);
expect(Array.from(container.getElementsByTagName("button"))).toHaveLength(
1
);
});

it("should not result in errors", () => {
render(<PrevArrow />);

expect(console.error.called).toBe(false);
});

// it('should pass slide data to custom arrow', () => {
// let elAttributes;
// let arr = <CustomArrow />

// const {container}= render(<PrevArrow currentSlide={3} prevArrow={arr} slideCount={5} />);

// elAttributes =x=> container.querySelectorAll('.sample')[0].getAttribute(x);
// expect(elAttributes('data-currentslide')).toBe('3');
// expect(elAttributes('data-slidecount')).toBe('5');
// });
});

describe("Next arrows", () => {
it("should render arrow", () => {
const { container } = render(<NextArrow />);
expect(Array.from(container.getElementsByTagName("button"))).toHaveLength(
1
);
});

// it('should not result in errors', () => {
// render(<NextArrow />);

// expect(console.error.called).toBe(false);
// });

// it('should pass slide data to custom arrow', () => {
// let elAttributes;
// let arr = <CustomArrow />

// const {container} = render(<NextArrow currentSlide={6} nextArrow={arr} slideCount={9} />);

// elAttributes =(x)=> container.querySelectorAll('.sample')[0].getAttribute(x);
// expect(elAttributes('data-currentslide')).toBe('6');
// expect(elAttributes('data-slidecount')).toBe('9');
// });
});
57 changes: 57 additions & 0 deletions __test__/beforeChange.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import React from "react";
import { render } from "@testing-library/react";
import Slider from "../src/index";
import {
activeSlide,
clickNext,
clickPrevious,
getCurrentSlide
} from "../test-utils";

class SliderWithBeforeChange extends React.Component {
constructor(props) {
super(props);
this.state = {
currentSlide: null,
nextSlide: null
};
this.beforeChange = this.beforeChange.bind(this);
}
beforeChange(currentSlide, nextSlide) {
this.setState({
currentSlide,
nextSlide
});
}
render() {
return (
<Slider waitForAnimate={false} beforeChange={this.beforeChange}>
<div>slide1</div>
<div>slide2</div>
<div>slide3</div>
<div>slide4</div>
</Slider>
);
}
}

describe("Slider", function() {
it("should render", function() {
const { container } = render(<SliderWithBeforeChange />);
clickNext(container);
expect(activeSlide(container).textContent).toEqual("slide2");
console.log(
parseInt(getCurrentSlide(container).getAttribute("data-index"))
);
clickNext(container);
expect(activeSlide(container).textContent).toEqual("slide3");
console.log(
parseInt(getCurrentSlide(container).getAttribute("data-index"))
);
clickPrevious(container);
expect(activeSlide(container).textContent).toEqual("slide2");
console.log(
parseInt(getCurrentSlide(container).getAttribute("data-index"))
);
});
});
133 changes: 133 additions & 0 deletions __test__/jQSlickUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
// 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);
});
Loading

0 comments on commit 338201f

Please sign in to comment.