breadcrumb.test.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. import { Breadcrumb } from '../../index';
  2. import { IconHome, IconArticle } from '@douyinfe/semi-icons';
  3. function getBr(props = {}) {
  4. let children
  5. if (props.children) {
  6. children = props.children.map(childProp => <Breadcrumb.Item {...childProp}></Breadcrumb.Item>)
  7. } else {
  8. children = [
  9. <Breadcrumb.Item>semi</Breadcrumb.Item>,
  10. <Breadcrumb.Item>design</Breadcrumb.Item>,
  11. <Breadcrumb.Item>byde</Breadcrumb.Item>
  12. ]
  13. }
  14. return mount(<Breadcrumb {...props}>{children}</Breadcrumb>, { attachTo: document.getElementById('container') });
  15. }
  16. describe('Breadcrumb', () => {
  17. beforeEach(() => {
  18. const div = document.createElement('div');
  19. div.setAttribute('id', 'container');
  20. document.body.appendChild(div);
  21. });
  22. afterEach(() => {
  23. const div = document.getElementById('container');
  24. if (div) {
  25. document.body.removeChild(div);
  26. }
  27. document.body.innerHTML = '';
  28. });
  29. it('custom className & style', () => {
  30. let props = {
  31. className: 'test',
  32. style: {
  33. color: 'red',
  34. },
  35. };
  36. const wrapper = getBr(props);
  37. expect(wrapper.hasClass('test')).toEqual(true);
  38. expect(wrapper.find('nav.test')).toHaveStyle('color', 'red');
  39. });
  40. it('compact = false', () => {
  41. let props = {
  42. compact: false,
  43. };
  44. let br = getBr(props);
  45. expect(br.exists('.semi-breadcrumb-wrapper-loose')).toEqual(true);
  46. expect(br.exists('.semi-breadcrumb-wrapper-compact')).toEqual(false);
  47. br.unmount();
  48. let br2 = getBr();
  49. expect(br2.exists('.semi-breadcrumb-wrapper-compact')).toEqual(true);
  50. expect(br2.exists('.semi-breadcrumb-wrapper-loose')).toEqual(false);
  51. });
  52. it('separator', () => {
  53. let props = {
  54. separator: '>'
  55. };
  56. let br = getBr(props);
  57. expect(br.find('.semi-breadcrumb-separator').children().at(0).text()).toEqual('>')
  58. expect(br.find('.semi-breadcrumb-separator').children().at(1).text()).toEqual('>')
  59. });
  60. it('maxItemCount', () => {
  61. let props = {
  62. maxItemCount: 3,
  63. children: [
  64. { children: 'semi' },
  65. { children: 'design' },
  66. { children: 'powered' },
  67. { children: 'by' },
  68. { children: 'douyin' },
  69. ]
  70. };
  71. let br = getBr(props);
  72. expect(br.find('.semi-breadcrumb-wrapper').children().length).toEqual(4); // semi/.../by/douyin
  73. expect(br.find('.semi-breadcrumb-item-wrap .semi-typography').children().at(0).text()).toEqual('semi');
  74. expect(br.exists('.semi-breadcrumb-collapse .semi-icon-more')).toEqual(true);
  75. expect(br.find('.semi-breadcrumb-item-wrap .semi-typography').children().at(1).text()).toEqual('by');
  76. expect(br.find('.semi-breadcrumb-item-wrap .semi-typography').children().at(2).text()).toEqual('douyin');
  77. });
  78. it('Item icon/href/noLink/separator', () => {
  79. let href = 'https://semi.design'
  80. let props = {
  81. children: [
  82. { icon: <IconHome />, separator: '???', children: 'semi' },
  83. { icon: <IconArticle />, children: 'design', href },
  84. { children: 'system', href: null },
  85. ]
  86. };
  87. let br = getBr(props);
  88. expect(br.exists('.semi-icon-home')).toEqual(true);
  89. expect(br.exists('.semi-icon-article')).toEqual(true);
  90. expect(br.find('.semi-breadcrumb-item-wrap a').getDOMNode().getAttribute("href")).toEqual(href);
  91. // when setting separator in item props, will not wrap in .semi-breadcrumb-separator
  92. expect(br.find('.semi-breadcrumb-item-wrap').at(0).children().at(1).text()).toEqual('???')
  93. expect(br.find('.semi-breadcrumb-separator').children().at(0).text()).toEqual('/')
  94. });
  95. it('onClick & item.onClick', () => {
  96. let spyClick = sinon.spy((item, e) => {
  97. });
  98. let spyItemClick = sinon.spy((item, e) => {
  99. });
  100. let props = {
  101. onClick: spyClick,
  102. children: [
  103. { onClick: spyItemClick, children: 'semi' },
  104. { children: 'design' },
  105. ]
  106. };
  107. let br = getBr(props);
  108. br.find('.semi-breadcrumb-item-wrap').children().at(0).simulate('click', {});
  109. expect(spyItemClick.calledOnce).toEqual(true);
  110. expect(spyClick.calledOnce).toEqual(true);
  111. let firstCall = spyClick.getCall(0);
  112. let firstArgs = firstCall.args;
  113. expect(firstArgs[0]).toEqual({ name: 'semi' });
  114. expect(firstArgs[1].constructor.name === 'SyntheticEvent').toEqual(true);
  115. expect(firstArgs.length === 2).toEqual(true);
  116. let itemCall = spyItemClick.getCall(0);
  117. let itemArgs = itemCall.args;
  118. expect(itemArgs[0]).toEqual({ name: 'semi' });
  119. expect(itemArgs[1].constructor.name === 'SyntheticEvent').toEqual(true);
  120. expect(itemArgs.length === 2).toEqual(true);
  121. });
  122. it('routes', () => {
  123. // object routes & string routes hybrid
  124. const objRoutes = [
  125. {
  126. path: '/',
  127. href: '/',
  128. icon: <IconHome />
  129. },
  130. {
  131. path: '/breadcrumb',
  132. href: '/components/breadcrumb',
  133. name: 'breadcrumb',
  134. icon: <IconArticle />
  135. },
  136. 'string only'
  137. ];
  138. let props = {
  139. routes: objRoutes
  140. }
  141. let br = getBr(props);
  142. let fisrtItem = br.find('.semi-breadcrumb-item-wrap').at(0);
  143. let secondItem = br.find('.semi-breadcrumb-item-wrap').at(1);
  144. let thirdItem = br.find('.semi-breadcrumb-item-wrap').at(2);
  145. expect(fisrtItem.exists('.semi-icon-home')).toEqual(true);
  146. expect(secondItem.exists('.semi-icon-article')).toEqual(true);
  147. expect(fisrtItem.find('.semi-breadcrumb-item-wrap a').getDOMNode().getAttribute("href")).toEqual('/');
  148. expect(secondItem.find('.semi-breadcrumb-item-wrap a').getDOMNode().getAttribute("href")).toEqual('/components/breadcrumb');
  149. expect(secondItem.find('.semi-breadcrumb-item-wrap .semi-typography').text()).toEqual('breadcrumb');
  150. expect(thirdItem.find('.semi-breadcrumb-item-wrap .semi-typography').text()).toEqual('string only');
  151. });
  152. it('renderItem', () => {
  153. const objRoutes = [
  154. {
  155. path: 'path',
  156. href: 'href',
  157. name: 'test',
  158. icon: <IconHome />,
  159. }
  160. ];
  161. let props = {
  162. routes: objRoutes,
  163. renderItem: (route) => (
  164. <>
  165. <div className='path'>{route.path}</div>
  166. <div className='href'>{route.href}</div>
  167. <div className='name'>{route.name}</div>
  168. </>
  169. )
  170. }
  171. let br = getBr(props);
  172. let item = br.find('.semi-breadcrumb-item-title');
  173. expect(item.find('.path').text()).toEqual('path');
  174. expect(item.find('.href').text()).toEqual('href');
  175. expect(item.find('.name').text()).toEqual('test');
  176. });
  177. // it('renderMore + moreType', () => {
  178. // let props = {
  179. // maxItemCount: 3,
  180. // children: [
  181. // { children: 'semi' },
  182. // { children: 'design' },
  183. // { children: 'powered' },
  184. // { children: 'by' },
  185. // { children: 'douyin' },
  186. // ],
  187. // renderMore: (restItem) => <div>rest</div>
  188. // };
  189. // let br = getBr(props);
  190. // expect(br.find('.semi-breadcrumb-collapse')).toEqual(true);
  191. // });
  192. // it('autoCollapse', () => {
  193. // });
  194. // not easy to test tooltip in jest unit test, ignore
  195. // it('showTooltip', () => {
  196. // });
  197. });