react-native-navigation的迁移库

LayoutTreeParser.test.ts 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. import * as _ from 'lodash';
  2. import { LayoutTreeParser } from './LayoutTreeParser';
  3. import { LayoutType } from './values/LayoutType';
  4. describe('LayoutTreeParser', () => {
  5. let uut;
  6. beforeEach(() => {
  7. uut = new LayoutTreeParser();
  8. });
  9. describe('parses into { type, data, children }', () => {
  10. it('unknown type', () => {
  11. expect(() => uut.parse({ wut: {} })).toThrow(new Error('unknown LayoutType "wut"'));
  12. });
  13. it('single component', () => {
  14. expect(uut.parse(LayoutExamples.singleComponent)).toEqual({
  15. type: LayoutType.Component,
  16. data: { name: 'MyReactComponent', options: LayoutExamples.options, passProps: LayoutExamples.passProps },
  17. children: []
  18. });
  19. });
  20. it('pass props', () => {
  21. const result = uut.parse({
  22. component: {
  23. name: 'MyScreen',
  24. passProps: LayoutExamples.passProps
  25. }
  26. });
  27. expect(result).toEqual({
  28. type: LayoutType.Component,
  29. data: { name: 'MyScreen', passProps: LayoutExamples.passProps },
  30. children: []
  31. });
  32. expect(result.data.passProps).toBe(LayoutExamples.passProps);
  33. expect(result.data.passProps.fnProp()).toEqual('Hello from a function');
  34. });
  35. it('stack of components with top bar', () => {
  36. expect(uut.parse(LayoutExamples.stackWithTopBar)).toEqual({
  37. type: LayoutType.Stack,
  38. data: {
  39. options: LayoutExamples.options
  40. },
  41. children: [
  42. {
  43. type: LayoutType.Component,
  44. data: { name: 'MyReactComponent1' },
  45. children: []
  46. },
  47. {
  48. type: LayoutType.Component,
  49. data: { name: 'MyReactComponent2', options: LayoutExamples.options },
  50. children: []
  51. }
  52. ]
  53. });
  54. });
  55. it('bottom tabs', () => {
  56. const result = uut.parse(LayoutExamples.bottomTabs);
  57. expect(_.keys(result)).toEqual(['type', 'data', 'children']);
  58. expect(result.type).toEqual(LayoutType.BottomTabs);
  59. expect(result.data).toEqual({});
  60. expect(result.children.length).toEqual(3);
  61. expect(result.children[0].type).toEqual(LayoutType.Stack);
  62. expect(result.children[1].type).toEqual(LayoutType.Stack);
  63. expect(result.children[2].type).toEqual(LayoutType.Component);
  64. });
  65. it('side menus', () => {
  66. const result = uut.parse(LayoutExamples.sideMenu);
  67. expect(_.keys(result)).toEqual(['type', 'data', 'children']);
  68. expect(result.type).toEqual(LayoutType.SideMenuRoot);
  69. expect(result.data).toEqual({});
  70. expect(result.children.length).toEqual(3);
  71. expect(result.children[0].type).toEqual(LayoutType.SideMenuLeft);
  72. expect(result.children[1].type).toEqual(LayoutType.SideMenuCenter);
  73. expect(result.children[2].type).toEqual(LayoutType.SideMenuRight);
  74. expect(result.children[0].children.length).toEqual(1);
  75. expect(result.children[0].children[0].type).toEqual(LayoutType.Component);
  76. expect(result.children[1].children.length).toEqual(1);
  77. expect(result.children[1].children[0].type).toEqual(LayoutType.Stack);
  78. expect(result.children[2].children.length).toEqual(1);
  79. expect(result.children[2].children[0].type).toEqual(LayoutType.Component);
  80. });
  81. it('side menu center is require', () => {
  82. expect(() => uut.parse({ sideMenu: {} })).toThrow(new Error('sideMenu.center is required'));
  83. });
  84. it('top tabs', () => {
  85. const result = uut.parse(LayoutExamples.topTabs);
  86. expect(_.keys(result)).toEqual(['type', 'data', 'children']);
  87. expect(result.type).toEqual(LayoutType.TopTabs);
  88. expect(result.data).toEqual({ options: LayoutExamples.options });
  89. expect(result.children.length).toEqual(5);
  90. expect(result.children[0].type).toEqual(LayoutType.Component);
  91. expect(result.children[1].type).toEqual(LayoutType.Component);
  92. expect(result.children[2].type).toEqual(LayoutType.Component);
  93. expect(result.children[3].type).toEqual(LayoutType.Component);
  94. expect(result.children[4].type).toEqual(LayoutType.Stack);
  95. });
  96. it('complex layout example', () => {
  97. const result = uut.parse(LayoutExamples.complexLayout);
  98. expect(result.type).toEqual('SideMenuRoot');
  99. expect(result.children[1].type).toEqual('SideMenuCenter');
  100. expect(result.children[1].children[0].type).toEqual('BottomTabs');
  101. expect(result.children[1].children[0].children[2].type).toEqual('Stack');
  102. expect(result.children[1].children[0].children[2].children[0].type).toEqual('TopTabs');
  103. expect(result.children[1].children[0].children[2].children[0].children[2].type).toEqual('TopTabs');
  104. expect(result.children[1].children[0].children[2].children[0].children[2].children[4].type).toEqual('Stack');
  105. expect(result.children[1].children[0].children[2].children[0].children[2].data).toEqual({ options: { topBar: { title: 'Hello1' } } });
  106. });
  107. });
  108. it('options for all containing types', () => {
  109. const options = {};
  110. expect(uut.parse({ component: { options } }).data.options).toBe(options);
  111. expect(uut.parse({ stack: { options } }).data.options).toBe(options);
  112. expect(uut.parse({ bottomTabs: { options } }).data.options).toBe(options);
  113. expect(uut.parse({ topTabs: { options } }).data.options).toBe(options);
  114. expect(uut.parse({ sideMenu: { options, center: { component: {} } } }).data.options).toBe(options);
  115. });
  116. });
  117. const LayoutExamples = {
  118. passProps: {
  119. strProp: 'string prop',
  120. numProp: 12345,
  121. objProp: { inner: { foo: 'bar' } },
  122. fnProp: () => 'Hello from a function'
  123. },
  124. options: {
  125. topBar: {
  126. title: 'Hello1'
  127. }
  128. },
  129. singleComponent: {
  130. component: {
  131. name: 'MyReactComponent',
  132. options: this.options,
  133. passProps: this.passProps
  134. }
  135. },
  136. stackWithTopBar: {
  137. stack: {
  138. children: [
  139. {
  140. component: {
  141. name: 'MyReactComponent1'
  142. }
  143. },
  144. {
  145. component: {
  146. name: 'MyReactComponent2',
  147. options: this.options
  148. }
  149. }
  150. ],
  151. options: this.options
  152. }
  153. },
  154. bottomTabs: {
  155. bottomTabs: {
  156. children: [
  157. { ...this.stackWithTopBar },
  158. { ...this.stackWithTopBar },
  159. {
  160. component: {
  161. name: 'MyReactComponent1'
  162. }
  163. }
  164. ]
  165. }
  166. },
  167. sideMenu: {
  168. sideMenu: {
  169. left: { ...this.singleComponent },
  170. center: { ...this.stackWithTopBar },
  171. right: { ...this.singleComponent }
  172. }
  173. },
  174. topTabs: {
  175. topTabs: {
  176. children: [
  177. { ...this.singleComponent },
  178. { ...this.singleComponent },
  179. { ...this.singleComponent },
  180. { ...this.singleComponent },
  181. { ...this.stackWithTopBar }
  182. ],
  183. options: this.options
  184. }
  185. },
  186. complexLayout: {
  187. sideMenu: {
  188. left: { ...this.singleComponent },
  189. center: {
  190. bottomTabs: {
  191. children: [
  192. { ...this.stackWithTopBar },
  193. { ...this.stackWithTopBar },
  194. {
  195. stack: {
  196. children: [
  197. {
  198. topTabs: {
  199. children: [
  200. { ...this.stackWithTopBar },
  201. { ...this.stackWithTopBar },
  202. {
  203. topTabs: {
  204. options: this.options,
  205. children: [
  206. { ...this.singleComponent },
  207. { ...this.singleComponent },
  208. { ...this.singleComponent },
  209. { ...this.singleComponent },
  210. { ...this.stackWithTopBar }
  211. ]
  212. }
  213. }
  214. ]
  215. }
  216. }
  217. ]
  218. }
  219. }
  220. ]
  221. }
  222. }
  223. }
  224. }
  225. }