typography.test.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import * as _ from 'lodash';
  2. import Typography from '../index';
  3. describe(`Typography`, () => {
  4. beforeEach(() => {
  5. document.getSelection = () => {
  6. return {
  7. removeAllRanges: () => {},
  8. };
  9. };
  10. });
  11. it('custom component', () => {
  12. let props = { component: 'div' };
  13. const typographyTitle = mount(
  14. <Typography.Title {...props} heading={1}>
  15. Semi Design
  16. </Typography.Title>
  17. );
  18. const title = typographyTitle.find('div.semi-typography-h1');
  19. expect(title.length).toEqual(1);
  20. const typographyText = mount(
  21. <Typography.Text {...props} id="text">
  22. Semi Design
  23. </Typography.Text>
  24. );
  25. const text = typographyText.find('div.semi-typography');
  26. expect(text.length).toEqual(1);
  27. const typographyParagraph = mount(<Typography.Paragraph {...props}>Semi Design</Typography.Paragraph>);
  28. const p = typographyParagraph.find('div.semi-typography-paragraph');
  29. expect(p.length).toEqual(1);
  30. typographyParagraph.unmount();
  31. });
  32. it('typography copyable', () => {
  33. const typographyParagraph = mount(<Typography.Paragraph copyable>Semi Design</Typography.Paragraph>);
  34. const p = typographyParagraph.find('.semi-icon-copy');
  35. expect(p.length).toEqual(1);
  36. p.at(0).simulate('click');
  37. expect(typographyParagraph.find('.semi-typography-action-copied').length).toEqual(1);
  38. typographyParagraph.setProps({ copyable: false });
  39. typographyParagraph.update();
  40. expect(typographyParagraph.find('.semi-icon-copy').length).toEqual(0);
  41. });
  42. it('typography link', () => {
  43. const text = mount(<Typography.Text link={{ href: 'https://semi.design/' }}>链接文本</Typography.Text>);
  44. expect(text.find('.semi-typography.semi-typography-link').length).toEqual(1);
  45. text.setProps({ disabled: true });
  46. text.update();
  47. expect(text.find('.semi-typography.semi-typography-disabled').length).toEqual(1);
  48. text.setProps({ underline: true, link: false });
  49. text.update();
  50. expect(text.find('.semi-typography u').length).toEqual(1);
  51. });
  52. it('typography ellipsis', () => {
  53. const typographyParagraph = mount(
  54. <Typography.Paragraph ellipsis={{ showTooltip: true }} style={{ width: 250 }}>
  55. 是一个很长很长很长很长5号标题
  56. </Typography.Paragraph>
  57. );
  58. // jest 测不出layout,补一些无效用例,提高coverage
  59. expect(typographyParagraph.find('semi-typography-ellipsis').length).toEqual(0);
  60. typographyParagraph.setProps({ children: '的撒的撒打算的撒的撒的撒打算打的撒的撒打算的撒的撒的撒打算打' });
  61. typographyParagraph.update();
  62. expect(typographyParagraph.find('semi-typography-ellipsis').length).toEqual(0);
  63. typographyParagraph.setProps({
  64. ellipsis: {
  65. expandText: 'expandText',
  66. collapseText: 'collapseText',
  67. rows: 1,
  68. showTooltip: {
  69. type: 'popover',
  70. },
  71. suffix: 'suffix',
  72. },
  73. });
  74. typographyParagraph.update();
  75. expect(typographyParagraph.find('semi-typography-ellipsis').length).toEqual(0);
  76. });
  77. it('typography Numeral', () => {
  78. let numeral = mount(
  79. <Typography.Numeral rule={'numbers'} truncate={'ceil'} precision={2}>
  80. <div className="price">
  81. <span>预期价格:{() => 1.555}; 成本: -1; 盈利: 0.555</span>
  82. <b>Currency symbols: $</b>
  83. </div>
  84. </Typography.Numeral>
  85. );
  86. expect(numeral.find('.price').text()).toEqual('1.56-1.00,0.56');
  87. numeral = mount(
  88. <Typography.Numeral rule={'exponential'} truncate={'floor'} precision={2}>
  89. <div className="price">
  90. Total revenue: <b>$ 1992.15</b>
  91. </div>
  92. </Typography.Numeral>
  93. )
  94. expect(numeral.find('.price').text()).toEqual('Total revenue: $ 1.99e+3')
  95. // test: parser
  96. numeral.setProps({
  97. parser: oldVal => oldVal.replace(/[^\d.]/g, '')
  98. })
  99. expect(numeral.find('.price').text()).toEqual('1992.15')
  100. })
  101. });