react-native-navigation的迁移库

Navigation.test.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. describe('Navigation', () => {
  2. let Navigation;
  3. beforeEach(() => {
  4. //TODO don't mock here, just create the actual object and assert on basic behavior
  5. jest.mock('./adapters/UniqueIdProvider');
  6. jest.mock('./adapters/NativeCommandsSender');
  7. jest.mock('./adapters/NativeEventsReceiver');
  8. jest.mock('./containers/ContainerRegistry');
  9. jest.mock('./commands/Commands');
  10. Navigation = require('./Navigation').default;
  11. });
  12. it('registerContainer delegates to ContainerRegistry', () => {
  13. expect(Navigation.containerRegistry.registerContainer).not.toHaveBeenCalled();
  14. const fn = jest.fn();
  15. Navigation.registerContainer('name', fn);
  16. expect(Navigation.containerRegistry.registerContainer).toHaveBeenCalledTimes(1);
  17. expect(Navigation.containerRegistry.registerContainer).toHaveBeenCalledWith('name', fn);
  18. });
  19. it('setRoot delegates to Commands', async () => {
  20. Navigation.commands.setRoot.mockReturnValue(Promise.resolve('result'));
  21. const params = {};
  22. const result = await Navigation.setRoot(params);
  23. expect(result).toEqual('result');
  24. expect(Navigation.commands.setRoot).toHaveBeenCalledTimes(1);
  25. expect(Navigation.commands.setRoot).toHaveBeenCalledWith(params);
  26. });
  27. it('showModal delegates to Commands', async () => {
  28. Navigation.commands.showModal.mockReturnValue(Promise.resolve('result'));
  29. const params = {};
  30. const result = await Navigation.showModal(params);
  31. expect(result).toEqual('result');
  32. expect(Navigation.commands.showModal).toHaveBeenCalledWith(params);
  33. expect(Navigation.commands.showModal).toHaveBeenCalledTimes(1);
  34. });
  35. it('dismissModal delegates to Commands', async () => {
  36. Navigation.commands.dismissModal.mockReturnValue(Promise.resolve('result'));
  37. const params = {};
  38. const result = await Navigation.dismissModal(params);
  39. expect(result).toEqual('result');
  40. expect(Navigation.commands.dismissModal).toHaveBeenCalledTimes(1);
  41. expect(Navigation.commands.dismissModal).toHaveBeenCalledWith(params);
  42. });
  43. it('dismissAllModals', async () => {
  44. Navigation.commands.dismissAllModals.mockReturnValue(Promise.resolve('result'));
  45. const result = await Navigation.dismissAllModals();
  46. expect(Navigation.commands.dismissAllModals).toHaveBeenCalledTimes(1);
  47. expect(result).toEqual('result');
  48. });
  49. it('events return public events', () => {
  50. const cb = jest.fn();
  51. Navigation.events().onAppLaunched(cb);
  52. expect(Navigation.nativeEventsReceiver.appLaunched).toHaveBeenCalledTimes(1);
  53. expect(Navigation.nativeEventsReceiver.appLaunched).toHaveBeenCalledWith(cb);
  54. });
  55. it('starts listening and handles internal events', () => {
  56. expect(Navigation.nativeEventsReceiver.containerStart).toHaveBeenCalledTimes(1);
  57. });
  58. it('push delegates to commands', async () => {
  59. Navigation.commands.push.mockReturnValue(Promise.resolve('result'));
  60. const params = {};
  61. const result = await Navigation.push('theContainerId', params);
  62. expect(result).toEqual('result');
  63. expect(Navigation.commands.push).toHaveBeenCalledWith('theContainerId', params);
  64. expect(Navigation.commands.push).toHaveBeenCalledTimes(1);
  65. });
  66. it('pop delegates to commands', async () => {
  67. Navigation.commands.pop.mockReturnValue(Promise.resolve('result'));
  68. const result = await Navigation.pop('theContainerId');
  69. expect(result).toEqual('result');
  70. expect(Navigation.commands.pop).toHaveBeenCalledWith('theContainerId');
  71. expect(Navigation.commands.pop).toHaveBeenCalledTimes(1);
  72. });
  73. it('popTo delegates to commands', async () => {
  74. Navigation.commands.popTo.mockReturnValue(Promise.resolve('result'));
  75. const params = {};
  76. const result = await Navigation.popTo('theContainerId');
  77. expect(result).toEqual('result');
  78. expect(Navigation.commands.popTo).toHaveBeenCalledWith('theContainerId');
  79. expect(Navigation.commands.popTo).toHaveBeenCalledTimes(1);
  80. });
  81. it('popToRoot delegates to commands', async () => {
  82. Navigation.commands.popToRoot.mockReturnValue(Promise.resolve('result'));
  83. const params = {};
  84. const result = await Navigation.popToRoot('theContainerId');
  85. expect(result).toEqual('result');
  86. expect(Navigation.commands.popToRoot).toHaveBeenCalledWith('theContainerId');
  87. expect(Navigation.commands.popToRoot).toHaveBeenCalledTimes(1);
  88. });
  89. });