0

我有一个创建反应应用程序项目。使用以下组件:

const LegalPage: React.FC = () => {
  const classes = useStyles()
  const { documentType } = useParams<{documentType: string}>()
  const [content, setContent] = useState<string>()

  const {t, i18n} = useTranslation()
  useEffect(() => {
    const loadFile = async () => {
      try {
      const response = await fetch(`/legalDocs/${i18n.language}/${documentType}.html`)
      const htmlContent = (response.status === 200) ? await response.text() :
        (`<p>Error reading ${documentType}. please try again, if issue persists please let us know at info@loanaid.ai</p>`)
      setContent(htmlContent)
      } catch (e) {
        console.error(e)
        setContent(`<p>Error reading ${documentType}. please try again, if issue persists please let us know at info@loanaid.ai</p>`)
      }
    }
    loadFile()
  }, [documentType, i18n.language])
  return (<Container className={classes.root}>
      <Typography variant="body1">
        {(content) ? parse(content) : ''}
      </Typography>
  </Container>
  )
}

export default LegalPage

我有 setupTests.ts

// setupTests.ts
import jestFetchMock from 'jest-fetch-mock'
jestFetchMock.enableMocks()

和测试:

const enTermsOfUse = '<div>terms of use</div>'
fetch.mockResponse({
  status: 200,
  text: () => Promise.resolve(enTermsOfUse)
})

test('loading privacy policy in bulgaria works ok ', async () =>{

  htmlContent = bgPrivacyPolicy
  param = 'privacy-policy'
  render(<LegalPage />)
})

LegalPage 中的 await fetch 如何始终返回 undefined。我做错了什么还是有错误?

4

0 回答 0