react-native-navigation的迁移库

Commands.test.js 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. const SimpleLayouts = require('./SimpleLayouts');
  2. const LayoutTreeParser = require('./LayoutTreeParser');
  3. const LayoutTreeCrawler = require('./LayoutTreeCrawler');
  4. const Store = require('../containers/Store');
  5. const UniqueIdProvider = require('../adapters/UniqueIdProvider.mock');
  6. const NativeCommandsSender = require('../adapters/NativeCommandsSender.mock');
  7. const Commands = require('./Commands');
  8. describe('Commands', () => {
  9. let uut;
  10. let mockCommandsSender;
  11. let store;
  12. beforeEach(() => {
  13. mockCommandsSender = new NativeCommandsSender();
  14. store = new Store();
  15. uut = new Commands(mockCommandsSender, new LayoutTreeParser(), new LayoutTreeCrawler(new UniqueIdProvider(), store));
  16. });
  17. describe('setRoot', () => {
  18. it('sends setRoot to native after parsing into a correct layout tree', () => {
  19. uut.setRoot({
  20. container: {
  21. name: 'com.example.MyScreen'
  22. }
  23. });
  24. expect(mockCommandsSender.setRoot).toHaveBeenCalledTimes(1);
  25. expect(mockCommandsSender.setRoot).toHaveBeenCalledWith({
  26. type: 'ContainerStack',
  27. id: 'ContainerStack+UNIQUE_ID',
  28. data: {},
  29. children: [
  30. {
  31. type: 'Container',
  32. id: 'Container+UNIQUE_ID',
  33. children: [],
  34. data: {
  35. name: 'com.example.MyScreen',
  36. navigationOptions: {}
  37. }
  38. }
  39. ]
  40. });
  41. });
  42. it('deep clones input to avoid mutation errors', () => {
  43. const obj = {};
  44. uut.setRoot({ container: { name: 'bla', inner: obj } });
  45. expect(mockCommandsSender.setRoot.mock.calls[0][0].children[0].data.inner).not.toBe(obj);
  46. });
  47. it('passProps into containers', () => {
  48. expect(store.getPropsForContainerId('Container+UNIQUE_ID')).toEqual({});
  49. uut.setRoot(SimpleLayouts.singleScreenWithAditionalParams);
  50. expect(store.getPropsForContainerId('Container+UNIQUE_ID')).toEqual(SimpleLayouts.passProps);
  51. });
  52. it('returns a promise with the resolved layout', async () => {
  53. mockCommandsSender.setRoot.mockReturnValue(Promise.resolve('the resolved layout'));
  54. const result = await uut.setRoot({ container: { name: 'com.example.MyScreen' } });
  55. expect(result).toEqual('the resolved layout');
  56. });
  57. });
  58. describe('setOptions', () => {
  59. it('deep clones input to avoid mutation errors', () => {
  60. const obj = { title: 'test' };
  61. uut.setOptions('theContainerId', obj);
  62. expect(mockCommandsSender.setOptions.mock.calls[0][1]).not.toBe(obj);
  63. });
  64. it('passes options for container', () => {
  65. uut.setOptions('theContainerId', { title: '1' });
  66. expect(mockCommandsSender.setOptions).toHaveBeenCalledTimes(1);
  67. expect(mockCommandsSender.setOptions).toHaveBeenCalledWith('theContainerId', { title: '1' });
  68. });
  69. });
  70. describe('showModal', () => {
  71. it('sends command to native after parsing into a correct layout tree', () => {
  72. uut.showModal({
  73. container: {
  74. name: 'com.example.MyScreen'
  75. }
  76. });
  77. expect(mockCommandsSender.showModal).toHaveBeenCalledTimes(1);
  78. expect(mockCommandsSender.showModal).toHaveBeenCalledWith({
  79. type: 'ContainerStack',
  80. id: 'ContainerStack+UNIQUE_ID',
  81. data: {},
  82. children: [{
  83. type: 'Container',
  84. id: 'Container+UNIQUE_ID',
  85. data: {
  86. name: 'com.example.MyScreen',
  87. navigationOptions: {}
  88. },
  89. children: []
  90. }]
  91. });
  92. });
  93. it('deep clones input to avoid mutation errors', () => {
  94. const obj = {};
  95. uut.showModal({ container: { name: 'name', inner: obj } });
  96. expect(mockCommandsSender.showModal.mock.calls[0][0].data.inner).not.toBe(obj);
  97. });
  98. it('passProps into containers', () => {
  99. expect(store.getPropsForContainerId('Container+UNIQUE_ID')).toEqual({});
  100. uut.showModal({
  101. container: {
  102. name: 'com.example.MyScreen',
  103. passProps: SimpleLayouts.passProps
  104. }
  105. });
  106. expect(store.getPropsForContainerId('Container+UNIQUE_ID')).toEqual(SimpleLayouts.passProps);
  107. });
  108. it('returns a promise with the resolved layout', async () => {
  109. mockCommandsSender.showModal.mockReturnValue(Promise.resolve('the resolved layout'));
  110. const result = await uut.showModal({ container: { name: 'com.example.MyScreen' } });
  111. expect(result).toEqual('the resolved layout');
  112. });
  113. });
  114. describe('dismissModal', () => {
  115. it('sends command to native', () => {
  116. uut.dismissModal('myUniqueId');
  117. expect(mockCommandsSender.dismissModal).toHaveBeenCalledTimes(1);
  118. expect(mockCommandsSender.dismissModal).toHaveBeenCalledWith('myUniqueId');
  119. });
  120. it('returns a promise with the id', async () => {
  121. mockCommandsSender.dismissModal.mockReturnValue(Promise.resolve('the id'));
  122. const result = await uut.dismissModal('myUniqueId');
  123. expect(result).toEqual('the id');
  124. });
  125. });
  126. describe('dismissAllModals', () => {
  127. it('sends command to native', () => {
  128. uut.dismissAllModals();
  129. expect(mockCommandsSender.dismissAllModals).toHaveBeenCalledTimes(1);
  130. expect(mockCommandsSender.dismissAllModals).toHaveBeenCalledWith();
  131. });
  132. it('returns a promise with the id', async () => {
  133. mockCommandsSender.dismissAllModals.mockReturnValue(Promise.resolve('the id'));
  134. const result = await uut.dismissAllModals();
  135. expect(result).toEqual('the id');
  136. });
  137. });
  138. describe('push', () => {
  139. it('deep clones input to avoid mutation errors', () => {
  140. const obj = {};
  141. uut.push('theContainerId', { name: 'name', inner: { foo: obj } });
  142. expect(mockCommandsSender.push.mock.calls[0][1].data.inner.foo).not.toBe(obj);
  143. });
  144. it('resolves with the parsed layout', async () => {
  145. mockCommandsSender.push.mockReturnValue(Promise.resolve('the resolved layout'));
  146. const result = await uut.push('theContainerId', { name: 'com.example.MyScreen' });
  147. expect(result).toEqual('the resolved layout');
  148. });
  149. it('parses into correct layout node and sends to native', () => {
  150. uut.push('theContainerId', { name: 'com.example.MyScreen' });
  151. expect(mockCommandsSender.push).toHaveBeenCalledTimes(1);
  152. expect(mockCommandsSender.push).toHaveBeenCalledWith('theContainerId', {
  153. type: 'Container',
  154. id: 'Container+UNIQUE_ID',
  155. data: {
  156. name: 'com.example.MyScreen',
  157. navigationOptions: {}
  158. },
  159. children: []
  160. });
  161. });
  162. });
  163. describe('pop', () => {
  164. it('pops a container, passing containerId', () => {
  165. uut.pop('theContainerId');
  166. expect(mockCommandsSender.pop).toHaveBeenCalledTimes(1);
  167. expect(mockCommandsSender.pop).toHaveBeenCalledWith('theContainerId');
  168. });
  169. it('pop returns a promise that resolves to containerId', async () => {
  170. mockCommandsSender.pop.mockReturnValue(Promise.resolve('theContainerId'));
  171. const result = await uut.pop('theContainerId');
  172. expect(result).toEqual('theContainerId');
  173. });
  174. });
  175. describe('popTo', () => {
  176. it('pops all containers until the passed Id is top', () => {
  177. uut.popTo('theContainerId');
  178. expect(mockCommandsSender.popTo).toHaveBeenCalledTimes(1);
  179. expect(mockCommandsSender.popTo).toHaveBeenCalledWith('theContainerId');
  180. });
  181. it('returns a promise that resolves to targetId', async () => {
  182. mockCommandsSender.popTo.mockReturnValue(Promise.resolve('theContainerId'));
  183. const result = await uut.popTo('theContainerId');
  184. expect(result).toEqual('theContainerId');
  185. });
  186. });
  187. describe('popToRoot', () => {
  188. it('pops all containers to root', () => {
  189. uut.popToRoot('theContainerId');
  190. expect(mockCommandsSender.popToRoot).toHaveBeenCalledTimes(1);
  191. expect(mockCommandsSender.popToRoot).toHaveBeenCalledWith('theContainerId');
  192. });
  193. it('returns a promise that resolves to targetId', async () => {
  194. mockCommandsSender.popToRoot.mockReturnValue(Promise.resolve('theContainerId'));
  195. const result = await uut.popToRoot('theContainerId');
  196. expect(result).toEqual('theContainerId');
  197. });
  198. });
  199. });