react-native-navigation的迁移库

OptionsProcessor.test.ts 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import { OptionsProcessor } from './OptionsProcessor';
  2. import { UniqueIdProvider } from '../adapters/UniqueIdProvider';
  3. import { Store } from '../components/Store';
  4. import * as _ from 'lodash';
  5. describe('navigation options', () => {
  6. let uut: OptionsProcessor;
  7. let options;
  8. let store: Store;
  9. beforeEach(() => {
  10. options = {};
  11. store = new Store();
  12. uut = new OptionsProcessor(store, new UniqueIdProvider());
  13. });
  14. it('processes colors into numeric AARRGGBB', () => {
  15. options.someKeyColor = 'red';
  16. options.color = 'blue';
  17. uut.processOptions(options);
  18. expect(options.someKeyColor).toEqual(0xffff0000);
  19. expect(options.color).toEqual(0xff0000ff);
  20. options.someKeyColor = 'yellow';
  21. uut.processOptions(options);
  22. expect(options.someKeyColor).toEqual(0xffffff00);
  23. });
  24. it('processes numeric colors', () => {
  25. options.someKeyColor = '#123456';
  26. uut.processOptions(options);
  27. expect(options.someKeyColor).toEqual(0xff123456);
  28. options.someKeyColor = 0x123456ff; // wut
  29. uut.processOptions(options);
  30. expect(options.someKeyColor).toEqual(0xff123456);
  31. });
  32. it('process colors with rgb functions', () => {
  33. options.someKeyColor = 'rgb(255, 0, 255)';
  34. uut.processOptions(options);
  35. expect(options.someKeyColor).toEqual(0xffff00ff);
  36. });
  37. it('process colors with special words', () => {
  38. options.someKeyColor = 'fuchsia';
  39. uut.processOptions(options);
  40. expect(options.someKeyColor).toEqual(0xffff00ff);
  41. });
  42. it('process colors with hsla functions', () => {
  43. options.someKeyColor = 'hsla(360, 100%, 100%, 1.0)';
  44. uut.processOptions(options);
  45. expect(options.someKeyColor).toEqual(0xffffffff);
  46. });
  47. it('unknown colors return undefined', () => {
  48. options.someKeyColor = 'wut';
  49. uut.processOptions(options);
  50. expect(options.someKeyColor).toEqual(undefined);
  51. });
  52. it('any keys ending with Color', () => {
  53. options.otherKeyColor = 'red';
  54. options.yetAnotherColor = 'blue';
  55. options.andAnotherColor = 'rgb(0, 255, 0)';
  56. uut.processOptions(options);
  57. expect(options.otherKeyColor).toEqual(0xffff0000);
  58. expect(options.yetAnotherColor).toEqual(0xff0000ff);
  59. expect(options.andAnotherColor).toEqual(0xff00ff00);
  60. });
  61. it('keys ending with Color case sensitive', () => {
  62. options.otherKey_color = 'red'; // eslint-disable-line camelcase
  63. uut.processOptions(options);
  64. expect(options.otherKey_color).toEqual('red');
  65. });
  66. it('any nested recursive keys ending with Color', () => {
  67. options.topBar = { textColor: 'red' };
  68. options.topBar.innerMostObj = { anotherColor: 'yellow' };
  69. uut.processOptions(options);
  70. expect(options.topBar.textColor).toEqual(0xffff0000);
  71. expect(options.topBar.innerMostObj.anotherColor).toEqual(0xffffff00);
  72. });
  73. it('resolve image sources with name/ending with icon', () => {
  74. options.icon = 'require("https://wix.github.io/react-native-navigation/_images/logo.png");';
  75. options.image = 'require("https://wix.github.io/react-native-navigation/_images/logo.png");';
  76. options.myImage = 'require("https://wix.github.io/react-native-navigation/_images/logo.png");';
  77. options.topBar = {
  78. myIcon: 'require("https://wix.github.io/react-native-navigation/_images/logo.png");',
  79. myOtherValue: 'value'
  80. };
  81. uut.processOptions(options);
  82. // As we can't import external images and we don't want to add an image here
  83. // I assign the icons to strings (what the require would generally look like)
  84. // and expect the value to be resovled, in this case it doesn't find anything and returns null
  85. expect(options.icon).toEqual(null);
  86. expect(options.topBar.myIcon).toEqual(null);
  87. expect(options.image).toEqual(null);
  88. expect(options.myImage).toEqual(null);
  89. expect(options.topBar.myOtherValue).toEqual('value');
  90. });
  91. it('passProps for Buttons options', () => {
  92. const passProps = { prop: 'prop' };
  93. options.rightButtons = [{ passProps, id: '1' }];
  94. uut.processOptions({ o: options });
  95. expect(store.getPropsForId('1')).toEqual(passProps);
  96. });
  97. it('passProps for custom component', () => {
  98. const passProps = { color: '#ff0000', some: 'thing' };
  99. options.component = { passProps, name: 'a' };
  100. uut.processOptions({ o: options });
  101. expect(store.getPropsForId(options.component.componentId)).toEqual(passProps);
  102. expect(Object.keys(options.component)).not.toContain('passProps');
  103. });
  104. it('generate component id for component in options', () => {
  105. options.component = { name: 'a' };
  106. uut.processOptions({ o: options });
  107. expect(options.component.componentId).toBeDefined();
  108. });
  109. it('passProps from options are not processed', () => {
  110. const passProps = { color: '#ff0000', some: 'thing' };
  111. const clonedProps = _.cloneDeep(passProps);
  112. options.component = { passProps, name: 'a' };
  113. uut.processOptions(options);
  114. expect(store.getPropsForId(options.component.componentId)).toEqual(clonedProps);
  115. });
  116. it('pass supplied componentId for component in options', () => {
  117. options.component = { name: 'a', id: 'Component1' };
  118. uut.processOptions({ o: options });
  119. expect(options.component.componentId).toEqual('Component1');
  120. });
  121. it('passProps must be with id next to it', () => {
  122. const passProps = { prop: 'prop' };
  123. options.rightButtons = [{ passProps }];
  124. uut.processOptions({ o: options });
  125. expect(store.getPropsForId('1')).toEqual({});
  126. });
  127. it('processes Options object', () => {
  128. options.someKeyColor = 'rgb(255, 0, 255)';
  129. options.topBar = { textColor: 'red' };
  130. options.topBar.innerMostObj = { anotherColor: 'yellow' };
  131. uut.processOptions({ o: options });
  132. expect(options.topBar.textColor).toEqual(0xffff0000);
  133. });
  134. it('undefined value return undefined ', () => {
  135. options.someImage = undefined;
  136. uut.processOptions(options);
  137. expect(options.someImage).toEqual(undefined);
  138. });
  139. });