react-native-navigation的迁移库

ComponentRegistry.test.tsx 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import * as React from 'react';
  2. import { AppRegistry, Text } from 'react-native';
  3. import * as renderer from 'react-test-renderer';
  4. import { ComponentRegistry } from './ComponentRegistry';
  5. import { Store } from './Store';
  6. describe('ComponentRegistry', () => {
  7. let uut;
  8. let store;
  9. let mockRegistry: any;
  10. class MyComponent extends React.Component {
  11. render() {
  12. return (
  13. <Text>
  14. {
  15. 'Hello, World!'
  16. }
  17. </Text>);
  18. }
  19. }
  20. beforeEach(() => {
  21. store = new Store();
  22. mockRegistry = AppRegistry.registerComponent = jest.fn(AppRegistry.registerComponent);
  23. uut = new ComponentRegistry(store);
  24. });
  25. it('registers component component by componentName into AppRegistry', () => {
  26. expect(mockRegistry).not.toHaveBeenCalled();
  27. uut.registerComponent('example.MyComponent.name', () => MyComponent);
  28. expect(mockRegistry).toHaveBeenCalledTimes(1);
  29. expect(mockRegistry.mock.calls[0][0]).toEqual('example.MyComponent.name');
  30. });
  31. it('saves the original component into the store', () => {
  32. expect(store.getOriginalComponentClassForName('example.MyComponent.name')).toBeUndefined();
  33. uut.registerComponent('example.MyComponent.name', () => MyComponent);
  34. const Class = store.getOriginalComponentClassForName('example.MyComponent.name');
  35. expect(Class).not.toBeUndefined();
  36. expect(Class).toEqual(MyComponent);
  37. expect(Object.getPrototypeOf(Class)).toEqual(React.Component);
  38. });
  39. it('resulting in a normal component', () => {
  40. uut.registerComponent('example.MyComponent.name', () => MyComponent);
  41. const Component = mockRegistry.mock.calls[0][1]();
  42. const tree = renderer.create(<Component componentId='123' />);
  43. expect(tree.toJSON()!.children).toEqual(['Hello, World!']);
  44. });
  45. });