Skip to content

Commit 93aa002

Browse files
committed
feat: add first utils
1 parent 32b18e7 commit 93aa002

21 files changed

+7732
-0
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-native"]
3+
}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
coverage
2+
node_modules

.travis.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
language: node_js
2+
cache:
3+
directories:
4+
- ~/.npm
5+
notifications:
6+
email: false
7+
node_js:
8+
- '9'
9+
after_success:
10+
- npm run travis-deploy-once "npm run semantic-release"
11+
branches:
12+
except:
13+
- /^v\d+\.\d+\.\d+$/

index.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { compose } from "recompose";
2+
import withError from "./src/withError";
3+
import withFocus from "./src/withFocus";
4+
import withFormik from "./src/withFormik";
5+
import withInputTypeProps from "./src/withInputTypeProps";
6+
import withTouched from "./src/withTouched";
7+
import makeReactNativeField from "./src/makeReactNativeField";
8+
import { withNextInputAutoFocusForm, withNextInputAutoFocusInput } from "./src/withNextInputAutoFocus";
9+
10+
const makeInputsGreatAgain = compose(withInputTypeProps, withError, withTouched, makeReactNativeField);
11+
12+
export default makeInputsGreatAgain;
13+
14+
export {
15+
makeInputsGreatAgain,
16+
makeReactNativeField,
17+
withError,
18+
withFocus,
19+
withFormik,
20+
withInputTypeProps,
21+
withTouched,
22+
withNextInputAutoFocusForm,
23+
withNextInputAutoFocusInput
24+
};

jest.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const path = require("path");
2+
3+
module.exports = {
4+
preset: "react-native",
5+
setupFiles: [path.join(__dirname, "./jest/preamble.js")]
6+
};

jest/preamble.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import Enzyme from "enzyme";
2+
import Adapter from "enzyme-adapter-react-16";
3+
4+
Enzyme.configure({ adapter: new Adapter() });
5+
6+
const mockDocumentForEnzymeMount = () => {
7+
const jsdom = require("jsdom");
8+
9+
const { JSDOM } = jsdom;
10+
11+
const { document } = new JSDOM("").window;
12+
global.document = document;
13+
};
14+
mockDocumentForEnzymeMount();

package.json

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"name": "react-native-formik",
3+
"version": "0.0.0-development",
4+
"description": "Make the most of your React Native forms with Formik",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "jest --coverage",
8+
"commitmsg": "validate-commit-msg",
9+
"commit": "git-cz",
10+
"semantic-release": "semantic-release",
11+
"travis-deploy-once": "travis-deploy-once"
12+
},
13+
"repository": {
14+
"type": "git",
15+
"url": "https://github.com/Almouro/react-native-formik.git"
16+
},
17+
"keywords": ["react-native", "formik", "forms"],
18+
"dependencies": {
19+
"lodash": "4.17.5",
20+
"recompose": "^0.26.0"
21+
},
22+
"author": "Almouro <contact@almouro.com> (http://alexandremoureaux.com/)",
23+
"license": "MIT",
24+
"bugs": {
25+
"url": "https://github.com/bamlab/react-native-formik/issues"
26+
},
27+
"homepage": "https://github.com/bamlab/react-native-formik#readme",
28+
"devDependencies": {
29+
"babel-core": "6.26.0",
30+
"babel-jest": "^22.4.1",
31+
"babel-preset-react-native": "4.0.0",
32+
"commitizen": "^2.9.6",
33+
"cz-conventional-changelog": "^2.1.0",
34+
"enzyme": "^3.3.0",
35+
"enzyme-adapter-react-16": "^1.0.0",
36+
"husky": "^0.14.3",
37+
"jest": "^22.4.2",
38+
"react": "16.2.0",
39+
"react-dom": "16.2.0",
40+
"react-native": "0.53.0",
41+
"semantic-release": "^15.0.2",
42+
"validate-commit-msg": "^2.14.0",
43+
"travis-deploy-once": "^4.4.0"
44+
},
45+
"config": {
46+
"commitizen": {
47+
"path": "./node_modules/cz-conventional-changelog"
48+
}
49+
}
50+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import React from "react";
2+
import PropTypes from "prop-types";
3+
import { compose, withContext } from "recompose";
4+
import { TextInput } from "react-native";
5+
import { mount } from "enzyme";
6+
7+
import { makeReactNativeField } from "../..";
8+
9+
console.error = jest.fn();
10+
11+
const setFieldValue = jest.fn();
12+
const setFieldTouched = jest.fn();
13+
14+
const withFormikMock = withContext({ formik: PropTypes.object }, () => ({
15+
formik: {
16+
setFieldValue,
17+
setFieldTouched,
18+
values: { email: "contact@bam.tech" }
19+
}
20+
}));
21+
const Input = compose(withFormikMock, makeReactNativeField)(TextInput);
22+
23+
describe("makeReactNativeField", () => {
24+
it("sets the input value", () => {
25+
const emailInput = mount(<Input name="email" />);
26+
expect(emailInput.find(TextInput).props().value).toEqual("contact@bam.tech");
27+
const otherInput = mount(<Input name="other" />);
28+
expect(otherInput.find(TextInput).props().value).toEqual(undefined);
29+
});
30+
31+
it("handles input value change", () => {
32+
const wrapper = mount(<Input name="email" />);
33+
wrapper
34+
.find(TextInput)
35+
.props()
36+
.onChangeText("New text");
37+
expect(setFieldValue).toHaveBeenCalledWith("email", "New text");
38+
});
39+
40+
it("allows override of onChangeText", () => {
41+
const onChangeText = jest.fn();
42+
const wrapper = mount(<Input name="email" onChangeText={onChangeText} />);
43+
wrapper
44+
.find(TextInput)
45+
.props()
46+
.onChangeText("New text");
47+
expect(onChangeText).toHaveBeenCalledWith("New text");
48+
expect(setFieldValue).toHaveBeenCalledWith("email", "New text");
49+
});
50+
51+
it("handles input touch event", () => {
52+
const wrapper = mount(<Input name="email" />);
53+
wrapper
54+
.find(TextInput)
55+
.props()
56+
.onBlur();
57+
expect(setFieldTouched).toHaveBeenCalledWith("email");
58+
});
59+
60+
it("allows override of onBlur", () => {
61+
const onBlur = jest.fn();
62+
const wrapper = mount(<Input name="email" onBlur={onBlur} />);
63+
wrapper
64+
.find(TextInput)
65+
.props()
66+
.onBlur();
67+
expect(setFieldTouched).toHaveBeenCalledWith("email");
68+
expect(onBlur).toHaveBeenCalled();
69+
});
70+
});

src/__tests__/withError.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import React from "react";
2+
import PropTypes from "prop-types";
3+
import { compose, withContext } from "recompose";
4+
import { TextInput } from "react-native";
5+
import { mount } from "enzyme";
6+
7+
import { withError } from "../..";
8+
9+
console.error = jest.fn();
10+
11+
const withFormikMock = withContext({ formik: PropTypes.object }, () => ({
12+
formik: {
13+
errors: { errored: "This is an error" }
14+
}
15+
}));
16+
const Input = compose(withFormikMock, withError)(TextInput);
17+
18+
describe("withError", () => {
19+
it("sets the error prop", () => {
20+
const erroredInput = mount(<Input name="errored" />);
21+
expect(erroredInput.find(TextInput).props().error).toEqual("This is an error");
22+
const validInput = mount(<Input name="valid" />);
23+
expect(validInput.find(TextInput).props().error).toBeFalsy();
24+
});
25+
26+
it("keeps other props", () => {
27+
const wrapper = mount(<Input name="inputName" someProp="someValue" />);
28+
expect(wrapper.find(TextInput).props().someProp).toEqual("someValue");
29+
});
30+
});

src/__tests__/withFocus.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import React from "react";
2+
import { TextInput } from "react-native";
3+
import { mount } from "enzyme";
4+
5+
import { withFocus } from "../..";
6+
7+
console.error = jest.fn();
8+
9+
const Input = withFocus(TextInput);
10+
11+
describe("withFocus", () => {
12+
it("sets the focus prop", () => {
13+
const wrapper = mount(<Input />);
14+
15+
expect(wrapper.find(TextInput).props().focused).toEqual(false);
16+
17+
wrapper
18+
.find(TextInput)
19+
.props()
20+
.onFocus();
21+
wrapper.update();
22+
expect(wrapper.find(TextInput).props().focused).toEqual(true);
23+
24+
wrapper
25+
.find(TextInput)
26+
.props()
27+
.onBlur();
28+
wrapper.update();
29+
expect(wrapper.find(TextInput).props().focused).toEqual(false);
30+
});
31+
32+
it("calls onFocus and onBlur if passed", () => {
33+
const onFocus = jest.fn();
34+
const onBlur = jest.fn();
35+
36+
const wrapper = mount(<Input onFocus={onFocus} onBlur={onBlur} />);
37+
38+
expect(wrapper.find(TextInput).props().focused).toEqual(false);
39+
40+
wrapper
41+
.find(TextInput)
42+
.props()
43+
.onFocus();
44+
wrapper.update();
45+
expect(onFocus).toHaveBeenCalled();
46+
expect(wrapper.find(TextInput).props().focused).toEqual(true);
47+
48+
wrapper
49+
.find(TextInput)
50+
.props()
51+
.onBlur();
52+
wrapper.update();
53+
expect(onBlur).toHaveBeenCalled();
54+
expect(wrapper.find(TextInput).props().focused).toEqual(false);
55+
});
56+
});

0 commit comments

Comments
 (0)