react-native-navigation的迁移库

Store.test.ts 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import * as React from 'react';
  2. import { Store } from './Store';
  3. import { IWrappedComponent } from './ComponentWrapper';
  4. describe('Store', () => {
  5. let uut: Store;
  6. beforeEach(() => {
  7. uut = new Store();
  8. });
  9. it('initial state', () => {
  10. expect(uut.getPropsForId('component1')).toEqual({});
  11. });
  12. it('holds props by id', () => {
  13. uut.updateProps('component1', { a: 1, b: 2 });
  14. expect(uut.getPropsForId('component1')).toEqual({ a: 1, b: 2 });
  15. });
  16. it('defensive for invalid Id and props', () => {
  17. uut.updateProps('component1', undefined);
  18. expect(uut.getPropsForId('component1')).toEqual({});
  19. });
  20. it('holds original components classes by componentName', () => {
  21. const MyWrappedComponent = () => class MyComponent extends React.Component {};
  22. uut.setComponentClassForName('example.mycomponent', MyWrappedComponent);
  23. expect(uut.getComponentClassForName('example.mycomponent')).toEqual(MyWrappedComponent);
  24. });
  25. it('clear props by component id when clear component', () => {
  26. uut.updateProps('refUniqueId', { foo: 'bar' });
  27. uut.clearComponent('refUniqueId');
  28. expect(uut.getPropsForId('refUniqueId')).toEqual({});
  29. });
  30. it('clear instance by component id when clear component', () => {
  31. uut.setComponentInstance('refUniqueId', ({} as IWrappedComponent));
  32. uut.clearComponent('refUniqueId');
  33. expect(uut.getComponentInstance('refUniqueId')).toEqual(undefined);
  34. });
  35. it('holds component instance by id', () => {
  36. uut.setComponentInstance('component1', ({} as IWrappedComponent));
  37. expect(uut.getComponentInstance('component1')).toEqual({});
  38. });
  39. it('calls component setProps when set props by id', () => {
  40. const instance: any = {setProps: jest.fn()};
  41. const props = { foo: 'bar' };
  42. uut.setComponentInstance('component1', instance);
  43. uut.updateProps('component1', props);
  44. expect(instance.setProps).toHaveBeenCalledWith(props);
  45. });
  46. it('not throw exeption when set props by id component not found', () => {
  47. expect(() => uut.updateProps('component1', { foo: 'bar' })).not.toThrow();
  48. });
  49. });