Skip to content

Commit 93bf2e9

Browse files
nikolasSTRML
authored andcommitted
Set up unit tests with jest and add a few tests (react-grid-layout#442)
1 parent 4158630 commit 93bf2e9

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

__tests__/utils-test.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import {bottom, collides, validateLayout} from '../lib/utils.js';
2+
3+
describe('bottom', () => {
4+
it('Handles an empty layout as input', () => {
5+
expect(bottom([])).toBe(0);
6+
});
7+
8+
it('Returns the bottom coordinate of the layout', () => {
9+
expect(bottom([
10+
{x: 0, y: 1, w: 1, h: 1},
11+
{x: 1, y: 2, w: 1, h: 1}
12+
])).toBe(3);
13+
});
14+
});
15+
16+
describe('collides', () => {
17+
it('Returns whether the layout items collide', () => {
18+
expect(collides(
19+
{x: 0, y: 1, w: 1, h: 1},
20+
{x: 1, y: 2, w: 1, h: 1}
21+
)).toBe(false);
22+
expect(collides(
23+
{x: 0, y: 1, w: 1, h: 1},
24+
{x: 0, y: 1, w: 1, h: 1}
25+
)).toBe(true);
26+
});
27+
});
28+
29+
describe('validateLayout', () => {
30+
it('Validates an empty layout', () => {
31+
validateLayout([]);
32+
});
33+
it('Validates a populated layout', () => {
34+
validateLayout([
35+
{x: 0, y: 1, w: 1, h: 1},
36+
{x: 1, y: 2, w: 1, h: 1}
37+
]);
38+
});
39+
it('Throws errors on invalid input', () => {
40+
expect(() => {
41+
validateLayout([
42+
{x: 0, y: 1, w: 1, h: 1},
43+
{x: 1, y: 2, w: 1}
44+
]);
45+
}).toThrowError('Layout[1].h must be a number!');
46+
});
47+
});

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "index.js",
66
"scripts": {
77
"lint": "make lint",
8-
"test": "echo \"Error: no test specified\" && exit 1",
8+
"test": "jest",
99
"build": "make build",
1010
"build-example": "make build-example",
1111
"dev": "make dev",
@@ -45,6 +45,7 @@
4545
"babel-cli": "^6.5.1",
4646
"babel-core": "^6.x",
4747
"babel-eslint": "^7.1.1",
48+
"babel-jest": "^17.0.2",
4849
"babel-loader": "^6.x",
4950
"babel-plugin-react-transform": "^2.0.0",
5051
"babel-plugin-transform-react-constant-elements": "^6.5.0",
@@ -60,6 +61,7 @@
6061
"eslint-plugin-react": "^6.8.0",
6162
"exports-loader": "^0.6.3",
6263
"imports-loader": "^0.6.5",
64+
"jest-cli": "^17.0.3",
6365
"jsxhint": "^0.15.1",
6466
"lodash": "^4.3.0",
6567
"pre-commit": "^1.1.2",

0 commit comments

Comments
 (0)