guideStore.spec.tsx 3.8 KB

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