Skip to content

Commit

Permalink
Rename cloneIfObject to protectAgainstParamReassignment; Fix isObject (
Browse files Browse the repository at this point in the history
…#391)

* Improve isObject; Rename cloneIfObject to protectAgainstParamReassignment

* Just give up and import lodash.isPlainObject

* Update Travis targets
  • Loading branch information
rkuykendall authored Feb 16, 2020
1 parent 761b3ef commit 49fc634
Show file tree
Hide file tree
Showing 21 changed files with 1,148 additions and 997 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
language: node_js
sudo: false
node_js:
- '10'
- 'node'
- 'lts/*'
before_install:
- npm install -g yarn
script:
Expand Down
6 changes: 1 addition & 5 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,7 @@ optionally validate the inputs by passing `true` as the second argument.
```jsx
class MyForm extends React.Component {
render() {
return (
<Formsy preventDefaultSubmit>
...
</Formsy>
);
return <Formsy preventDefaultSubmit>...</Formsy>;
}
}
```
Expand Down
5 changes: 2 additions & 3 deletions __tests__/Formsy-spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ describe('Update a form', () => {
}
const form = mount(<TestForm />);
const FoundForm = form.find(TestForm);
const submitEvent = {preventDefault: jest.fn()};
const submitEvent = { preventDefault: jest.fn() };
FoundForm.simulate('submit', submitEvent);
expect(submitEvent.preventDefault).toHaveBeenCalled();
});
Expand All @@ -486,12 +486,11 @@ describe('Update a form', () => {
}
const form = mount(<TestForm />);
const FoundForm = form.find(TestForm);
const submitEvent = {preventDefault: jest.fn()};
const submitEvent = { preventDefault: jest.fn() };
FoundForm.simulate('submit', submitEvent);
expect(submitEvent.preventDefault).not.toHaveBeenCalled();
});


it('should trigger an onValidSubmit when submitting a valid form', () => {
const isCalled = jest.fn();

Expand Down
55 changes: 33 additions & 22 deletions __tests__/Utils-spec.tsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,43 @@
import * as utils from '../src/utils';

const VALUES = [
undefined,
null,
'',
0,
100,
'Hello',
'Goodbye',
{ foo: 'bar' },
{ foo: 'lounge' },
[{ foo: ['bar'] }],
[{ foo: [] }],
['a', 7],
['a', 8],
['a', 7, 7],
new Date(1000),
new Date(2000),
() => 2 + 2,
() => 2 + 3,
utils.noop,
];

function getReadable(value, index) {
return `${typeof value} at ${index}`;
}

const TYPES = {
isArray: [[], [{ foo: ['bar'] }], [{ foo: [] }], ['a', 7], ['a', 8], ['a', 7, 7]],
isDate: [new Date(1000), new Date(2000)],
isFunction: [() => 2 + 2, () => 2 + 3, utils.noop],
isNumber: [0, 100, 0.4],
isObject: [{}, { foo: 'bar' }, { foo: 'lounge' }],
isString: ['', 'Hello', 'Goodbye'],
isTypeUndefined: [undefined],
};

const VALUES = [
null,
...TYPES.isArray,
...TYPES.isDate,
...TYPES.isFunction,
...TYPES.isNumber,
...TYPES.isObject,
...TYPES.isString,
...TYPES.isTypeUndefined,
];

describe('Utils', () => {
// For each function in types
Object.keys(TYPES).forEach(isFn => {
// Create a test for that functiojn
it(isFn, () => {
// For each value in values
VALUES.forEach(value => {
// Make sure that if it is in that types TYPES array, it returns true
expect(utils[isFn](value)).toBe(TYPES[isFn].includes(value));
});
});
});

// For every value in values, run isSame(a, b) with every other value in the array.
// Expect isSame to return true only if you are at the same point in the array.
VALUES.forEach((a, idxa) => {
Expand Down
4 changes: 4 additions & 0 deletions dist/FormsyContext.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import React from 'react';
import { FormsyContextInterface } from './interfaces';
declare const _default: React.Context<FormsyContextInterface>;
export default _default;
105 changes: 55 additions & 50 deletions dist/Wrapper.d.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,55 @@
import React from 'react';
import PropTypes from 'prop-types';
import { RequiredValidation, Validations } from './interfaces';
declare const propTypes: {
innerRef: PropTypes.Requireable<(...args: any[]) => any>;
name: PropTypes.Validator<string>;
required: PropTypes.Requireable<string | boolean | object>;
validations: PropTypes.Requireable<string | object>;
value: PropTypes.Requireable<any>;
};
export interface WrapperProps<V> {
innerRef?: (ref: any) => void;
name: string;
required?: RequiredValidation<V>;
validationError?: any;
validationErrors?: any;
validations?: Validations<V>;
value?: V;
}
export interface WrapperState<V> {
[key: string]: unknown;
externalError: null;
formSubmitted: boolean;
isPristine: boolean;
isRequired: boolean;
isValid: boolean;
pristineValue: any;
validationError: any[];
value: V;
}
export interface InjectedProps<V> {
errorMessage: any;
errorMessages: any;
hasValue: boolean;
isFormDisabled: boolean;
isFormSubmitted: boolean;
isPristine: boolean;
isRequired: boolean;
isValid: boolean;
isValidValue: (value: V) => boolean;
ref?: any;
resetValue: any;
setValidations: any;
setValue: (value: V) => void;
showError: boolean;
showRequired: boolean;
}
export declare type PassDownProps<V> = WrapperProps<V> & InjectedProps<V>;
export { propTypes };
export default function <T, V>(WrappedComponent: React.ComponentType<T & PassDownProps<V>>): React.ComponentType<Omit<T & WrapperProps<V>, keyof InjectedProps<V>>>;
import React from 'react';
import PropTypes from 'prop-types';
import { RequiredValidation, Validations } from './interfaces';
declare const propTypes: {
innerRef: PropTypes.Requireable<(...args: any[]) => any>;
name: PropTypes.Validator<string>;
required: PropTypes.Requireable<string | boolean | object>;
validations: PropTypes.Requireable<string | object>;
value: PropTypes.Requireable<any>;
};
export interface WrapperProps<V> {
innerRef?: (ref: any) => void;
name: string;
required?: RequiredValidation<V>;
validationError?: any;
validationErrors?: any;
validations?: Validations<V>;
value?: V;
}
export interface WrapperState<V> {
[key: string]: unknown;
externalError: null;
formSubmitted: boolean;
isPristine: boolean;
isRequired: boolean;
isValid: boolean;
pristineValue: any;
validationError: any[];
value: V;
}
export interface InjectedProps<V> {
errorMessage: any;
errorMessages: any;
hasValue: boolean;
isFormDisabled: boolean;
isFormSubmitted: boolean;
isPristine: boolean;
isRequired: boolean;
isValid: boolean;
isValidValue: (value: V) => boolean;
ref?: any;
resetValue: () => void;
setValidations: (validations: Validations<V>, required: RequiredValidation<V>) => void;
setValue: (value: V) => void;
showError: boolean;
showRequired: boolean;
}
export interface WrapperInstanceMethods {
isValid: () => boolean;
getValue: () => any;
getErrorMessage: () => any;
}
export declare type PassDownProps<V> = WrapperProps<V> & InjectedProps<V>;
export { propTypes };
export default function <T, V>(WrappedComponent: React.ComponentType<T & PassDownProps<V>>): React.ComponentType<Omit<T & WrapperProps<V>, keyof InjectedProps<V>>>;
Loading

0 comments on commit 49fc634

Please sign in to comment.