-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kevin Kirchhoff
committed
Aug 2, 2020
1 parent
f860ec9
commit f83256a
Showing
3 changed files
with
225 additions
and
3 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import PropTypes from 'prop-types'; | ||
import React from 'react'; | ||
import { Button, Col, Form, ListGroup, ListGroupItem } from 'react-bootstrap'; | ||
|
||
const RecipeIngredients = ({ addNewIngredient, ingredients, removeIngredient, updateIngredient }) => ( | ||
<> | ||
<ListGroup> | ||
{ingredients.map(({ name: ingredientName, quantity, unit }, index) => ( | ||
<ListGroupItem key={index}> | ||
<Button | ||
onClick={() => removeIngredient(index)} | ||
variant="link" | ||
className="float-right" | ||
> | ||
<i className="fas fa-times text-secondary" aria-hidden /> | ||
</Button> | ||
<Form.Row> | ||
<Form.Group as={Col} lg="4" xs="12" controlId={`ingredient-quantity-input-${index}`}> | ||
<Form.Label column="sm">Amount</Form.Label> | ||
<Form.Control | ||
className="ingredient-quantity-input" | ||
defaultValue={quantity} | ||
required | ||
type="number" | ||
step="any" | ||
onChange={(e) => updateIngredient(index, { | ||
name: ingredientName, | ||
quantity: Number(e.target.value), | ||
unit, | ||
})}> | ||
</Form.Control> | ||
</Form.Group> | ||
<Form.Group as={Col} lg="4" xs="12" controlId={`ingredient-unit-input-${index}`}> | ||
<Form.Label column="sm">Unit</Form.Label> | ||
<Form.Control | ||
className="ingredient-unit-input" | ||
defaultValue={unit} | ||
onChange={(e) => updateIngredient(index, { | ||
name: ingredientName, | ||
quantity, | ||
unit: e.target.value, | ||
})}> | ||
</Form.Control> | ||
</Form.Group> | ||
<Form.Group as={Col} lg="4" xs="12" controlId={`ingredient-name-input-${index}`}> | ||
<Form.Label column="sm">Ingredient</Form.Label> | ||
<Form.Control | ||
className="ingredient-name-input" | ||
required | ||
defaultValue={ingredientName} | ||
onChange={(e) => updateIngredient(index, { | ||
name: e.target.value, | ||
quantity, | ||
unit, | ||
})}> | ||
</Form.Control> | ||
</Form.Group> | ||
</Form.Row> | ||
</ListGroupItem> | ||
))} | ||
</ListGroup> | ||
<i className="fas fa-plus-square add-btn" tabIndex="0" onClick={addNewIngredient}></i> | ||
</> | ||
); | ||
|
||
RecipeIngredients.propTypes = { | ||
addNewIngredient: PropTypes.func.isRequired, | ||
ingredients: PropTypes.arrayOf(PropTypes.shape({ | ||
name: PropTypes.string.isRequired, | ||
quantity: PropTypes.number.isRequired, | ||
unit: PropTypes.string.isRequired, | ||
})).isRequired, | ||
removeIngredient: PropTypes.func.isRequired, | ||
updateIngredient: PropTypes.func.isRequired, | ||
}; | ||
|
||
export default RecipeIngredients; |
105 changes: 105 additions & 0 deletions
105
app/webpack/tests/components/recipe-form/RecipeIngredients.test.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import React from 'react'; | ||
import { shallow } from 'enzyme'; | ||
import RecipeIngredients from '../../../components/recipe-form/RecipeIngredients'; | ||
|
||
const ingredients = [{ | ||
name: 'broccoli', | ||
quantity: 1, | ||
unit: 'head', | ||
}, | ||
{ | ||
name: 'butter', | ||
quantity: 1.5, | ||
unit: 'tbsp', | ||
}]; | ||
|
||
describe('RecipeIngredients', () => { | ||
it('should render inputs for each ingredient', () => { | ||
const wrapper = shallow(<RecipeIngredients | ||
addNewIngredient={() => {}} | ||
ingredients={ingredients} | ||
removeIngredient={() => {}} | ||
updateIngredient={() => {}} | ||
/>); | ||
|
||
['name', 'quantity', 'unit'].forEach((field) => { | ||
const inputs = wrapper.find(`.ingredient-${field}-input`); | ||
expect(inputs).toHaveLength(ingredients.length); | ||
expect(inputs.at(0).props()).toHaveProperty('defaultValue', ingredients[0][field]); | ||
expect(inputs.at(1).props()).toHaveProperty('defaultValue', ingredients[1][field]); | ||
}); | ||
}); | ||
|
||
it('should call the updateIngredient when the quantity changes', () => { | ||
const updateIngredient = jest.fn(); | ||
const wrapper = shallow(<RecipeIngredients | ||
addNewIngredient={() => {}} | ||
ingredients={ingredients} | ||
removeIngredient={() => {}} | ||
updateIngredient={updateIngredient} | ||
/>); | ||
|
||
wrapper.find('.ingredient-quantity-input').at(1).simulate('change', { | ||
target: { value: '2.5' } | ||
}); | ||
|
||
expect(updateIngredient).toHaveBeenCalledWith(1, { | ||
name: 'butter', | ||
quantity: 2.5, | ||
unit: 'tbsp', | ||
}); | ||
}); | ||
|
||
it('should call the updateIngredient when the unit changes', () => { | ||
const updateIngredient = jest.fn(); | ||
const wrapper = shallow(<RecipeIngredients | ||
addNewIngredient={() => {}} | ||
ingredients={ingredients} | ||
removeIngredient={() => {}} | ||
updateIngredient={updateIngredient} | ||
/>); | ||
|
||
wrapper.find('.ingredient-unit-input').at(1).simulate('change', { | ||
target: { value: 'tsp' } | ||
}); | ||
|
||
expect(updateIngredient).toHaveBeenCalledWith(1, { | ||
name: 'butter', | ||
quantity: 1.5, | ||
unit: 'tsp', | ||
}); | ||
}); | ||
it('should call the updateIngredient when the unit changes', () => { | ||
const updateIngredient = jest.fn(); | ||
const wrapper = shallow(<RecipeIngredients | ||
addNewIngredient={() => {}} | ||
ingredients={ingredients} | ||
removeIngredient={() => {}} | ||
updateIngredient={updateIngredient} | ||
/>); | ||
|
||
wrapper.find('.ingredient-name-input').at(1).simulate('change', { | ||
target: { value: 'olive oil' } | ||
}); | ||
|
||
expect(updateIngredient).toHaveBeenCalledWith(1, { | ||
name: 'olive oil', | ||
quantity: 1.5, | ||
unit: 'tbsp', | ||
}); | ||
}); | ||
|
||
it('should call addNewIngredient when the add button is clicked', () => { | ||
const addNewIngredient = jest.fn(); | ||
const wrapper = shallow(<RecipeIngredients | ||
addNewIngredient={addNewIngredient} | ||
ingredients={[]} | ||
removeIngredient={() => {}} | ||
updateIngredient={() => {}} | ||
/>); | ||
|
||
wrapper.find('.add-btn').simulate('click'); | ||
|
||
expect(addNewIngredient).toHaveBeenCalled(); | ||
}); | ||
}); |