react-native-navigation的迁移库

Redux.test.js 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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('passes props into wrapped components', () => {
  28. const renderCountIncrement = jest.fn();
  29. const HOC = class extends React.Component {
  30. render() {
  31. return (
  32. <Provider store={store.reduxStore}>
  33. <MyConnectedComponent {...this.props}/>
  34. </Provider>
  35. );
  36. }
  37. };
  38. const CompFromNavigation = Navigation.registerComponent('ComponentName', () => HOC);
  39. const tree = renderer.create(<CompFromNavigation componentId='componentId' renderCountIncrement={renderCountIncrement}/>);
  40. expect(tree.toJSON().children).toEqual(['no name']);
  41. expect(renderCountIncrement).toHaveBeenCalledTimes(1);
  42. });
  43. it('rerenders as a result of an underlying state change (by selector)', () => {
  44. const renderCountIncrement = jest.fn();
  45. const tree = renderer.create(
  46. <Provider store={store.reduxStore}>
  47. <MyConnectedComponent renderCountIncrement={renderCountIncrement} />
  48. </Provider>
  49. );
  50. expect(tree.toJSON().children).toEqual(['no name']);
  51. expect(renderCountIncrement).toHaveBeenCalledTimes(1);
  52. store.reduxStore.dispatch({ type: 'redux.MyStore.setName', name: 'Bob' });
  53. expect(store.selectors.getName(store.reduxStore.getState())).toEqual('Bob');
  54. expect(tree.toJSON().children).toEqual(['Bob']);
  55. expect(renderCountIncrement).toHaveBeenCalledTimes(2);
  56. });
  57. it('rerenders as a result of an underlying state change with a new key', () => {
  58. const renderCountIncrement = jest.fn();
  59. const tree = renderer.create(
  60. <Provider store={store.reduxStore}>
  61. <MyConnectedComponent printAge={true} renderCountIncrement={renderCountIncrement} />
  62. </Provider>
  63. );
  64. expect(tree.toJSON().children).toEqual(null);
  65. expect(renderCountIncrement).toHaveBeenCalledTimes(1);
  66. store.reduxStore.dispatch({ type: 'redux.MyStore.setAge', age: 30 });
  67. expect(store.selectors.getAge(store.reduxStore.getState())).toEqual(30);
  68. expect(tree.toJSON().children).toEqual(['30']);
  69. expect(renderCountIncrement).toHaveBeenCalledTimes(2);
  70. });
  71. });