guideStore.spec.jsx 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import React from 'react';
  2. import GuideStore from 'app/stores/guideStore';
  3. import GuideAnchor from 'app/components/assistant/guideAnchor';
  4. import ConfigStore from 'app/stores/configStore';
  5. describe('GuideStore', function() {
  6. const anchor1 = <GuideAnchor target="target 1" />;
  7. const anchor2 = <GuideAnchor target="target 2" />;
  8. let data;
  9. beforeEach(function() {
  10. ConfigStore.config = {
  11. user: {
  12. isSuperuser: true,
  13. },
  14. };
  15. GuideStore.init();
  16. data = {
  17. Guide1: {
  18. id: 1,
  19. required_targets: ['target 1'],
  20. steps: [
  21. {message: 'Message 1', target: 'target 1', title: '1. Title 1'},
  22. {message: 'Message 2', target: 'target 2', title: '2. Title 2'},
  23. {message: 'Message 3', target: 'target 3', title: '3. Title 3'},
  24. ],
  25. seen: true,
  26. },
  27. Guide2: {
  28. id: 2,
  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. GuideStore.onRegisterAnchor(anchor1);
  38. GuideStore.onRegisterAnchor(anchor2);
  39. });
  40. afterEach(function() {});
  41. it('should move through the steps in the guide', function() {
  42. GuideStore.onFetchSucceeded(data);
  43. const guide = GuideStore.state.currentGuide;
  44. // Should pick the first non-seen guide in alphabetic order.
  45. expect(guide.id).toEqual(2);
  46. expect(guide.steps).toHaveLength(2);
  47. GuideStore.onNextStep();
  48. expect(GuideStore.state.currentStep).toEqual(1);
  49. GuideStore.onCloseGuide();
  50. expect(GuideStore.state.currentGuide).toEqual(null);
  51. });
  52. it('should force show a guide', function() {
  53. GuideStore.onFetchSucceeded(data);
  54. window.location.hash = '#assistant';
  55. GuideStore.onURLChange();
  56. expect(GuideStore.state.currentGuide.id).toEqual(1);
  57. // Should prune steps that don't have anchors.
  58. expect(GuideStore.state.currentGuide.steps).toHaveLength(2);
  59. GuideStore.onCloseGuide();
  60. expect(GuideStore.state.currentGuide.id).toEqual(2);
  61. window.location.hash = '';
  62. });
  63. it('should record analytics events when guide is cued', function() {
  64. const spy = jest.spyOn(GuideStore, 'recordCue');
  65. GuideStore.onFetchSucceeded(data);
  66. expect(spy).toHaveBeenCalledWith(data.Guide2.id);
  67. expect(spy).toHaveBeenCalledTimes(1);
  68. spy.mockRestore();
  69. });
  70. it('should not send multiple cue analytics events for same guide', function() {
  71. const spy = jest.spyOn(GuideStore, 'recordCue');
  72. GuideStore.onFetchSucceeded(data);
  73. expect(spy).toHaveBeenCalledWith(data.Guide2.id);
  74. expect(spy).toHaveBeenCalledTimes(1);
  75. GuideStore.updateCurrentGuide();
  76. expect(spy).toHaveBeenCalledTimes(1);
  77. spy.mockRestore();
  78. });
  79. });