如何在Jest测试中模拟StatusBarManager

如何在Jest测试中模拟StatusBarManager

本文介绍了如何在Jest测试中模拟StatusBarManager.getHeight?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用34号博览会和 react-native-ui-lib (来自wix),并且在为我的组件设置笑话测试时遇到了问题.该问题似乎出现在 wix库的链接

I'm using expo 34 and react-native-ui-lib from wix and have a problem to setup jest tests for my components. The problem looks to appear in link of the wix library

function setStatusBarHeight() {
  statusBarHeight = isIOS ? 20 : StatusBarManager.HEIGHT;
  if (isIOS) {
    // override guesstimate height with the actual height from StatusBarManager
    StatusBarManager.getHeight(data => (statusBarHeight = data.height));
  }
}

TypeError:StatusBarManager.getHeight不是函数

TypeError: StatusBarManager.getHeight is not a function

有什么办法嘲笑StatusBarManager.getHeight吗?

我尝试在根文件夹中创建jest-setup.js

I've tried to create jest-setup.js in my root folder

import { NativeModules } from 'react-native';

NativeModules.StatusBarManager = {getHeight: jest.fn()};

// mock native modules
jest.mock('@react-native-community/blur', () => {});

但是没有用.我目前的jest.config.js

But it didn't work.My current jest.config.js

module.exports = {
    preset: "jest-expo",
    moduleFileExtensions: ['js','jsx','json', 'ts', 'tsx'],
    transform: {
      "^.+\\.(js|jsx|ts|tsx)$": "babel-jest"
    },
    testMatch: [
      "**/*.test.ts?(x)"
    ],
  }

推荐答案

我能够使用此代码解决此问题

I was able to solve this problem with this code

import React from 'react'
import 'react-native'
import { NativeModules } from 'react-native' // !this module
import renderer from 'react-test-renderer'
import Description from './Description'

// and this mock
NativeModules.StatusBarManager = {getHeight: jest.fn()}

describe('TextArea test', () => {
  it('Empty TextArea with title', () => {
    const description = renderer.create(
    <Description {...{

这篇关于如何在Jest测试中模拟StatusBarManager.getHeight?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 05:15