guideStore.spec.tsx 3.6 KB

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