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

时间:2017-03-27 11:17:20

标签: javascript react-native jsx jestjs 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)

在这一行:

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

测试代码是:

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

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

4 个答案:

答案 0 :(得分:6)

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

  1. 创建文件__mocks__/react-native-code-push.js
  2. 将以下代码添加到其中:

    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);
    

答案 1 :(得分: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()
    }));
});

答案 2 :(得分:1)

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;
});

答案 3 :(得分:0)

您需要模拟code-push才能工作,此行CodePush.CheckFrequency.MANUAL将始终生成null