carousel.test.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import { Carousel } from '../../index';
  2. import { BASE_CLASS_PREFIX } from '../../../semi-foundation/base/constants';
  3. function getCarousel(carouselProps) {
  4. const contentStyle = {
  5. display:'flex',
  6. justifyContent: 'center',
  7. alignItems: 'center',
  8. color: '#fff',
  9. background: 'lightBlue',
  10. };
  11. return <Carousel style={{ width: '600px', height: '240px'}} {...carouselProps}>
  12. <div style={contentStyle}>
  13. <h3>index0</h3>
  14. </div>
  15. <div style={contentStyle}>
  16. <h3>index1</h3>
  17. </div>
  18. <div style={contentStyle}>
  19. <h3>index2</h3>
  20. </div>
  21. </Carousel>
  22. }
  23. function getSingleCarousel(carouselProps) {
  24. const contentStyle = {
  25. display:'flex',
  26. justifyContent: 'center',
  27. alignItems: 'center',
  28. color: '#fff',
  29. background: 'lightBlue',
  30. };
  31. return <Carousel style={{ width: '600px', height: '240px'}} {...carouselProps}>
  32. <div style={contentStyle}>
  33. <h3>index0</h3>
  34. </div>
  35. </Carousel>
  36. }
  37. describe('Carousel', () => {
  38. it('Carousel render basicly', () => {
  39. let props = {};
  40. const carousel = mount(getCarousel(props))
  41. expect(carousel.find(`.${BASE_CLASS_PREFIX}-carousel-content`).children().length).toEqual(3);
  42. expect(carousel.find(`.${BASE_CLASS_PREFIX}-carousel-content-item-active`).text()).toEqual('index0');
  43. carousel.unmount();
  44. });
  45. it('Carousel with custom className & style', () => {
  46. let props = {
  47. className: 'test',
  48. style: {
  49. color: 'red'
  50. }
  51. };
  52. const carousel = shallow(getCarousel(props));
  53. expect(carousel.exists('.test')).toEqual(true);
  54. expect(carousel.find('div.test')).toHaveStyle('color', 'red');
  55. });
  56. it('Carousel with defaultActiveIndex', () => {
  57. let props = {
  58. defaultActiveIndex: 2
  59. };
  60. const carousel = mount(getCarousel(props));
  61. const carouselContent = carousel.find(`.${BASE_CLASS_PREFIX}-carousel-content-item-active`).text();
  62. expect(carouselContent).toEqual('index2');
  63. });
  64. it('different theme', () => {
  65. let primary = mount(getCarousel({ theme: 'primary' }));
  66. let light = mount(getCarousel({ theme: 'light' }));
  67. let dark = mount(getCarousel({ theme: 'dark' }));
  68. expect(primary.exists(`.${BASE_CLASS_PREFIX}-carousel-arrow-primary`)).toEqual(true);
  69. expect(primary.exists(`.${BASE_CLASS_PREFIX}-carousel-indicator-item-primary`)).toEqual(true);
  70. expect(light.exists(`.${BASE_CLASS_PREFIX}-carousel-arrow-light`)).toEqual(true);
  71. expect(light.exists(`.${BASE_CLASS_PREFIX}-carousel-indicator-item-light`)).toEqual(true);
  72. expect(dark.exists(`.${BASE_CLASS_PREFIX}-carousel-arrow-dark`)).toEqual(true);
  73. expect(dark.exists(`.${BASE_CLASS_PREFIX}-carousel-indicator-item-dark`)).toEqual(true);
  74. });
  75. it('different indicator type', () => {
  76. let dot = mount(getCarousel({ indicatorType: 'dot' }));
  77. let line = mount(getCarousel({ indicatorType: 'line' }));
  78. let columnar = mount(getCarousel({ indicatorType: 'columnar' }));
  79. expect(dot.exists(`.${BASE_CLASS_PREFIX}-carousel-indicator-dot`)).toEqual(true);
  80. expect(dot.exists(`.${BASE_CLASS_PREFIX}-carousel-indicator-line`)).toEqual(false);
  81. expect(line.exists(`.${BASE_CLASS_PREFIX}-carousel-indicator-line`)).toEqual(true);
  82. expect(line.exists(`.${BASE_CLASS_PREFIX}-carousel-indicator-columnar`)).toEqual(false);
  83. expect(columnar.exists(`.${BASE_CLASS_PREFIX}-carousel-indicator-columnar`)).toEqual(true);
  84. expect(columnar.exists(`.${BASE_CLASS_PREFIX}-carousel-indicator-dot`)).toEqual(false);
  85. });
  86. it('different indicator position', () => {
  87. let left = mount(getCarousel({ indicatorPosition: 'left' }));
  88. let center = mount(getCarousel({ indicatorPosition: 'center' }));
  89. let right = mount(getCarousel({ indicatorPosition: 'right' }));
  90. expect(left.exists(`.${BASE_CLASS_PREFIX}-carousel-indicator-left`)).toEqual(true);
  91. expect(left.exists(`.${BASE_CLASS_PREFIX}-carousel-indicator-center`)).toEqual(false);
  92. expect(center.exists(`.${BASE_CLASS_PREFIX}-carousel-indicator-center`)).toEqual(true);
  93. expect(center.exists(`.${BASE_CLASS_PREFIX}-carousel-indicator-right`)).toEqual(false);
  94. expect(right.exists(`.${BASE_CLASS_PREFIX}-carousel-indicator-right`)).toEqual(true);
  95. expect(right.exists(`.${BASE_CLASS_PREFIX}-carousel-indicator-left`)).toEqual(false);
  96. });
  97. it('different indicator size', () => {
  98. let small = mount(getCarousel({ indicatorSize: 'small' }));
  99. let medium = mount(getCarousel({ indicatorSize: 'medium' }));
  100. expect(small.exists(`.${BASE_CLASS_PREFIX}-carousel-indicator-item-small`)).toEqual(true);
  101. expect(small.exists(`.${BASE_CLASS_PREFIX}-carousel-indicator-item-medium`)).toEqual(false);
  102. expect(medium.exists(`.${BASE_CLASS_PREFIX}-carousel-indicator-item-medium`)).toEqual(true);
  103. expect(medium.exists(`.${BASE_CLASS_PREFIX}-carousel-indicator-item-small`)).toEqual(false);
  104. });
  105. it('show arrow and arrow change', () => {
  106. let spyOnChange = sinon.spy(() => {})
  107. let show = mount(getCarousel({ onChange: spyOnChange }));
  108. let hide = mount(getCarousel({ showArrow: false }));
  109. let hover = mount(getCarousel({ arrowType: 'hover' }));
  110. expect(show.exists(`.${BASE_CLASS_PREFIX}-carousel-arrow`)).toEqual(true);
  111. expect(hide.exists(`.${BASE_CLASS_PREFIX}-carousel-arrow`)).toEqual(false);
  112. expect(hover.exists(`.${BASE_CLASS_PREFIX}-carousel-arrow-hover`)).toEqual(true);
  113. show.find(`.${BASE_CLASS_PREFIX}-carousel-arrow-prev`).simulate('click');
  114. expect(spyOnChange.calledOnce).toBe(true);
  115. expect(show.find(`.${BASE_CLASS_PREFIX}-carousel-content-item-active`).text()).toEqual('index2');
  116. show.find(`.${BASE_CLASS_PREFIX}-carousel-arrow-next`).simulate('click');
  117. expect(show.find(`.${BASE_CLASS_PREFIX}-carousel-content-item-active`).text()).toEqual('index0');
  118. });
  119. it('indicator change with click or trigger', () => {
  120. let spyOnChange = sinon.spy(() => {})
  121. let carousel = mount(getCarousel({ onChange: spyOnChange }));
  122. carousel.find(`.${BASE_CLASS_PREFIX}-carousel-indicator-item`).at(2).simulate('click');
  123. expect(spyOnChange.calledOnce).toBe(true);
  124. expect(carousel.find(`.${BASE_CLASS_PREFIX}-carousel-content-item-active`).text()).toEqual('index2');
  125. let spyOnChangeHover = sinon.spy(() => {})
  126. let carouselHover = mount(getCarousel({ onChange: spyOnChangeHover, trigger: 'hover' }));
  127. carouselHover.find(`.${BASE_CLASS_PREFIX}-carousel-indicator-item`).at(2).simulate('mouseEnter', {});
  128. expect(spyOnChangeHover.calledOnce).toBe(true);
  129. expect(carouselHover.find(`.${BASE_CLASS_PREFIX}-carousel-content-item-active`).text()).toEqual('index2');
  130. });
  131. it('single index', () => {
  132. let carousel = mount(getSingleCarousel({}));
  133. expect(carousel.exists(`.${BASE_CLASS_PREFIX}-carousel-indicator`)).toEqual(false);
  134. expect(carousel.exists(`.${BASE_CLASS_PREFIX}-carousel-arrow`)).toEqual(false);
  135. carousel.unmount();
  136. });
  137. })