guideStore.spec.tsx 3.7 KB

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