guideStore.spec.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import {User} from 'sentry-fixture/user';
  2. import ConfigStore from 'sentry/stores/configStore';
  3. import GuideStore from 'sentry/stores/guideStore';
  4. import ModalStore from 'sentry/stores/modalStore';
  5. import {trackAnalytics} from 'sentry/utils/analytics';
  6. jest.mock('sentry/utils/analytics');
  7. describe('GuideStore', function () {
  8. let data;
  9. beforeEach(function () {
  10. jest.clearAllMocks();
  11. ConfigStore.config = TestStubs.Config({
  12. user: User({
  13. id: '5',
  14. isSuperuser: false,
  15. dateJoined: '2020-01-01T00:00:00',
  16. }),
  17. });
  18. GuideStore.init();
  19. data = [
  20. {
  21. guide: 'issue',
  22. seen: false,
  23. },
  24. {guide: 'issue_stream', seen: true},
  25. ];
  26. GuideStore.registerAnchor('issue_number');
  27. GuideStore.registerAnchor('exception');
  28. GuideStore.registerAnchor('breadcrumbs');
  29. GuideStore.registerAnchor('issue_stream');
  30. });
  31. afterEach(() => {
  32. GuideStore.teardown();
  33. });
  34. it('should move through the steps in the guide', function () {
  35. GuideStore.fetchSucceeded(data);
  36. // Should pick the first non-seen guide in alphabetic order.
  37. expect(GuideStore.getState().currentStep).toEqual(0);
  38. expect(GuideStore.getState().currentGuide?.guide).toEqual('issue');
  39. // Should prune steps that don't have anchors.
  40. expect(GuideStore.getState().currentGuide?.steps).toHaveLength(3);
  41. GuideStore.nextStep();
  42. expect(GuideStore.getState().currentStep).toEqual(1);
  43. GuideStore.nextStep();
  44. expect(GuideStore.getState().currentStep).toEqual(2);
  45. GuideStore.closeGuide();
  46. expect(GuideStore.getState().currentGuide).toEqual(null);
  47. });
  48. it('should force show a guide with #assistant', function () {
  49. data = [
  50. {
  51. guide: 'issue',
  52. seen: true,
  53. },
  54. {guide: 'issue_stream', seen: false},
  55. ];
  56. GuideStore.fetchSucceeded(data);
  57. window.location.hash = '#assistant';
  58. window.dispatchEvent(new Event('load'));
  59. expect(GuideStore.getState().currentGuide?.guide).toEqual('issue');
  60. GuideStore.closeGuide();
  61. expect(GuideStore.getState().currentGuide?.guide).toEqual('issue_stream');
  62. window.location.hash = '';
  63. });
  64. it('should force hide', function () {
  65. expect(GuideStore.state.forceHide).toEqual(false);
  66. GuideStore.setForceHide(true);
  67. expect(GuideStore.state.forceHide).toEqual(true);
  68. GuideStore.setForceHide(false);
  69. expect(GuideStore.state.forceHide).toEqual(false);
  70. });
  71. it('should record analytics events when guide is cued', function () {
  72. const spy = jest.spyOn(GuideStore, 'recordCue');
  73. GuideStore.fetchSucceeded(data);
  74. expect(spy).toHaveBeenCalledWith('issue');
  75. expect(trackAnalytics).toHaveBeenCalledWith('assistant.guide_cued', {
  76. guide: 'issue',
  77. organization: null,
  78. });
  79. expect(spy).toHaveBeenCalledTimes(1);
  80. window.dispatchEvent(new Event('load'));
  81. expect(spy).toHaveBeenCalledTimes(1);
  82. GuideStore.nextStep();
  83. expect(spy).toHaveBeenCalledTimes(1);
  84. spy.mockRestore();
  85. });
  86. it('only shows guides with server data and content', function () {
  87. data = [
  88. {
  89. guide: 'issue',
  90. seen: true,
  91. },
  92. {
  93. guide: 'has_no_content',
  94. seen: false,
  95. },
  96. ];
  97. GuideStore.fetchSucceeded(data);
  98. expect(GuideStore.state.guides.length).toBe(1);
  99. expect(GuideStore.state.guides[0].guide).toBe(data[0].guide);
  100. });
  101. it('hides when a modal is open', function () {
  102. expect(GuideStore.getState().forceHide).toBe(false);
  103. ModalStore.openModal(() => {}, {});
  104. expect(GuideStore.getState().forceHide).toBe(true);
  105. ModalStore.closeModal();
  106. expect(GuideStore.getState().forceHide).toBe(false);
  107. });
  108. });