测试 React Native Image.getSize:无法监视原始值;给定的字符串

发布于 2025-01-10 21:14:26 字数 1545 浏览 0 评论 0原文

我有一个钩子可以检测 React Native Image 的方向:

import { useState, useEffect } from 'react'
import { Image } from 'react-native'

const useFindImageSize = (image) => {
  const [width, setWidth] = useState(0)
  const [height, setHeight] = useState(0)

  useEffect(() => {
    (async() => {
      await Image.getSize(image,
        (width, height) => {
          setWidth(width)
          setHeight(height)
        })
    })()
  }, [image])

  return { width, height }
}

并且最初编写了一个测试来查看 getSize 是否已被调用:

import { Image } from 'react-native'
import { renderHook } from '@testing-library/react-hooks'
import useFindImageSize from '../useFindImageSize'

describe('useFindImageSize', () => {
  const getSize = jest.spyOn(
    Image, 'getSize').mockImplementation(jest.fn()
  )

  afterEach(() => {
    jest.clearAllMocks()
  })

  it('should call getSize', () => {
    const { result } = renderHook(() =>
      useFindImageSize('test_image')
    )
    expect(getSize).toHaveBeenCalledTimes(1)
  })
})

一个我认为可行的基本测试,(这是基于 这关于同一主题的问题/答案)。

但我在运行测试时遇到此错误:

 ● ImageOrientation › useFindImageSize › encountered a declaration exception
Cannot spyOn on a primitive value; string given

Whichrefers to const getSizeSpyOn = jest.spyOn(

该钩子将图像 uri 作为其参数,然后决定它应该是纵向还是横向图像,但我很困惑如何解决这个问题,有人知道我做错了什么吗?

I have a hook that detects the orientation of a React Native Image:

import { useState, useEffect } from 'react'
import { Image } from 'react-native'

const useFindImageSize = (image) => {
  const [width, setWidth] = useState(0)
  const [height, setHeight] = useState(0)

  useEffect(() => {
    (async() => {
      await Image.getSize(image,
        (width, height) => {
          setWidth(width)
          setHeight(height)
        })
    })()
  }, [image])

  return { width, height }
}

And have written a test initially to see if getSize has been called:

import { Image } from 'react-native'
import { renderHook } from '@testing-library/react-hooks'
import useFindImageSize from '../useFindImageSize'

describe('useFindImageSize', () => {
  const getSize = jest.spyOn(
    Image, 'getSize').mockImplementation(jest.fn()
  )

  afterEach(() => {
    jest.clearAllMocks()
  })

  it('should call getSize', () => {
    const { result } = renderHook(() =>
      useFindImageSize('test_image')
    )
    expect(getSize).toHaveBeenCalledTimes(1)
  })
})

A basic test which I would've thought would work, (this is based on this question/answer about the same topic).

But I'm getting this error when running my test:

 ● ImageOrientation › useFindImageSize › encountered a declaration exception
Cannot spyOn on a primitive value; string given

Which refers to const getSizeSpyOn = jest.spyOn(

The hook takes an image uri as its argument and then decides whether it should be a portrait or landscape image, but I'm pretty stuck on how to get around this. Anyone know what I'm doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愚人国度 2025-01-17 21:14:26

尝试类似的方法:

const getSize = jest.spyOn(Image.prototype, 'getSize')
  .mockImplementation(jest.fn())

与您的代码相同,仅添加“.prototype”(不带引号)。

Try something like:

const getSize = jest.spyOn(Image.prototype, 'getSize')
  .mockImplementation(jest.fn())

Same as your code, with just ".prototype" added (without quotes).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文