Skip to content

Commit

Permalink
Migrate to react testing lib instead of enzyme (#679)
Browse files Browse the repository at this point in the history
* Migrate to react testing lib instead of enzyme

* Reformat the tests with prettier
  • Loading branch information
felixmosh committed Jul 28, 2022
1 parent 6003348 commit 901aed2
Show file tree
Hide file tree
Showing 33 changed files with 1,393 additions and 1,323 deletions.
6 changes: 3 additions & 3 deletions __test_utils__/DynamicInputForm.tsx
Expand Up @@ -18,7 +18,7 @@ class DynamicInputForm extends React.Component<DynamicInputFormProps, { input: a
private addInput = () => {
const { inputName } = this.props;
this.setState({
input: <TestInput name={inputName} value="" />,
input: <TestInput name={inputName} value="" testId="test-input" />,
});
};

Expand All @@ -28,11 +28,11 @@ class DynamicInputForm extends React.Component<DynamicInputFormProps, { input: a

return (
<>
<Formsy onSubmit={onSubmit}>
<Formsy onSubmit={onSubmit} data-testid="form">
{input}
{children}
</Formsy>
<button type="button" onClick={this.addInput}>
<button type="button" onClick={this.addInput} data-testid="add-input-btn">
Add input
</button>
</>
Expand Down
19 changes: 17 additions & 2 deletions __test_utils__/TestInput.tsx
Expand Up @@ -4,15 +4,30 @@ import React from 'react';
import { withFormsy } from '../src';
import { PassDownProps } from '../src/withFormsy';

export type FormsyInputProps = Omit<React.HTMLProps<HTMLInputElement>, 'required' | 'value'> & PassDownProps<string>;
export type FormsyInputProps = Omit<React.HTMLProps<HTMLInputElement>, 'required' | 'value'> &
PassDownProps<string> & { testId?: string };

class TestInput extends React.Component<FormsyInputProps> {
updateValue = (event) => {
this.props.setValue(event.target[this.props.type === 'checkbox' ? 'checked' : 'value']);
};

render() {
return <input type={this.props.type || 'text'} value={this.props.value || ''} onChange={this.updateValue} />;
return (
<input
type={this.props.type || 'text'}
value={this.props.value || ''}
onChange={this.updateValue}
data-is-valid={this.props.isValid}
data-is-pristine={this.props.isPristine}
data-error-message={this.props.errorMessage}
data-error-messages={this.props.errorMessages.join(';')}
data-is-form-disabled={this.props.isFormDisabled}
data-is-form-submitted={this.props.isFormSubmitted}
data-value={JSON.stringify(this.props.value)}
data-testid={this.props.testId}
/>
);
}
}

Expand Down
10 changes: 7 additions & 3 deletions __test_utils__/TestInputHoc.tsx
@@ -1,10 +1,14 @@
import React from 'react';
import { withFormsy } from '../src';
import { PassDownProps } from '../src/withFormsy';

class TestComponent extends React.Component {
type TestComponentProps = { testId?: string; name?: string } & PassDownProps<string>;

class TestComponent extends React.Component<TestComponentProps> {
render() {
return <input />;
const { testId, name } = this.props;
return <input data-testid={testId} name={name} />;
}
}

export default withFormsy(TestComponent as any);
export default withFormsy<TestComponentProps, any>(TestComponent);
31 changes: 0 additions & 31 deletions __test_utils__/expectIsValid.tsx

This file was deleted.

14 changes: 0 additions & 14 deletions __test_utils__/getInput.ts

This file was deleted.

31 changes: 31 additions & 0 deletions __test_utils__/getValidState.tsx
@@ -0,0 +1,31 @@
import { render } from '@testing-library/react';
import React from 'react';

import Formsy from '../src';
import { InputFactory } from './TestInput';

const TestInput = InputFactory({
render() {
const { value, isValid, testId } = this.props;
return <input value={value || ''} readOnly data-is-valid={isValid} data-testid={testId} />;
},
});

function ValidationForm(props: { validations: string; value?: any }) {
const { validations, value } = props;

return (
<Formsy>
<TestInput name="foo" validations={validations} value={value} testId="test-input" />
</Formsy>
);
}

export async function getValidState(testForm: React.ComponentElement<any, any>) {
const form = render(testForm);
const inputComponent = await form.findByTestId('test-input');

return inputComponent.dataset.isValid === 'true';
}

export default ValidationForm;
3 changes: 0 additions & 3 deletions __test_utils__/immediate.ts

This file was deleted.

0 comments on commit 901aed2

Please sign in to comment.