Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to react testing lib instead of enzyme #679

Merged
merged 2 commits into from
Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions __test_utils__/DynamicInputForm.tsx
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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;
Loading