Skip to content
This repository was archived by the owner on Nov 16, 2018. It is now read-only.

Commit d444aef

Browse files
committed
Setup jest + first test
1 parent f1d4e45 commit d444aef

File tree

5 files changed

+122
-21
lines changed

5 files changed

+122
-21
lines changed

Diff for: jestEnvironment.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require.requireActual('babel/polyfill')
2+

Diff for: package.json

+26-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
"build": "NODE_ENV=production webpack --output-file react-bootstrap-datetimepicker.js",
1414
"build-min": "NODE_ENV=production COMPRESS=1 webpack --output-file react-bootstrap-datetimepicker.min.js",
1515
"examples": "webpack-dev-server --config ./examples/webpack.config.js",
16-
"test-watch": "./node_modules/.bin/grunt watch 2>&1 >/dev/null & karma start karma.dev.js",
17-
"test": "./node_modules/.bin/grunt build && karma start karma.ci.js",
16+
"test": "jest",
1817
"lint": "eslint ."
1918
},
2019
"keywords": [
@@ -29,14 +28,38 @@
2928
"peerDependencies": {
3029
"react": ">=0.12"
3130
},
31+
"jest": {
32+
"scriptPreprocessor": "<rootDir>/node_modules/babel-jest",
33+
"unmockedModulePathPatterns": [
34+
"core-js/.*",
35+
"<rootDir>/node_modules/react",
36+
"babel",
37+
"<rootDir>/node_modules/babel"
38+
],
39+
"setupEnvScriptFile": "<rootDir>/jestEnvironment.js",
40+
"testFileExtensions": [
41+
"es6",
42+
"js"
43+
],
44+
"moduleFileExtensions": [
45+
"js",
46+
"json",
47+
"es6"
48+
],
49+
"testPathDirs": [
50+
"src/"
51+
]
52+
},
3253
"devDependencies": {
3354
"babel": "^5.6.14",
3455
"babel-core": "^5.6.17",
3556
"babel-eslint": "^3.1.23",
57+
"babel-jest": "^5.3.0",
3658
"babel-loader": "^5.3.1",
3759
"babel-runtime": "^5.6.18",
3860
"envify": "~3.2.0",
3961
"eslint": "^0.24.1",
62+
"eslint-plugin-jasmine": "^1.1.0",
4063
"eslint-plugin-react": "^2.7.0",
4164
"grunt": "~0.4.2",
4265
"grunt-amd-wrap": "^1.0.1",
@@ -50,6 +73,7 @@
5073
"grunt-react": "~0.10.0",
5174
"grunt-shell": "~0.6.4",
5275
"html-webpack-plugin": "^1.1.0",
76+
"jest-cli": "facebook/jest#0.5.x",
5377
"jsx-loader": "^0.12.2",
5478
"react": "^0.13.3",
5579
"requirejs": "~2.1.9",

Diff for: src/__tests__/.eslintrc

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"env": {
3+
"jasmine": true
4+
},
5+
plugins: [
6+
"jasmine"
7+
]
8+
}

Diff for: src/__tests__/DateTimePickerMonths-test.js

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
jest.dontMock("moment");
2+
import React from "react/addons";
3+
jest.dontMock("../DateTimePickerMonths.js");
4+
const { TestUtils } = React.addons;
5+
6+
describe("DateTimePickerMonths", function() {
7+
const moment = require("moment");
8+
const DateTimePickerMonths = require("../DateTimePickerMonths.js");
9+
let subtractYearMock, addYearMock, setViewMonthMock, showYearsMock, months;
10+
11+
beforeEach(() => {
12+
subtractYearMock = jest.genMockFunction();
13+
addYearMock = jest.genMockFunction();
14+
showYearsMock = jest.genMockFunction();
15+
setViewMonthMock = jest.genMockFunction();
16+
months = TestUtils.renderIntoDocument(
17+
<DateTimePickerMonths
18+
addYear={addYearMock}
19+
selectedDate={moment()}
20+
setViewMonth={setViewMonthMock}
21+
showYears={showYearsMock}
22+
subtractYear={subtractYearMock}
23+
viewDate={moment()}
24+
/>
25+
);
26+
});
27+
28+
describe("Controls", function() {
29+
it("calls subtractYear when clicking the prev arrow", function() {
30+
const prevArrow = TestUtils.findRenderedDOMComponentWithClass(months, "prev");
31+
TestUtils.Simulate.click(prevArrow);
32+
expect(subtractYearMock.mock.calls.length).toBe(1);
33+
});
34+
35+
it("calls addYear when clicking the next arrow", function() {
36+
const nextArrow = TestUtils.findRenderedDOMComponentWithClass(months, "next");
37+
TestUtils.Simulate.click(nextArrow);
38+
expect(addYearMock.mock.calls.length).toBe(1);
39+
});
40+
41+
it("calls showYears when clicking the year", function() {
42+
const year = TestUtils.findRenderedDOMComponentWithClass(months, "switch");
43+
TestUtils.Simulate.click(year);
44+
expect(showYearsMock.mock.calls.length).toBe(1);
45+
});
46+
47+
it("calls setViewMonth when clicking a month", function() {
48+
const month = TestUtils.findRenderedDOMComponentWithClass(months, "active");
49+
TestUtils.Simulate.click(month);
50+
expect(setViewMonthMock.mock.calls.length).toBe(1);
51+
});
52+
});
53+
54+
describe('UI', function() {
55+
it('renders 12 months', function() {
56+
const monthList = TestUtils.scryRenderedDOMComponentsWithClass(months, "month");
57+
expect(monthList.length).toBe(12);
58+
});
59+
60+
it('rendersJanuary through December', function() {
61+
const monthList = TestUtils.scryRenderedDOMComponentsWithClass(months, "month");
62+
expect(monthList.map((x) => x.props.children)).toEqual(['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]);
63+
});
64+
65+
it("has an active month that is now's month", function() {
66+
const active = TestUtils.findRenderedDOMComponentWithClass(months, "active");
67+
expect(active.props.children).toBe(moment().format("MMM"));
68+
});
69+
70+
it("has no active month that if viewDate is another year than selectedDate", function() {
71+
months = TestUtils.renderIntoDocument(
72+
<DateTimePickerMonths
73+
addYear={addYearMock}
74+
selectedDate={moment()}
75+
setViewMonth={setViewMonthMock}
76+
showYears={showYearsMock}
77+
subtractYear={subtractYearMock}
78+
viewDate={moment().add(2, 'year')}
79+
/>
80+
);
81+
const active = TestUtils.scryRenderedDOMComponentsWithClass(months, "active");
82+
expect(active.length).toBe(0);
83+
});
84+
});
85+
86+
});

Diff for: test/DateTimePickerHoursSpec.jsx

-19
This file was deleted.

0 commit comments

Comments
 (0)