-
Notifications
You must be signed in to change notification settings - Fork 3
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
Program name component #32
Merged
cabarnes
merged 6 commits into
rovercode:development
from
cabarnes:program-name-component
Oct 16, 2018
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4d11b6b
Modify change name action to call API
cabarnes d4196d6
Create program name component to display and change program name
cabarnes ada18a5
Add program name component to mission control
cabarnes 7acb91e
Add confirmation dialog for saving new name
cabarnes 3152cda
Only show 'Save' when the name has changed
cabarnes 594c2b3
Merge branch 'development' into program-name-component
hbradio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,96 @@ | ||
import React, { Component, Fragment } from 'react'; | ||
import { Confirm, Input } from 'semantic-ui-react'; | ||
import { connect } from 'react-redux'; | ||
import { hot } from 'react-hot-loader'; | ||
import { withCookies } from 'react-cookie'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import { changeName as actionChangeName } from '@/actions/code'; | ||
|
||
const mapStateToProps = ({ code }) => ({ code }); | ||
const mapDispatchToProps = (dispatch, { cookies }) => ({ | ||
changeName: (id, name) => { | ||
const changeNameAction = actionChangeName(id, name, { | ||
headers: { | ||
Authorization: `JWT ${cookies.get('auth_jwt')}`, | ||
}, | ||
}); | ||
return dispatch(changeNameAction); | ||
}, | ||
}); | ||
|
||
class Console extends Component { | ||
constructor(props) { | ||
super(props); | ||
|
||
this.state = { | ||
confirmOpen: false, | ||
editingName: null, | ||
previousPropName: null, // eslint-disable-line react/no-unused-state | ||
}; | ||
} | ||
|
||
static getDerivedStateFromProps(props, state) { | ||
const { code } = props; | ||
const { previousPropName } = state; | ||
|
||
if (code.name !== previousPropName) { | ||
return { | ||
editingName: code.name, | ||
previousPropName: code.name, | ||
}; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
closeConfirm = () => this.setState({ confirmOpen: false }) | ||
|
||
openConfirm = () => this.setState({ confirmOpen: true }) | ||
|
||
handleChange = (e) => { | ||
this.setState({ | ||
editingName: e.target.value, | ||
}); | ||
} | ||
|
||
handleSave = () => { | ||
const { changeName, code } = this.props; | ||
const { editingName } = this.state; | ||
|
||
changeName(code.id, editingName); | ||
this.closeConfirm(); | ||
} | ||
|
||
render() { | ||
const { confirmOpen, editingName } = this.state; | ||
|
||
return ( | ||
<Fragment> | ||
<Input | ||
type="text" | ||
label="Name:" | ||
defaultValue={editingName} | ||
onChange={this.handleChange} | ||
action={{ content: 'Save', onClick: this.openConfirm }} | ||
/> | ||
<Confirm | ||
content="Are you sure that you want to change the name of this program?" | ||
open={confirmOpen} | ||
onCancel={this.closeConfirm} | ||
onConfirm={this.handleSave} | ||
/> | ||
</Fragment> | ||
); | ||
} | ||
} | ||
|
||
Console.propTypes = { | ||
code: PropTypes.shape({ | ||
id: PropTypes.number, | ||
name: PropTypes.string, | ||
}).isRequired, | ||
changeName: PropTypes.func.isRequired, | ||
}; | ||
|
||
export default hot(module)(withCookies(connect(mapStateToProps, mapDispatchToProps)(Console))); |
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,85 @@ | ||
import React from 'react'; | ||
import { Confirm, Input } from 'semantic-ui-react'; | ||
import { mount, shallow } from 'enzyme'; | ||
import toJson from 'enzyme-to-json'; | ||
import configureStore from 'redux-mock-store'; | ||
import { Cookies } from 'react-cookie'; | ||
|
||
import { changeName } from '@/actions/code'; | ||
import ProgramName from '../ProgramName'; | ||
|
||
const cookiesValues = { auth_jwt: '1234' }; | ||
const cookies = new Cookies(cookiesValues); | ||
|
||
describe('The Console component', () => { | ||
const mockStore = configureStore(); | ||
const context = { cookies }; | ||
let store; | ||
|
||
beforeEach(() => { | ||
store = mockStore({ | ||
code: { | ||
name: 'test name', | ||
}, | ||
}); | ||
store.dispatch = jest.fn(); | ||
}); | ||
|
||
test('renders on the page with no errors', () => { | ||
const wrapper = mount(<ProgramName store={store} />, { context }); | ||
expect(toJson(wrapper)).toMatchSnapshot(); | ||
}); | ||
|
||
test('displays name', () => { | ||
const wrapper = shallow(<ProgramName store={store} />, { context }).dive().dive(); | ||
|
||
expect(wrapper.find(Confirm).prop('open')).toBe(false); | ||
expect(wrapper.find(Input).length).toBe(1); | ||
expect(wrapper.find(Input).props().defaultValue).toBe('test name'); | ||
}); | ||
|
||
test('handles change', () => { | ||
const wrapper = shallow(<ProgramName store={store} />, { context }).dive().dive(); | ||
|
||
wrapper.find(Input).simulate('change', { target: { value: 'new name' } }); | ||
wrapper.update(); | ||
|
||
expect(wrapper.find(Confirm).prop('open')).toBe(false); | ||
expect(wrapper.find(Input).props().defaultValue).toBe('new name'); | ||
}); | ||
|
||
test('handles save cancel', () => { | ||
const wrapper = shallow(<ProgramName store={store} />, { context }).dive().dive(); | ||
|
||
// User opens save confirmation modal | ||
wrapper.find(Input).props().action.onClick(); | ||
wrapper.update(); | ||
|
||
expect(wrapper.find(Confirm).prop('open')).toBe(true); | ||
|
||
// User clicks 'cancel' | ||
wrapper.find(Confirm).prop('onCancel')(); | ||
wrapper.update(); | ||
|
||
expect(wrapper.find(Confirm).prop('open')).toBe(false); | ||
expect(store.dispatch).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test('handles save confirm', () => { | ||
const wrapper = shallow(<ProgramName store={store} />, { context }).dive().dive(); | ||
|
||
// User opens save confirmation modal | ||
wrapper.find(Input).props().action.onClick(); | ||
wrapper.update(); | ||
|
||
expect(wrapper.find(Confirm).prop('open')).toBe(true); | ||
|
||
// User clicks 'OK' | ||
wrapper.find(Confirm).prop('onConfirm')(); | ||
wrapper.update(); | ||
|
||
expect(wrapper.find(Confirm).prop('open')).toBe(false); | ||
expect(store.dispatch).toHaveBeenCalled(); | ||
expect(store.dispatch).toHaveBeenCalledWith(changeName('test name')); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like the
Save
button built into Semantic's object when thecontent: 'Save'
is used?I was curious if the
Save
button could be hidden whenpreviousPropName == editingName
, but that could be more trouble than it's worth.