list.test.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. import { List, Avatar, Button, ButtonGroup } from '../../index';
  2. import { BASE_CLASS_PREFIX } from '../../../semi-foundation/base/constants';
  3. const dataSource = [
  4. '从明天起,做一个幸福的人',
  5. '喂马,劈柴,周游世界',
  6. '从明天起,关心粮食和蔬菜',
  7. '我有一所房子,面朝大海,春暖花开',
  8. ];
  9. function renderList(props) {
  10. const realProps = {
  11. dataSource,
  12. renderItem: item => <List.Item>{item}</List.Item>,
  13. ...props,
  14. };
  15. return mount(<List {...realProps} />, {
  16. attachTo: document.getElementById('container'),
  17. });
  18. }
  19. describe('List', () => {
  20. beforeEach(() => {
  21. const div = document.createElement('div');
  22. div.setAttribute('id', 'container');
  23. document.body.appendChild(div);
  24. });
  25. afterEach(() => {
  26. const div = document.getElementById('container');
  27. if (div) {
  28. document.body.removeChild(div);
  29. }
  30. document.body.innerHTML = '';
  31. });
  32. it('List with jsx', () => {
  33. const list = mount(<List>
  34. <List.Item>从明天起,做一个幸福的人</List.Item>
  35. <List.Item>喂马,劈柴,周游世界</List.Item>
  36. <List.Item>从明天起,关心粮食和蔬菜</List.Item>
  37. <List.Item>我有一所房子,面朝大海,春暖花开菜</List.Item>
  38. </List>, {
  39. attachTo: document.getElementById('container'),
  40. });;
  41. const item = document.querySelectorAll(`.${BASE_CLASS_PREFIX}-list-item`);
  42. expect(item.length).toEqual(4);
  43. expect(
  44. list
  45. .find(`.${BASE_CLASS_PREFIX}-list-item`)
  46. .at(0)
  47. .getDOMNode()
  48. .textContent
  49. ).toEqual('从明天起,做一个幸福的人');
  50. list.unmount();
  51. });
  52. it('List with basic renderItem', () => {
  53. const list = renderList();
  54. const item = document.querySelectorAll(`.${BASE_CLASS_PREFIX}-list-item`);
  55. expect(item.length).toEqual(4);
  56. expect(
  57. list
  58. .find(`.${BASE_CLASS_PREFIX}-list-item`)
  59. .at(0)
  60. .getDOMNode()
  61. .textContent
  62. ).toEqual('从明天起,做一个幸福的人');
  63. list.unmount();
  64. });
  65. it('List with renderItem header/main/extra', () => {
  66. const list = renderList({
  67. renderItem: item => (
  68. <List.Item
  69. header={<Avatar color="blue">SE</Avatar>}
  70. main={
  71. <div>
  72. <span style={{ color: 'var(--semi-color-text-0)', fontWeight: 500 }}>示例标题</span>
  73. {item}
  74. </div>
  75. }
  76. extra={
  77. <ButtonGroup theme="borderless">
  78. <Button>编辑</Button>
  79. <Button>更多</Button>
  80. </ButtonGroup>
  81. }
  82. />
  83. )
  84. });
  85. // item header
  86. expect(
  87. list
  88. .find(`.${BASE_CLASS_PREFIX}-list-item-body-header .${BASE_CLASS_PREFIX}-avatar-label`)
  89. .at(0)
  90. .getDOMNode()
  91. .textContent
  92. ).toEqual('SE');
  93. // item main
  94. expect(
  95. list
  96. .find(`.${BASE_CLASS_PREFIX}-list-item-body-main span`)
  97. .at(0)
  98. .getDOMNode()
  99. .textContent
  100. ).toEqual('示例标题');
  101. // item extra
  102. expect(list.exists(`.${BASE_CLASS_PREFIX}-list-item-extra .${BASE_CLASS_PREFIX}-button-group`)).toEqual(true);
  103. list.unmount();
  104. });
  105. it('List with className / style', () => {
  106. const props = {
  107. className: 'test',
  108. style: { height: 420 },
  109. };
  110. const list = renderList(props);
  111. expect(list.hasClass('test')).toEqual(true);
  112. expect(list.find('div.test')).toHaveStyle('height', 420);
  113. list.unmount();
  114. });
  115. it('List with bordered', () => {
  116. const list = renderList({ bordered: true });
  117. expect(list.exists(`.${BASE_CLASS_PREFIX}-list-bordered`)).toEqual(true);
  118. list.unmount();
  119. });
  120. it('List with size', () => {
  121. const smallList = renderList({ size: 'small' });
  122. expect(smallList.exists(`.${BASE_CLASS_PREFIX}-list-small`)).toEqual(true);
  123. smallList.unmount();
  124. const defaultList = renderList({ size: 'default' });
  125. expect(defaultList.exists(`.${BASE_CLASS_PREFIX}-list-default`)).toEqual(true);
  126. defaultList.unmount();
  127. const largeList = renderList({ size: 'large' });
  128. expect(largeList.exists(`.${BASE_CLASS_PREFIX}-list-large`)).toEqual(true);
  129. largeList.unmount();
  130. });
  131. it('List with layout', () => {
  132. const horizontalList = renderList({ layout: 'horizontal' });
  133. expect(horizontalList.exists(`.${BASE_CLASS_PREFIX}-list-flex`)).toEqual(true);
  134. horizontalList.unmount();
  135. const verticalList = renderList({ layout: 'vertical' });
  136. expect(verticalList.exists(`.${BASE_CLASS_PREFIX}-list-flex`)).toEqual(false);
  137. verticalList.unmount();
  138. });
  139. it('List with grid', () => {
  140. const list = renderList({
  141. grid: {
  142. gutter: 18,
  143. span: 6,
  144. }
  145. });
  146. const item = document.querySelectorAll(`.${BASE_CLASS_PREFIX}-col-6`);
  147. expect(item.length).toEqual(4);
  148. expect(list.find(`.${BASE_CLASS_PREFIX}-col-6`).at(0)).toHaveStyle('paddingLeft', 9);
  149. expect(list.find(`.${BASE_CLASS_PREFIX}-col-6`).at(0)).toHaveStyle('paddingRight', 9);
  150. list.unmount();
  151. });
  152. it('List with split', () => {
  153. const list = renderList({ split: false });
  154. expect(list.exists(`.${BASE_CLASS_PREFIX}-list-split`)).toEqual(false);
  155. list.unmount();
  156. });
  157. it('List with onClick', () => {
  158. const spyOnClick = sinon.spy(() => { });
  159. const list = renderList({ onClick: spyOnClick });
  160. list.find(`.${BASE_CLASS_PREFIX}-list-item`).at(0).simulate('click');
  161. expect(spyOnClick.calledOnce).toBe(true);
  162. list.unmount();
  163. });
  164. it('List with jsx', () => {
  165. const spyOnClick = sinon.spy(() => { });
  166. const list = mount(<List>
  167. <List.Item onClick={spyOnClick}>从明天起,做一个幸福的人</List.Item>
  168. <List.Item>喂马,劈柴,周游世界</List.Item>
  169. <List.Item>从明天起,关心粮食和蔬菜</List.Item>
  170. <List.Item>我有一所房子,面朝大海,春暖花开菜</List.Item>
  171. </List>, {
  172. attachTo: document.getElementById('container'),
  173. });;
  174. list.find(`.${BASE_CLASS_PREFIX}-list-item`).at(0).simulate('click');
  175. expect(spyOnClick.calledOnce).toBe(true);
  176. list.unmount();
  177. });
  178. it('List with loading', () => {
  179. const loadingList = renderList({ loading: true });
  180. expect(loadingList.exists(`.${BASE_CLASS_PREFIX}-spin-hidden`)).toEqual(false);
  181. loadingList.unmount();
  182. const noloadingList = renderList({ loading: false });
  183. expect(noloadingList.exists(`.${BASE_CLASS_PREFIX}-spin-hidden`)).toEqual(true);
  184. noloadingList.unmount();
  185. });
  186. it('List with loadMore', () => {
  187. const list = renderList({ loadMore: <Button>加载更多</Button> });
  188. expect(
  189. list
  190. .find(`.${BASE_CLASS_PREFIX}-list .${BASE_CLASS_PREFIX}-button .${BASE_CLASS_PREFIX}-button-content`)
  191. .at(0)
  192. .getDOMNode()
  193. .textContent
  194. ).toEqual('加载更多');
  195. list.unmount();
  196. });
  197. it('List with emptyContent', () => {
  198. const list = renderList({
  199. emptyContent: <span className='empty'>自定义空内容</span>,
  200. dataSource: []
  201. });
  202. expect(
  203. list
  204. .find(`.${BASE_CLASS_PREFIX}-list-items .${BASE_CLASS_PREFIX}-list-empty .empty`)
  205. .at(0)
  206. .getDOMNode()
  207. .textContent
  208. ).toEqual('自定义空内容');
  209. list.unmount();
  210. const list2 = renderList({
  211. dataSource: []
  212. });
  213. expect(
  214. list2
  215. .find(`.${BASE_CLASS_PREFIX}-list-items .${BASE_CLASS_PREFIX}-list-empty`)
  216. .getDOMNode()
  217. .textContent
  218. ).toEqual('暂无数据');
  219. list2.unmount();
  220. });
  221. it('List with footer/header', () => {
  222. const list = renderList({
  223. header: <div className='header'>头部</div>,
  224. footer: <div className='footer'>尾部</div>,
  225. });
  226. expect(
  227. list
  228. .find(`.${BASE_CLASS_PREFIX}-list-header .header`)
  229. .at(0)
  230. .getDOMNode()
  231. .textContent
  232. ).toEqual('头部');
  233. expect(
  234. list
  235. .find(`.${BASE_CLASS_PREFIX}-list-footer .footer`)
  236. .at(0)
  237. .getDOMNode()
  238. .textContent
  239. ).toEqual('尾部');
  240. list.unmount();
  241. });
  242. });