react-native-navigation的迁移库

ScreenRegistry.test.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import React, {Component} from 'react';
  2. import {AppRegistry, Text} from 'react-native';
  3. import renderer from 'react-test-renderer';
  4. describe('ComponentRegistry', () => {
  5. let uut;
  6. let myScreenRef;
  7. let testParentRef;
  8. let screenStore;
  9. class MyScreen extends Component {
  10. constructor(props) {
  11. super(props);
  12. myScreenRef = this; //eslint-disable-line
  13. }
  14. render() {
  15. return <Text>{'Hello, World!'}</Text>;
  16. }
  17. }
  18. class TestParent extends Component { //eslint-disable-line
  19. constructor(props) {
  20. super(props);
  21. testParentRef = this; //eslint-disable-line
  22. this.ChildClass = props.ChildClass;
  23. this.state = {propsFromState: {}};
  24. }
  25. render() {
  26. const Child = this.ChildClass;
  27. return (
  28. <Child screenId="screen1" {...this.state.propsFromState}/>
  29. );
  30. }
  31. }
  32. beforeEach(() => {
  33. uut = require('./ScreenRegistry');
  34. screenStore = require('./ScreenStore');
  35. });
  36. afterEach(() => {
  37. myScreenRef = null;
  38. testParentRef = null;
  39. });
  40. describe('registerScreen', () => {
  41. beforeEach(() => {
  42. AppRegistry.registerComponent = jest.fn(AppRegistry.registerComponent);
  43. });
  44. it('registers screen component by screenKey into AppRegistry', () => {
  45. expect(AppRegistry.registerComponent).not.toHaveBeenCalled();
  46. uut.registerScreen('example.MyScreen.key', () => MyScreen);
  47. expect(AppRegistry.registerComponent).toHaveBeenCalledTimes(1);
  48. expect(AppRegistry.registerComponent.mock.calls[0][0]).toEqual('example.MyScreen.key');
  49. });
  50. it('resulting in a normal component', () => {
  51. uut.registerScreen('example.MyScreen.key', () => MyScreen);
  52. const Screen = AppRegistry.registerComponent.mock.calls[0][1]();
  53. const tree = renderer.create(<Screen screenId="123"/>);
  54. expect(tree.toJSON().children).toEqual(['Hello, World!']);
  55. });
  56. });
  57. describe('NavigationScreen wrapping', () => {
  58. const screenKey = 'example.MyScreen';
  59. beforeEach(() => {
  60. uut.registerScreen(screenKey, () => MyScreen);
  61. });
  62. it('must have screenId as prop', () => {
  63. const NavigationScreen = screenStore.getScreenClass(screenKey);
  64. expect(() => {
  65. renderer.create(<NavigationScreen/>);
  66. }).toThrow(new Error('Screen example.MyScreen does not have a screenId!'));
  67. });
  68. it('wraps the screen and saves to store', () => {
  69. const NavigationScreen = screenStore.getScreenClass(screenKey);
  70. expect(NavigationScreen).not.toBeInstanceOf(MyScreen);
  71. const tree = renderer.create(<NavigationScreen screenId={'screen1'}/>);
  72. expect(tree.toJSON().children).toEqual(['Hello, World!']);
  73. expect(myScreenRef).toBeInstanceOf(MyScreen);
  74. });
  75. it('injects props from wrapper into original screen', () => {
  76. const NavigationScreen = screenStore.getScreenClass(screenKey);
  77. renderer.create(<NavigationScreen screenId={'screen1'} myProp={'yo'}/>);
  78. expect(myScreenRef.props.myProp).toEqual('yo');
  79. });
  80. it('updates props from wrapper into original screen', () => {
  81. const NavigationScreen = screenStore.getScreenClass(screenKey);
  82. renderer.create(<TestParent ChildClass={NavigationScreen}/>);
  83. expect(myScreenRef.props.foo).toEqual(undefined);
  84. testParentRef.setState({propsFromState: {foo: 'yo'}});
  85. expect(myScreenRef.props.foo).toEqual('yo');
  86. });
  87. it('pulls props from the PropsStore and injects them into the inner screen', () => {
  88. require('./PropsStore').setPropsForScreenId('screen123', {numberProp: 1, stringProp: 'hello', objectProp: {a: 2}});
  89. const NavigationScreen = screenStore.getScreenClass(screenKey);
  90. renderer.create(<NavigationScreen screenId={'screen123'}/>);
  91. expect(myScreenRef.props).toEqual({screenId: 'screen123', numberProp: 1, stringProp: 'hello', objectProp: {a: 2}});
  92. });
  93. it('updates props from PropsStore into inner screen', () => {
  94. const NavigationScreen = screenStore.getScreenClass(screenKey);
  95. renderer.create(<TestParent ChildClass={NavigationScreen}/>);
  96. require('./PropsStore').setPropsForScreenId('screen1', {myProp: 'hello'});
  97. expect(myScreenRef.props.foo).toEqual(undefined);
  98. expect(myScreenRef.props.myProp).toEqual(undefined);
  99. testParentRef.setState({propsFromState: {foo: 'yo'}});
  100. expect(myScreenRef.props.foo).toEqual('yo');
  101. expect(myScreenRef.props.myProp).toEqual('hello');
  102. });
  103. it('protects screenId from change', () => {
  104. const NavigationScreen = screenStore.getScreenClass(screenKey);
  105. renderer.create(<TestParent ChildClass={NavigationScreen}/>);
  106. expect(myScreenRef.props.screenId).toEqual('screen1');
  107. testParentRef.setState({propsFromState: {screenId: 'ERROR'}});
  108. expect(myScreenRef.props.screenId).toEqual('screen1');
  109. });
  110. });
  111. });