Skip to content

Commit

Permalink
Merge pull request #279 from keyasuda/fix/camera-permisson
Browse files Browse the repository at this point in the history
カメラのパーミションダイアログが出ない端末があるのでnavigator.mediaDevices.getUserMediaを明示的に一度呼んでやる
  • Loading branch information
keyasuda authored Dec 30, 2024
2 parents a0e1767 + 7131939 commit 8aa7087
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/component/app/AppBar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@ const MockCodeReader = (props) => {
return <>code reader</>
}

// navigator.mediaDevicesのモック
Object.defineProperty(global.navigator, 'mediaDevices', {
value: {
getUserMedia: jest.fn().mockImplementation(() =>
Promise.resolve({
getTracks: () => [
{
stop: () => {},
},
],
})
),
},
writable: true,
})

jest.mock('./CodeReader', () => ({
__esModule: true,
namedExport: jest.fn(),
Expand Down
9 changes: 8 additions & 1 deletion src/component/app/AppBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,14 @@ const AppBar = (props) => {
edge="end"
color="inherit"
aria-label="scan"
onClick={() => setOpenScanner(true)}
onClick={async () => {
const stream = await navigator.mediaDevices.getUserMedia({
video: true,
})
stream.getTracks().forEach((track) => track.stop())

setOpenScanner(true)
}}
>
<Icon>qr_code_scanner</Icon>
</IconButton>
Expand Down

0 comments on commit 8aa7087

Please sign in to comment.