代码推送 用于反应本机的开玩笑测试



我已经配置了一个用于代码推送的应用程序,除了开玩笑的测试外,它运行良好。由于此错误,它在渲染应用程序中失败:

TypeError: Cannot read property 'CheckFrequency' of undefined
  at Object.<anonymous> (app/index.js:7:66)
  at Object.<anonymous> (index.ios.js:5:12)
  at Object.<anonymous> (__tests__/index.ios.js:4:12)

在这一行中:

const codePushOptions = { checkFrequency: codePush.CheckFrequency.MANUAL };

测试代码为:

import App from '../index.ios';
it('renders correctly', () => {
  const tree = renderer.create(
      <App />,
  );
});
我在将

codePush集成到我目前正在处理的 React Native 应用程序中时遇到了这个问题。对我有用的是:

  1. 创建文件__mocks__/react-native-code-push.js .

向其添加以下代码:

const codePush = {
  InstallMode: {ON_NEXT_RESTART: 'ON_APP_RESTART'},
  CheckFrequency: {ON_APP_RESUME: 'ON_APP_RESUME'}
};
const cb = _ => app => app;
Object.assign(cb, codePush);
export default cb;

在我的index.js文件中,我有:

import codePush from 'react-native-code-push';
import MyApp from './src/'
const codePushOptions = {
  installMode: codePush.InstallMode.ON_NEXT_RESTART,
  checkFrequency: codePush.CheckFrequency.ON_APP_RESUME
};
export default codePush(codePushOptions)(MyApp);

与Tom Hall描述的类似,这个模拟确实对我有用:

jest.mock('react-native-code-push', () => {
  const cp = (_: any) => (app: any) => app;
  Object.assign(cp, {
    InstallMode: {},
    CheckFrequency: {},
    SyncStatus: {},
    UpdateState: {},
    DeploymentStatus: {},
    DEFAULT_UPDATE_DIALOG: {},
    checkForUpdate: jest.fn(),
    codePushify: jest.fn(),
    getConfiguration: jest.fn(),
    getCurrentPackage: jest.fn(),
    getUpdateMetadata: jest.fn(),
    log: jest.fn(),
    notifyAppReady: jest.fn(),
    notifyApplicationReady: jest.fn(),
    sync: jest.fn(),
  });
  return cp;
});

在测试中,在import App from '../index.ios'; 下方添加以下内容:

jest.mock('react-native-code-push', () => {
    return jest.fn(() => ({
        InstallMode: jest.fn(),
        CheckFrequency: jest.fn(),
        CodePushComponent: jest.fn(),
        codePushify: jest.fn()
    }));
});
您需要

一个模型才能code-push工作,此行CodePush.CheckFrequency.MANUAL将始终产生null

最新更新