guideStore.spec.jsx 3.8 KB

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