react-native-navigation的迁移库

ContainerRegistry.test.js 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. let containerStore;
  9. class MyContainer extends Component {
  10. constructor(props) {
  11. super(props);
  12. myContainerRef = 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('./ContainerRegistry');
  34. containerStore = require('./ContainerStore');
  35. });
  36. afterEach(() => {
  37. myContainerRef = null;
  38. testParentRef = null;
  39. });
  40. describe('registerContainer', () => {
  41. beforeEach(() => {
  42. AppRegistry.registerComponent = jest.fn(AppRegistry.registerComponent);
  43. });
  44. it('registers container component by containerKey into AppRegistry', () => {
  45. expect(AppRegistry.registerComponent).not.toHaveBeenCalled();
  46. uut.registerContainer('example.MyContainer.key', () => MyContainer);
  47. expect(AppRegistry.registerComponent).toHaveBeenCalledTimes(1);
  48. expect(AppRegistry.registerComponent.mock.calls[0][0]).toEqual('example.MyContainer.key');
  49. });
  50. it('resulting in a normal component', () => {
  51. uut.registerContainer('example.MyContainer.key', () => MyContainer);
  52. const Container = AppRegistry.registerComponent.mock.calls[0][1]();
  53. const tree = renderer.create(<Container screenId="123"/>);
  54. expect(tree.toJSON().children).toEqual(['Hello, World!']);
  55. });
  56. });
  57. describe('NavigationContainer wrapping', () => {
  58. const containerKey = 'example.MyContainer';
  59. beforeEach(() => {
  60. uut.registerContainer(containerKey, () => MyContainer);
  61. });
  62. it('must have screenId as prop', () => {
  63. const NavigationContainer = containerStore.getContainerClass(containerKey);
  64. expect(() => {
  65. renderer.create(<NavigationContainer/>);
  66. }).toThrow(new Error('Screen example.MyContainer does not have a screenId!'));
  67. });
  68. it('wraps the container and saves to store', () => {
  69. const NavigationContainer = containerStore.getContainerClass(containerKey);
  70. expect(NavigationContainer).not.toBeInstanceOf(MyContainer);
  71. const tree = renderer.create(<NavigationContainer screenId={'screen1'}/>);
  72. expect(tree.toJSON().children).toEqual(['Hello, World!']);
  73. expect(myContainerRef).toBeInstanceOf(MyContainer);
  74. });
  75. it('injects props from wrapper into original container', () => {
  76. const NavigationContainer = containerStore.getContainerClass(containerKey);
  77. renderer.create(<NavigationContainer screenId={'screen1'} myProp={'yo'}/>);
  78. expect(myContainerRef.props.myProp).toEqual('yo');
  79. });
  80. it('updates props from wrapper into original container', () => {
  81. const NavigationContainer = containerStore.getContainerClass(containerKey);
  82. renderer.create(<TestParent ChildClass={NavigationContainer}/>);
  83. expect(myContainerRef.props.foo).toEqual(undefined);
  84. testParentRef.setState({propsFromState: {foo: 'yo'}});
  85. expect(myContainerRef.props.foo).toEqual('yo');
  86. });
  87. it('pulls props from the PropsStore and injects them into the inner container', () => {
  88. require('./PropsStore').setPropsForScreenId('screen123', {numberProp: 1, stringProp: 'hello', objectProp: {a: 2}});
  89. const NavigationContainer = containerStore.getContainerClass(containerKey);
  90. renderer.create(<NavigationContainer screenId={'screen123'}/>);
  91. expect(myContainerRef.props).toEqual({screenId: 'screen123', numberProp: 1, stringProp: 'hello', objectProp: {a: 2}});
  92. });
  93. it('updates props from PropsStore into inner container', () => {
  94. const NavigationContainer = containerStore.getContainerClass(containerKey);
  95. renderer.create(<TestParent ChildClass={NavigationContainer}/>);
  96. require('./PropsStore').setPropsForScreenId('screen1', {myProp: 'hello'});
  97. expect(myContainerRef.props.foo).toEqual(undefined);
  98. expect(myContainerRef.props.myProp).toEqual(undefined);
  99. testParentRef.setState({propsFromState: {foo: 'yo'}});
  100. expect(myContainerRef.props.foo).toEqual('yo');
  101. expect(myContainerRef.props.myProp).toEqual('hello');
  102. });
  103. it('protects screenId from change', () => {
  104. const NavigationContainer = containerStore.getContainerClass(containerKey);
  105. renderer.create(<TestParent ChildClass={NavigationContainer}/>);
  106. expect(myContainerRef.props.screenId).toEqual('screen1');
  107. testParentRef.setState({propsFromState: {screenId: 'ERROR'}});
  108. expect(myContainerRef.props.screenId).toEqual('screen1');
  109. });
  110. });
  111. });