Code Push打破反应原生的开玩笑测试

Big*_*her 6 javascript jsx jestjs react-native react-native-code-push

我已经为代码推送配置了一个应用程序,除了开玩笑测试之外它运行良好.它无法为此错误呈现应用:

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)
Run Code Online (Sandbox Code Playgroud)

在这一行:

const codePushOptions = { checkFrequency: codePush.CheckFrequency.MANUAL };
Run Code Online (Sandbox Code Playgroud)

测试代码是:

import App from '../index.ios';

it('renders correctly', () => {
  const tree = renderer.create(
      <App />,
  );
});
Run Code Online (Sandbox Code Playgroud)

小智 9

与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;
});
Run Code Online (Sandbox Code Playgroud)

  • 为什么要咖喱化?它不会那样过去。修改为 `const cp = (app: any) =&gt; app;` 后就通过了。 (3认同)

小智 6

我在整合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;
Run Code Online (Sandbox Code Playgroud)

在我的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);
Run Code Online (Sandbox Code Playgroud)


Rem*_*rio 0

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