autoplayVideo.spec.tsx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. // Use a proxy to prevent <video ref={ref}/> from overriding our ref.current
  11. const makeProxyMock = (video: Partial<HTMLVideoElement>) => {
  12. return new Proxy(
  13. {current: video},
  14. {
  15. get(obj, prop) {
  16. return obj[prop];
  17. },
  18. set(obj, prop) {
  19. if (prop === 'current') {
  20. obj.current = obj.current;
  21. }
  22. return true;
  23. },
  24. }
  25. );
  26. };
  27. describe('autoplayVideo', () => {
  28. it('sets mute and calls play', () => {
  29. const mock = makeProxyMock({
  30. muted: false,
  31. play: jest.fn().mockReturnValue(Promise.resolve()),
  32. });
  33. // @ts-ignore we are mocking useRef
  34. React.useRef.mockImplementation(() => mock);
  35. render(<AutoplayVideo aria-label="video" src="https://example.com/video.mp4" />);
  36. expect(screen.getByLabelText('video')).toBeInTheDocument();
  37. expect(mock.current.muted).toBe(true);
  38. expect(mock.current.play).toHaveBeenCalledTimes(1);
  39. });
  40. it('handles non promise-like return from play', () => {
  41. const mock = makeProxyMock({
  42. muted: false,
  43. play: jest.fn().mockReturnValue(null),
  44. });
  45. // @ts-ignore we are mocking useRef
  46. React.useRef.mockImplementation(() => mock);
  47. render(<AutoplayVideo aria-label="video" src="https://example.com/video.mp4" />);
  48. expect(screen.getByLabelText('video')).toBeInTheDocument();
  49. expect(mock.current.muted).toBe(true);
  50. // Seems that useEffect runs, so no mocking or tick is needed.
  51. // Was tested manually by removing the ?.catch safe access and the test fails
  52. expect(mock.current.play).toHaveBeenCalledTimes(1);
  53. });
  54. });