react-native-navigation的迁移库

LayoutTreeCrawler.test.ts 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import { LayoutType } from './LayoutType';
  2. import { LayoutTreeCrawler, LayoutNode } from './LayoutTreeCrawler';
  3. import { UniqueIdProvider } from '../adapters/UniqueIdProvider.mock';
  4. import { Store } from '../components/Store';
  5. describe('LayoutTreeCrawler', () => {
  6. let uut;
  7. let store;
  8. beforeEach(() => {
  9. store = new Store();
  10. uut = new LayoutTreeCrawler(new UniqueIdProvider(), store);
  11. });
  12. it('crawls a layout tree and adds unique id to each node', () => {
  13. const node: any = { type: LayoutType.Stack, children: [{ type: LayoutType.BottomTabs }] };
  14. uut.crawl(node);
  15. expect(node.id).toEqual('Stack+UNIQUE_ID');
  16. expect(node.children[0].id).toEqual('BottomTabs+UNIQUE_ID');
  17. });
  18. it('crawls a layout tree and ensures data exists', () => {
  19. const node: any = { type: LayoutType.Stack, children: [{ type: LayoutType.BottomTabs }] };
  20. uut.crawl(node);
  21. expect(node.data).toEqual({});
  22. expect(node.children[0].data).toEqual({});
  23. });
  24. it('crawls a layout tree and ensures children exists', () => {
  25. const node: any = { type: LayoutType.Stack, children: [{ type: LayoutType.BottomTabs }] };
  26. uut.crawl(node);
  27. expect(node.children[0].children).toEqual([]);
  28. });
  29. it('crawls a layout tree and asserts known layout type', () => {
  30. const node = { type: LayoutType.Stack, children: [{ type: 'Bob' }] };
  31. expect(() => uut.crawl(node)).toThrowError('Unknown layout type Bob');
  32. });
  33. it('saves passProps into store for Component nodes', () => {
  34. const node = {
  35. type: LayoutType.BottomTabs, children: [
  36. { type: LayoutType.Component, data: { name: 'the name', passProps: { myProp: 123 } } }]
  37. };
  38. expect(store.getPropsForComponentId('Component+UNIQUE_ID')).toEqual({});
  39. uut.crawl(node);
  40. expect(store.getPropsForComponentId('Component+UNIQUE_ID')).toEqual({ myProp: 123 });
  41. });
  42. it('Components: injects options from original component class static property', () => {
  43. const theStyle = {};
  44. const MyComponent = class {
  45. static get options() {
  46. return theStyle;
  47. }
  48. };
  49. const node: any = { type: LayoutType.Component, data: { name: 'theComponentName' } };
  50. store.setOriginalComponentClassForName('theComponentName', MyComponent);
  51. uut.crawl(node);
  52. expect(node.data.options).toEqual(theStyle);
  53. });
  54. it('Components: merges options from component class static property with passed options, favoring passed options', () => {
  55. const theStyle = {
  56. bazz: 123,
  57. inner: {
  58. foo: 'bar'
  59. },
  60. opt: 'exists only in static'
  61. };
  62. const MyComponent = class {
  63. static get options() {
  64. return theStyle;
  65. }
  66. };
  67. const passedOptions = {
  68. aaa: 'exists only in passed',
  69. bazz: 789,
  70. inner: {
  71. foo: 'this is overriden'
  72. }
  73. };
  74. const node = { type: LayoutType.Component, data: { name: 'theComponentName', options: passedOptions } };
  75. store.setOriginalComponentClassForName('theComponentName', MyComponent);
  76. uut.crawl(node);
  77. expect(node.data.options).toEqual({
  78. aaa: 'exists only in passed',
  79. bazz: 789,
  80. inner: {
  81. foo: 'this is overriden'
  82. },
  83. opt: 'exists only in static'
  84. });
  85. });
  86. it('Component: deepClones options', () => {
  87. const theStyle = {};
  88. const MyComponent = class {
  89. static get options() {
  90. return theStyle;
  91. }
  92. };
  93. const node: any = { type: LayoutType.Component, data: { name: 'theComponentName' } };
  94. store.setOriginalComponentClassForName('theComponentName', MyComponent);
  95. uut.crawl(node);
  96. expect(node.data.options).not.toBe(theStyle);
  97. });
  98. it('Components: must contain data name', () => {
  99. const node = { type: LayoutType.Component, data: {} };
  100. expect(() => uut.crawl(node)).toThrowError('Missing component data.name');
  101. });
  102. it('Components: options default obj', () => {
  103. const MyComponent = class { };
  104. const node: any = { type: LayoutType.Component, data: { name: 'theComponentName' } };
  105. store.setOriginalComponentClassForName('theComponentName', MyComponent);
  106. uut.crawl(node);
  107. expect(node.data.options).toEqual({});
  108. });
  109. describe('navigation options', () => {
  110. let options;
  111. let node;
  112. beforeEach(() => {
  113. options = {};
  114. node = { type: LayoutType.Component, data: { name: 'theComponentName', options } };
  115. });
  116. it('processes colors into numeric AARRGGBB', () => {
  117. options.someKeyColor = 'red';
  118. uut.crawl(node);
  119. expect(node.data.options.someKeyColor).toEqual(0xffff0000);
  120. });
  121. it('processes colors into numeric AARRGGBB', () => {
  122. options.someKeyColor = 'yellow';
  123. uut.crawl(node);
  124. expect(node.data.options.someKeyColor).toEqual(0xffffff00);
  125. });
  126. it('processes numeric colors', () => {
  127. options.someKeyColor = '#123456';
  128. uut.crawl(node);
  129. expect(node.data.options.someKeyColor).toEqual(0xff123456);
  130. });
  131. it('processes numeric colors with rrggbbAA', () => {
  132. options.someKeyColor = 0x123456ff; // wut
  133. uut.crawl(node);
  134. expect(node.data.options.someKeyColor).toEqual(0xff123456);
  135. });
  136. it('process colors with rgb functions', () => {
  137. options.someKeyColor = 'rgb(255, 0, 255)';
  138. uut.crawl(node);
  139. expect(node.data.options.someKeyColor).toEqual(0xffff00ff);
  140. });
  141. it('process colors with special words', () => {
  142. options.someKeyColor = 'fuchsia';
  143. uut.crawl(node);
  144. expect(node.data.options.someKeyColor).toEqual(0xffff00ff);
  145. });
  146. it('process colors with hsla functions', () => {
  147. options.someKeyColor = 'hsla(360, 100%, 100%, 1.0)';
  148. uut.crawl(node);
  149. expect(node.data.options.someKeyColor).toEqual(0xffffffff);
  150. });
  151. it('unknown colors return undefined', () => {
  152. options.someKeyColor = 'wut';
  153. uut.crawl(node);
  154. expect(node.data.options.someKeyColor).toEqual(undefined);
  155. });
  156. it('any keys ending with Color', () => {
  157. options.otherKeyColor = 'red';
  158. options.yetAnotherColor = 'blue';
  159. options.andAnotherColor = 'rgb(0, 255, 0)';
  160. uut.crawl(node);
  161. expect(node.data.options.otherKeyColor).toEqual(0xffff0000);
  162. expect(node.data.options.yetAnotherColor).toEqual(0xff0000ff);
  163. expect(node.data.options.andAnotherColor).toEqual(0xff00ff00);
  164. });
  165. it('keys ending with Color case sensitive', () => {
  166. options.otherKey_color = 'red'; // eslint-disable-line camelcase
  167. uut.crawl(node);
  168. expect(node.data.options.otherKey_color).toEqual('red');
  169. });
  170. it('any nested recursive keys ending with Color', () => {
  171. options.innerObj = { theKeyColor: 'red' };
  172. options.innerObj.innerMostObj = { anotherColor: 'yellow' };
  173. uut.crawl(node);
  174. expect(node.data.options.innerObj.theKeyColor).toEqual(0xffff0000);
  175. expect(node.data.options.innerObj.innerMostObj.anotherColor).toEqual(0xffffff00);
  176. });
  177. });
  178. describe('LayoutNode', () => {
  179. it('convertable from same data structure', () => {
  180. const x = {
  181. id: 'theId',
  182. type: LayoutType.Component,
  183. data: {},
  184. children: []
  185. };
  186. let got;
  187. function expectingLayoutNode(param: LayoutNode) {
  188. got = param;
  189. }
  190. expectingLayoutNode(x);
  191. expect(got).toBe(x);
  192. });
  193. });
  194. });