react-native-navigation的迁移库

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. const React = require('react');
  2. require('react-native');
  3. const renderer = require('react-test-renderer');
  4. const { Provider } = require('react-redux');
  5. const { Navigation } = require('../../lib/dist/index');
  6. describe('redux support', () => {
  7. let MyConnectedComponent;
  8. let store;
  9. beforeEach(() => {
  10. MyConnectedComponent = require('./MyComponent');
  11. store = require('./MyStore');
  12. });
  13. it('renders normally', () => {
  14. const HOC = class extends React.Component {
  15. render() {
  16. return (
  17. <Provider store={store.reduxStore}>
  18. <MyConnectedComponent />
  19. </Provider>
  20. );
  21. }
  22. };
  23. Navigation.registerComponent('ComponentName', () => HOC);
  24. const tree = renderer.create(<HOC />);
  25. expect(tree.toJSON().children).toEqual(['no name']);
  26. });
  27. it('rerenders as a result of an underlying state change (by selector)', () => {
  28. const renderCountIncrement = jest.fn();
  29. const tree = renderer.create(
  30. <Provider store={store.reduxStore}>
  31. <MyConnectedComponent renderCountIncrement={renderCountIncrement} />
  32. </Provider>
  33. );
  34. expect(tree.toJSON().children).toEqual(['no name']);
  35. expect(renderCountIncrement).toHaveBeenCalledTimes(1);
  36. store.reduxStore.dispatch({ type: 'redux.MyStore.setName', name: 'Bob' });
  37. expect(store.selectors.getName(store.reduxStore.getState())).toEqual('Bob');
  38. expect(tree.toJSON().children).toEqual(['Bob']);
  39. expect(renderCountIncrement).toHaveBeenCalledTimes(2);
  40. });
  41. it('rerenders as a result of an underlying state change with a new key', () => {
  42. const renderCountIncrement = jest.fn();
  43. const tree = renderer.create(
  44. <Provider store={store.reduxStore}>
  45. <MyConnectedComponent printAge={true} renderCountIncrement={renderCountIncrement} />
  46. </Provider>
  47. );
  48. expect(tree.toJSON().children).toEqual(null);
  49. expect(renderCountIncrement).toHaveBeenCalledTimes(1);
  50. store.reduxStore.dispatch({ type: 'redux.MyStore.setAge', age: 30 });
  51. expect(store.selectors.getAge(store.reduxStore.getState())).toEqual(30);
  52. expect(tree.toJSON().children).toEqual(['30']);
  53. expect(renderCountIncrement).toHaveBeenCalledTimes(2);
  54. });
  55. });