react-native-navigation的迁移库

ContainerRegistry.test.js 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. function getRegisteredComponentClassFromAppRegistry() {
  20. return AppRegistry.registerComponent.mock.calls[0][1]();
  21. }
  22. it('registers container component by containerKey into AppRegistry', () => {
  23. expect(AppRegistry.registerComponent).not.toHaveBeenCalled();
  24. uut.registerContainer('example.MyContainer.key', () => MyContainer);
  25. expect(AppRegistry.registerComponent).toHaveBeenCalledTimes(1);
  26. expect(AppRegistry.registerComponent.mock.calls[0][0]).toEqual('example.MyContainer.key');
  27. });
  28. it('wraps the container', () => {
  29. uut.registerContainer('example.MyContainer', () => MyContainer);
  30. const NavigationContainer = getRegisteredComponentClassFromAppRegistry();
  31. const tree = renderer.create(
  32. <NavigationContainer/>
  33. );
  34. expect(tree.toJSON().children).toEqual(['Hello, World!']);
  35. });
  36. it('passes props from wrapper into original container', () => {
  37. uut.registerContainer('example.MyContainer', () => MyContainer);
  38. const NavigationContainer = getRegisteredComponentClassFromAppRegistry();
  39. const tree = renderer.create(
  40. <NavigationContainer name="Daniel"/>
  41. );
  42. expect(tree.toJSON().children).toEqual(['Hello, Daniel!']);
  43. });
  44. //
  45. //xit('updates props into original container', () => {
  46. //uut.registerContainer('example.MyContainer', () => MyContainer);
  47. //
  48. //const NavigationContainer = getRegisteredComponentClassFromAppRegistry();
  49. //
  50. //class TestParent extends Component {
  51. // constructor(props) {
  52. // super(props);
  53. // this.state = {};
  54. // }
  55. //
  56. // render() {
  57. // return (
  58. // <NavigationContainer/>
  59. // );
  60. // }
  61. //}
  62. //
  63. //let navContainerRef;
  64. //const NavigationContainer = getRegisteredComponentClassFromAppRegistry();
  65. //const tree = renderer.create(
  66. // <NavigationContainer ref={(r) => navContainerRef = r} name={_.get(navContainerRef, 'state.name')}/>
  67. //);
  68. //expect(tree.toJSON().children).toEqual(['Hello, World!']);
  69. //navContainerRef.setState({name: 'Gandalf'});
  70. //expect(tree.toJSON().children).toEqual(['Hello, Gandalf!']);
  71. //});
  72. });