react-native-navigation的迁移库

OptionsProcessor.test.ts 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import { OptionsProcessor } from './OptionsProcessor';
  2. import { UniqueIdProvider } from '../adapters/UniqueIdProvider';
  3. import { Store } from '../components/Store';
  4. import { Options, OptionsModalPresentationStyle } from '../interfaces/Options';
  5. import { mock, when, anyString, instance, anyNumber, verify } from 'ts-mockito';
  6. import { ColorService } from '../adapters/ColorService';
  7. import { AssetService } from '../adapters/AssetResolver';
  8. jest.mock('lodash/uniqueId', () => {
  9. return (prefix: string) => `${prefix}1`
  10. })
  11. describe('navigation options', () => {
  12. let uut: OptionsProcessor;
  13. const mockedStore = mock(Store) as Store;
  14. const store = instance(mockedStore) as Store;
  15. beforeEach(() => {
  16. const mockedAssetService = mock(AssetService) as AssetService;
  17. when(mockedAssetService.resolveFromRequire(anyNumber())).thenReturn({
  18. height: 100,
  19. scale: 1,
  20. uri: 'lol',
  21. width: 100
  22. });
  23. const assetService = instance(mockedAssetService);
  24. const mockedColorService = mock(ColorService) as ColorService;
  25. when(mockedColorService.toNativeColor(anyString())).thenReturn(666);
  26. const colorService = instance(mockedColorService);
  27. uut = new OptionsProcessor(store, new UniqueIdProvider(), colorService, assetService);
  28. });
  29. it('keeps original values if values were not processed', () => {
  30. const options: Options = {
  31. blurOnUnmount: false,
  32. popGesture: false,
  33. modalPresentationStyle: OptionsModalPresentationStyle.fullScreen,
  34. animations: { dismissModal: { alpha: { from: 0, to: 1 } } },
  35. };
  36. uut.processOptions(options);
  37. expect(options).toEqual({
  38. blurOnUnmount: false,
  39. popGesture: false,
  40. modalPresentationStyle: OptionsModalPresentationStyle.fullScreen,
  41. animations: { dismissModal: { alpha: { from: 0, to: 1 } } },
  42. });
  43. });
  44. it('processes color keys', () => {
  45. const options: Options = {
  46. statusBar: { backgroundColor: 'red' },
  47. topBar: { background: { color: 'blue' } },
  48. };
  49. uut.processOptions(options);
  50. expect(options).toEqual({
  51. statusBar: { backgroundColor: 666 },
  52. topBar: { background: { color: 666 } },
  53. });
  54. });
  55. it('processes image keys', () => {
  56. const options: Options = {
  57. backgroundImage: 123,
  58. rootBackgroundImage: 234,
  59. bottomTab: { icon: 345, selectedIcon: 345 },
  60. };
  61. uut.processOptions(options);
  62. expect(options).toEqual({
  63. backgroundImage: { height: 100, scale: 1, uri: 'lol', width: 100 },
  64. rootBackgroundImage: { height: 100, scale: 1, uri: 'lol', width: 100 },
  65. bottomTab: {
  66. icon: { height: 100, scale: 1, uri: 'lol', width: 100 },
  67. selectedIcon: { height: 100, scale: 1, uri: 'lol', width: 100 }
  68. }
  69. });
  70. });
  71. it('calls store if component has passProps', () => {
  72. const passProps = { some: 'thing' };
  73. const options = { topBar: { title: { component: { passProps, name: 'a' } } } };
  74. uut.processOptions(options);
  75. verify(mockedStore.updateProps('CustomComponent1', passProps)).called();
  76. });
  77. it('generates componentId for component id was not passed', () => {
  78. const options = { topBar: { title: { component: { name: 'a' } } } };
  79. uut.processOptions(options);
  80. expect(options).toEqual({
  81. topBar: { title: { component: { name: 'a', componentId: 'CustomComponent1' } } },
  82. });
  83. });
  84. it('copies passed id to componentId key', () => {
  85. const options = { topBar: { title: { component: { name: 'a', id: 'Component1' } } } };
  86. uut.processOptions(options);
  87. expect(options).toEqual({
  88. topBar: { title: { component: { name: 'a', id: 'Component1', componentId: 'Component1' } } },
  89. });
  90. });
  91. it('calls store when button has passProps and id', () => {
  92. const passProps = { prop: 'prop' };
  93. const options = { topBar: { rightButtons: [{ passProps, id: '1' }] } };
  94. uut.processOptions(options);
  95. verify(mockedStore.updateProps('1', passProps)).called();
  96. });
  97. it('do not touch passProps when id for button is missing', () => {
  98. const passProps = { prop: 'prop' };
  99. const options = { topBar: { rightButtons: [{ passProps } as any] } };
  100. uut.processOptions(options);
  101. expect(options).toEqual({ topBar: { rightButtons: [{ passProps }] } });
  102. });
  103. it('omits passProps when processing buttons or components', () => {
  104. const options = {
  105. topBar: {
  106. rightButtons: [{ passProps: {}, id: 'btn1' }],
  107. leftButtons: [{ passProps: {}, id: 'btn2' }],
  108. title: { component: { name: 'helloThere1', passProps: {} } },
  109. background: { component: { name: 'helloThere2', passProps: {} } },
  110. },
  111. };
  112. uut.processOptions(options);
  113. expect(options.topBar.rightButtons[0].passProps).toBeUndefined();
  114. expect(options.topBar.leftButtons[0].passProps).toBeUndefined();
  115. expect(options.topBar.title.component.passProps).toBeUndefined();
  116. expect(options.topBar.background.component.passProps).toBeUndefined();
  117. });
  118. });