react-native-navigation的迁移库

OptionsProcessor.test.ts 4.8KB

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