react-native-navigation的迁移库

ContainerRegistry.test.js 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. it('updates props into original container', () => {
  45. uut.registerContainer('example.MyContainer', () => MyContainer);
  46. const NavigationContainer = getRegisteredComponentClassFromAppRegistry();
  47. let testParentRef = null;
  48. class TestParent extends Component { //eslint-disable-line
  49. constructor(props) {
  50. super(props);
  51. this.state = {};
  52. }
  53. render() {
  54. return (
  55. <NavigationContainer name={this.state.name}/>
  56. );
  57. }
  58. }
  59. const tree = renderer.create(
  60. <TestParent ref={(r) => testParentRef = r}/>
  61. );
  62. expect(tree.toJSON().children).toEqual(['Hello, World!']);
  63. testParentRef.setState({name: 'Gandalf'});
  64. expect(tree.toJSON().children).toEqual(['Hello, Gandalf!']);
  65. });
  66. });