react-native-navigation的迁移库

ContainerRegistry.test.js 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import React, {Component} from 'react';
  2. import {AppRegistry, Text} from 'react-native';
  3. import renderer from 'react-test-renderer';
  4. describe('ContainerRegistry', () => {
  5. let uut;
  6. let store;
  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 containerId="container1" {...this.state.propsFromState}/>
  29. );
  30. }
  31. }
  32. beforeEach(() => {
  33. uut = require('./ContainerRegistry');
  34. store = require('./Store');
  35. });
  36. describe('registerContainer', () => {
  37. beforeEach(() => {
  38. AppRegistry.registerComponent = jest.fn(AppRegistry.registerComponent);
  39. });
  40. it('registers container component by containerName into AppRegistry', () => {
  41. expect(AppRegistry.registerComponent).not.toHaveBeenCalled();
  42. uut.registerContainer('example.MyContainer.name', () => MyContainer);
  43. expect(AppRegistry.registerComponent).toHaveBeenCalledTimes(1);
  44. expect(AppRegistry.registerComponent.mock.calls[0][0]).toEqual('example.MyContainer.name');
  45. });
  46. it('resulting in a normal component', () => {
  47. uut.registerContainer('example.MyContainer.name', () => MyContainer);
  48. const Container = AppRegistry.registerComponent.mock.calls[0][1]();
  49. const tree = renderer.create(<Container containerId="123"/>);
  50. expect(tree.toJSON().children).toEqual(['Hello, World!']);
  51. });
  52. });
  53. describe('NavigationContainer wrapping passed container', () => {
  54. const containerName = 'example.MyContainer';
  55. beforeEach(() => {
  56. uut.registerContainer(containerName, () => MyContainer);
  57. });
  58. it('must have containerId as prop', () => {
  59. const NavigationContainer = store.getContainerClass(containerName);
  60. expect(() => {
  61. renderer.create(<NavigationContainer/>);
  62. }).toThrow(new Error('Container example.MyContainer does not have a containerId!'));
  63. });
  64. it('wraps the container and saves to store', () => {
  65. const NavigationContainer = store.getContainerClass(containerName);
  66. expect(NavigationContainer).not.toBeInstanceOf(MyContainer);
  67. const tree = renderer.create(<NavigationContainer containerId={'container1'}/>);
  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 = store.getContainerClass(containerName);
  73. renderer.create(<NavigationContainer containerId={'container1'} myProp={'yo'}/>);
  74. expect(myContainerRef.props.myProp).toEqual('yo');
  75. });
  76. it('updates props from wrapper into original container', () => {
  77. const NavigationContainer = store.getContainerClass(containerName);
  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. store.setPropsForContainerId('container123', {numberProp: 1, stringProp: 'hello', objectProp: {a: 2}});
  85. const NavigationContainer = store.getContainerClass(containerName);
  86. renderer.create(<NavigationContainer containerId={'container123'}/>);
  87. expect(myContainerRef.props).toEqual({containerId: 'container123', numberProp: 1, stringProp: 'hello', objectProp: {a: 2}});
  88. });
  89. it('updates props from PropsStore into inner container', () => {
  90. const NavigationContainer = store.getContainerClass(containerName);
  91. renderer.create(<TestParent ChildClass={NavigationContainer}/>);
  92. store.setPropsForContainerId('container1', {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 containerId from change', () => {
  100. const NavigationContainer = store.getContainerClass(containerName);
  101. renderer.create(<TestParent ChildClass={NavigationContainer}/>);
  102. expect(myContainerRef.props.containerId).toEqual('container1');
  103. testParentRef.setState({propsFromState: {containerId: 'ERROR'}});
  104. expect(myContainerRef.props.containerId).toEqual('container1');
  105. });
  106. });
  107. });