guideStore.spec.jsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import React from 'react';
  2. import GuideStore from 'app/stores/guideStore';
  3. import GuideAnchor from 'app/components/assistant/guideAnchor';
  4. describe('GuideStore', function() {
  5. let sandbox;
  6. let anchor1 = <GuideAnchor target="target 1" type="text" />;
  7. let anchor2 = <GuideAnchor target="target 2" type="text" />;
  8. let data;
  9. beforeEach(function() {
  10. GuideStore.init();
  11. sandbox = sinon.sandbox.create();
  12. data = {
  13. issue: {
  14. cue: 'Click here for a tour of the issue page',
  15. id: 1,
  16. page: 'issue',
  17. required_targets: ['target 1'],
  18. steps: [
  19. {message: 'Message 1', target: 'target 1', title: '1. Title 1'},
  20. {message: 'Message 2', target: 'target 2', title: '2. Title 2'},
  21. {message: 'Message 3', target: 'target 3', title: '3. Title 3'},
  22. ],
  23. seen: false,
  24. },
  25. other: {
  26. cue: 'Some other guide here',
  27. id: 2,
  28. page: 'random',
  29. required_targets: ['target 1'],
  30. steps: [
  31. {message: 'Message 1', target: 'target 1', title: '1. Title 1'},
  32. {message: 'Message 2', target: 'target 2', title: '2. Title 2'},
  33. ],
  34. seen: false,
  35. },
  36. };
  37. });
  38. afterEach(function() {
  39. sandbox.restore();
  40. });
  41. it('should add guides to store', function() {
  42. GuideStore.onFetchSucceeded(data);
  43. expect(GuideStore.state.guides).toEqual(data);
  44. expect(GuideStore.state.currentStep).toEqual(0);
  45. });
  46. it('should register anchors', function() {
  47. GuideStore.onRegisterAnchor(anchor1);
  48. GuideStore.onRegisterAnchor(anchor2);
  49. expect(GuideStore.state.anchors).toEqual(new Set([anchor1, anchor2]));
  50. });
  51. it('should move through the steps in the guide', function() {
  52. GuideStore.onRegisterAnchor(anchor1);
  53. GuideStore.onRegisterAnchor(anchor2);
  54. GuideStore.onFetchSucceeded(data);
  55. // GuideStore should prune steps that don't have anchors.
  56. expect(GuideStore.state.currentGuide.steps).toHaveLength(2);
  57. expect(GuideStore.state.currentGuide.seen).toEqual(false);
  58. GuideStore.onNextStep();
  59. expect(GuideStore.state.currentStep).toEqual(1);
  60. GuideStore.onNextStep();
  61. expect(GuideStore.state.currentStep).toEqual(2);
  62. GuideStore.onCloseGuideOrSupport();
  63. expect(
  64. Object.keys(GuideStore.state.guides).filter(
  65. key => GuideStore.state.guides[key].seen == true
  66. )
  67. ).toEqual(['issue']);
  68. });
  69. it('should not show seen guides', function() {
  70. data.issue.seen = true;
  71. data.other.seen = true;
  72. GuideStore.onRegisterAnchor(anchor1);
  73. GuideStore.onRegisterAnchor(anchor2);
  74. GuideStore.onFetchSucceeded(data);
  75. expect(GuideStore.state.currentGuide).toEqual(null);
  76. });
  77. it('should force show a guide', function() {
  78. data.issue.seen = true;
  79. GuideStore.onRegisterAnchor(anchor1);
  80. GuideStore.onRegisterAnchor(anchor2);
  81. GuideStore.state.forceShow = true;
  82. GuideStore.onFetchSucceeded(data);
  83. expect(GuideStore.state.currentGuide.id).toEqual(1);
  84. GuideStore.onCloseGuideOrSupport();
  85. expect(GuideStore.state.currentGuide.id).toEqual(2);
  86. GuideStore.onCloseGuideOrSupport();
  87. expect(GuideStore.state.currentGuide).toEqual(null);
  88. });
  89. it('should record analytics events when guide is cued', function() {
  90. let mockRecordCue = jest.fn();
  91. GuideStore.recordCue = mockRecordCue;
  92. GuideStore.onRegisterAnchor(anchor1);
  93. GuideStore.onRegisterAnchor(anchor2);
  94. GuideStore.onFetchSucceeded(data);
  95. expect(mockRecordCue).toHaveBeenCalledWith(data.issue.id, data.issue.cue);
  96. expect(mockRecordCue).toHaveBeenCalledTimes(1);
  97. GuideStore.onCloseGuideOrSupport();
  98. // Should trigger a record when a new guide is cued
  99. expect(GuideStore.state.currentGuide).toEqual(data.other);
  100. expect(mockRecordCue).toHaveBeenCalledWith(data.other.id, data.other.cue);
  101. expect(mockRecordCue).toHaveBeenCalledTimes(2);
  102. });
  103. it('should not send multiple cue analytics events for same guide', function() {
  104. let mockRecordCue = jest.fn();
  105. GuideStore.recordCue = mockRecordCue;
  106. GuideStore.onRegisterAnchor(anchor1);
  107. GuideStore.onRegisterAnchor(anchor2);
  108. GuideStore.onFetchSucceeded(data);
  109. expect(mockRecordCue).toHaveBeenCalledWith(data.issue.id, data.issue.cue);
  110. expect(mockRecordCue).toHaveBeenCalledTimes(1);
  111. GuideStore.updateCurrentGuide();
  112. expect(mockRecordCue).toHaveBeenCalledTimes(1);
  113. });
  114. });