react-native-navigation的迁移库

Redux.test.js 1.9KB

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