-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'sift-15' of https://github.com/ubclaunchpad/sift-web in…
…to andrew
- Loading branch information
Showing
3 changed files
with
82 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import React, {Component} from 'react'; | ||
|
||
class ConceptInput extends Component { | ||
static displayName = 'ConceptInput'; | ||
|
||
state = { | ||
tokens: [], | ||
inputValue: '' | ||
} | ||
|
||
handleOnKeyPress(e) { | ||
if (e.key === 'Enter' && this.state.inputValue !== '') { | ||
this.setState({ | ||
tokens: [...this.state.tokens, this.state.inputValue], | ||
inputValue: '' | ||
}); | ||
} | ||
} | ||
|
||
handleOnChange(e) { | ||
this.setState({inputValue: e.target.value}); | ||
} | ||
|
||
deleteToken(tokenIndex){ | ||
const newState = this.state.tokens; | ||
newState.splice(tokenIndex, 1); | ||
this.setState({tokens: newState}) | ||
} | ||
|
||
render() { | ||
const listItem = this.state.tokens.map((token, index) => | ||
( | ||
<div key={index}> | ||
{token} | ||
<button onClick={index => this.deleteToken(index)}>Delete Token</button> | ||
</div> | ||
) | ||
) | ||
return ( | ||
<div> | ||
<input | ||
type="text" | ||
value={this.state.inputValue} | ||
onKeyPress={this.handleOnKeyPress.bind(this)} | ||
onChange={this.handleOnChange.bind(this)}/> | ||
{listItem} | ||
</div> | ||
); | ||
} | ||
} | ||
export default ConceptInput; |
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,29 @@ | ||
import ConceptInput from './../ConceptInput'; | ||
import React from 'react'; | ||
import {shallow} from 'enzyme'; | ||
|
||
describe('<ConceptInput />', () => { | ||
it('Should add a token under the textbox', () => { | ||
const wrapper = shallow(<ConceptInput />); | ||
const input = wrapper.find('input'); | ||
input.simulate('change', {target: {value: 'giraffe'}}); | ||
input.simulate('keypress', {key: 'Enter'}); | ||
expect(wrapper.state('tokens')[0]).toEqual('giraffe'); | ||
}); | ||
it('if the input is empty, do not create a token', () => { | ||
const wrapper = shallow(<ConceptInput />); | ||
const input = wrapper.find('input'); | ||
input.simulate('change', {target: {value: ''}}); | ||
input.simulate('keypress', {key: 'Enter'}); | ||
expect(wrapper.state('tokens').length).toEqual(0); | ||
}); | ||
it('Should remove the selected token', () => { | ||
const wrapper = shallow(<ConceptInput />); | ||
const input = wrapper.find('input'); | ||
input.simulate('change', {target: {value: 'giraffe'}}); | ||
input.simulate('keypress', {key: 'Enter'}); | ||
const button = wrapper.find('button'); | ||
button.simulate('click'); | ||
expect(wrapper.state('tokens').length).toEqual(0); | ||
}); | ||
}); |
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