tabs.test.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import { useState } from 'react';
  2. import { TabPane, Tabs } from '../../index';
  3. import { BASE_CLASS_PREFIX } from '../../../semi-foundation/base/constants';
  4. let defaultTabPane = [
  5. { itemKey: 'itemKeyA', tab: 'titleA', children: 'contentA' },
  6. { itemKey: 'itemKeyB', tab: 'titleB', children: 'contentB' }
  7. ]
  8. let ACTIVE = `.${BASE_CLASS_PREFIX}-tabs-tab-active`;
  9. function getTabs(tabProps, tabPaneProps = defaultTabPane) {
  10. let tabPane = tabPaneProps.map(pane => {
  11. return <TabPane {...pane}></TabPane>
  12. });
  13. return <Tabs {...tabProps}>
  14. {tabPane}
  15. </Tabs>
  16. }
  17. describe('Tabs', () => {
  18. it('tabs render basicly', () => {
  19. let props = {};
  20. const tabs = mount(getTabs(props, defaultTabPane))
  21. // render in same time
  22. expect(tabs.find(`.${BASE_CLASS_PREFIX}-tabs-content`).children().length).toEqual(2);
  23. // default active first pane
  24. expect(tabs.find(`.${BASE_CLASS_PREFIX}-tabs-tab-active`).text()).toEqual('titleA');
  25. });
  26. it('Tabs with custom className & style & contentStyle', () => {
  27. let props = {
  28. className: 'test',
  29. style: {
  30. color: 'red'
  31. }
  32. };
  33. const tabs = shallow(getTabs(props));
  34. expect(tabs.exists('.test')).toEqual(true);
  35. expect(tabs.find('div.test')).toHaveStyle('color', 'red');
  36. });
  37. it('Tabs with defaultActiveKey', () => {
  38. let tabProps = {
  39. defaultActiveKey: 'itemKeyB'
  40. };
  41. const tabs = mount(getTabs(tabProps));
  42. const activeTabContent = tabs.find(`.${BASE_CLASS_PREFIX}-tabs-tab-active`).text();
  43. expect(activeTabContent).toEqual('titleB');
  44. });
  45. it('different type Tabs', () => {
  46. let lineTabs = mount(getTabs({ type: 'line' }));
  47. let cardTabs = mount(getTabs({ type: 'card' }));
  48. let buttonTabs = mount(getTabs({ type: 'button' }));
  49. expect(lineTabs.exists(`.${BASE_CLASS_PREFIX}-tabs-bar-button`)).toEqual(false);
  50. expect(lineTabs.exists(`.${BASE_CLASS_PREFIX}-tabs-bar-card`)).toEqual(false);
  51. expect(cardTabs.exists(`.${BASE_CLASS_PREFIX}-tabs-bar-card`)).toEqual(true);
  52. expect(buttonTabs.exists(`.${BASE_CLASS_PREFIX}-tabs-bar-button`)).toEqual(true);
  53. });
  54. it('tabList', () => {
  55. let tabList = [
  56. { tab: "文档", itemKey: "1" },
  57. { tab: "快速起步", itemKey: "2" },
  58. { tab: "帮助", itemKey: "3" }
  59. ];
  60. const contentList = [
  61. (<div>文档</div>),
  62. (<div>快速起步</div>),
  63. (<div>帮助</div>)
  64. ]
  65. class TabListDemo extends React.Component {
  66. state = {
  67. key: '1'
  68. };
  69. render() {
  70. return (
  71. <Tabs
  72. tabList={tabList}
  73. >
  74. {contentList[this.state.key - 1]}
  75. </Tabs>
  76. )
  77. }
  78. }
  79. const tabs = mount(<TabListDemo></TabListDemo>);
  80. expect(tabs.find(`.${BASE_CLASS_PREFIX}-tabs-content`).children().length).toEqual(1);
  81. })
  82. it('should not toggle whenn clicked disabled pane', () => {
  83. let spyOnChange = sinon.spy((activeKey) => {
  84. })
  85. let tabProps = {
  86. defaultActiveKey: 'itemKeyA',
  87. onChange: spyOnChange
  88. };
  89. let tabPaneProps = [
  90. { itemKey: 'itemKeyA', tab: 'titleA' },
  91. { itemKey: 'itemKeyB', tab: 'titleB', disabled: true },
  92. ];
  93. let tabs = mount(getTabs(tabProps, tabPaneProps));
  94. expect(tabs.exists(`.${BASE_CLASS_PREFIX}-tabs-tab-disabled`)).toEqual(true);
  95. expect(tabs.find(`.${BASE_CLASS_PREFIX}-tabs-tab-disabled`).text()).toEqual('titleB');
  96. tabs.find(`.${BASE_CLASS_PREFIX}-tabs-tab`).at(1).simulate('click');
  97. expect(spyOnChange.calledOnce).toBe(false);
  98. expect(tabs.find(`.${BASE_CLASS_PREFIX}-tabs-tab-active`).text()).toEqual('titleA');
  99. });
  100. it('onTabClick', () => {
  101. let onTabClick = (key, e) => {
  102. // debugger
  103. };
  104. let spyOnTabClick = sinon.spy(onTabClick);
  105. let props = {
  106. onTabClick: spyOnTabClick
  107. };
  108. const tabs = mount(getTabs(props));
  109. tabs.find(`.${BASE_CLASS_PREFIX}-tabs-tab`).at(1).simulate('click');
  110. expect(spyOnTabClick.calledWithMatch("itemKeyB")).toBe(true);
  111. expect(spyOnTabClick.calledOnce).toBe(true);
  112. });
  113. it('onChange', () => {
  114. let onChange = value => {
  115. // debugger
  116. };
  117. let spyOnChange = sinon.spy(onChange);
  118. let tabsProps = {
  119. onChange: spyOnChange
  120. };
  121. let paneProps = defaultTabPane;
  122. const tabs = mount(getTabs(tabsProps, paneProps));
  123. tabs.find(`.${BASE_CLASS_PREFIX}-tabs-tab`).at(1).simulate('click');
  124. expect(spyOnChange.calledWithMatch("itemKeyB")).toBe(true);
  125. expect(spyOnChange.calledOnce).toBe(true);
  126. });
  127. it('should update when activeKey change', () => {
  128. let tabsProps = {
  129. activeKey: 'itemKeyA'
  130. };
  131. const tabs = mount(getTabs(tabsProps, defaultTabPane));
  132. expect(tabs.find(`.${BASE_CLASS_PREFIX}-tabs-tab-active`).text()).toEqual('titleA');
  133. tabs.setProps({ activeKey: 'itemKeyB' });
  134. tabs.update()
  135. expect(tabs.find(`.${BASE_CLASS_PREFIX}-tabs-tab-active`).text()).toEqual('titleB');
  136. });
  137. it('contentStyle', () => {
  138. let tabsProps = {
  139. contentStyle: {
  140. color: 'red'
  141. }
  142. };
  143. const tabs = mount(getTabs(tabsProps, defaultTabPane));
  144. expect(tabs.find(`.${BASE_CLASS_PREFIX}-tabs-content`)).toHaveStyle('color', 'red');
  145. });
  146. it('tarBarExtraContent', () => {
  147. let extraContent = (<div className='extra'>Extra</div>);
  148. let tabsProps = {
  149. tabBarExtraContent: extraContent
  150. };
  151. const tabs = mount(getTabs(tabsProps, defaultTabPane));
  152. expect(tabs.contains(extraContent)).toEqual(true);
  153. });
  154. it('tabpane closable', () => {
  155. const Demo = () => {
  156. const [tabList, $tabList] = useState([
  157. {tab: '文档', itemKey:'1', text:'文档', closable:true},
  158. {tab: '快速起步', itemKey:'2', text:'快速起步', closable:true},
  159. {tab: '帮助', itemKey:'3', text:'帮助'},
  160. ]);
  161. const close = (key)=>{
  162. const newTabList = [...tabList];
  163. const closeIndex = newTabList.findIndex(t=>t.itemKey===key);
  164. newTabList.splice(closeIndex, 1);
  165. $tabList(newTabList);
  166. }
  167. return <Tabs type="card" defaultActiveKey="1" onTabClose={close}>
  168. {
  169. tabList.map(t=><TabPane closable={t.closable} tab={t.tab} itemKey={t.itemKey} key={t.itemKey}>{t.text}</TabPane>)
  170. }
  171. </Tabs>
  172. }
  173. const demo = mount(<Demo />);
  174. const firstTab = demo.find(`.${BASE_CLASS_PREFIX}-tabs-tab`).at(0);
  175. const secondTab = demo.find(`.${BASE_CLASS_PREFIX}-tabs-tab`).at(1);
  176. const thirdTab = demo.find(`.${BASE_CLASS_PREFIX}-tabs-tab`).at(2);
  177. expect(firstTab.exists(`.${BASE_CLASS_PREFIX}-tabs-tab-icon-close`)).toEqual(true);
  178. expect(secondTab.exists(`.${BASE_CLASS_PREFIX}-tabs-tab-icon-close`)).toEqual(true);
  179. expect(thirdTab.exists(`.${BASE_CLASS_PREFIX}-tabs-tab-icon-close`)).toEqual(false);
  180. demo.find(`.${BASE_CLASS_PREFIX}-tabs-tab-icon-close`).at(0).simulate('click');
  181. expect(demo.find(`.${BASE_CLASS_PREFIX}-tabs-tab`).length).toEqual(2)
  182. expect(demo.find(`.${BASE_CLASS_PREFIX}-tabs-tab`).at(0).hasClass(`${BASE_CLASS_PREFIX}-tabs-tab-active`)).toEqual(true);
  183. });
  184. })