Skip to content

Commit 2908923

Browse files
committed
Merge pull request quri#71 from quri/tests
Tests first pass
2 parents 5405d76 + 9a906b2 commit 2908923

8 files changed

+395
-21
lines changed

jestEnvironment.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require.requireActual("babel/polyfill");
2+

package.json

Lines changed: 26 additions & 2 deletions
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",

src/__tests__/.eslintrc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"env": {
3+
"jasmine": true
4+
},
5+
"globals": {
6+
"jest": false
7+
},
8+
plugins: [
9+
"jasmine"
10+
]
11+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import React from "react/addons";
2+
import Constants from "../Constants.js";
3+
jest.dontMock("../DateTimePickerHours.js");
4+
const { TestUtils } = React.addons;
5+
6+
describe("DateTimePickerHours", function() {
7+
const DateTimePickerHours = require("../DateTimePickerHours.js");
8+
let hours, onSwitchMock, setSelectedHourMock;
9+
10+
11+
describe("Controls", function() {
12+
13+
beforeEach(() => {
14+
setSelectedHourMock = jest.genMockFunction();
15+
onSwitchMock = jest.genMockFunction();
16+
hours = TestUtils.renderIntoDocument(
17+
<DateTimePickerHours
18+
mode={Constants.MODE_TIME}
19+
onSwitch={onSwitchMock}
20+
setSelectedHour={setSelectedHourMock}
21+
/>
22+
);
23+
});
24+
25+
it("calls setSelectedHour when clicking a hour", function() {
26+
const hour = TestUtils.scryRenderedDOMComponentsWithClass(hours, "hour")[0];
27+
TestUtils.Simulate.click(hour);
28+
expect(setSelectedHourMock.mock.calls.length).toBe(1);
29+
});
30+
31+
it("calls onSwitch when clicking the switch", function() {
32+
const switchIcon = TestUtils.findRenderedDOMComponentWithClass(hours, "picker-switch");
33+
TestUtils.Simulate.click(switchIcon);
34+
expect(onSwitchMock.mock.calls.length).toBe(1);
35+
});
36+
});
37+
38+
describe("UI", function() {
39+
beforeEach(() => {
40+
setSelectedHourMock = jest.genMockFunction();
41+
onSwitchMock = jest.genMockFunction();
42+
});
43+
44+
it("renders the switch if mode is time", function() {
45+
hours = TestUtils.renderIntoDocument(
46+
<DateTimePickerHours
47+
mode={Constants.MODE_TIME}
48+
onSwitch={onSwitchMock}
49+
setSelectedHour={setSelectedHourMock}
50+
/>
51+
);
52+
const switchList = TestUtils.scryRenderedDOMComponentsWithClass(hours, "list-unstyled");
53+
expect(switchList.length).toBe(1);
54+
});
55+
56+
it("does not render the switch if mode is date", function() {
57+
hours = TestUtils.renderIntoDocument(
58+
<DateTimePickerHours
59+
mode={Constants.MODE_DATE}
60+
onSwitch={onSwitchMock}
61+
setSelectedHour={setSelectedHourMock}
62+
/>
63+
);
64+
const switchList = TestUtils.scryRenderedDOMComponentsWithClass(hours, "list-unstyled");
65+
expect(switchList.length).toBe(0);
66+
});
67+
68+
it("renders 24 Hours", function() {
69+
hours = TestUtils.renderIntoDocument(
70+
<DateTimePickerHours
71+
mode={Constants.MODE_DATE}
72+
onSwitch={onSwitchMock}
73+
setSelectedHour={setSelectedHourMock}
74+
/>
75+
);
76+
const hourList = TestUtils.scryRenderedDOMComponentsWithClass(hours, "hour");
77+
expect(hourList.length).toBe(24);
78+
});
79+
80+
it("renders 01 to 24", function() {
81+
hours = TestUtils.renderIntoDocument(
82+
<DateTimePickerHours
83+
mode={Constants.MODE_DATE}
84+
onSwitch={onSwitchMock}
85+
setSelectedHour={setSelectedHourMock}
86+
/>
87+
);
88+
const hourList = TestUtils.scryRenderedDOMComponentsWithClass(hours, "hour");
89+
expect(hourList.map((x) => x.props.children)).toEqual(["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24"]);
90+
});
91+
92+
});
93+
94+
});
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import React from "react/addons";
2+
import Constants from "../Constants.js";
3+
jest.dontMock("../DateTimePickerMinutes.js");
4+
const { TestUtils } = React.addons;
5+
6+
describe("DateTimePickerMinutes", function() {
7+
const DateTimePickerMinutes = require("../DateTimePickerMinutes.js");
8+
let minutes, onSwitchMock, setSelectedMinuteMock;
9+
10+
11+
describe("Controls", function() {
12+
13+
beforeEach(() => {
14+
setSelectedMinuteMock = jest.genMockFunction();
15+
onSwitchMock = jest.genMockFunction();
16+
minutes = TestUtils.renderIntoDocument(
17+
<DateTimePickerMinutes
18+
mode={Constants.MODE_TIME}
19+
onSwitch={onSwitchMock}
20+
setSelectedMinute={setSelectedMinuteMock}
21+
/>
22+
);
23+
});
24+
25+
it("calls setSelectedMinute when clicking a minute", function() {
26+
const minute = TestUtils.scryRenderedDOMComponentsWithClass(minutes, "minute")[0];
27+
TestUtils.Simulate.click(minute);
28+
expect(setSelectedMinuteMock.mock.calls.length).toBe(1);
29+
});
30+
31+
it("calls onSwitch when clicking the switch", function() {
32+
const switchIcon = TestUtils.findRenderedDOMComponentWithClass(minutes, "picker-switch");
33+
TestUtils.Simulate.click(switchIcon);
34+
expect(onSwitchMock.mock.calls.length).toBe(1);
35+
});
36+
});
37+
38+
describe("UI", function() {
39+
beforeEach(() => {
40+
setSelectedMinuteMock = jest.genMockFunction();
41+
onSwitchMock = jest.genMockFunction();
42+
});
43+
44+
it("renders the switch if mode is time", function() {
45+
minutes = TestUtils.renderIntoDocument(
46+
<DateTimePickerMinutes
47+
mode={Constants.MODE_TIME}
48+
onSwitch={onSwitchMock}
49+
setSelectedMinute={setSelectedMinuteMock}
50+
/>
51+
);
52+
const switchList = TestUtils.scryRenderedDOMComponentsWithClass(minutes, "list-unstyled");
53+
expect(switchList.length).toBe(1);
54+
});
55+
56+
it("does not render the switch if mode is date", function() {
57+
minutes = TestUtils.renderIntoDocument(
58+
<DateTimePickerMinutes
59+
mode={Constants.MODE_DATE}
60+
onSwitch={onSwitchMock}
61+
setSelectedMinute={setSelectedMinuteMock}
62+
/>
63+
);
64+
const switchList = TestUtils.scryRenderedDOMComponentsWithClass(minutes, "list-unstyled");
65+
expect(switchList.length).toBe(0);
66+
});
67+
68+
it("renders 12 different Minutes", function() {
69+
minutes = TestUtils.renderIntoDocument(
70+
<DateTimePickerMinutes
71+
mode={Constants.MODE_DATE}
72+
onSwitch={onSwitchMock}
73+
setSelectedMinute={setSelectedMinuteMock}
74+
/>
75+
);
76+
const minuteList = TestUtils.scryRenderedDOMComponentsWithClass(minutes, "minute");
77+
expect(minuteList.length).toBe(12);
78+
});
79+
80+
it("renders 01 to 24", function() {
81+
minutes = TestUtils.renderIntoDocument(
82+
<DateTimePickerMinutes
83+
mode={Constants.MODE_DATE}
84+
onSwitch={onSwitchMock}
85+
setSelectedMinute={setSelectedMinuteMock}
86+
/>
87+
);
88+
const minuteList = TestUtils.scryRenderedDOMComponentsWithClass(minutes, "minute");
89+
expect(minuteList.map((x) => x.props.children)).toEqual(["00", "05", "10", "15", "20", "25", "30", "35", "40", "45", "50", "55"]);
90+
});
91+
92+
});
93+
94+
});
Lines changed: 86 additions & 0 deletions
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+
});

0 commit comments

Comments
 (0)