card.test.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import { Card, Rating, Typography, Avatar, CardGroup } from '../../index';
  2. import { cssClasses } from '@douyinfe/semi-foundation/icons/constants';
  3. import { BASE_CLASS_PREFIX } from '../../../semi-foundation/base/constants';
  4. const { Meta } = Card;
  5. function getCard(props) {
  6. return mount(<Card {...props}></Card>, { attachTo: document.getElementById('container') });
  7. }
  8. function getMeta(props = {}) {
  9. return mount(<Meta {...props}></Meta>);
  10. }
  11. describe('Card', () => {
  12. it('Card with actions', () => {
  13. const propsWithActions = {
  14. actions: [<Rating defaultValue={4} />, <Rating defaultValue={4} />],
  15. };
  16. const cardWithActions = getCard(propsWithActions);
  17. expect(cardWithActions.exists(`.${BASE_CLASS_PREFIX}-card-body-actions`)).toEqual(true);
  18. expect(cardWithActions.find(`.${BASE_CLASS_PREFIX}-card-body-actions-item`).length).toEqual(2);
  19. cardWithActions.unmount();
  20. const props = {};
  21. const card = getCard(props);
  22. expect(card.exists(`.${BASE_CLASS_PREFIX}-card-body-actions`)).toEqual(false);
  23. card.unmount();
  24. });
  25. it('Card with bordered', () => {
  26. const propsWithBordered = {
  27. bordered: true,
  28. };
  29. const cardWithBordered = getCard(propsWithBordered);
  30. expect(cardWithBordered.exists(`.${BASE_CLASS_PREFIX}-card-bordered`)).toEqual(true);
  31. cardWithBordered.unmount();
  32. const props = {
  33. bordered: false,
  34. };
  35. const card = getCard(props);
  36. expect(card.exists(`.${BASE_CLASS_PREFIX}-card-body-bordered`)).toEqual(false);
  37. card.unmount();
  38. });
  39. it('Card with cover', () => {
  40. const propsWithCover = {
  41. cover: (
  42. <img
  43. alt="example"
  44. src="https://lf3-static.bytednsdoc.com/obj/eden-cn/ptlz_zlp/ljhwZthlaukjlkulzlp/root-web-sites/card-cover-docs-demo.jpeg"
  45. />
  46. ),
  47. };
  48. const cardWithCover = getCard(propsWithCover);
  49. expect(cardWithCover.exists(`.${BASE_CLASS_PREFIX}-card-cover`)).toEqual(true);
  50. cardWithCover.unmount();
  51. const props = {};
  52. const card = getCard(props);
  53. expect(card.exists(`.${BASE_CLASS_PREFIX}-card-body-cover`)).toEqual(false);
  54. card.unmount();
  55. });
  56. it('Card with headerExtraContent', () => {
  57. const propsWithHeaderExtraContent = {
  58. headerExtraContent: <Typography.Text link={{}}>更多</Typography.Text>,
  59. };
  60. const cardWithHeaderExtraContent = getCard(propsWithHeaderExtraContent);
  61. expect(cardWithHeaderExtraContent.exists(`.${BASE_CLASS_PREFIX}-card-header-wrapper-extra`)).toEqual(true);
  62. cardWithHeaderExtraContent.unmount();
  63. const props = {};
  64. const card = getCard(props);
  65. expect(card.exists(`.${BASE_CLASS_PREFIX}-card-header-wrapper-extra`)).toEqual(false);
  66. card.unmount();
  67. });
  68. it('Card with footer/header', () => {
  69. const propsWithFooter = {
  70. footer: <Typography.Text link={{}}>更多</Typography.Text>,
  71. header: <Typography.Text link={{}}>更多</Typography.Text>,
  72. };
  73. const cardWithFooter = getCard(propsWithFooter);
  74. expect(cardWithFooter.exists(`.${BASE_CLASS_PREFIX}-card-footer`)).toEqual(true);
  75. expect(cardWithFooter.exists(`.${BASE_CLASS_PREFIX}-card-header`)).toEqual(true);
  76. cardWithFooter.unmount();
  77. const props = {};
  78. const card = getCard(props);
  79. expect(card.exists(`.${BASE_CLASS_PREFIX}-card-footer`)).toEqual(false);
  80. expect(card.exists(`.${BASE_CLASS_PREFIX}-card-header`)).toEqual(false);
  81. card.unmount();
  82. });
  83. it('Card with footerLine/headerLine', () => {
  84. const propsWithLine = {
  85. footer: <Typography.Text link={{}}>更多</Typography.Text>,
  86. header: <Typography.Text link={{}}>更多</Typography.Text>,
  87. headerLine: true,
  88. footerLine: true,
  89. };
  90. const cardWithLine = getCard(propsWithLine);
  91. expect(cardWithLine.exists(`.${BASE_CLASS_PREFIX}-card-header-bordered`)).toEqual(true);
  92. expect(cardWithLine.exists(`.${BASE_CLASS_PREFIX}-card-footer-bordered`)).toEqual(true);
  93. cardWithLine.unmount();
  94. const props = {
  95. footer: <Typography.Text link={{}}>更多</Typography.Text>,
  96. header: <Typography.Text link={{}}>更多</Typography.Text>,
  97. headerLine: false,
  98. footerLine: false,
  99. };
  100. const card = getCard(props);
  101. expect(card.exists(`.${BASE_CLASS_PREFIX}-card-footer-bordered`)).toEqual(false);
  102. expect(card.exists(`.${BASE_CLASS_PREFIX}-card-header-bordered`)).toEqual(false);
  103. card.unmount();
  104. });
  105. it('Card with title', () => {
  106. const propsWithTitle = {
  107. title: 'semi',
  108. };
  109. const cardWithTitle = getCard(propsWithTitle);
  110. expect(cardWithTitle.exists(`.${BASE_CLASS_PREFIX}-card-header-wrapper-title`)).toEqual(true);
  111. cardWithTitle.unmount();
  112. const props = {};
  113. const card = getCard(props);
  114. expect(card.exists(`.${BASE_CLASS_PREFIX}-card-header-wrapper-title`)).toEqual(false);
  115. card.unmount();
  116. });
  117. it('Card with loading', () => {
  118. const propsWithLoading = {
  119. loading: true,
  120. };
  121. const cardWithLoading = mount(<Card {...propsWithLoading}>content</Card>);
  122. expect(cardWithLoading.exists(`.${BASE_CLASS_PREFIX}-card-body .${BASE_CLASS_PREFIX}-skeleton`)).toEqual(true);
  123. cardWithLoading.unmount();
  124. const props = {
  125. loading: false,
  126. };
  127. const card = mount(<Card {...props}>content</Card>);
  128. expect(card.exists(`.${BASE_CLASS_PREFIX}-card-body .${BASE_CLASS_PREFIX}-skeleton`)).toEqual(false);
  129. card.unmount();
  130. });
  131. it('Card with shadows', () => {
  132. const propsWithHover = {
  133. shadows: 'hover',
  134. };
  135. const cardWithHover = getCard(propsWithHover);
  136. expect(cardWithHover.exists(`.${BASE_CLASS_PREFIX}-card-shadows-hover`)).toEqual(true);
  137. cardWithHover.unmount();
  138. const propsWithShow = {
  139. shadows: 'show',
  140. };
  141. const cardWithShow = getCard(propsWithShow);
  142. expect(cardWithShow.exists(`.${BASE_CLASS_PREFIX}-card-shadows-show`)).toEqual(true);
  143. cardWithShow.unmount();
  144. });
  145. it('Card with custom className', () => {
  146. const propsWithClassName = { className: 'test' };
  147. const wrapper = getCard(propsWithClassName);
  148. expect(wrapper.hasClass('test')).toEqual(true);
  149. });
  150. it('Card Meta', () => {
  151. const meta = getMeta({
  152. avatar: (
  153. <Avatar
  154. size="default"
  155. src="https://lf3-static.bytednsdoc.com/obj/eden-cn/ptlz_zlp/ljhwZthlaukjlkulzlp/card-meta-avatar-docs-demo.jpg"
  156. />
  157. ),
  158. title: 'title',
  159. description: 'description',
  160. });
  161. expect(meta.exists(`.${BASE_CLASS_PREFIX}-card-meta`)).toEqual(true);
  162. expect(meta.exists(`.${BASE_CLASS_PREFIX}-card-meta-avatar`)).toEqual(true);
  163. expect(meta.exists(`.${BASE_CLASS_PREFIX}-card-meta-wrapper`)).toEqual(true);
  164. expect(meta.exists(`.${BASE_CLASS_PREFIX}-card-meta-wrapper-title`)).toEqual(true);
  165. expect(meta.exists(`.${BASE_CLASS_PREFIX}-card-meta-wrapper-description`)).toEqual(true);
  166. });
  167. it('Card Group', () => {
  168. const cardGroup = mount(
  169. <CardGroup spacing={16}>
  170. {Array.from(Array(10)).map((i, idx) => (
  171. <Card key={idx}>Content</Card>
  172. ))}
  173. </CardGroup>
  174. );
  175. expect(cardGroup.exists(`.${BASE_CLASS_PREFIX}-space`)).toEqual(true);
  176. expect(cardGroup.find(`.${BASE_CLASS_PREFIX}-card`).length).toEqual(10);
  177. });
  178. });