test(web): AutoUpdatingCameraImage

This commit is contained in:
Paul Armstrong 2021-02-10 20:08:58 -08:00 committed by Blake Blackshear
parent e729bd52aa
commit 53288d361c
2 changed files with 43 additions and 1 deletions

View File

@ -4,7 +4,7 @@ import { useCallback, useState } from 'preact/hooks';
const MIN_LOAD_TIMEOUT_MS = 200;
export default function AutoUpdatingCameraImage({ camera, searchParams, showFps = true }) {
export default function AutoUpdatingCameraImage({ camera, searchParams = '', showFps = true }) {
const [key, setKey] = useState(Date.now());
const [fps, setFps] = useState(0);

View File

@ -0,0 +1,42 @@
import { h } from 'preact';
import AutoUpdatingCameraImage from '../AutoUpdatingCameraImage';
import { screen, render } from '@testing-library/preact';
let mockOnload;
jest.mock('../CameraImage', () => {
function CameraImage({ onload, searchParams }) {
mockOnload = () => {
onload();
};
return <div data-testid="camera-image">{searchParams}</div>;
}
return {
__esModule: true,
default: CameraImage,
};
});
describe('AutoUpdatingCameraImage', () => {
let dateNowSpy;
beforeEach(() => {
dateNowSpy = jest.spyOn(Date, 'now').mockReturnValue(0);
});
test('shows FPS by default', async () => {
render(<AutoUpdatingCameraImage camera="tacos" />);
expect(screen.queryByText('Displaying at 0fps')).toBeInTheDocument();
});
test('does not show FPS if turned off', async () => {
render(<AutoUpdatingCameraImage camera="tacos" showFps={false} />);
expect(screen.queryByText('Displaying at 0fps')).not.toBeInTheDocument();
});
test('on load, sets a new cache key to search params', async () => {
dateNowSpy.mockReturnValueOnce(100).mockReturnValueOnce(200).mockReturnValueOnce(300);
render(<AutoUpdatingCameraImage camera="tacos" searchParams="foo" />);
mockOnload();
jest.runAllTimers();
expect(screen.queryByText('cache=100&foo')).toBeInTheDocument();
});
});