react-native-navigation的迁移库

LayoutTreeParser.test.ts 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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: 'Hello1' } } });
  116. });
  117. });
  118. it('options for all containing types', () => {
  119. expect(uut.parse({ component: { options } }).data.options).toBe(options);
  120. expect(uut.parse({ stack: { options } }).data.options).toBe(options);
  121. expect(uut.parse({ bottomTabs: { options } }).data.options).toBe(options);
  122. expect(uut.parse({ topTabs: { options } }).data.options).toBe(options);
  123. expect(uut.parse({ sideMenu: { options, center: { component: {} } } }).data.options).toBe(options);
  124. });
  125. it('pass user provided id as is', () => {
  126. const component = { id: 'compId' };
  127. expect(uut.parse({ component }).id).toEqual('compId');
  128. expect(uut.parse({ stack: { id: 'stackId' } }).id).toEqual('stackId');
  129. expect(uut.parse({ stack: { children: [{ component }] } }).children[0].id).toEqual('compId');
  130. expect(uut.parse({ bottomTabs: { id: 'myId' } }).id).toEqual('myId');
  131. expect(uut.parse({ bottomTabs: { children: [{ component }] } }).children[0].id).toEqual('compId');
  132. expect(uut.parse({ topTabs: { id: 'myId' } }).id).toEqual('myId');
  133. expect(uut.parse({ topTabs: { children: [{ component }] } }).children[0].id).toEqual('compId');
  134. expect(uut.parse({ sideMenu: { id: 'myId', center: { component } } }).id).toEqual('myId');
  135. expect(uut.parse({ sideMenu: { center: { id: 'myId', component } } }).children[0].id).toEqual('myId');
  136. expect(uut.parse({ sideMenu: { center: { component }, left: { id: 'theId', component } } }).children[0].id).toEqual('theId');
  137. expect(uut.parse({ sideMenu: { center: { component }, right: { id: 'theId', component } } }).children[1].id).toEqual('theId');
  138. });
  139. });
  140. /* Layout Examples: */
  141. const passProps = {
  142. strProp: 'string prop',
  143. numProp: 12345,
  144. objProp: { inner: { foo: 'bar' } },
  145. fnProp: () => 'Hello from a function'
  146. };
  147. const options = {
  148. topBar: {
  149. title: 'Hello1'
  150. }
  151. };
  152. const singleComponent = {
  153. component: {
  154. name: 'MyReactComponent',
  155. options,
  156. passProps
  157. }
  158. };
  159. const externalComponent = {
  160. externalComponent: {
  161. name: 'MyReactComponent',
  162. options,
  163. passProps
  164. }
  165. };
  166. const stackWithTopBar = {
  167. stack: {
  168. children: [
  169. {
  170. component: {
  171. name: 'MyReactComponent1'
  172. }
  173. },
  174. {
  175. component: {
  176. name: 'MyReactComponent2',
  177. options
  178. }
  179. }
  180. ],
  181. options
  182. }
  183. };
  184. const bottomTabs = {
  185. bottomTabs: {
  186. children: [
  187. stackWithTopBar,
  188. stackWithTopBar,
  189. {
  190. component: {
  191. name: 'MyReactComponent1'
  192. }
  193. }
  194. ]
  195. }
  196. };
  197. const sideMenu = {
  198. sideMenu: {
  199. left: singleComponent,
  200. center: stackWithTopBar,
  201. right: singleComponent
  202. }
  203. };
  204. const topTabs = {
  205. topTabs: {
  206. children: [
  207. singleComponent,
  208. singleComponent,
  209. singleComponent,
  210. singleComponent,
  211. stackWithTopBar
  212. ],
  213. options
  214. }
  215. };
  216. const complexLayout = {
  217. sideMenu: {
  218. left: singleComponent,
  219. center: {
  220. bottomTabs: {
  221. children: [
  222. stackWithTopBar,
  223. stackWithTopBar,
  224. {
  225. stack: {
  226. children: [
  227. {
  228. topTabs: {
  229. children: [
  230. stackWithTopBar,
  231. stackWithTopBar,
  232. {
  233. topTabs: {
  234. options,
  235. children: [
  236. singleComponent,
  237. singleComponent,
  238. singleComponent,
  239. singleComponent,
  240. stackWithTopBar
  241. ]
  242. }
  243. }
  244. ]
  245. }
  246. }
  247. ]
  248. }
  249. }
  250. ]
  251. }
  252. }
  253. }
  254. };
  255. const LayoutExamples = {
  256. passProps,
  257. options,
  258. singleComponent,
  259. stackWithTopBar,
  260. bottomTabs,
  261. sideMenu,
  262. topTabs,
  263. complexLayout,
  264. externalComponent
  265. };