backtop.test.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { BackTop } from '../../index';
  2. import sinon from 'sinon';
  3. describe('BackTop', () => {
  4. beforeEach(() => {
  5. jest.spyOn(window, 'requestAnimationFrame').mockImplementation(cb => cb());
  6. });
  7. afterEach(() => {
  8. window.requestAnimationFrame.mockRestore();
  9. });
  10. it('test className & style', () => {
  11. const props = {
  12. className: 'test',
  13. style: {
  14. color: 'red',
  15. },
  16. visibilityHeight: -1,
  17. };
  18. const demo = mount(
  19. <div>
  20. <div style={{height: 1600, width: 300, background: 'grey'}}></div>
  21. <BackTop {...props}></BackTop>
  22. </div>
  23. );
  24. expect(demo.exists(`.semi-backtop.test`)).toEqual(true);
  25. expect(demo.find(`.semi-backtop`)).toHaveStyle('color', 'red');
  26. demo.unmount();
  27. });
  28. it('test onClick', () => {
  29. const onClick = sinon.spy(() => {});
  30. const props = {
  31. onClick,
  32. className: 'test',
  33. visibilityHeight: -1,
  34. };
  35. const demo = mount(
  36. <div>
  37. <div style={{height: 1600, width: 300, background: 'grey'}}></div>
  38. <BackTop {...props}></BackTop>
  39. </div>
  40. );
  41. demo.find('.semi-backtop.test').simulate('click');
  42. expect(onClick.calledOnce).toEqual(true);
  43. demo.unmount();
  44. });
  45. });