react-native-navigation的迁移库

LayoutTreeParser.test.ts 10.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. import * as _ from 'lodash';
  2. import { LayoutTreeParser } from './LayoutTreeParser';
  3. import { LayoutType } from './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: {} })).toThrowError('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('external component', () => {
  21. expect(uut.parse(LayoutExamples.externalComponent)).toEqual({
  22. type: LayoutType.ExternalComponent,
  23. data: { name: 'MyReactComponent', options: LayoutExamples.options, passProps: LayoutExamples.passProps },
  24. children: []
  25. });
  26. });
  27. it('pass props', () => {
  28. const result = uut.parse({
  29. component: {
  30. name: 'MyScreen',
  31. passProps: LayoutExamples.passProps
  32. }
  33. });
  34. expect(result).toEqual({
  35. type: LayoutType.Component,
  36. data: { name: 'MyScreen', passProps: LayoutExamples.passProps },
  37. children: []
  38. });
  39. expect(result.data.passProps).toBe(LayoutExamples.passProps);
  40. expect(result.data.passProps.fnProp()).toEqual('Hello from a function');
  41. });
  42. it('stack of components with top bar', () => {
  43. expect(uut.parse(LayoutExamples.stackWithTopBar)).toEqual({
  44. type: LayoutType.Stack,
  45. data: {
  46. options: LayoutExamples.options
  47. },
  48. children: [
  49. {
  50. type: LayoutType.Component,
  51. data: { name: 'MyReactComponent1' },
  52. children: []
  53. },
  54. {
  55. type: LayoutType.Component,
  56. data: { name: 'MyReactComponent2', options: LayoutExamples.options },
  57. children: []
  58. }
  59. ]
  60. });
  61. });
  62. it('bottom tabs', () => {
  63. const result = uut.parse(LayoutExamples.bottomTabs);
  64. expect(_.keys(result)).toEqual(['id', 'type', 'data', 'children']);
  65. expect(result.id).toEqual(undefined);
  66. expect(result.type).toEqual(LayoutType.BottomTabs);
  67. expect(result.data).toEqual({});
  68. expect(result.children.length).toEqual(3);
  69. expect(result.children[0].type).toEqual(LayoutType.Stack);
  70. expect(result.children[1].type).toEqual(LayoutType.Stack);
  71. expect(result.children[2].type).toEqual(LayoutType.Component);
  72. });
  73. it('side menus', () => {
  74. const result = uut.parse(LayoutExamples.sideMenu);
  75. expect(_.keys(result)).toEqual(['id', 'type', 'data', 'children']);
  76. expect(result.id).toEqual(undefined);
  77. expect(result.type).toEqual(LayoutType.SideMenuRoot);
  78. expect(result.data).toEqual({});
  79. expect(result.children.length).toEqual(3);
  80. expect(result.children[0].type).toEqual(LayoutType.SideMenuLeft);
  81. expect(result.children[1].type).toEqual(LayoutType.SideMenuCenter);
  82. expect(result.children[2].type).toEqual(LayoutType.SideMenuRight);
  83. expect(result.children[0].children.length).toEqual(1);
  84. expect(result.children[0].children[0].type).toEqual(LayoutType.Component);
  85. expect(result.children[1].children.length).toEqual(1);
  86. expect(result.children[1].children[0].type).toEqual(LayoutType.Stack);
  87. expect(result.children[2].children.length).toEqual(1);
  88. expect(result.children[2].children[0].type).toEqual(LayoutType.Component);
  89. });
  90. it('side menu center is require', () => {
  91. expect(() => uut.parse({ sideMenu: {} })).toThrowError('sideMenu.center is required');
  92. });
  93. it('top tabs', () => {
  94. const result = uut.parse(LayoutExamples.topTabs);
  95. expect(_.keys(result)).toEqual(['id', 'type', 'data', 'children']);
  96. expect(result.id).toEqual(undefined);
  97. expect(result.type).toEqual(LayoutType.TopTabs);
  98. expect(result.data).toEqual({ options: LayoutExamples.options });
  99. expect(result.children.length).toEqual(5);
  100. expect(result.children[0].type).toEqual(LayoutType.Component);
  101. expect(result.children[1].type).toEqual(LayoutType.Component);
  102. expect(result.children[2].type).toEqual(LayoutType.Component);
  103. expect(result.children[3].type).toEqual(LayoutType.Component);
  104. expect(result.children[4].type).toEqual(LayoutType.Stack);
  105. });
  106. it('complex layout example', () => {
  107. const result = uut.parse(LayoutExamples.complexLayout);
  108. expect(result.type).toEqual('SideMenuRoot');
  109. expect(result.children[1].type).toEqual('SideMenuCenter');
  110. expect(result.children[1].children[0].type).toEqual('BottomTabs');
  111. expect(result.children[1].children[0].children[2].type).toEqual('Stack');
  112. expect(result.children[1].children[0].children[2].children[0].type).toEqual('TopTabs');
  113. expect(result.children[1].children[0].children[2].children[0].children[2].type).toEqual('TopTabs');
  114. expect(result.children[1].children[0].children[2].children[0].children[2].children[4].type).toEqual('Stack');
  115. expect(result.children[1].children[0].children[2].children[0].children[2].data).toEqual({ options: { topBar: { title: { text: 'Hello1'} } } });
  116. });
  117. it('split view', () => {
  118. const result = uut.parse(LayoutExamples.splitView);
  119. const master = uut.parse(LayoutExamples.splitView.splitView.master);
  120. const detail = uut.parse(LayoutExamples.splitView.splitView.detail);
  121. expect(result.type).toEqual('SplitView');
  122. expect(result.children[0]).toEqual(master);
  123. expect(result.children[1]).toEqual(detail);
  124. });
  125. });
  126. it('options for all containing types', () => {
  127. expect(uut.parse({ component: { options } }).data.options).toBe(options);
  128. expect(uut.parse({ stack: { options } }).data.options).toBe(options);
  129. expect(uut.parse({ bottomTabs: { options } }).data.options).toBe(options);
  130. expect(uut.parse({ topTabs: { options } }).data.options).toBe(options);
  131. expect(uut.parse({ sideMenu: { options, center: { component: {} } } }).data.options).toBe(options);
  132. expect(uut.parse(LayoutExamples.splitView).data.options).toBe(optionsSplitView);
  133. });
  134. it('pass user provided id as is', () => {
  135. const component = { id: 'compId' };
  136. expect(uut.parse({ component }).id).toEqual('compId');
  137. expect(uut.parse({ stack: { id: 'stackId' } }).id).toEqual('stackId');
  138. expect(uut.parse({ stack: { children: [{ component }] } }).children[0].id).toEqual('compId');
  139. expect(uut.parse({ bottomTabs: { id: 'myId' } }).id).toEqual('myId');
  140. expect(uut.parse({ bottomTabs: { children: [{ component }] } }).children[0].id).toEqual('compId');
  141. expect(uut.parse({ topTabs: { id: 'myId' } }).id).toEqual('myId');
  142. expect(uut.parse({ topTabs: { children: [{ component }] } }).children[0].id).toEqual('compId');
  143. expect(uut.parse({ sideMenu: { id: 'myId', center: { component } } }).id).toEqual('myId');
  144. expect(uut.parse({ sideMenu: { center: { id: 'myId', component } } }).children[0].id).toEqual('myId');
  145. expect(uut.parse({ sideMenu: { center: { component }, left: { id: 'theId', component } } }).children[0].id).toEqual('theId');
  146. expect(uut.parse({ sideMenu: { center: { component }, right: { id: 'theId', component } } }).children[1].id).toEqual('theId');
  147. });
  148. });
  149. /* Layout Examples: */
  150. const passProps = {
  151. strProp: 'string prop',
  152. numProp: 12345,
  153. objProp: { inner: { foo: 'bar' } },
  154. fnProp: () => 'Hello from a function'
  155. };
  156. const options = {
  157. topBar: {
  158. title: {
  159. text: 'Hello1'
  160. }
  161. }
  162. };
  163. const optionsSplitView = {
  164. displayMode: 'auto',
  165. primaryEdge: 'leading',
  166. minWidth: 150,
  167. maxWidth: 300,
  168. };
  169. const singleComponent = {
  170. component: {
  171. name: 'MyReactComponent',
  172. options,
  173. passProps
  174. }
  175. };
  176. const externalComponent = {
  177. externalComponent: {
  178. name: 'MyReactComponent',
  179. options,
  180. passProps
  181. }
  182. };
  183. const stackWithTopBar = {
  184. stack: {
  185. children: [
  186. {
  187. component: {
  188. name: 'MyReactComponent1'
  189. }
  190. },
  191. {
  192. component: {
  193. name: 'MyReactComponent2',
  194. options
  195. }
  196. }
  197. ],
  198. options
  199. }
  200. };
  201. const bottomTabs = {
  202. bottomTabs: {
  203. children: [
  204. stackWithTopBar,
  205. stackWithTopBar,
  206. {
  207. component: {
  208. name: 'MyReactComponent1'
  209. }
  210. }
  211. ]
  212. }
  213. };
  214. const sideMenu = {
  215. sideMenu: {
  216. left: singleComponent,
  217. center: stackWithTopBar,
  218. right: singleComponent
  219. }
  220. };
  221. const topTabs = {
  222. topTabs: {
  223. children: [
  224. singleComponent,
  225. singleComponent,
  226. singleComponent,
  227. singleComponent,
  228. stackWithTopBar
  229. ],
  230. options
  231. }
  232. };
  233. const complexLayout = {
  234. sideMenu: {
  235. left: singleComponent,
  236. center: {
  237. bottomTabs: {
  238. children: [
  239. stackWithTopBar,
  240. stackWithTopBar,
  241. {
  242. stack: {
  243. children: [
  244. {
  245. topTabs: {
  246. children: [
  247. stackWithTopBar,
  248. stackWithTopBar,
  249. {
  250. topTabs: {
  251. options,
  252. children: [
  253. singleComponent,
  254. singleComponent,
  255. singleComponent,
  256. singleComponent,
  257. stackWithTopBar
  258. ]
  259. }
  260. }
  261. ]
  262. }
  263. }
  264. ]
  265. }
  266. }
  267. ]
  268. }
  269. }
  270. }
  271. };
  272. const splitView = {
  273. splitView: {
  274. master: {
  275. stack: {
  276. children: [
  277. singleComponent,
  278. ],
  279. options
  280. },
  281. },
  282. detail: stackWithTopBar,
  283. options: optionsSplitView,
  284. },
  285. };
  286. const LayoutExamples = {
  287. passProps,
  288. options,
  289. singleComponent,
  290. stackWithTopBar,
  291. bottomTabs,
  292. sideMenu,
  293. topTabs,
  294. complexLayout,
  295. externalComponent,
  296. splitView
  297. };