|
| 1 | + |
| 2 | +import { render } from '@testing-library/react'; |
| 3 | +import fetchMock from 'jest-fetch-mock'; |
| 4 | +import * as VirtualVideo from '../VirtualVideo'; |
| 5 | + |
| 6 | + |
| 7 | +describe('VirtualVideo', () => { |
| 8 | + it('should call canPlayUrl and return false if no url specified', async () => { |
| 9 | + const canPlayType = jest.fn(); |
| 10 | + |
| 11 | + render(<VirtualVideo.VirtualVideo canPlayType={canPlayType} />); |
| 12 | + |
| 13 | + await new Promise(resolve => setTimeout(resolve, 10)); |
| 14 | + |
| 15 | + expect(canPlayType).toBeCalledWith(false); |
| 16 | + }); |
| 17 | + |
| 18 | + it('should call canPlayUrl and return true if valid url specified', async () => { |
| 19 | + const canPlayType = jest.fn(); |
| 20 | + |
| 21 | + render(<VirtualVideo.VirtualVideo src="https://app.heartex.ai/static/samples/opossum_snow.mp4" canPlayType={canPlayType} />); |
| 22 | + |
| 23 | + await new Promise(resolve => setTimeout(resolve, 10)); |
| 24 | + |
| 25 | + expect(canPlayType).toBeCalledWith(true); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should call canPlayUrl and return true if valid relative url specified', async () => { |
| 29 | + const canPlayType = jest.fn(); |
| 30 | + |
| 31 | + render(<VirtualVideo.VirtualVideo src="/files/opossum_intro.webm" canPlayType={canPlayType} />); |
| 32 | + |
| 33 | + await new Promise(resolve => setTimeout(resolve, 10)); |
| 34 | + |
| 35 | + expect(canPlayType).toBeCalledWith(true); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should call canPlayUrl and return true if valid url specified, even if content-type is binary/octet-stream', async () => { |
| 39 | + const canPlayType = jest.fn(); |
| 40 | + |
| 41 | + // return binary/octet-stream for all requests, mimicking the situation where |
| 42 | + // the server doesn't set the content-type header and defaults to binary/octet-stream |
| 43 | + fetchMock.mockResponseOnce('', { |
| 44 | + headers: { |
| 45 | + 'content-type': 'binary/octet-stream', |
| 46 | + }, |
| 47 | + }); |
| 48 | + |
| 49 | + render(<VirtualVideo.VirtualVideo src="https://app.heartex.ai/static/samples/opossum_snow.mp4" canPlayType={canPlayType} />); |
| 50 | + |
| 51 | + await new Promise(resolve => setTimeout(resolve, 10)); |
| 52 | + |
| 53 | + expect(canPlayType).toBeCalledWith(true); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should call canPlayUrl and return true if valid file is specified, and content-type is binary/octet-stream but no file extension', async () => { |
| 57 | + const canPlayType = jest.fn(); |
| 58 | + |
| 59 | + // return binary/octet-stream for all requests, mimicking the situation where |
| 60 | + // the server doesn't set the content-type header and defaults to binary/octet-stream |
| 61 | + fetchMock.mockResponseOnce('', { |
| 62 | + headers: { |
| 63 | + 'content-type': 'binary/octet-stream', |
| 64 | + }, |
| 65 | + }); |
| 66 | + |
| 67 | + render(<VirtualVideo.VirtualVideo src="https://app.heartex.ai/static/samples/opossum_snow" canPlayType={canPlayType} />); |
| 68 | + |
| 69 | + await new Promise(resolve => setTimeout(resolve, 10)); |
| 70 | + |
| 71 | + expect(canPlayType).toBeCalledWith(true); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should call canPlayUrl and return false if invalid url specified', async () => { |
| 75 | + const canPlayType = jest.fn(); |
| 76 | + |
| 77 | + render(<VirtualVideo.VirtualVideo src="https://app.heartex.ai/static/samples/opossum_snow.avi" canPlayType={canPlayType} />); |
| 78 | + |
| 79 | + await new Promise(resolve => setTimeout(resolve, 10)); |
| 80 | + |
| 81 | + expect(canPlayType).toBeCalledWith(false); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should call canPlayUrl and return false if invalid url specified, even if content-type is binary/octet-stream', async () => { |
| 85 | + const canPlayType = jest.fn(); |
| 86 | + |
| 87 | + // return binary/octet-stream for all requests, mimicking the situation where |
| 88 | + // the server doesn't set the content-type header and defaults to binary/octet-stream |
| 89 | + fetchMock.mockResponseOnce('', { |
| 90 | + headers: { |
| 91 | + 'content-type': 'binary/octet-stream', |
| 92 | + }, |
| 93 | + }); |
| 94 | + |
| 95 | + render(<VirtualVideo.VirtualVideo src="https://app.heartex.ai/static/samples/opossum_snow.avi" canPlayType={canPlayType} />); |
| 96 | + |
| 97 | + await new Promise(resolve => setTimeout(resolve, 10)); |
| 98 | + |
| 99 | + expect(canPlayType).toBeCalledWith(false); |
| 100 | + }); |
| 101 | +}); |
0 commit comments