Skip to content

Commit

Permalink
Merge pull request #283 from keyasuda/fix/warnings
Browse files Browse the repository at this point in the history
テスト時の警告解消
  • Loading branch information
keyasuda authored Jan 2, 2025
2 parents 8b3d301 + 515d309 commit de35797
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 25 deletions.
20 changes: 13 additions & 7 deletions src/component/SignInButton.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
TestingLibraryElementError,
} from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { act } from '@testing-library/react'

import * as authModule from './authentication'
import SignInButton from './SignInButton'
Expand All @@ -20,12 +21,14 @@ describe('SignInButton', () => {
signIn.mockResolvedValue()
})

const renderIt = () => {
render(
<SignInButton afterSignedIn={afterSignedIn}>
<div>children</div>
</SignInButton>
)
const renderIt = async () => {
await act(async () => {
render(
<SignInButton afterSignedIn={afterSignedIn}>
<div>children</div>
</SignInButton>
)
})
}

describe('signed out', () => {
Expand All @@ -41,7 +44,10 @@ describe('SignInButton', () => {
initAuth.mockImplementationOnce(() => false)
isSignedIn.mockImplementationOnce(() => true)
await renderIt()
await userEvent.click(screen.getByRole('button'))

await act(async () => {
await userEvent.click(screen.getByRole('button'))
})

expect(signIn).toHaveBeenCalled()
})
Expand Down
9 changes: 7 additions & 2 deletions src/component/SignInButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ const SignInButton = (props) => {
let [signedIn, setSignedIn] = useState(false)

useEffect(async () => {
setSignedIn(await initAuth())
setLoaded(true)
const init = async () => {
const authStatus = await initAuth()
setSignedIn(authStatus)
setLoaded(true)
}

init()

window.addEventListener('focus', () => setSignedIn(isSignedIn()), false)
}, [])
Expand Down
29 changes: 21 additions & 8 deletions src/component/app/AppBar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,22 @@ describe('AppBar', () => {
id: 'belonginguuid',
})

beforeEach(() => {
beforeEach(async () => {
// async を追加
renderIt('')
const button = screen.getByLabelText('scan')
userEvent.click(button)
// クリックイベントを act でラップ
await act(async () => {
await userEvent.click(button)
})
})

describe('code is of belonging', () => {
it('should navigate to belonging page', () => {
act(() => codeReaderOnRead(code))
it('should navigate to belonging page', async () => {
// async を追加
await act(async () => {
codeReaderOnRead(code)
})
expect(history.push).toHaveBeenCalledWith(
'/app/file-id/belongings/belonginguuid'
)
Expand All @@ -148,8 +155,11 @@ describe('AppBar', () => {
id: 'storageuuid',
})

it('should navigate to storage page', () => {
act(() => codeReaderOnRead(code))
it('should navigate to storage page', async () => {
// async を追加
await act(async () => {
codeReaderOnRead(code)
})
expect(history.push).toHaveBeenCalledWith(
'/app/file-id/storages/storageuuid'
)
Expand All @@ -159,8 +169,11 @@ describe('AppBar', () => {
describe('code is unknown', () => {
const codes = ['generalpurposebarcode', '1234567890']
codes.map((code) => {
it('should navigate to belonging page, code as ID', () => {
act(() => codeReaderOnRead(code))
it('should navigate to belonging page, code as ID', async () => {
// async を追加
await act(async () => {
codeReaderOnRead(code)
})
expect(history.push).toHaveBeenCalledWith(
`/app/file-id/belongings/${code}`
)
Expand Down
6 changes: 3 additions & 3 deletions src/component/app/AppBar.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useEffect, useState, useRef } from 'react'
import { useParams, useHistory, Link } from 'react-router-dom'
import { useDispatch, useSelector } from 'react-redux'
import { fade, makeStyles } from '@material-ui/core/styles'
import { alpha, makeStyles } from '@material-ui/core/styles'
import { default as MUIAppBar } from '@material-ui/core/AppBar'
import Toolbar from '@material-ui/core/Toolbar'
import InputBase from '@material-ui/core/InputBase'
Expand Down Expand Up @@ -31,9 +31,9 @@ const AppBar = (props) => {
search: {
position: 'relative',
borderRadius: theme.shape.borderRadius,
backgroundColor: fade(theme.palette.common.white, 0.15),
backgroundColor: alpha(theme.palette.common.white, 0.15),
'&:hover': {
backgroundColor: fade(theme.palette.common.white, 0.25),
backgroundColor: alpha(theme.palette.common.white, 0.25),
},
marginRight: 0,
marginLeft: 0,
Expand Down
9 changes: 8 additions & 1 deletion src/component/app/belongings/Belongings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,14 @@ const Belongings = (props) => {
>
<Icon>add</Icon>
</Fab>
<Dialog open={dialogOpen} onClose={handleClose} disableBackdropClick>
<Dialog
open={dialogOpen}
onClose={(event, reason) => {
if (reason !== 'backdropClick') {
handleClose()
}
}}
>
<DialogTitle>新しい物品を作成</DialogTitle>
<DialogContent>
<DialogContentText component="div">
Expand Down
9 changes: 8 additions & 1 deletion src/component/app/belongings/EditDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,14 @@ const EditDialog = (props) => {

return (
<>
<Dialog open={open} onClose={handleClose} disableBackdropClick>
<Dialog
open={open}
onClose={(event, reason) => {
if (reason !== 'backdropClick') {
handleClose()
}
}}
>
<DialogTitle>
{newItem && <>物品の追加</>}
{!newItem && <>物品の編集</>}
Expand Down
9 changes: 8 additions & 1 deletion src/component/app/print/PrintQueue.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,14 @@ const PrintQueue = (props) => {
<Sheets items={items} ref={sheetRef} />
</div>

<Dialog open={dialogOpen} onClose={handleClose} disableBackdropClick>
<Dialog
open={dialogOpen}
onClose={(event, reason) => {
if (reason !== 'backdropClick') {
handleClose()
}
}}
>
<DialogTitle>印刷済みコード</DialogTitle>
<DialogContent>
<DialogContentText>
Expand Down
9 changes: 8 additions & 1 deletion src/component/app/storages/EditDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@ const EditDialog = (props) => {
const [printed, setPrinted] = useState(item.printed)

return (
<Dialog open={open} onClose={handleClose} disableBackdropClick>
<Dialog
open={dialogOpen}
onClose={(event, reason) => {
if (reason !== 'backdropClick') {
handleClose()
}
}}
>
<DialogTitle>保管場所の編集</DialogTitle>
<DialogContent>
<DialogContentText component="div">
Expand Down
9 changes: 8 additions & 1 deletion src/component/app/storages/Storages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,14 @@ const Storages = (props) => {
>
<Icon>add</Icon>
</Fab>
<Dialog open={dialogOpen} onClose={handleClose} disableBackdropClick>
<Dialog
open={dialogOpen}
onClose={(event, reason) => {
if (reason !== 'backdropClick') {
handleClose()
}
}}
>
<DialogTitle>新しい保管場所を作成</DialogTitle>
<DialogContent>
<DialogContentText component="div">
Expand Down

0 comments on commit de35797

Please sign in to comment.