makeSafeRefluxStore.spec.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import {createAction, createStore, Listenable} from 'reflux';
  2. import {makeSafeRefluxStore, SafeRefluxStore} from 'sentry/utils/makeSafeRefluxStore';
  3. describe('makeSafeRefluxStore', () => {
  4. it('cleans up all listeners on teardown', () => {
  5. const safeStore = createStore(makeSafeRefluxStore({})) as unknown as SafeRefluxStore;
  6. const fakeAction = createAction({'status update': ''});
  7. const anotherAction = createAction({'other status update': ''});
  8. safeStore.unsubscribeListeners.push(safeStore.listenTo(fakeAction, () => null));
  9. safeStore.unsubscribeListeners.push(safeStore.listenTo(anotherAction, () => null));
  10. // @ts-ignore idk why this thinks it's a never type
  11. const stopListenerSpy = jest.spyOn(safeStore.unsubscribeListeners[0], 'stop');
  12. safeStore.teardown();
  13. expect(stopListenerSpy).toHaveBeenCalled();
  14. expect(safeStore.unsubscribeListeners).toHaveLength(0);
  15. });
  16. it('does not override unsubscribeListeners', () => {
  17. const stop = jest.fn();
  18. const subscription = {stop, listenable: {} as unknown as Listenable};
  19. const safeStore = createStore(
  20. makeSafeRefluxStore({
  21. unsubscribeListeners: [subscription],
  22. })
  23. ) as unknown as SafeRefluxStore;
  24. expect(safeStore.unsubscribeListeners[0]).toBe(subscription);
  25. });
  26. it('tears down subscriptions', () => {
  27. const stop = jest.fn();
  28. const subscription = {stop, listenable: {} as unknown as Listenable};
  29. const safeStore = createStore(
  30. makeSafeRefluxStore({
  31. unsubscribeListeners: [subscription],
  32. })
  33. ) as unknown as SafeRefluxStore;
  34. safeStore.teardown();
  35. expect(stop).toHaveBeenCalled();
  36. expect(safeStore.unsubscribeListeners.length).toBe(0);
  37. });
  38. it('allows for custom tear down implementation', () => {
  39. const teardown = jest.fn();
  40. const subscription = {
  41. stop: jest.fn(),
  42. listenable: {} as unknown as Listenable,
  43. };
  44. const safeStore = createStore(
  45. makeSafeRefluxStore({
  46. unsubscribeListeners: [subscription],
  47. teardown: function () {
  48. teardown();
  49. },
  50. })
  51. ) as unknown as SafeRefluxStore;
  52. safeStore.teardown();
  53. expect(teardown).toHaveBeenCalled();
  54. expect(safeStore.unsubscribeListeners[0]).toBe(subscription);
  55. });
  56. });