Guest User

Untitled

a guest
Jan 18th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. class TabbedArea extends React.Component{
  2.  
  3.  
  4. constructor(props) {
  5. super(props)
  6.  
  7. this.handleClick = this.handleClick.bind(this);
  8. this.state = {
  9. activeIndex: this.props.activeIndex || 0
  10. };
  11. }
  12.  
  13. componentWillReceiveProps(nextProps){
  14. if (this.state.activeIndex !== nextProps.activeIndex) {
  15. this.setState({
  16. activeIndex: nextProps.activeIndex
  17. });
  18. }
  19. }
  20.  
  21. handleClick(index, e) {
  22. e.preventDefault();
  23. this.setState({
  24. activeIndex: index
  25. });
  26. }
  27.  
  28. tabNodes() {
  29. return (_.map(this.props.children, (child, index) => {
  30. let className = classNames({'active': this.state.activeIndex === index});
  31. return (
  32. <li key={index} onClick={(e) => this.handleClick(index, e)}>
  33. <a className={className} href="#">{child.props.display}</a>
  34. </li>
  35. )
  36. }
  37. )
  38. )
  39. }
  40.  
  41. contentNodes() {
  42. return (
  43. _.map(this.props.children, (child, index) => {
  44. if(this.state.activeIndex === index) {
  45. return (
  46. <div className="TabPane" key={index}>
  47. {child.props.children}
  48. </div>
  49. )
  50. }
  51. }
  52. )
  53. )
  54. }
  55.  
  56. render() {
  57.  
  58. return (
  59. <div className={`${styles.ParcelResultsTrackingHistory} col-md-12`}>
  60. <ul className="nav nav-tabs" id="navTabs">
  61. {this.tabNodes()}
  62. </ul>
  63. <section>
  64. {this.contentNodes()}
  65. </section>
  66. </div>
  67. );
  68. }
  69.  
  70. }
  71.  
  72. export default TabbedArea;
  73.  
  74. describe('Given the Tabbed Area component is rendered', () => {
  75.  
  76.  
  77. describe('Initial Snapshots', () => {
  78. let component
  79. beforeEach(() => {
  80. component = shallow(<TabbedArea />)
  81. })
  82. it('should be as expected', () => {
  83. expect(shallowToJson(component)).toMatchSnapshot()
  84. })
  85. })
  86.  
  87. describe('I click on the second tab', () => {
  88. let component
  89. let tab2
  90.  
  91. component = shallow(<TabbedArea/>)
  92. tab2 = component.find('ul li').at(1);
  93. tab2.simulate('click');
  94.  
  95. describe('the content of the second tab is rendered', () => {
  96. component.update();
  97.  
  98. it('should match the snapshot', () => {
  99. expect(shallowToJson(component)).toMatchSnapshot()
  100. });
  101.  
  102. });
  103.  
  104. });
  105.  
  106. describe('Clicking on the tab', () => {
  107. const wrapper = mount(<TabbedArea/>)
  108. const handleClick = jest.spyOn(wrapper.instance(), 'handleClick');
  109. wrapper.update();
  110. const tab = wrapper.find('ul#navTabs li').first()
  111. tab.simulate('click')
  112.  
  113. it('will call handleClick', () => {
  114. expect(handleClick).toBeCalled();
  115. });
  116. });
  117.  
  118. })
Add Comment
Please, Sign In to add comment