react-native-navigation的迁移库

LayoutTreeCrawler.test.js 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import LayoutTypes from './LayoutTypes';
  2. import LayoutTreeCrawler from './LayoutTreeCrawler';
  3. import Store from '../containers/Store';
  4. import UniqueIdProvider from '../adapters/UniqueIdProvider.mock';
  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 = { type: LayoutTypes.ContainerStack, children: [{ type: LayoutTypes.BottomTabs }] };
  14. uut.crawl(node);
  15. expect(node.id).toEqual('ContainerStack+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 = { type: LayoutTypes.ContainerStack, children: [{ type: LayoutTypes.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 = { type: LayoutTypes.ContainerStack, children: [{ type: LayoutTypes.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: LayoutTypes.ContainerStack, children: [{ type: 'Bob' }] };
  31. expect(() => uut.crawl(node)).toThrow(new Error('Unknown layout type Bob'));
  32. });
  33. it('saves passProps into store for Container nodes', () => {
  34. const node = {
  35. type: LayoutTypes.BottomTabs, children: [
  36. { type: LayoutTypes.Container, data: { name: 'the name', passProps: { myProp: 123 } } }]
  37. };
  38. expect(store.getPropsForContainerId('Container+UNIQUE_ID')).toEqual({});
  39. uut.crawl(node);
  40. expect(store.getPropsForContainerId('Container+UNIQUE_ID')).toEqual({ myProp: 123 });
  41. });
  42. it('Containers: injects navigationOptions from original container class static property', () => {
  43. const theStyle = {};
  44. const MyContainer = class {
  45. static navigationOptions = theStyle;
  46. };
  47. const node = { type: LayoutTypes.Container, data: { name: 'theContainerName' } };
  48. store.setOriginalContainerClassForName('theContainerName', MyContainer);
  49. uut.crawl(node);
  50. expect(node.data.navigationOptions).toEqual(theStyle);
  51. });
  52. it('Containers: deepClones navigationOptions', () => {
  53. const theStyle = {};
  54. const MyContainer = class {
  55. static navigationOptions = theStyle;
  56. };
  57. const node = { type: LayoutTypes.Container, data: { name: 'theContainerName' } };
  58. store.setOriginalContainerClassForName('theContainerName', MyContainer);
  59. uut.crawl(node);
  60. expect(node.data.navigationOptions).not.toBe(theStyle);
  61. });
  62. it('Containers: must contain data name', () => {
  63. const node = { type: LayoutTypes.Container, data: {} };
  64. expect(() => uut.crawl(node)).toThrow(new Error('Missing container data.name'));
  65. });
  66. it('Containers: navigationOptions default obj', () => {
  67. const MyContainer = class { };
  68. const node = { type: LayoutTypes.Container, data: { name: 'theContainerName' } };
  69. store.setOriginalContainerClassForName('theContainerName', MyContainer);
  70. uut.crawl(node);
  71. expect(node.data.navigationOptions).toEqual({});
  72. });
  73. });