react-native-navigation的迁移库

ContainerRegistry.test.js 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. class MyContainer extends Component {
  6. render() {
  7. const txt = `Hello, ${_.get(this.props, 'name', 'World')}!`;
  8. return (
  9. <Text>{txt}</Text>
  10. );
  11. }
  12. }
  13. describe('ComponentRegistry', () => {
  14. let uut;
  15. beforeEach(() => {
  16. AppRegistry.registerComponent = jest.fn(AppRegistry.registerComponent);
  17. uut = require('./ContainerRegistry');
  18. });
  19. describe('registerContainer', () => {
  20. function getRegisteredComponentClassFromAppRegistry() {
  21. return AppRegistry.registerComponent.mock.calls[0][1]();
  22. }
  23. function renderRegisteredContainer(props) {
  24. const Container = getRegisteredComponentClassFromAppRegistry();
  25. return renderer.create(
  26. <Container screenId="screen1" {...props}/>
  27. );
  28. }
  29. it('registers container component by containerKey into AppRegistry', () => {
  30. expect(AppRegistry.registerComponent).not.toHaveBeenCalled();
  31. uut.registerContainer('example.MyContainer.key', () => MyContainer);
  32. expect(AppRegistry.registerComponent).toHaveBeenCalledTimes(1);
  33. expect(AppRegistry.registerComponent.mock.calls[0][0]).toEqual('example.MyContainer.key');
  34. });
  35. it('wraps the container', () => {
  36. uut.registerContainer('example.MyContainer', () => MyContainer);
  37. const tree = renderRegisteredContainer();
  38. expect(tree.toJSON().children).toEqual(['Hello, World!']);
  39. });
  40. it('passes props from wrapper into original container', () => {
  41. uut.registerContainer('example.MyContainer', () => MyContainer);
  42. const tree = renderRegisteredContainer({name: 'Daniel'});
  43. expect(tree.toJSON().children).toEqual(['Hello, Daniel!']);
  44. });
  45. it('injects and updates props into original container', () => {
  46. uut.registerContainer('example.MyContainer', () => MyContainer);
  47. const NavigationContainer = getRegisteredComponentClassFromAppRegistry();
  48. class TestParent extends Component { //eslint-disable-line
  49. constructor(props) {
  50. super(props);
  51. this.state = {};
  52. }
  53. render() {
  54. return (
  55. <NavigationContainer screenId="screen1" name={this.state.name}/>
  56. );
  57. }
  58. }
  59. let testParentRef = null;
  60. const tree = renderer.create(
  61. <TestParent ref={(r) => testParentRef = r}/>
  62. );
  63. expect(tree.toJSON().children).toEqual(['Hello, World!']);
  64. testParentRef.setState({name: 'Gandalf'});
  65. expect(tree.toJSON().children).toEqual(['Hello, Gandalf!']);
  66. });
  67. it('asserts has screenId as prop', () => {
  68. uut.registerContainer('example.MyContainer.key', () => MyContainer);
  69. const NavigationContainer = getRegisteredComponentClassFromAppRegistry();
  70. expect(() => {
  71. renderer.create(<NavigationContainer/>);
  72. }).toThrow(new Error('Screen example.MyContainer.key does not have a screenId!'));
  73. });
  74. });
  75. });