Skip to content

Commit

Permalink
jest's expect + sinon is better than chai
Browse files Browse the repository at this point in the history
  • Loading branch information
xobotyi committed Jul 22, 2018
1 parent a76eb45 commit f8a602b
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 109 deletions.
2 changes: 1 addition & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module.exports = function karmaConfig(config) {
config.set({
browsers: ['ChromeHeadless'],
singleRun: true,
frameworks: ['mocha', 'chai-spies', 'chai'],
frameworks: ['mocha'],
files: ['./test.js'],
preprocessors: {'./test.js': 'webpack'},
reporters: ['mocha'].concat(coverageReporters),
Expand Down
8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,14 @@
"babel-preset-react": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"babel-register": "^6.26.0",
"chai": "^4.1.2",
"chai-spies": "^1.0.0",
"codacy-coverage": "^3.0.0",
"cross-env": "^5.2.0",
"enzyme": "^3.3.0",
"enzyme-adapter-react-16": "^1.1.1",
"enzyme-to-json": "^3.3.4",
"expect": "^23.4.0",
"istanbul-instrumenter-loader": "^3.0.1",
"karma": "^2.0.4",
"karma-chai": "^0.1.0",
"karma-chai-spies": "^0.1.4",
"karma-chrome-launcher": "^2.2.0",
"karma-coverage-istanbul-reporter": "^2.0.1",
"karma-mocha": "^1.3.0",
Expand All @@ -59,6 +56,7 @@
"react": "^16.4.1",
"react-dom": "^16.4.1",
"rimraf": "^2.6.2",
"sinon": "^6.1.4",
"webpack": "^4.15.1",
"webpack-cli": "^3.0.8",
"webpack-dev-server": "^3.1.4"
Expand All @@ -71,6 +69,6 @@
"test:coverage": "cross-env NODE_ENV=test COVERAGE=true karma start --single-run",
"push-codacy-coverage": "cat ./coverage/lcov.info | codacy-coverage -p .",
"build": "rimraf lib && babel src -d dist",
"prepublish": "npm test && npm run build"
"prepublishOnly": "npm test && npm run build"
}
}
4 changes: 0 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import chai from "chai";
import spies from "chai-spies";
import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

chai.use(spies);

Enzyme.configure({adapter: new Adapter()});

const context = require.context('./tests', true, /\.spec\.js$/);
Expand Down
49 changes: 25 additions & 24 deletions tests/Scrollbars/getset.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import expect from "expect";
import React from "react";
import { render, unmountComponentAtNode } from "react-dom";
import { Scrollbar } from "react-scrollbars-custom";
Expand All @@ -24,49 +25,49 @@ export default function createTests(scrollbarWidth) {
it("should return scrollTop",
(done) => sbRender(function () {
this.scrollTop = 50;
expect(this.scrollTop).to.be.equal(50);
expect(this.content.scrollTop).to.be.equal(50);
expect(this.scrollTop).toBe(50);
expect(this.content.scrollTop).toBe(50);

done();
}));

it("should return scrollLeft",
(done) => sbRender(function () {
this.scrollLeft = 50;
expect(this.scrollLeft).to.be.equal(50);
expect(this.content.scrollLeft).to.be.equal(50);
expect(this.scrollLeft).toBe(50);
expect(this.content.scrollLeft).toBe(50);

done();
}));

it("should return scrollHeight",
(done) => sbRender(function () {
expect(this.scrollHeight).to.be.equal(200);
expect(this.content.scrollHeight).to.be.equal(200);
expect(this.scrollHeight).toBe(200);
expect(this.content.scrollHeight).toBe(200);

done();
}));

it("should return scrollWidth",
(done) => sbRender(function () {
expect(this.scrollWidth).to.be.equal(200);
expect(this.content.scrollWidth).to.be.equal(200);
expect(this.scrollWidth).toBe(200);
expect(this.content.scrollWidth).toBe(200);

done();
}));

it("should return clientHeight",
(done) => sbRender(function () {
expect(this.clientHeight).to.be.equal(92);
expect(this.content.clientHeight).to.be.equal(92);
expect(this.clientHeight).toBe(92);
expect(this.content.clientHeight).toBe(92);

done();
}));

it("should return clientWidth",
(done) => sbRender(function () {
expect(this.clientWidth).to.be.equal(92);
expect(this.content.clientWidth).to.be.equal(92);
expect(this.clientWidth).toBe(92);
expect(this.content.clientWidth).toBe(92);

done();
}));
Expand All @@ -76,53 +77,53 @@ export default function createTests(scrollbarWidth) {
it("scrollTop/scrollToTop/scrollToBottom",
(done) => sbRender(function () {
this.scrollTop = 50;
expect(this.scrollTop).to.be.equal(50);
expect(this.content.scrollTop).to.be.equal(50);
expect(this.scrollTop).toBe(50);
expect(this.content.scrollTop).toBe(50);
this.scrollToTop();
expect(this.content.scrollTop).to.be.equal(0);
expect(this.content.scrollTop).toBe(0);
this.scrollToBottom();
expect(this.content.scrollTop).to.be.equal(this.content.scrollHeight - this.content.clientHeight);
expect(this.content.scrollTop).toBe(this.content.scrollHeight - this.content.clientHeight);

done();
}));

it("scrollLeft/scrollToLeft/scrollToRight",
(done) => sbRender(function () {
this.scrollLeft = 50;
expect(this.scrollLeft).to.be.equal(50);
expect(this.content.scrollLeft).to.be.equal(50);
expect(this.scrollLeft).toBe(50);
expect(this.content.scrollLeft).toBe(50);
this.scrollToLeft();
expect(this.content.scrollLeft).to.be.equal(0);
expect(this.content.scrollLeft).toBe(0);
this.scrollToRight();
expect(this.content.scrollLeft).to.be.equal(this.content.scrollWidth - this.content.clientWidth);
expect(this.content.scrollLeft).toBe(this.content.scrollWidth - this.content.clientWidth);

done();
}));

it("scrollHeight should not be settable",
(done) => sbRender(function () {
expect(() => {this.scrollHeight = 0;}).to.throw(TypeError);
expect(() => {this.scrollHeight = 0;}).toThrow(TypeError);

done();
}));

it("scrollWidth should not be settable",
(done) => sbRender(function () {
expect(() => {this.scrollWidth = 0;}).to.throw(TypeError);
expect(() => {this.scrollWidth = 0;}).toThrow(TypeError);

done();
}));

it("clientHeight should not be settable",
(done) => sbRender(function () {
expect(() => {this.clientHeight = 0;}).to.throw(TypeError);
expect(() => {this.clientHeight = 0;}).toThrow(TypeError);

done();
}));

it("clientWidth should not be settable",
(done) => sbRender(function () {
expect(() => {this.clientWidth = 0;}).to.throw(TypeError);
expect(() => {this.clientWidth = 0;}).toThrow(TypeError);

done();
}));
Expand Down
2 changes: 1 addition & 1 deletion tests/Scrollbars/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import getset from "./getset";
import rendering from "./rendering";
import getset from "./getset";
import scrolling from "./scrolling";

export default function createTests(scrollbarWidth) {
Expand Down
87 changes: 44 additions & 43 deletions tests/Scrollbars/rendering.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import expect from "expect";
import React from "react";
import { findDOMNode, render, unmountComponentAtNode } from "react-dom";
import { Scrollbar } from "react-scrollbars-custom";
Expand All @@ -17,41 +18,41 @@ export default function createTests(scrollbarWidth) {
describe("when <Scrollbar /> is rendered", function () {
it("renders content wrapper", (done) => {
render(<Scrollbar />, node, function () {
expect(this.wrapper).to.be.an.instanceof(Node);
expect(this.wrapper).toBeInstanceOf(Node);

done();
});
});
it("renders content", (done) => {
render(<Scrollbar />, node, function () {
expect(this.content).to.be.an.instanceof(Node);
expect(this.content).toBeInstanceOf(Node);

done();
});
});
it("content should have an absolute positioning", (done) => {
render(<Scrollbar />, node, function () {
expect(this.content.style.position).to.be.equal("absolute");
expect(this.content.style.top).to.be.equal("0px");
expect(this.content.style.left).to.be.equal("0px");
expect(this.content.style.right).to.be.equal("0px");
expect(this.content.style.bottom).to.be.equal("0px");
expect(this.content.style.position).toBe("absolute");
expect(this.content.style.top).toBe("0px");
expect(this.content.style.left).toBe("0px");
expect(this.content.style.right).toBe("0px");
expect(this.content.style.bottom).toBe("0px");

done();
});
});
it("renders tracks", (done) => {
render(<Scrollbar />, node, function () {
expect(this.trackVertical).to.be.an.instanceof(Node);
expect(this.trackHorizontal).to.be.an.instanceof(Node);
expect(this.trackVertical).toBeInstanceOf(Node);
expect(this.trackHorizontal).toBeInstanceOf(Node);

done();
});
});
it("renders thumbs", (done) => {
render(<Scrollbar />, node, function () {
expect(this.thumbVertical).to.be.an.instanceof(Node);
expect(this.thumbHorizontal).to.be.an.instanceof(Node);
expect(this.thumbVertical).toBeInstanceOf(Node);
expect(this.thumbHorizontal).toBeInstanceOf(Node);

done();
});
Expand All @@ -64,8 +65,8 @@ export default function createTests(scrollbarWidth) {
function () {
setTimeout(() => {
// 92 / 200 * 92 = 42.32
expect(this.thumbVertical.style.height).to.be.equal("43px");
expect(this.thumbHorizontal.style.width).to.be.equal("43px");
expect(this.thumbVertical.style.height).toBe("43px");
expect(this.thumbHorizontal.style.width).toBe("43px");

done();
}, 50);
Expand All @@ -78,8 +79,8 @@ export default function createTests(scrollbarWidth) {
node,
function () {
setTimeout(() => {
expect(this.thumbVertical.style.height).to.be.equal("50px");
expect(this.thumbHorizontal.style.width).to.be.equal("50px");
expect(this.thumbVertical.style.height).toBe("50px");
expect(this.thumbHorizontal.style.width).toBe("50px");

done();
}, 50);
Expand All @@ -92,7 +93,7 @@ export default function createTests(scrollbarWidth) {
render(<Scrollbar tagName="label" />,
node,
function () {
expect(findDOMNode(this).tagName.toLowerCase()).to.be.equal("label");
expect(findDOMNode(this).tagName.toLowerCase()).toBe("label");

done();
});
Expand All @@ -104,8 +105,8 @@ export default function createTests(scrollbarWidth) {
render(<Scrollbar style={ {maxWidth: "100%"} } />,
node,
function () {
expect(findDOMNode(this).style.maxWidth).to.be.equal("100%");
expect(findDOMNode(this).style.display).to.be.equal("grid");
expect(findDOMNode(this).style.maxWidth).toBe("100%");
expect(findDOMNode(this).style.display).toBe("grid");

done();
});
Expand All @@ -120,8 +121,8 @@ export default function createTests(scrollbarWidth) {
node,
function () {
setTimeout(() => {
expect(this.trackVertical.style.display).to.be.equal("none");
expect(this.trackHorizontal.style.display).to.be.equal("none");
expect(this.trackVertical.style.display).toBe("none");
expect(this.trackHorizontal.style.display).toBe("none");

done();
}, 50);
Expand All @@ -135,8 +136,8 @@ export default function createTests(scrollbarWidth) {
node,
function () {
setTimeout(() => {
expect(this.trackVertical.style.display).to.not.be.equal("none");
expect(this.trackHorizontal.style.display).to.not.be.equal("none");
expect(this.trackVertical.style.display).not.toBe("none");
expect(this.trackHorizontal.style.display).not.toBe("none");

done();
}, 50);
Expand All @@ -150,7 +151,7 @@ export default function createTests(scrollbarWidth) {
node,
function () {
setTimeout(() => {
expect(this.trackVertical.style.display).to.not.be.equal("none");
expect(this.trackVertical.style.display).not.toBe("none");

done();
}, 50);
Expand All @@ -164,7 +165,7 @@ export default function createTests(scrollbarWidth) {
node,
function () {
setTimeout(() => {
expect(this.trackHorizontal.style.display).to.not.be.equal("none");
expect(this.trackHorizontal.style.display).not.toBe("none");

done();
}, 50);
Expand All @@ -180,12 +181,12 @@ export default function createTests(scrollbarWidth) {
node,
function () {
setTimeout(() => {
expect(this.trackVertical.style.display).to.be.equal("none");
expect(this.content.style.overflowY).to.be.equal("hidden");
expect(this.content.style.marginRight).to.be.equal("");
expect(this.trackHorizontal.style.display).to.not.be.equal("none");
expect(this.content.style.overflowX).to.not.be.equal("hidden");
expect(this.content.style.marginBottom).to.not.be.equal("");
expect(this.trackVertical.style.display).toBe("none");
expect(this.content.style.overflowY).toBe("hidden");
expect(this.content.style.marginRight).toBe("");
expect(this.trackHorizontal.style.display).not.toBe("none");
expect(this.content.style.overflowX).not.toBe("hidden");
expect(this.content.style.marginBottom).not.toBe("");

done();
}, 50);
Expand All @@ -201,12 +202,12 @@ export default function createTests(scrollbarWidth) {
node,
function () {
setTimeout(() => {
expect(this.trackVertical.style.display).to.not.be.equal("none");
expect(this.content.style.overflowY).to.not.be.equal("hidden");
expect(this.content.style.marginRight).to.not.be.equal("");
expect(this.trackHorizontal.style.display).to.be.equal("none");
expect(this.content.style.overflowX).to.be.equal("hidden");
expect(this.content.style.marginBottom).to.be.equal("");
expect(this.trackVertical.style.display).not.toBe("none");
expect(this.content.style.overflowY).not.toBe("hidden");
expect(this.content.style.marginRight).not.toBe("");
expect(this.trackHorizontal.style.display).toBe("none");
expect(this.content.style.overflowX).toBe("hidden");
expect(this.content.style.marginBottom).toBe("");

done();
}, 50);
Expand All @@ -222,13 +223,13 @@ export default function createTests(scrollbarWidth) {
node,
function () {
setTimeout(() => {
expect(this.trackHorizontal.style.display).to.be.equal("none");
expect(this.trackVertical.style.display).to.be.equal("none");
expect(this.content.style.overflowX).to.be.equal("hidden");
expect(this.content.style.overflowY).to.be.equal("hidden");
expect(this.content.style.overflow).to.be.equal("hidden");
expect(this.content.style.marginBottom).to.be.equal("");
expect(this.content.style.marginRight).to.be.equal("");
expect(this.trackHorizontal.style.display).toBe("none");
expect(this.trackVertical.style.display).toBe("none");
expect(this.content.style.overflowX).toBe("hidden");
expect(this.content.style.overflowY).toBe("hidden");
expect(this.content.style.overflow).toBe("hidden");
expect(this.content.style.marginBottom).toBe("");
expect(this.content.style.marginRight).toBe("");

done();
}, 50);
Expand Down
Loading

0 comments on commit f8a602b

Please sign in to comment.