refactor(Tabs): cleanup, test coverage
This commit is contained in:
parent
7632881468
commit
52a68868cd
@ -9,16 +9,36 @@ class Tabs extends Component {
|
|||||||
|
|
||||||
this.toggle = this.toggle.bind(this);
|
this.toggle = this.toggle.bind(this);
|
||||||
this.state = {
|
this.state = {
|
||||||
activeTab: '1',
|
activeTab: new Array(4).fill('1'),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
toggle(tab) {
|
lorem() {
|
||||||
if (this.state.activeTab !== tab) {
|
return 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit.'
|
||||||
|
}
|
||||||
|
|
||||||
|
toggle(tabPane, tab) {
|
||||||
|
const newArray = this.state.activeTab.slice()
|
||||||
|
newArray[tabPane] = tab
|
||||||
this.setState({
|
this.setState({
|
||||||
activeTab: tab,
|
activeTab: newArray,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tabPane() {
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<TabPane tabId="1">
|
||||||
|
{`1. ${this.lorem()}`}
|
||||||
|
</TabPane>
|
||||||
|
<TabPane tabId="2">
|
||||||
|
{`2. ${this.lorem()}`}
|
||||||
|
</TabPane>
|
||||||
|
<TabPane tabId="3">
|
||||||
|
{`3. ${this.lorem()}`}
|
||||||
|
</TabPane>
|
||||||
|
</>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
@ -29,207 +49,129 @@ class Tabs extends Component {
|
|||||||
<Nav tabs>
|
<Nav tabs>
|
||||||
<NavItem>
|
<NavItem>
|
||||||
<NavLink
|
<NavLink
|
||||||
className={classnames({ active: this.state.activeTab === '1' })}
|
active={this.state.activeTab[0] === '1'}
|
||||||
onClick={() => { this.toggle('1'); }}
|
onClick={() => { this.toggle(0, '1'); }}
|
||||||
>
|
>
|
||||||
Home
|
Home
|
||||||
</NavLink>
|
</NavLink>
|
||||||
</NavItem>
|
</NavItem>
|
||||||
<NavItem>
|
<NavItem>
|
||||||
<NavLink
|
<NavLink
|
||||||
className={classnames({ active: this.state.activeTab === '2' })}
|
active={this.state.activeTab[0] === '2'}
|
||||||
onClick={() => { this.toggle('2'); }}
|
onClick={() => { this.toggle(0, '2'); }}
|
||||||
>
|
>
|
||||||
Profile
|
Profile
|
||||||
</NavLink>
|
</NavLink>
|
||||||
</NavItem>
|
</NavItem>
|
||||||
<NavItem>
|
<NavItem>
|
||||||
<NavLink
|
<NavLink
|
||||||
className={classnames({ active: this.state.activeTab === '3' })}
|
active={this.state.activeTab[0] === '3'}
|
||||||
onClick={() => { this.toggle('3'); }}
|
onClick={() => { this.toggle(0, '3'); }}
|
||||||
>
|
>
|
||||||
Messages
|
Messages
|
||||||
</NavLink>
|
</NavLink>
|
||||||
</NavItem>
|
</NavItem>
|
||||||
</Nav>
|
</Nav>
|
||||||
<TabContent activeTab={this.state.activeTab}>
|
<TabContent activeTab={this.state.activeTab[0]}>
|
||||||
<TabPane tabId="1">
|
{this.tabPane()}
|
||||||
1. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore
|
|
||||||
et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
|
|
||||||
aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum
|
|
||||||
dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui
|
|
||||||
officia deserunt mollit anim id est laborum.
|
|
||||||
</TabPane>
|
|
||||||
<TabPane tabId="2">
|
|
||||||
2. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore
|
|
||||||
et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
|
|
||||||
aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum
|
|
||||||
dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui
|
|
||||||
officia deserunt mollit anim id est laborum.
|
|
||||||
</TabPane>
|
|
||||||
<TabPane tabId="3">
|
|
||||||
2. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore
|
|
||||||
et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
|
|
||||||
aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum
|
|
||||||
dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui
|
|
||||||
officia deserunt mollit anim id est laborum.
|
|
||||||
</TabPane>
|
|
||||||
</TabContent>
|
</TabContent>
|
||||||
</Col>
|
</Col>
|
||||||
<Col xs="12" md="6" className="mb-4">
|
<Col xs="12" md="6" className="mb-4">
|
||||||
<Nav tabs>
|
<Nav tabs>
|
||||||
<NavItem>
|
<NavItem>
|
||||||
<NavLink
|
<NavLink
|
||||||
className={classnames({ active: this.state.activeTab === '1' })}
|
active={this.state.activeTab[1] === '1'}
|
||||||
onClick={() => { this.toggle('1'); }}
|
onClick={() => { this.toggle(1, '1'); }}
|
||||||
>
|
>
|
||||||
<i className="icon-calculator"></i>
|
<i className="icon-calculator"></i>
|
||||||
</NavLink>
|
</NavLink>
|
||||||
</NavItem>
|
</NavItem>
|
||||||
<NavItem>
|
<NavItem>
|
||||||
<NavLink
|
<NavLink
|
||||||
className={classnames({ active: this.state.activeTab === '2' })}
|
active={this.state.activeTab[1] === '2'}
|
||||||
onClick={() => { this.toggle('2'); }}
|
onClick={() => { this.toggle(1, '2'); }}
|
||||||
>
|
>
|
||||||
<i className="icon-basket-loaded"></i>
|
<i className="icon-basket-loaded"></i>
|
||||||
</NavLink>
|
</NavLink>
|
||||||
</NavItem>
|
</NavItem>
|
||||||
<NavItem>
|
<NavItem>
|
||||||
<NavLink
|
<NavLink
|
||||||
className={classnames({ active: this.state.activeTab === '3' })}
|
active={this.state.activeTab[1] === '3'}
|
||||||
onClick={() => { this.toggle('3'); }}
|
onClick={() => { this.toggle(1, '3'); }}
|
||||||
>
|
>
|
||||||
<i className="icon-pie-chart"></i>
|
<i className="icon-pie-chart"></i>
|
||||||
</NavLink>
|
</NavLink>
|
||||||
</NavItem>
|
</NavItem>
|
||||||
</Nav>
|
</Nav>
|
||||||
<TabContent activeTab={this.state.activeTab}>
|
<TabContent activeTab={this.state.activeTab[1]}>
|
||||||
<TabPane tabId="1">
|
{this.tabPane()}
|
||||||
1. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore
|
|
||||||
et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
|
|
||||||
aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum
|
|
||||||
dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui
|
|
||||||
officia deserunt mollit anim id est laborum.
|
|
||||||
</TabPane>
|
|
||||||
<TabPane tabId="2">
|
|
||||||
2. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore
|
|
||||||
et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
|
|
||||||
aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum
|
|
||||||
dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui
|
|
||||||
officia deserunt mollit anim id est laborum.
|
|
||||||
</TabPane>
|
|
||||||
<TabPane tabId="3">
|
|
||||||
2. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore
|
|
||||||
et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
|
|
||||||
aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum
|
|
||||||
dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui
|
|
||||||
officia deserunt mollit anim id est laborum.
|
|
||||||
</TabPane>
|
|
||||||
</TabContent>
|
</TabContent>
|
||||||
</Col>
|
</Col>
|
||||||
<Col xs="12" md="6" className="mb-4">
|
<Col xs="12" md="6" className="mb-4">
|
||||||
<Nav tabs>
|
<Nav tabs>
|
||||||
<NavItem>
|
<NavItem>
|
||||||
<NavLink
|
<NavLink
|
||||||
className={classnames({ active: this.state.activeTab === '1' })}
|
active={this.state.activeTab[2] === '1'}
|
||||||
onClick={() => { this.toggle('1'); }}
|
onClick={() => { this.toggle(2, '1'); }}
|
||||||
>
|
>
|
||||||
<i className="icon-calculator"></i> <span className={this.state.activeTab === '1' ? '' : 'd-none'}> Calculator</span>
|
<i className="icon-calculator"></i> <span className={this.state.activeTab[2] === '1' ? '' : 'd-none'}> Calculator</span>
|
||||||
</NavLink>
|
</NavLink>
|
||||||
</NavItem>
|
</NavItem>
|
||||||
<NavItem>
|
<NavItem>
|
||||||
<NavLink
|
<NavLink
|
||||||
className={classnames({ active: this.state.activeTab === '2' })}
|
active={this.state.activeTab[2] === '2'}
|
||||||
onClick={() => { this.toggle('2'); }}
|
onClick={() => { this.toggle(2, '2'); }}
|
||||||
>
|
>
|
||||||
<i className="icon-basket-loaded"></i> <span
|
<i className="icon-basket-loaded"></i> <span
|
||||||
className={this.state.activeTab === '2' ? '' : 'd-none'}> Shopping cart</span>
|
className={this.state.activeTab[2] === '2' ? '' : 'd-none'}> Shopping cart</span>
|
||||||
</NavLink>
|
</NavLink>
|
||||||
</NavItem>
|
</NavItem>
|
||||||
<NavItem>
|
<NavItem>
|
||||||
<NavLink
|
<NavLink
|
||||||
className={classnames({ active: this.state.activeTab === '3' })}
|
className={classnames({ active: this.state.activeTab[2] === '3' })}
|
||||||
onClick={() => { this.toggle('3'); }}
|
onClick={() => { this.toggle(2,'3'); }}
|
||||||
>
|
>
|
||||||
<i className="icon-pie-chart"></i> <span className={this.state.activeTab === '3' ? '' : 'd-none'}> Charts</span>
|
<i className="icon-pie-chart"></i> <span className={this.state.activeTab[2] === '3' ? '' : 'd-none'}> Charts</span>
|
||||||
</NavLink>
|
</NavLink>
|
||||||
</NavItem>
|
</NavItem>
|
||||||
</Nav>
|
</Nav>
|
||||||
<TabContent activeTab={this.state.activeTab}>
|
<TabContent activeTab={this.state.activeTab[2]}>
|
||||||
<TabPane tabId="1">
|
{this.tabPane()}
|
||||||
1. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore
|
|
||||||
et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
|
|
||||||
aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum
|
|
||||||
dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui
|
|
||||||
officia deserunt mollit anim id est laborum.
|
|
||||||
</TabPane>
|
|
||||||
<TabPane tabId="2">
|
|
||||||
2. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore
|
|
||||||
et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
|
|
||||||
aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum
|
|
||||||
dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui
|
|
||||||
officia deserunt mollit anim id est laborum.
|
|
||||||
</TabPane>
|
|
||||||
<TabPane tabId="3">
|
|
||||||
2. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore
|
|
||||||
et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
|
|
||||||
aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum
|
|
||||||
dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui
|
|
||||||
officia deserunt mollit anim id est laborum.
|
|
||||||
</TabPane>
|
|
||||||
</TabContent>
|
</TabContent>
|
||||||
</Col>
|
</Col>
|
||||||
<Col xs="12" md="6" className="mb-4">
|
<Col xs="12" md="6" className="mb-4">
|
||||||
<Nav tabs>
|
<Nav tabs>
|
||||||
<NavItem>
|
<NavItem>
|
||||||
<NavLink
|
<NavLink
|
||||||
className={classnames({ active: this.state.activeTab === '1' })}
|
active={this.state.activeTab[3] === '1'}
|
||||||
onClick={() => { this.toggle('1'); }}
|
onClick={() => { this.toggle(3, '1'); }}
|
||||||
>
|
>
|
||||||
<i className="icon-calculator"></i> <span className={this.state.activeTab === '1' ? '' : 'd-none'}> Calculator </span>{'\u00A0'}<Badge
|
<i className="icon-calculator"></i>
|
||||||
color="success">New</Badge>
|
<span className={this.state.activeTab[3] === '1' ? '' : 'd-none'}> Calc</span>
|
||||||
|
{'\u00A0'}<Badge color="success">New</Badge>
|
||||||
</NavLink>
|
</NavLink>
|
||||||
</NavItem>
|
</NavItem>
|
||||||
<NavItem>
|
<NavItem>
|
||||||
<NavLink
|
<NavLink
|
||||||
className={classnames({ active: this.state.activeTab === '2' })}
|
active={this.state.activeTab[3] === '2'}
|
||||||
onClick={() => { this.toggle('2'); }}
|
onClick={() => { this.toggle(3, '2'); }}
|
||||||
>
|
>
|
||||||
<i className="icon-basket-loaded"></i> <span
|
<i className="icon-basket-loaded"></i>
|
||||||
className={this.state.activeTab === '2' ? '' : 'd-none'}> Shopping cart </span>{'\u00A0'}<Badge pill color="danger">29</Badge>
|
<span className={this.state.activeTab[3] === '2' ? '' : 'd-none'}> Cart</span>
|
||||||
|
{'\u00A0'}<Badge pill color="danger">29</Badge>
|
||||||
</NavLink>
|
</NavLink>
|
||||||
</NavItem>
|
</NavItem>
|
||||||
<NavItem>
|
<NavItem>
|
||||||
<NavLink
|
<NavLink
|
||||||
className={classnames({ active: this.state.activeTab === '3' })}
|
active={this.state.activeTab[3] === '3'}
|
||||||
onClick={() => { this.toggle('3'); }}
|
onClick={() => { this.toggle(3, '3'); }} >
|
||||||
>
|
<i className="icon-pie-chart"></i>
|
||||||
<i className="icon-pie-chart"></i> <span className={this.state.activeTab === '3' ? '' : 'd-none'}> Charts</span>
|
<span className={this.state.activeTab[3] === '3' ? '' : 'd-none'}> Charts</span>
|
||||||
</NavLink>
|
</NavLink>
|
||||||
</NavItem>
|
</NavItem>
|
||||||
</Nav>
|
</Nav>
|
||||||
<TabContent activeTab={this.state.activeTab}>
|
<TabContent activeTab={this.state.activeTab[3]}>
|
||||||
<TabPane tabId="1">
|
{this.tabPane()}
|
||||||
1. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore
|
|
||||||
et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
|
|
||||||
aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum
|
|
||||||
dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui
|
|
||||||
officia deserunt mollit anim id est laborum.
|
|
||||||
</TabPane>
|
|
||||||
<TabPane tabId="2">
|
|
||||||
2. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore
|
|
||||||
et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
|
|
||||||
aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum
|
|
||||||
dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui
|
|
||||||
officia deserunt mollit anim id est laborum.
|
|
||||||
</TabPane>
|
|
||||||
<TabPane tabId="3">
|
|
||||||
2. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore
|
|
||||||
et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
|
|
||||||
aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum
|
|
||||||
dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui
|
|
||||||
officia deserunt mollit anim id est laborum.
|
|
||||||
</TabPane>
|
|
||||||
</TabContent>
|
</TabContent>
|
||||||
</Col>
|
</Col>
|
||||||
</Row>
|
</Row>
|
||||||
|
@ -1,9 +1,21 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import ReactDOM from 'react-dom';
|
import ReactDOM from 'react-dom';
|
||||||
import Tabs from './Tabs';
|
import Tabs from './Tabs';
|
||||||
|
import {mount} from 'enzyme/build';
|
||||||
|
|
||||||
it('renders without crashing', () => {
|
it('renders without crashing', () => {
|
||||||
const div = document.createElement('div');
|
const div = document.createElement('div');
|
||||||
ReactDOM.render(<Tabs />, div);
|
ReactDOM.render(<Tabs />, div);
|
||||||
ReactDOM.unmountComponentAtNode(div);
|
ReactDOM.unmountComponentAtNode(div);
|
||||||
});
|
});
|
||||||
|
it('toggle click without crashing', () => {
|
||||||
|
const wrapper = mount(<Tabs />);
|
||||||
|
for (let pane=0; pane<4; pane++) {
|
||||||
|
for( let tabId=1; tabId<4; tabId++) {
|
||||||
|
let Tab = wrapper.find('.nav-tabs .nav-item .nav-link').at((3*pane)+tabId-1);
|
||||||
|
Tab.simulate('click');
|
||||||
|
expect(wrapper.state().activeTab[pane]).toEqual((tabId).toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
wrapper.unmount()
|
||||||
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user