refactor(Form*): using shared tests

parent 0fb32730
import React from 'react';
import { shallow } from 'enzyme';
import fakeEvent from 'fake-event';
import FormA from '../FormA';
import { commonFormValidation, commonFormOnUpdate } from '../test/shared/shouldBehaveLikeForm';
describe('<FormA />', () => {
beforeEach(() =>{
......@@ -10,32 +7,10 @@ describe('<FormA />', () => {
});
describe('render error messages', () => {
test('render name error message', () =>{
const component = shallow(<FormA {...this.commonProps} />);
component.setState({ fields: {age: 12} });
component.find('form').simulate('submit', fakeEvent());
expect(component.text()).toEqual(expect.stringContaining('Name is Required'));
});
test('render age error message', () => {
const component = shallow(<FormA {...this.commonProps} />);
component.setState({ fields: {name: 'Name'} });
component.find('form').simulate('submit', fakeEvent());
expect(component.text()).toEqual(expect.stringContaining('Age is Required'));
});
commonFormValidation.bind(this)(FormA);
});
describe('change events update states', () =>{
test('update name state', () =>{
const component = shallow(<FormA {...this.commonProps} />);
component.find('input[name="name"]').simulate('change', fakeEvent({ target: { name: 'name', value: 'Name'} }));
expect(component.state('fields').name).toEqual('Name');
});
test('update age state', () =>{
const component = shallow(<FormA {...this.commonProps} />);
component.find('input[name="age"]').simulate('change', fakeEvent({ target: { name: 'age', value: 20} }));
expect(component.state('fields').age).toEqual(20);
});
commonFormOnUpdate.bind(this)(FormA);
});
});
\ No newline at end of file
......@@ -3,6 +3,7 @@ import { shallow } from 'enzyme';
import fakeEvent from 'fake-event';
import FormB from '../FormB';
import { commonFormValidation, commonFormOnUpdate } from '../test/shared/shouldBehaveLikeForm';
describe('<FormB />', () => {
beforeEach(() =>{
......@@ -10,19 +11,7 @@ describe('<FormB />', () => {
});
describe('render error messages', () => {
test('render name error message', () =>{
const component = shallow(<FormB {...this.commonProps} />);
component.setState({ fields: {age: 12, gender: 'male'} });
component.find('form').simulate('submit', fakeEvent());
expect(component.text()).toEqual(expect.stringContaining('Name is Required'));
});
test('render age error message', () => {
const component = shallow(<FormB {...this.commonProps} />);
component.setState({ fields: {name: 'Name', gender: 'male'} });
component.find('form').simulate('submit', fakeEvent());
expect(component.text()).toEqual(expect.stringContaining('Age is Required'));
});
commonFormValidation.bind(this)(FormB);
test('render age error message', () => {
const component = shallow(<FormB {...this.commonProps} />);
......@@ -33,17 +22,7 @@ describe('<FormB />', () => {
});
describe('change events update states', () =>{
test('update name state', () =>{
const component = shallow(<FormB {...this.commonProps} />);
component.find('input[name="name"]').simulate('change', fakeEvent({ target: { name: 'name', value: 'Name'} }));
expect(component.state('fields').name).toEqual('Name');
});
test('update age state', () =>{
const component = shallow(<FormB {...this.commonProps} />);
component.find('input[name="age"]').simulate('change', fakeEvent({ target: { name: 'age', value: 20} }));
expect(component.state('fields').age).toEqual(20);
});
commonFormOnUpdate.bind(this)(FormB);
test('update Gender state', () =>{
const component = shallow(<FormB {...this.commonProps} />);
......
import React from "react";
import { shallow } from 'enzyme';
import fakeEvent from 'fake-event';
export const commonFormValidation = function (Form) {
test('render name error message', () =>{
const component = shallow(<Form {...this.commonProps} />);
component.setState({ fields: {age: 12, gender: 'male'} });
component.find('form').simulate('submit', fakeEvent());
expect(component.text()).toEqual(expect.stringContaining('Name is Required'));
});
test('render age error message', () => {
const component = shallow(<Form {...this.commonProps} />);
component.setState({ fields: {name: 'Name', gender: 'male'} });
component.find('form').simulate('submit', fakeEvent());
expect(component.text()).toEqual(expect.stringContaining('Age is Required'));
});
}
export const commonFormOnUpdate = function (Form) {
test('update name state', () =>{
const component = shallow(<Form {...this.commonProps} />);
component.find('input[name="name"]').simulate('change', fakeEvent({ target: { name: 'name', value: 'Name'} }));
expect(component.state('fields').name).toEqual('Name');
});
test('update age state', () =>{
const component = shallow(<Form {...this.commonProps} />);
component.find('input[name="age"]').simulate('change', fakeEvent({ target: { name: 'age', value: 20} }));
expect(component.state('fields').age).toEqual(20);
});
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment