autoplayVideo.spec.tsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import * as React from 'react';
  2. import {render, screen} from 'sentry-test/reactTestingLibrary';
  3. import {AutoplayVideo} from 'sentry/components/autoplayVideo';
  4. jest.mock('react', () => {
  5. return {
  6. ...jest.requireActual('react'),
  7. useRef: jest.fn(),
  8. };
  9. });
  10. // XXX: Mocking useRef throws an error for AnimatePrecense, so it must be mocked as well
  11. jest.mock('framer-motion', () => {
  12. return {
  13. ...jest.requireActual('framer-motion'),
  14. AnimatePresence: jest.fn().mockImplementation(({children}) => <div>{children}</div>),
  15. };
  16. });
  17. // Use a proxy to prevent <video ref={ref}/> from overriding our ref.current
  18. const makeProxyMock = (video: Partial<HTMLVideoElement>) => {
  19. return new Proxy(
  20. {current: video},
  21. {
  22. get(obj, prop) {
  23. return obj[prop];
  24. },
  25. set(_obj, _prop) {
  26. return true;
  27. },
  28. }
  29. );
  30. };
  31. describe('autoplayVideo', () => {
  32. it('sets mute and calls play', () => {
  33. const mock = makeProxyMock({
  34. muted: false,
  35. play: jest.fn().mockReturnValue(Promise.resolve()),
  36. });
  37. // @ts-expect-error we are mocking useRef
  38. React.useRef.mockImplementation(() => mock);
  39. render(<AutoplayVideo aria-label="video" src="https://example.com/video.mp4" />);
  40. expect(screen.getByLabelText('video')).toBeInTheDocument();
  41. expect(mock.current.muted).toBe(true);
  42. expect(mock.current.play).toHaveBeenCalledTimes(1);
  43. });
  44. it('handles non promise-like return from play', () => {
  45. const mock = makeProxyMock({
  46. muted: false,
  47. play: jest.fn().mockReturnValue(null),
  48. });
  49. // @ts-expect-error we are mocking useRef
  50. React.useRef.mockImplementation(() => mock);
  51. render(<AutoplayVideo aria-label="video" src="https://example.com/video.mp4" />);
  52. expect(screen.getByLabelText('video')).toBeInTheDocument();
  53. expect(mock.current.muted).toBe(true);
  54. // Seems that useEffect runs, so no mocking or tick is needed.
  55. // Was tested manually by removing the ?.catch safe access and the test fails
  56. expect(mock.current.play).toHaveBeenCalledTimes(1);
  57. });
  58. });