7

我已经为代码推送配置了一个应用程序,它运行良好,除了开玩笑测试。它无法针对此错误渲染应用程序:

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 />,
  );
});
4

4 回答 4

10

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);
于 2017-11-21T12:12:02.313 回答
4

Similar to what Tom Hall describes, this mock does work for me:

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;
});
于 2017-08-25T10:47:05.303 回答
1

在您的测试中,在您的 下方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()
    }));
});
于 2017-06-05T00:56:31.983 回答
0

你需要一个模型code-push才能工作,这条线CodePush.CheckFrequency.MANUAL总是会产生null.

于 2017-04-01T16:06:15.060 回答