react-native-navigation的迁移库

OptionsProcessor.test.ts 4.8KB

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