react-native-navigation的迁移库

EventsRegistry.test.ts 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { EventsRegistry } from './EventsRegistry';
  2. import { NativeEventsReceiver } from '../adapters/NativeEventsReceiver.mock';
  3. import { CommandsObserver } from './CommandsObserver';
  4. describe('EventsRegistry', () => {
  5. let uut: EventsRegistry;
  6. const mockNativeEventsReceiver = new NativeEventsReceiver();
  7. let commandsObserver: CommandsObserver;
  8. beforeEach(() => {
  9. commandsObserver = new CommandsObserver();
  10. uut = new EventsRegistry(mockNativeEventsReceiver, commandsObserver);
  11. });
  12. it('exposes onAppLaunched event', () => {
  13. const subscription = {};
  14. const cb = jest.fn();
  15. mockNativeEventsReceiver.registerOnAppLaunched.mockReturnValueOnce(subscription);
  16. const result = uut.onAppLaunched(cb);
  17. expect(result).toBe(subscription);
  18. expect(mockNativeEventsReceiver.registerOnAppLaunched).toHaveBeenCalledTimes(1);
  19. expect(mockNativeEventsReceiver.registerOnAppLaunched).toHaveBeenCalledWith(cb);
  20. });
  21. it('exposes componentDidAppear event', () => {
  22. const subscription = {};
  23. const cb = jest.fn();
  24. mockNativeEventsReceiver.registerComponentDidAppear.mockReturnValueOnce(subscription);
  25. const result = uut.componentDidAppear(cb);
  26. expect(result).toBe(subscription);
  27. expect(mockNativeEventsReceiver.registerComponentDidAppear).toHaveBeenCalledTimes(1);
  28. mockNativeEventsReceiver.registerComponentDidAppear.mock.calls[0][0]({ componentId: 'theId', componentName: 'theName' });
  29. expect(cb).toHaveBeenCalledWith('theId', 'theName');
  30. });
  31. it('exposes componentDidDisappear event', () => {
  32. const subscription = {};
  33. const cb = jest.fn();
  34. mockNativeEventsReceiver.registerComponentDidDisappear.mockReturnValueOnce(subscription);
  35. const result = uut.componentDidDisappear(cb);
  36. expect(result).toBe(subscription);
  37. expect(mockNativeEventsReceiver.registerComponentDidDisappear).toHaveBeenCalledTimes(1);
  38. mockNativeEventsReceiver.registerComponentDidDisappear.mock.calls[0][0]({ componentId: 'theId', componentName: 'theName' });
  39. expect(cb).toHaveBeenCalledWith('theId', 'theName');
  40. });
  41. it('exposes onNavigationButtonPressed event', () => {
  42. const subscription = {};
  43. const cb = jest.fn();
  44. mockNativeEventsReceiver.registerOnNavigationButtonPressed.mockReturnValueOnce(subscription);
  45. const result = uut.onNavigationButtonPressed(cb);
  46. expect(result).toBe(subscription);
  47. expect(mockNativeEventsReceiver.registerOnNavigationButtonPressed).toHaveBeenCalledTimes(1);
  48. mockNativeEventsReceiver.registerOnNavigationButtonPressed.mock.calls[0][0]({ componentId: 'theId', buttonId: 'theBtnId' });
  49. expect(cb).toHaveBeenCalledWith('theId', 'theBtnId');
  50. });
  51. it('exposes onNavigationCommand registers listener to commandObserver', () => {
  52. const cb = jest.fn();
  53. const result = uut.onNavigationCommand(cb);
  54. expect(result).toBeDefined();
  55. commandsObserver.notify('theCommandName', { x: 1 });
  56. expect(cb).toHaveBeenCalledTimes(1);
  57. expect(cb).toHaveBeenCalledWith('theCommandName', { x: 1 });
  58. });
  59. it('onNavigationCommand unregister', () => {
  60. const cb = jest.fn();
  61. const result = uut.onNavigationCommand(cb);
  62. result.remove();
  63. commandsObserver.notify('theCommandName', { x: 1 });
  64. expect(cb).not.toHaveBeenCalled();
  65. });
  66. });