From d1a63912fe04582b9be1df9e7e3a8e4c85be76dd Mon Sep 17 00:00:00 2001 From: CXG6495 Date: Fri, 18 Oct 2019 13:34:54 -0500 Subject: [PATCH 1/3] added in the ability to edit the hometown, and tests for it [closes #81] --- ui/src/services/WishDetailsService.js | 6 +++ ui/src/services/WishDetailsService.test.js | 19 +++++++++ ui/src/wishDetails/WishDetails.js | 28 ++++++++++++- ui/src/wishDetails/WishDetails.test.js | 47 +++++++++++++++++++++- 4 files changed, 97 insertions(+), 3 deletions(-) diff --git a/ui/src/services/WishDetailsService.js b/ui/src/services/WishDetailsService.js index ec393cfd..e49f87a9 100644 --- a/ui/src/services/WishDetailsService.js +++ b/ui/src/services/WishDetailsService.js @@ -12,6 +12,11 @@ const makeAWish = async(wish) => { return response.data; } +const editAWish = async(id, wish) => { + let response = await axios.put(`${expressDomain}/wishes/${id}`, wish); + return response.data; +} + export const getWishes = async (types) => { let typeParams = types && types.length ? `?types=${types.toString()}` : ''; // TODO query param options @@ -22,5 +27,6 @@ export const getWishes = async (types) => { export default { getWishDetails, makeAWish, + editAWish, getWishes } diff --git a/ui/src/services/WishDetailsService.test.js b/ui/src/services/WishDetailsService.test.js index f0e8ce98..c8705acd 100644 --- a/ui/src/services/WishDetailsService.test.js +++ b/ui/src/services/WishDetailsService.test.js @@ -53,6 +53,25 @@ describe('Wish details service', () => { expect(axios.post).toHaveBeenCalledWith(`${expressDomain}/wishes`, wish); }) + it('should edit a wish when required', async () => { + const wish = { + "child": { + "firstName": "test2", + "lastName": "test2", + "age": "4", + "hometown": "Not Atlanta", + "illness": "sick" + }, + "type": "go", + "details": "to disneyland" + } + + axios.put = jest.fn(() => Promise.resolve({ data: wish })); + const response = await WishDetailsService.editAWish(wishId, wish); + expect(axios.put).toHaveBeenCalledWith(`${expressDomain}/wishes/${wishId}`, wish); + expect(response.child.hometown).toEqual('Not Atlanta') + }); + it('should get wishes', async () => { await WishDetailsService.getWishes(); expect(axios.get).toHaveBeenCalledWith(`${expressDomain}/wishes`); diff --git a/ui/src/wishDetails/WishDetails.js b/ui/src/wishDetails/WishDetails.js index 63a6c3be..cac3c274 100644 --- a/ui/src/wishDetails/WishDetails.js +++ b/ui/src/wishDetails/WishDetails.js @@ -36,6 +36,24 @@ export default class WishDetails extends Component { } } + updateHometownField(hometown) { + this.setState(prevState => ({ + ...prevState, + wishDetails: { + ...prevState.wishDetails, + child: { + ...prevState.wishDetails.child, + hometown: hometown + } + } + })); + } + // () => this.updateHometown({child: wishDetails.child, type: wishDetails.type, details: wishDetails.details} + updateHometown = async (wish) => { + const { id } = this.props.match.params + return await WishDetailsService.editAWish(id, wish); + } + async componentDidMount() { const { id } = this.props.match.params const wishDetails = await WishDetailsService.getWishDetails(id) @@ -69,9 +87,15 @@ export default class WishDetails extends Component { } render() { - const { child, details, sponsor } = this.state.wishDetails + const { child, details, sponsor, type } = this.state.wishDetails const { name, age, hometown } = child + const wish = { + child: child, + type: type, + details: details + } + return (
@@ -95,7 +119,7 @@ export default class WishDetails extends Component {
- {hometown} + this.updateHometown(wish)} onChange={(e) => this.updateHometownField(e.target.value)} value={hometown}/>

Illness Summary

diff --git a/ui/src/wishDetails/WishDetails.test.js b/ui/src/wishDetails/WishDetails.test.js index 663917f6..b0fb7a45 100644 --- a/ui/src/wishDetails/WishDetails.test.js +++ b/ui/src/wishDetails/WishDetails.test.js @@ -5,8 +5,29 @@ import Rocket from '../assets/images/icn_To_Go_Rocket_White_Inside_130x130.png' import Alien from '../assets/images/icn_To_Meet_Alien_White_Inside_130x130.png' import Astronaut from '../assets/images/icn_To_Be_Astronaut_White_Inside_130x130.png' import Telescope from '../assets/images/icn_To_See_Telescope_White_Inside_130x130.png' +import axios from 'axios'; jest.mock('../services/WishDetailsService', () => ({ + editAWish: jest.fn(() => { + return ({ + _id: 'mock child id', + child: { + name: 'child name', + hometown: 'Round Rock', + illness: '', + age: '' + }, + type: '', + details: '', + sponser: { + name: '', + logo: '', + links: [] + }, + createdAt: '', + updatedAt: '' + }) + }), getWishDetails: jest.fn(() => { return { id: '', @@ -32,7 +53,7 @@ jest.mock('../services/WishDetailsService', () => ({ describe('Initial Render', () => { let wishInfo beforeEach(() => { - wishInfo = shallow() + wishInfo = shallow() wishInfo.instance().setState({ id: '', wishDetails: { @@ -61,6 +82,30 @@ describe('Initial Render', () => { expect(wishInfo.exists('.wishInfo')) }) + it('should allow the hometown field to be edited', async () => { + const hometownInput = wishInfo.find('[data-test="hometown-input"]') + const instance = wishInfo.instance(); + const updateHometownSpy = jest.spyOn(instance, 'updateHometown') + + let event = { + target: { + value: 'Round Rock' + } + } + hometownInput.simulate('change', event) + expect(instance.state.wishDetails.child.hometown).toEqual('Round Rock'); + + const wish = { + child: instance.state.wishDetails.child, + type: instance.state.wishDetails.type, + details: instance.state.wishDetails.details + } + + hometownInput.simulate('blur', event) + axios.put = jest.fn(() => Promise.resolve({ data: wish })); + expect(updateHometownSpy).toHaveBeenCalled() + }) + describe('Getting image for wish type', () => { it('should return rocket image for type GO', () => { let currentWishState = wishInfo.instance().state.wishDetails From 6072a820db9807065e360138299803a3c27472e5 Mon Sep 17 00:00:00 2001 From: CXG6495 Date: Mon, 21 Oct 2019 09:08:59 -0500 Subject: [PATCH 2/3] update hometown when clicking wish summary link, updating tests --- ui/src/wishDetails/WishDetails.js | 14 ++++++++++++-- ui/src/wishDetails/WishDetails.test.js | 5 +++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/ui/src/wishDetails/WishDetails.js b/ui/src/wishDetails/WishDetails.js index cac3c274..19d45651 100644 --- a/ui/src/wishDetails/WishDetails.js +++ b/ui/src/wishDetails/WishDetails.js @@ -1,5 +1,4 @@ import React, { Component } from 'react' -import { Link } from 'react-router-dom' import WishDetailsService from '../services/WishDetailsService' import Rocket from '../assets/images/icn_To_Go_Rocket_White_Inside_130x130.png' import Alien from '../assets/images/icn_To_Meet_Alien_White_Inside_130x130.png' @@ -10,6 +9,13 @@ import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' import { faCamera } from '@fortawesome/free-solid-svg-icons' import WishHeader from '../wishList/wishHeader' +const styles = { + link:{ + color: '#000000', + textDecoration: 'underline' + } +} + export default class WishDetails extends Component { constructor(props) { super(props) @@ -86,6 +92,10 @@ export default class WishDetails extends Component { return image } + goToWishSummary(wish) { + this.updateHometown(wish).then(() => this.props.history.push('/wish-summary')) + } + render() { const { child, details, sponsor, type } = this.state.wishDetails const { name, age, hometown } = child @@ -100,7 +110,7 @@ export default class WishDetails extends Component {
diff --git a/ui/src/wishDetails/WishDetails.test.js b/ui/src/wishDetails/WishDetails.test.js index b0fb7a45..7ba4714b 100644 --- a/ui/src/wishDetails/WishDetails.test.js +++ b/ui/src/wishDetails/WishDetails.test.js @@ -86,6 +86,7 @@ describe('Initial Render', () => { const hometownInput = wishInfo.find('[data-test="hometown-input"]') const instance = wishInfo.instance(); const updateHometownSpy = jest.spyOn(instance, 'updateHometown') + const goToWishSummarySpy = jest.spyOn(instance, 'goToWishSummary') let event = { target: { @@ -104,6 +105,10 @@ describe('Initial Render', () => { hometownInput.simulate('blur', event) axios.put = jest.fn(() => Promise.resolve({ data: wish })); expect(updateHometownSpy).toHaveBeenCalled() + + const goToSummaryLink = wishInfo.find('[data-test="go-to-summary-link"]') + goToSummaryLink.simulate('click', event) + expect(goToWishSummarySpy).toHaveBeenCalled() }) describe('Getting image for wish type', () => { From b7f11d8a50d99797590fde0d999bd5dcfc15b686 Mon Sep 17 00:00:00 2001 From: CXG6495 Date: Mon, 21 Oct 2019 15:25:28 -0500 Subject: [PATCH 3/3] a -> span to avoid not needing href --- ui/src/wishDetails/WishDetails.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ui/src/wishDetails/WishDetails.js b/ui/src/wishDetails/WishDetails.js index 19d45651..7bb67df6 100644 --- a/ui/src/wishDetails/WishDetails.js +++ b/ui/src/wishDetails/WishDetails.js @@ -110,7 +110,7 @@ export default class WishDetails extends Component {
- this.goToWishSummary(wish)} to="">Back to Summary + this.goToWishSummary(wish)} to="">Back to Summary