|
| 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 | +}); |
0 commit comments