Skip to content

Commit 6fc0ccb

Browse files
committed
Merge pull request ismaelga#8 from bananafunctor/master
Added basic unit test for testing dependant dropdowns
2 parents 92056ab + 5f29a36 commit 6fc0ccb

File tree

4 files changed

+175
-1
lines changed

4 files changed

+175
-1
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"]
3+
}

__tests__/form.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
jest.dontMock('./formSchema.json')
2+
.dontMock('..');
3+
4+
describe('React Form', () => {
5+
it('Should render all three levels', function() {
6+
var React = require('react');
7+
var ReactDOM = require('react-dom');
8+
var TestUtils = require('react-addons-test-utils');
9+
const Form = require("..");
10+
11+
var schema = require('./formSchema.json')
12+
var renderedForm = TestUtils.renderIntoDocument(<Form schema = {schema}/>);
13+
var likeColorSelect = TestUtils.findRenderedDOMComponentWithTag(renderedForm, "select");
14+
15+
//Set value of first dropdown to 'maybe'
16+
// Note: Simply changing property 'value' will not trigger the event, we must also trigger the value onChange event.
17+
likeColorSelect.value = "maybe";
18+
TestUtils.Simulate.change(likeColorSelect, { value : "maybe" });
19+
20+
// Count amount of selects present in form
21+
var count = TestUtils.scryRenderedDOMComponentsWithTag(renderedForm, "select").length
22+
expect(count).toEqual(2);
23+
24+
// Rinse and repeat...
25+
var likeColorSelect2 = TestUtils.scryRenderedDOMComponentsWithTag(renderedForm, "select")[1];
26+
likeColorSelect2.value = "Yes";
27+
TestUtils.Simulate.change(likeColorSelect2, { value : "Yes"});
28+
29+
count = TestUtils.scryRenderedDOMComponentsWithTag(renderedForm, "select").length
30+
expect(count).toEqual(3);
31+
});
32+
33+
});

__tests__/formSchema.json

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
{
2+
"definitions": {
3+
"trueFave": {
4+
"title": "A true favourite",
5+
"type": "object",
6+
"properties": {
7+
"realFave": {
8+
"title": "Yes/No?",
9+
"type": "string"
10+
}
11+
},
12+
"oneOf": [
13+
{
14+
"properties": {
15+
"realFave": {
16+
"enum": [
17+
"No"
18+
]
19+
}
20+
}
21+
},
22+
{
23+
"properties": {
24+
"realFave": {
25+
"enum": [
26+
"Yes"
27+
]
28+
},
29+
"favo": {
30+
"title": "Which one is it?",
31+
"type": "string",
32+
"enum": [
33+
"Red",
34+
"Blueberry",
35+
"rainbow"
36+
]
37+
}
38+
}
39+
}
40+
],
41+
"x-hints": {
42+
"form": {
43+
"selector": "realFave"
44+
}
45+
}
46+
}
47+
},
48+
"title": "Example form",
49+
"description": "A form based on a schema",
50+
"type": "object",
51+
"x-hints": {
52+
"form": {
53+
"classes": "my-nice-form"
54+
}
55+
},
56+
"properties": {
57+
"color": {
58+
"title": "Favourite colour",
59+
"type": "object",
60+
"properties": {
61+
"hasFave": {
62+
"title": "Do you have a favourite colour?",
63+
"type": "string"
64+
}
65+
},
66+
"oneOf": [
67+
{
68+
"properties": {
69+
"hasFave": {
70+
"enum": [
71+
"no"
72+
]
73+
}
74+
}
75+
},
76+
{
77+
"properties": {
78+
"hasFave": {
79+
"enum": [
80+
"maybe"
81+
]
82+
},
83+
"trueFave": {
84+
"title": "You must have a favourite colour!",
85+
"$ref": "#/definitions/trueFave"
86+
}
87+
}
88+
},
89+
{
90+
"properties": {
91+
"hasFave": {
92+
"enum": [
93+
"yes"
94+
]
95+
},
96+
"fave": {
97+
"title": "Your favourite colour",
98+
"type": "string",
99+
"enum": [
100+
"",
101+
"red",
102+
"green",
103+
"blue",
104+
"yellow",
105+
"orange",
106+
"purple",
107+
"other"
108+
]
109+
}
110+
}
111+
}
112+
],
113+
"x-hints": {
114+
"form": {
115+
"selector": "hasFave"
116+
}
117+
}
118+
}
119+
}
120+
}

package.json

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"start": "node server.js",
77
"gh-pages": "webpack --config ./config/webpack.gh-pages.js",
88
"deploy-gh-pages": "scripts/deploy.sh",
9-
"dist": "webpack --config ./config/webpack.dist.js && webpack --config ./config/webpack.dist.min.js"
9+
"dist": "webpack --config ./config/webpack.dist.js && webpack --config ./config/webpack.dist.min.js",
10+
"test": "jest"
1011
},
1112
"keywords": [
1213
"JSON-Schema",
@@ -35,20 +36,37 @@
3536
"url": "https://github.com/ismaelga/react-json-editor.git"
3637
},
3738
"devDependencies": {
39+
"babel-jest": "^6.0.1",
40+
"babel-preset-react": "^6.3.13",
3841
"css-loader": "^0.9.1",
3942
"file-loader": "^0.8.1",
4043
"highlight.js": "^8.4.0",
4144
"html-loader": "^0.2.3",
4245
"html-webpack-plugin": "^1.1.0",
46+
"jest-cli": "^0.8.2",
4347
"jsx-loader": "^0.12.2",
4448
"marked": "^0.3.3",
4549
"purecss": "^0.5.0",
4650
"react-ghfork": "^0.3.0",
4751
"react-hot-loader": "^1.1.6",
52+
"react-addons-test-utils": "^0.14.6",
53+
"react-dom": "^0.14.6",
4854
"style-loader": "^0.8.3",
4955
"url-loader": "^0.5.5",
5056
"webpack": "^1.6.0",
5157
"webpack-dev-server": "^1.7.0",
5258
"xtend": "^4.0.0"
59+
},
60+
"jest": {
61+
"scriptPreprocessor": "<rootDir>/node_modules/babel-jest",
62+
"unmockedModulePathPatterns": [
63+
"<rootDir>/node_modules/react",
64+
"<rootDir>/node_modules/react-dom",
65+
"<rootDir>/node_modules/react-addons-test-utils",
66+
"<rootDir>/node_modules/fbjs"
67+
],
68+
"modulePathIgnorePatterns": [
69+
"<rootDir>/node_modules/"
70+
]
5371
}
5472
}

0 commit comments

Comments
 (0)