Skip to content

Commit 259fb5d

Browse files
committed
Add initial set of tests and move linting to Grunt task
Fixes #1
1 parent 09ca091 commit 259fb5d

11 files changed

+181
-4
lines changed

.babelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": ["react", "es2015"]
3+
}

.eslintrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"env": {
33
"browser": true,
44
"es6": true,
5+
"mocha": true,
56
"node": true
67
},
78
"extends": [
@@ -15,6 +16,7 @@
1516
"indent": 2,
1617
"no-console": 1,
1718
"no-trailing-spaces": 2,
19+
"no-unused-vars": [0, {"ignore": ["React"]}],
1820
"quotes": [2, "double"],
1921
"semi": 1
2022
}

Gruntfile.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module.exports = function(grunt) {
22
grunt.loadNpmTasks("grunt-esperanto");
3+
grunt.loadNpmTasks("grunt-mocha-test");
4+
grunt.loadNpmTasks("grunt-eslint");
35

46
grunt.initConfig({
57
esperanto: {
@@ -21,6 +23,18 @@ module.exports = function(grunt) {
2123
src : ["**.js"],
2224
dest: "dist/"
2325
}
26+
},
27+
mochaTest: {
28+
test: {
29+
options: {
30+
colors: true,
31+
require: ["babel-register", "./test/test_setup.js"]
32+
},
33+
src: ["test/**/*.js"]
34+
}
35+
},
36+
eslint: {
37+
target: ["Gruntfile.js", "test", "src"]
2438
}
2539
});
2640

@@ -38,5 +52,7 @@ module.exports = function(grunt) {
3852
});
3953

4054
grunt.registerTask("build", ["esperanto", "esperanto-cjs-cleanup"]);
55+
grunt.registerTask("lint", ["eslint"]);
56+
grunt.registerTask("test", ["lint", "build", "mochaTest"]);
4157
grunt.registerTask("default", ["build"]);
4258
};

package.json

100644100755
Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
"README.md",
1111
"src/"
1212
],
13-
"scripts": {
14-
"test": "eslint Gruntfile.js src"
15-
},
13+
"scripts": {},
1614
"keywords": [
1715
"react",
1816
"globalize",
@@ -36,12 +34,24 @@
3634
},
3735
"devDependencies": {
3836
"babel-eslint": "^4.1.6",
37+
"babel-preset-es2015": "^6.6.0",
38+
"babel-preset-react": "^6.5.0",
39+
"babel-register": "^6.7.2",
40+
"chai": "^3.5.0",
3941
"cldr-data": ">=25",
4042
"cldrjs": "^0.4.3",
43+
"enzyme": "^1.2.0",
4144
"eslint": "^1.10.3",
4245
"eslint-config-defaults": "^7.1.1",
4346
"eslint-plugin-react": "^3.11.2",
47+
"globalize": "1.1.x",
4448
"grunt": "^0.4.5",
45-
"grunt-esperanto": "^0.4.0"
49+
"grunt-eslint": "^18.0.0",
50+
"grunt-esperanto": "^0.4.0",
51+
"grunt-mocha-test": "^0.12.7",
52+
"mocha": "^2.4.5",
53+
"react": ">= 0.13.0 < 1.0.0",
54+
"react-addons-test-utils": "^0.14.7",
55+
"react-dom": "^0.14.7"
4656
}
4757
}

test/formatCurrency.spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*global expect React shallow Globalize*/
2+
import FormatCurrency from "../dist/currency";
3+
4+
describe("formatCurrency Component", () => {
5+
it("renders as a <span>", () => {
6+
const wrapper = shallow(<FormatCurrency currency="USD">{150}</FormatCurrency>);
7+
expect(wrapper.type()).to.equal("span");
8+
});
9+
10+
it("formats 150 as $150.00", () => {
11+
const wrapper = shallow(<FormatCurrency currency="USD">{150}</FormatCurrency>);
12+
expect(wrapper.text()).to.equal("$150.00");
13+
});
14+
});

test/formatDate.spec.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*global expect React shallow Globalize*/
2+
import FormatDate from "../dist/date";
3+
4+
describe("formatDate Component", () => {
5+
it("renders as a <span>", () => {
6+
const wrapper = shallow(<FormatDate options={{date: "medium"}}>{new Date()}</FormatDate>);
7+
expect(wrapper.type()).to.equal("span");
8+
});
9+
10+
it("formats date using default pattern as 1/1/2016", () => {
11+
const wrapper = shallow(<FormatDate>{new Date("Jan 01 2016")}</FormatDate>);
12+
expect(wrapper.text()).to.equal("1/1/2016");
13+
});
14+
15+
it("formats date using options 'Jan 1, 2016'", () => {
16+
const wrapper = shallow(<FormatDate options={{date: "medium"}}>{new Date(2016, 0, 1)}</FormatDate>);
17+
expect(wrapper.text()).to.equal("Jan 1, 2016");
18+
});
19+
});

test/formatMessage.spec.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*global expect React shallow Globalize*/
2+
import FormatMessage from "../dist/message";
3+
4+
Globalize.loadMessages({
5+
en: {
6+
salutations: {
7+
hi: "Hi"
8+
},
9+
variables: {
10+
hello: "Hello, {0} {1} {2}"
11+
},
12+
party: [
13+
"{hostGender, select,",
14+
" female {{host} invites {guest} to her party}",
15+
" male {{host} invites {guest} to his party}",
16+
" other {{host} invites {guest} to their party}",
17+
"}"
18+
],
19+
task: [
20+
"You have {count, plural,",
21+
" =0 {no tasks}",
22+
" one {one task}",
23+
" other {{formattedCount} tasks}",
24+
"} remaining"
25+
]
26+
}
27+
});
28+
29+
describe("formatMessage Component", () => {
30+
it("renders as a <span>", () => {
31+
const wrapper = shallow(<FormatMessage path="salutations/hi" />);
32+
expect(wrapper.type()).to.equal("span");
33+
});
34+
35+
it("uses default message and prints 'Hi'", () => {
36+
const wrapper = shallow(<FormatMessage>Hi</FormatMessage>);
37+
expect(wrapper.text()).to.equal("Hi");
38+
});
39+
40+
it("resolves path and prints 'Hi'", () => {
41+
const wrapper = shallow(<FormatMessage path="salutations/hi" />);
42+
expect(wrapper.text()).to.equal("Hi");
43+
});
44+
45+
it("properly replaces variables", () => {
46+
const wrapper = shallow(<FormatMessage path="variables/hello" variables={["Wolfgang", "Amadeus", "Mozart"]} />);
47+
expect(wrapper.text()).to.equal("Hello, Wolfgang Amadeus Mozart");
48+
});
49+
50+
it("uses proper gender inflection", () => {
51+
const wrapper = shallow(<FormatMessage path="party" variables={{guest:"Mozart", guestGender:"male", host:"Beethoven", hostGender:"other"}} />);
52+
expect(wrapper.text()).to.equal("Beethoven invites Mozart to their party");
53+
});
54+
55+
it("uses proper plural inflection", () => {
56+
const wrapper = shallow(<FormatMessage path="task" variables={{count: 1}} />);
57+
expect(wrapper.text()).to.equal("You have one task remaining");
58+
});
59+
});

test/formatNumber.spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*global expect React shallow Globalize*/
2+
import FormatNumber from "../dist/number";
3+
4+
describe("formatNumber Component", () => {
5+
it("renders as a <span>", () => {
6+
const wrapper = shallow(<FormatNumber options={{ round: "floor" }}>{Math.PI}</FormatNumber>);
7+
expect(wrapper.type()).to.equal("span");
8+
});
9+
10+
it("formats pi as 3.141", () => {
11+
const wrapper = shallow(<FormatNumber options={{ round: "floor" }}>{Math.PI}</FormatNumber>);
12+
expect(wrapper.text()).to.equal("3.141");
13+
});
14+
});

test/formatRelativeTime.spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*global expect React shallow Globalize*/
2+
import FormatRelativeTime from "../dist/relative-time";
3+
4+
describe("formatRelativeTime Component", () => {
5+
it("renders as a <span>", () => {
6+
const wrapper = shallow(<FormatRelativeTime unit="week">{1}</FormatRelativeTime>);
7+
expect(wrapper.type()).to.equal("span");
8+
});
9+
10+
it("formats value of 1 week from now as 'next week'", () => {
11+
const wrapper = shallow(<FormatRelativeTime unit="week">{1}</FormatRelativeTime>);
12+
expect(wrapper.text()).to.equal("next week");
13+
});
14+
});

test/general.spec.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/*global expect React shallow Globalize*/
2+
import FormatCurrency from "../dist/currency";
3+
4+
describe("General Functionality Tests", () => {
5+
it("overrides default locale to format 150 as 150,00 €", () => {
6+
const wrapper = shallow(<FormatCurrency locale="de" currency="EUR">{150}</FormatCurrency>);
7+
expect(wrapper.text()).to.equal("150,00 €");
8+
});
9+
});

0 commit comments

Comments
 (0)