Created
June 2, 2018 11:18
-
-
Save yashikagarg13/ba5d9c0df670754fd701ca2ff9e218b9 to your computer and use it in GitHub Desktop.
Simple react component for writing test using Jest and Enzyme
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from "react"; | |
export const Form = ({name, age, onSubmit, onUpdate}) => { | |
return ( | |
<form onSubmit={onSubmit}> | |
<div className="form-group"> | |
<label className="control-label">Name</label> | |
<div className="controls"> | |
<input | |
type="text" | |
className="input-md form-control" | |
id="name" | |
name="name" | |
value={name} | |
onChange={onUpdate.bind(null, "name")}/> | |
</div> | |
</div> | |
<div className="form-group"> | |
<label className="control-label">Age</label> | |
<div className="controls"> | |
<input | |
type="number" | |
className="input-md form-control" | |
id="age" | |
name="age" | |
onChange={onUpdate.bind(null, "age")}/> | |
</div> | |
</div> | |
<div className="controls clearfix"> | |
<button | |
type="submit" | |
className="btn btn-primary btn-md btn-block">Submit</button> | |
</div> | |
</form> | |
); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe('PageForm', () => { | |
let form; | |
const props = { | |
name: 'Xyz', | |
age: 20, | |
onSubmit: jest.fn(), | |
onUpdate: jest.fn() | |
} | |
beforeEach(() => { | |
form = shallow(<Form {...props} />); | |
}); | |
it('should exists', () => { | |
// Given | |
// When | |
// Then | |
expect(form.length).toEqual(1); | |
}); | |
it('should render name input', () => { | |
// Given | |
// When | |
let nameInput = form.find('input[id="name"]'); | |
// Then | |
expect(nameInput.length).toEqual(1); | |
}); | |
it('should render age input', () => { | |
// Given | |
// When | |
let ageInput = form.find('input[id="age"]'); | |
// Then | |
expect(ageInput.length).toEqual(1); | |
}); | |
it('should render submit button', () => { | |
// Given | |
// When | |
let submitBtn = form.find('button[type="submit"]'); | |
// Then | |
expect(submitBtn.length).toEqual(1); | |
}); | |
it('should call "onUpdate" on change of input', () => { | |
// Given | |
// When | |
let nameInput = form.find('input[id="name"]'); | |
nameInput.simulate('change'); | |
// Then | |
expect(props.onUpdate).toHaveBeenCalledWith('name'); | |
}); | |
it('should call "onSubmit" on form submit', () => { | |
// Given | |
// When | |
let submitBtn = form.find('form'); | |
submitBtn.simulate('submit'); | |
// Then | |
expect(props.onSubmit).toHaveBeenCalled(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment