react-native-navigation的迁移库

ContainerRegistry.test.js 4.8KB

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