react-native-navigation的迁移库

ContainerRegistry.test.js 4.9KB

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