This repository has been archived by the owner on Nov 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add very basic implementation of Variable component
- Loading branch information
Dom Harrington
committed
Jun 22, 2018
1 parent
e2743fa
commit 625a7bf
Showing
3 changed files
with
102 additions
and
1 deletion.
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,64 @@ | ||
const React = require('react'); | ||
const { shallow } = require('enzyme'); | ||
|
||
const Variable = require('../src/Variable'); | ||
|
||
test('should render variable', () => { | ||
const variable = shallow(<Variable value="123456" />); | ||
|
||
expect(variable.text()).toBe('123456'); | ||
}); | ||
|
||
test('should render the first of multiple values', () => { | ||
const variable = shallow( | ||
<Variable | ||
k="apiKey" | ||
value={[{ name: 'project1', apiKey: '123' }, { name: 'project2', apiKey: '456' }]} | ||
/>, | ||
); | ||
|
||
expect(variable.text()).toBe('123'); | ||
}); | ||
|
||
test('should render whatever the selected name is', () => { | ||
const variable = shallow( | ||
<Variable | ||
k="apiKey" | ||
value={[{ name: 'project1', apiKey: '123' }, { name: 'project2', apiKey: '456' }]} | ||
selected="project2" | ||
/>, | ||
); | ||
|
||
expect(variable.text()).toBe('456'); | ||
}); | ||
|
||
test('should show dropdown when clicked', () => { | ||
const variable = shallow( | ||
<Variable | ||
k="apiKey" | ||
value={[{ name: 'project1', apiKey: '123' }, { name: 'project2', apiKey: '456' }]} | ||
selected="project2" | ||
/>, | ||
); | ||
|
||
variable.simulate('click'); | ||
|
||
expect(variable.find('ul li').map(el => el.text())).toEqual(['project1', 'project2']); | ||
}); | ||
|
||
test('should select value when clicked', () => { | ||
const variable = shallow( | ||
<Variable | ||
k="apiKey" | ||
value={[{ name: 'project1', apiKey: '123' }, { name: 'project2', apiKey: '456' }, { name: 'project3'}]} | ||
selected="project1" | ||
/>, | ||
); | ||
|
||
variable.simulate('click'); | ||
variable.find('ul li').at(1).simulate('click', { target: { innerText: variable.find('ul li').at(1).text() } }); | ||
|
||
expect(variable.text()).toBe('456'); | ||
}); | ||
|
||
test('should render auth dropdown if default'); |
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,36 @@ | ||
const React = require('react'); | ||
|
||
class Variable extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { showDropdown: false, selected: props.selected }; | ||
|
||
this.showDropdown = this.showDropdown.bind(this); | ||
this.selectValue = this.selectValue.bind(this); | ||
} | ||
showDropdown() { | ||
this.setState({ showDropdown: true }); | ||
} | ||
selectValue(event) { | ||
this.setState({ selected: event.target.innerText, showDropdown: false }); | ||
} | ||
render() { | ||
const { k, value } = this.props; | ||
|
||
const { selected } = this.state; | ||
|
||
if (this.state.showDropdown) { | ||
return ( | ||
<ul>{value.map(key => <li onClick={this.selectValue} key={key.name}>{key.name}</li>)}</ul> | ||
); | ||
} | ||
|
||
if (Array.isArray(value)) { | ||
const selectedValue = selected ? value.find(key => key.name === selected) : value[0]; | ||
return <span onClick={this.showDropdown}>{selectedValue[k]}</span>; | ||
} | ||
return <span>{value}</span>; | ||
} | ||
} | ||
|
||
module.exports = Variable; |
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