react-native-navigation的迁移库

ComponentRegistry.test.tsx 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. let mockWrapper: any;
  11. class WrappedComponent extends React.Component {
  12. render() {
  13. return (
  14. <Text>
  15. {
  16. 'Hello, World!'
  17. }
  18. </Text>);
  19. }
  20. }
  21. beforeEach(() => {
  22. store = new Store();
  23. mockRegistry = AppRegistry.registerComponent = jest.fn(AppRegistry.registerComponent);
  24. mockWrapper = jest.mock('./ComponentWrapper');
  25. mockWrapper.wrap = () => WrappedComponent;
  26. uut = new ComponentRegistry(store, {} as any);
  27. });
  28. it('registers component by componentName into AppRegistry', () => {
  29. expect(mockRegistry).not.toHaveBeenCalled();
  30. const result = uut.registerComponent('example.MyComponent.name', () => {}, mockWrapper);
  31. expect(mockRegistry).toHaveBeenCalledTimes(1);
  32. expect(mockRegistry.mock.calls[0][0]).toEqual('example.MyComponent.name');
  33. expect(mockRegistry.mock.calls[0][1]()).toEqual(result());
  34. });
  35. it('saves the wrapper component generator to the store', () => {
  36. expect(store.getComponentClassForName('example.MyComponent.name')).toBeUndefined();
  37. uut.registerComponent('example.MyComponent.name', () => {}, mockWrapper);
  38. const Class = store.getComponentClassForName('example.MyComponent.name');
  39. expect(Class).not.toBeUndefined();
  40. expect(Class()).toEqual(WrappedComponent);
  41. });
  42. it('resulting in a normal component', () => {
  43. uut.registerComponent('example.MyComponent.name', () => {}, mockWrapper);
  44. const Component = mockRegistry.mock.calls[0][1]();
  45. const tree = renderer.create(<Component componentId='123' />);
  46. expect(tree.toJSON()!.children).toEqual(['Hello, World!']);
  47. });
  48. it('should not invoke generator', () => {
  49. const generator = jest.fn(() => {});
  50. uut.registerComponent('example.MyComponent.name', generator);
  51. expect(generator).toHaveBeenCalledTimes(0);
  52. });
  53. it('saves wrapped component to store', () => {
  54. jest.spyOn(store, 'setComponentClassForName');
  55. const generator = jest.fn(() => {});
  56. const componentName = 'example.MyComponent.name';
  57. uut.registerComponent(componentName, generator, mockWrapper);
  58. expect(store.getComponentClassForName(componentName)()).toEqual(WrappedComponent);
  59. });
  60. });