forked from blakeblackshear/frigate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Testing Image and Canvas calls requires a lot of heavy dependencies, so this skips that part of the tests
- Loading branch information
1 parent
a202c44
commit 1aa9a7a
Showing
4 changed files
with
73 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { h } from 'preact'; | ||
import * as Api from '../../api'; | ||
import * as Hooks from '../../hooks'; | ||
import CameraImage from '../CameraImage'; | ||
import { render, screen } from '@testing-library/preact'; | ||
|
||
jest.mock('../../api/baseUrl'); | ||
|
||
describe('CameraImage', () => { | ||
beforeEach(() => { | ||
jest.spyOn(Api, 'useConfig').mockImplementation(() => { | ||
return { data: { cameras: { front: { name: 'front', width: 1280, height: 720 } } } }; | ||
}); | ||
jest.spyOn(Api, 'useApiHost').mockReturnValue('http:https://base-url.local:5000'); | ||
jest.spyOn(Hooks, 'useResizeObserver').mockImplementation(() => [{ width: 0 }]); | ||
}); | ||
|
||
test('renders an activity indicator while loading', async () => { | ||
render(<CameraImage camera="front" />); | ||
expect(screen.queryByLabelText('Loading…')).toBeInTheDocument(); | ||
}); | ||
|
||
test('creates a scaled canvas using the available width & height, preserving camera aspect ratio', async () => { | ||
jest.spyOn(Hooks, 'useResizeObserver').mockReturnValueOnce([{ width: 720 }]); | ||
|
||
render(<CameraImage camera="front" />); | ||
expect(screen.queryByLabelText('Loading…')).toBeInTheDocument(); | ||
expect(screen.queryByTestId('cameraimage-canvas')).toMatchInlineSnapshot(` | ||
<canvas | ||
data-testid="cameraimage-canvas" | ||
height="405" | ||
width="720" | ||
/> | ||
`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { useEffect, useMemo, useState } from 'preact/hooks'; | ||
|
||
export function useResizeObserver(...refs) { | ||
const [dimensions, setDimensions] = useState( | ||
new Array(refs.length).fill({ width: 0, height: 0, x: -Infinity, y: -Infinity }) | ||
); | ||
const resizeObserver = useMemo( | ||
() => | ||
new ResizeObserver((entries) => { | ||
window.requestAnimationFrame(() => { | ||
setDimensions(entries.map((entry) => entry.contentRect)); | ||
}); | ||
}), | ||
[] | ||
); | ||
|
||
useEffect(() => { | ||
refs.forEach((ref) => { | ||
resizeObserver.observe(ref.current); | ||
}); | ||
|
||
return () => { | ||
refs.forEach((ref) => { | ||
resizeObserver.unobserve(ref.current); | ||
}); | ||
}; | ||
}, [refs, resizeObserver]); | ||
|
||
return dimensions; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters