progress.test.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. import React from 'react';
  2. import { mount } from 'enzyme';
  3. import { BASE_CLASS_PREFIX } from '../../../semi-foundation/base/constants';
  4. import Progress from '../index';
  5. const getProgress = (props = {}) => mount(<Progress {...props} />);
  6. describe('Progress', () => {
  7. it('percent pass invalid value like NaN', () => {
  8. const p = getProgress({ percent: 30 });
  9. function testNaN() {
  10. p.setProps({ percent: NaN });
  11. }
  12. expect(testNaN).toThrow('');
  13. });
  14. it('render successfully', () => {
  15. const p = getProgress();
  16. const node = p.find(`.${BASE_CLASS_PREFIX}-progress`);
  17. expect(node.length).toEqual(1);
  18. });
  19. it('render circle progress', () => {
  20. const p = getProgress({ type: 'circle' });
  21. const node = p.find(`svg.${BASE_CLASS_PREFIX}-progress-circle-ring`);
  22. expect(node.length).toEqual(1);
  23. });
  24. it('process change success', () => {
  25. const p = getProgress({ type: 'circle', percent: 30 });
  26. p.setProps({ percent: 50 });
  27. const percentNumber = p.state('percentNumber');
  28. setTimeout(() => {
  29. expect(percentNumber).toEqual(50);
  30. }, 500);
  31. });
  32. it('classname & style', () => {
  33. const p = getProgress({ className: 'test', color: 'red' });
  34. const node = p.find(`.${BASE_CLASS_PREFIX}-progress`);
  35. expect(node.length).toEqual(1);
  36. });
  37. it('showInfo', () => {
  38. const p = getProgress({ showInfo: true, percent: 50 });
  39. expect(p.exists('.semi-progress-line-text')).toEqual(true);
  40. });
  41. it('stroke & size & orbitStroke', () => {
  42. let props = {
  43. stroke: '#fc8800',
  44. size: 'small',
  45. orbitStroke: '#f93920',
  46. };
  47. const p = getProgress(props);
  48. expect(p.exists('.semi-progress-large'));
  49. });
  50. it('Gradient Accuracy [strokeGradient true & stroke type is Array]', () => {
  51. let props = {
  52. stroke: [
  53. {
  54. percent: 50,
  55. color: '#fff',
  56. },
  57. {
  58. percent: 52,
  59. color: 'rgba(0, 0, 0, 0)',
  60. },
  61. ],
  62. strokeGradient: true,
  63. percent: 51,
  64. type: 'circle',
  65. };
  66. const p = getProgress(props);
  67. const _stroke = p
  68. .find('.semi-progress-circle-ring-inner')
  69. .at(0)
  70. .getDOMNode()
  71. .getAttribute('stroke');
  72. expect(_stroke).toEqual('#8080807f');
  73. });
  74. it('Gradient Accuracy [strokeGradient false & stroke type is Array]', () => {
  75. let props = {
  76. stroke: [
  77. {
  78. percent: 3,
  79. color: '#fff',
  80. },
  81. ],
  82. percent: 90,
  83. type: 'circle',
  84. };
  85. const p = getProgress(props);
  86. const _stroke = p
  87. .find('.semi-progress-circle-ring-inner')
  88. .at(0)
  89. .getDOMNode()
  90. .getAttribute('stroke');
  91. expect(_stroke).toEqual('#ffffffff');
  92. });
  93. it('Gradient Compatibility [strokeGradient true & stroke type is Array]', () => {
  94. let props = {
  95. stroke: [
  96. { percent: 0, color: 'red' },
  97. { percent: 10, color: '#b2140c' },
  98. { percent: 50, color: 'rgb(0, 99, 167)' },
  99. { percent: 100, color: 'hsla(125, 50%, 46% / 1)' },
  100. ],
  101. strokeGradient: true,
  102. percent: 55,
  103. type: 'circle',
  104. };
  105. const p = getProgress(props);
  106. const _stroke = p
  107. .find('.semi-progress-circle-ring-inner')
  108. .at(0)
  109. .getDOMNode()
  110. .getAttribute('stroke');
  111. expect(_stroke).toEqual('#066b9dff');
  112. });
  113. it('direction', () => {
  114. const p = getProgress({ direction: 'vertical' });
  115. expect(p.exists('.semi-progress-vertical')).toEqual(true);
  116. });
  117. it('format', () => {
  118. let props = {
  119. percent: 70,
  120. showInfo: true,
  121. format: () => 'semi',
  122. type: 'circle',
  123. };
  124. const p = getProgress(props);
  125. expect(p.find('.semi-progress-circle-text').text()).toEqual('semi');
  126. });
  127. it('strokeLinecap & strokeWidth', () => {
  128. let props = {
  129. percent: 70,
  130. strokeLinecap: 'square',
  131. type: 'circle',
  132. strokeWidth: 10,
  133. };
  134. const p = getProgress(props);
  135. let firstCircle = p
  136. .find('circle')
  137. .at(0)
  138. .getDOMNode();
  139. expect(firstCircle.getAttribute('stroke-linecap')).toEqual('square');
  140. expect(firstCircle.getAttribute('stroke-width')).toEqual('10');
  141. });
  142. it('width', () => {
  143. let props = {
  144. width: 120,
  145. type: 'circle',
  146. };
  147. const p = getProgress(props);
  148. let svgRing = p
  149. .find('.semi-progress-circle-ring')
  150. .at(0)
  151. .getDOMNode();
  152. expect(svgRing.getAttribute('width')).toEqual('120');
  153. });
  154. it('motion = false', () => {
  155. let props = {
  156. motion: false,
  157. percent: 70,
  158. showInfo: true,
  159. };
  160. const p = getProgress(props);
  161. expect(p.find('.semi-progress-line-text').text()).toEqual('70%');
  162. p.setProps({ percent: 80 });
  163. p.update();
  164. expect(p.find('.semi-progress-line-text').text()).toEqual('80%');
  165. p.unmount();
  166. });
  167. it('pass invalid percent like 101, -1', () => {
  168. let props = {
  169. percent: 101,
  170. showInfo: true,
  171. };
  172. const p = getProgress(props);
  173. expect(p.find('.semi-progress-line-text').text()).toEqual('100%');
  174. let minProps = {
  175. percent: -2,
  176. showInfo: true,
  177. };
  178. const minp = getProgress(minProps);
  179. expect(minp.find('.semi-progress-line-text').text()).toEqual('0%');
  180. });
  181. });