refactor(Tabs): cleanup, test coverage
This commit is contained in:
parent
7632881468
commit
52a68868cd
@ -1,5 +1,5 @@
|
||||
import React, { Component } from 'react';
|
||||
import { Badge, Col, Nav, NavItem, NavLink, Row, TabContent, TabPane } from 'reactstrap';
|
||||
import React, {Component} from 'react';
|
||||
import {Badge, Col, Nav, NavItem, NavLink, Row, TabContent, TabPane} from 'reactstrap';
|
||||
import classnames from 'classnames';
|
||||
|
||||
class Tabs extends Component {
|
||||
@ -9,16 +9,36 @@ class Tabs extends Component {
|
||||
|
||||
this.toggle = this.toggle.bind(this);
|
||||
this.state = {
|
||||
activeTab: '1',
|
||||
activeTab: new Array(4).fill('1'),
|
||||
};
|
||||
}
|
||||
|
||||
toggle(tab) {
|
||||
if (this.state.activeTab !== tab) {
|
||||
this.setState({
|
||||
activeTab: tab,
|
||||
});
|
||||
}
|
||||
lorem() {
|
||||
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({
|
||||
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() {
|
||||
@ -29,207 +49,129 @@ class Tabs extends Component {
|
||||
<Nav tabs>
|
||||
<NavItem>
|
||||
<NavLink
|
||||
className={classnames({ active: this.state.activeTab === '1' })}
|
||||
onClick={() => { this.toggle('1'); }}
|
||||
active={this.state.activeTab[0] === '1'}
|
||||
onClick={() => { this.toggle(0, '1'); }}
|
||||
>
|
||||
Home
|
||||
</NavLink>
|
||||
</NavItem>
|
||||
<NavItem>
|
||||
<NavLink
|
||||
className={classnames({ active: this.state.activeTab === '2' })}
|
||||
onClick={() => { this.toggle('2'); }}
|
||||
active={this.state.activeTab[0] === '2'}
|
||||
onClick={() => { this.toggle(0, '2'); }}
|
||||
>
|
||||
Profile
|
||||
</NavLink>
|
||||
</NavItem>
|
||||
<NavItem>
|
||||
<NavLink
|
||||
className={classnames({ active: this.state.activeTab === '3' })}
|
||||
onClick={() => { this.toggle('3'); }}
|
||||
active={this.state.activeTab[0] === '3'}
|
||||
onClick={() => { this.toggle(0, '3'); }}
|
||||
>
|
||||
Messages
|
||||
</NavLink>
|
||||
</NavItem>
|
||||
</Nav>
|
||||
<TabContent activeTab={this.state.activeTab}>
|
||||
<TabPane tabId="1">
|
||||
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 activeTab={this.state.activeTab[0]}>
|
||||
{this.tabPane()}
|
||||
</TabContent>
|
||||
</Col>
|
||||
<Col xs="12" md="6" className="mb-4">
|
||||
<Nav tabs>
|
||||
<NavItem>
|
||||
<NavLink
|
||||
className={classnames({ active: this.state.activeTab === '1' })}
|
||||
onClick={() => { this.toggle('1'); }}
|
||||
active={this.state.activeTab[1] === '1'}
|
||||
onClick={() => { this.toggle(1, '1'); }}
|
||||
>
|
||||
<i className="icon-calculator"></i>
|
||||
</NavLink>
|
||||
</NavItem>
|
||||
<NavItem>
|
||||
<NavLink
|
||||
className={classnames({ active: this.state.activeTab === '2' })}
|
||||
onClick={() => { this.toggle('2'); }}
|
||||
active={this.state.activeTab[1] === '2'}
|
||||
onClick={() => { this.toggle(1, '2'); }}
|
||||
>
|
||||
<i className="icon-basket-loaded"></i>
|
||||
</NavLink>
|
||||
</NavItem>
|
||||
<NavItem>
|
||||
<NavLink
|
||||
className={classnames({ active: this.state.activeTab === '3' })}
|
||||
onClick={() => { this.toggle('3'); }}
|
||||
active={this.state.activeTab[1] === '3'}
|
||||
onClick={() => { this.toggle(1, '3'); }}
|
||||
>
|
||||
<i className="icon-pie-chart"></i>
|
||||
</NavLink>
|
||||
</NavItem>
|
||||
</Nav>
|
||||
<TabContent activeTab={this.state.activeTab}>
|
||||
<TabPane tabId="1">
|
||||
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 activeTab={this.state.activeTab[1]}>
|
||||
{this.tabPane()}
|
||||
</TabContent>
|
||||
</Col>
|
||||
<Col xs="12" md="6" className="mb-4">
|
||||
<Nav tabs>
|
||||
<NavItem>
|
||||
<NavLink
|
||||
active={this.state.activeTab[2] === '1'}
|
||||
onClick={() => { this.toggle(2, '1'); }}
|
||||
>
|
||||
<i className="icon-calculator"></i> <span className={this.state.activeTab[2] === '1' ? '' : 'd-none'}> Calculator</span>
|
||||
</NavLink>
|
||||
</NavItem>
|
||||
<NavItem>
|
||||
<NavLink
|
||||
active={this.state.activeTab[2] === '2'}
|
||||
onClick={() => { this.toggle(2, '2'); }}
|
||||
>
|
||||
<i className="icon-basket-loaded"></i> <span
|
||||
className={this.state.activeTab[2] === '2' ? '' : 'd-none'}> Shopping cart</span>
|
||||
</NavLink>
|
||||
</NavItem>
|
||||
<NavItem>
|
||||
<NavLink
|
||||
className={classnames({ active: this.state.activeTab[2] === '3' })}
|
||||
onClick={() => { this.toggle(2,'3'); }}
|
||||
>
|
||||
<i className="icon-pie-chart"></i> <span className={this.state.activeTab[2] === '3' ? '' : 'd-none'}> Charts</span>
|
||||
</NavLink>
|
||||
</NavItem>
|
||||
</Nav>
|
||||
<TabContent activeTab={this.state.activeTab[2]}>
|
||||
{this.tabPane()}
|
||||
</TabContent>
|
||||
</Col>
|
||||
<Col xs="12" md="6" className="mb-4">
|
||||
<Nav tabs>
|
||||
<NavItem>
|
||||
<NavLink
|
||||
className={classnames({ active: this.state.activeTab === '1' })}
|
||||
onClick={() => { this.toggle('1'); }}
|
||||
active={this.state.activeTab[3] === '1'}
|
||||
onClick={() => { this.toggle(3, '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[3] === '1' ? '' : 'd-none'}> Calc</span>
|
||||
{'\u00A0'}<Badge color="success">New</Badge>
|
||||
</NavLink>
|
||||
</NavItem>
|
||||
<NavItem>
|
||||
<NavLink
|
||||
className={classnames({ active: this.state.activeTab === '2' })}
|
||||
onClick={() => { this.toggle('2'); }}
|
||||
active={this.state.activeTab[3] === '2'}
|
||||
onClick={() => { this.toggle(3, '2'); }}
|
||||
>
|
||||
<i className="icon-basket-loaded"></i> <span
|
||||
className={this.state.activeTab === '2' ? '' : 'd-none'}> Shopping cart</span>
|
||||
<i className="icon-basket-loaded"></i>
|
||||
<span className={this.state.activeTab[3] === '2' ? '' : 'd-none'}> Cart</span>
|
||||
{'\u00A0'}<Badge pill color="danger">29</Badge>
|
||||
</NavLink>
|
||||
</NavItem>
|
||||
<NavItem>
|
||||
<NavLink
|
||||
className={classnames({ active: this.state.activeTab === '3' })}
|
||||
onClick={() => { this.toggle('3'); }}
|
||||
>
|
||||
<i className="icon-pie-chart"></i> <span className={this.state.activeTab === '3' ? '' : 'd-none'}> Charts</span>
|
||||
active={this.state.activeTab[3] === '3'}
|
||||
onClick={() => { this.toggle(3, '3'); }} >
|
||||
<i className="icon-pie-chart"></i>
|
||||
<span className={this.state.activeTab[3] === '3' ? '' : 'd-none'}> Charts</span>
|
||||
</NavLink>
|
||||
</NavItem>
|
||||
</Nav>
|
||||
<TabContent activeTab={this.state.activeTab}>
|
||||
<TabPane tabId="1">
|
||||
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>
|
||||
</Col>
|
||||
<Col xs="12" md="6" className="mb-4">
|
||||
<Nav tabs>
|
||||
<NavItem>
|
||||
<NavLink
|
||||
className={classnames({ active: this.state.activeTab === '1' })}
|
||||
onClick={() => { this.toggle('1'); }}
|
||||
>
|
||||
<i className="icon-calculator"></i> <span className={this.state.activeTab === '1' ? '' : 'd-none'}> Calculator </span>{'\u00A0'}<Badge
|
||||
color="success">New</Badge>
|
||||
</NavLink>
|
||||
</NavItem>
|
||||
<NavItem>
|
||||
<NavLink
|
||||
className={classnames({ active: this.state.activeTab === '2' })}
|
||||
onClick={() => { this.toggle('2'); }}
|
||||
>
|
||||
<i className="icon-basket-loaded"></i> <span
|
||||
className={this.state.activeTab === '2' ? '' : 'd-none'}> Shopping cart </span>{'\u00A0'}<Badge pill color="danger">29</Badge>
|
||||
</NavLink>
|
||||
</NavItem>
|
||||
<NavItem>
|
||||
<NavLink
|
||||
className={classnames({ active: this.state.activeTab === '3' })}
|
||||
onClick={() => { this.toggle('3'); }}
|
||||
>
|
||||
<i className="icon-pie-chart"></i> <span className={this.state.activeTab === '3' ? '' : 'd-none'}> Charts</span>
|
||||
</NavLink>
|
||||
</NavItem>
|
||||
</Nav>
|
||||
<TabContent activeTab={this.state.activeTab}>
|
||||
<TabPane tabId="1">
|
||||
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 activeTab={this.state.activeTab[3]}>
|
||||
{this.tabPane()}
|
||||
</TabContent>
|
||||
</Col>
|
||||
</Row>
|
||||
|
@ -1,9 +1,21 @@
|
||||
import React from 'react';
|
||||
import ReactDOM from 'react-dom';
|
||||
import Tabs from './Tabs';
|
||||
import {mount} from 'enzyme/build';
|
||||
|
||||
it('renders without crashing', () => {
|
||||
const div = document.createElement('div');
|
||||
ReactDOM.render(<Tabs />, 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