From 53288d361c7167061bb42c399989da3ce1f6699a Mon Sep 17 00:00:00 2001 From: Paul Armstrong Date: Wed, 10 Feb 2021 20:08:58 -0800 Subject: [PATCH] test(web): AutoUpdatingCameraImage --- .../components/AutoUpdatingCameraImage.jsx | 2 +- .../AutoUpdatingCameraImage.test.jsx | 42 +++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 web/src/components/__tests__/AutoUpdatingCameraImage.test.jsx diff --git a/web/src/components/AutoUpdatingCameraImage.jsx b/web/src/components/AutoUpdatingCameraImage.jsx index ec1f70929..b5cd2e8d0 100644 --- a/web/src/components/AutoUpdatingCameraImage.jsx +++ b/web/src/components/AutoUpdatingCameraImage.jsx @@ -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); diff --git a/web/src/components/__tests__/AutoUpdatingCameraImage.test.jsx b/web/src/components/__tests__/AutoUpdatingCameraImage.test.jsx new file mode 100644 index 000000000..b302ee122 --- /dev/null +++ b/web/src/components/__tests__/AutoUpdatingCameraImage.test.jsx @@ -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
{searchParams}
; + } + return { + __esModule: true, + default: CameraImage, + }; +}); + +describe('AutoUpdatingCameraImage', () => { + let dateNowSpy; + beforeEach(() => { + dateNowSpy = jest.spyOn(Date, 'now').mockReturnValue(0); + }); + + test('shows FPS by default', async () => { + render(); + expect(screen.queryByText('Displaying at 0fps')).toBeInTheDocument(); + }); + + test('does not show FPS if turned off', async () => { + render(); + 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(); + mockOnload(); + jest.runAllTimers(); + expect(screen.queryByText('cache=100&foo')).toBeInTheDocument(); + }); +});