onboarding.spec.jsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import {browserHistory} from 'react-router';
  2. import {mountWithTheme} from 'sentry-test/enzyme';
  3. import {initializeOrg} from 'sentry-test/initializeOrg';
  4. import {act} from 'sentry-test/reactTestingLibrary';
  5. import ProjectsStore from 'sentry/stores/projectsStore';
  6. import Onboarding from 'sentry/views/onboarding/onboarding';
  7. const MockStep = ({name, data, active, project, onComplete, onUpadte}) => (
  8. <div>
  9. {active && <div id="is_active" />}
  10. <div id="step_name">{name}</div>
  11. <div id="project_slug">{project && project.slug}</div>
  12. <a id="complete" href="#" onClick={() => onComplete(data)} />
  13. <a id="update" href="#" onClick={() => onUpadte(data)} />
  14. </div>
  15. );
  16. const makeMockStep = preFill => p => <MockStep {...preFill} {...p} />;
  17. const MOCKED_STEPS = [
  18. {
  19. id: 'step1',
  20. title: 'Step One',
  21. Component: makeMockStep({name: 'step_1', data: {}}),
  22. },
  23. {
  24. id: 'step2',
  25. title: 'Step Two',
  26. Component: makeMockStep({name: 'step_2', data: {}}),
  27. },
  28. {
  29. id: 'step3',
  30. title: 'Step Three',
  31. Component: makeMockStep({name: 'step_3', data: {}}),
  32. },
  33. ];
  34. describe('Onboarding', function () {
  35. it('redirects to first step if invalid step ID present', function () {
  36. browserHistory.replace = jest.fn();
  37. const params = {
  38. step: 'bad-step',
  39. orgId: 'org-bar',
  40. };
  41. mountWithTheme(<Onboarding steps={MOCKED_STEPS} params={params} />);
  42. expect(browserHistory.replace).toHaveBeenCalledWith('/onboarding/org-bar/step1/');
  43. });
  44. it('renders one step', function () {
  45. const params = {
  46. step: 'step1',
  47. orgId: 'org-bar',
  48. };
  49. const wrapper = mountWithTheme(<Onboarding steps={MOCKED_STEPS} params={params} />);
  50. // Validate that the first step is shown
  51. expect(wrapper.find('#step_name').text()).toEqual('step_1');
  52. // Validate that the progress bar is displayed and active
  53. expect(wrapper.find('ProgressStep').first().props().active).toBe(true);
  54. });
  55. it('moves to next step on complete', function () {
  56. browserHistory.replace = jest.fn();
  57. const params = {
  58. step: 'step1',
  59. orgId: 'org-bar',
  60. };
  61. const wrapper = mountWithTheme(<Onboarding steps={MOCKED_STEPS} params={params} />);
  62. wrapper.find('#complete').simulate('click');
  63. expect(browserHistory.push).toHaveBeenCalledWith('/onboarding/org-bar/step2/');
  64. });
  65. it('renders second step', function () {
  66. const params = {
  67. step: 'step2',
  68. orgId: 'org-bar',
  69. };
  70. const wrapper = mountWithTheme(<Onboarding steps={MOCKED_STEPS} params={params} />);
  71. // Validate that second step is visible
  72. expect(wrapper.find('#step_name').text()).toEqual('step_2');
  73. expect(wrapper.find('MockStep').find('#active').exists()).toBe(false);
  74. });
  75. it('goes back when back button clicked', function () {
  76. browserHistory.push = jest.fn();
  77. const params = {
  78. step: 'step2',
  79. orgId: 'org-bar',
  80. };
  81. const wrapper = mountWithTheme(<Onboarding steps={MOCKED_STEPS} params={params} />);
  82. wrapper.find('Back Button').simulate('click');
  83. expect(browserHistory.replace).toHaveBeenCalledWith('/onboarding/org-bar/step1/');
  84. });
  85. it('passes the first existing project', function () {
  86. const {organization, projects, routerContext} = initializeOrg({
  87. // Multiple projects with different creation dates, to ensure it picks
  88. // the oldest project
  89. projects: [
  90. {id: 1, slug: 'first', dateCreated: 'May 16 2019 13:55:20 GMT-0700'},
  91. {id: 2, slug: 'second', dateCreated: 'May 17 2019 13:55:20 GMT-0700'},
  92. ],
  93. });
  94. act(() => ProjectsStore.loadInitialData(projects));
  95. const params = {
  96. step: 'step1',
  97. orgId: organization.slug,
  98. };
  99. const wrapper = mountWithTheme(
  100. <Onboarding steps={MOCKED_STEPS} params={params} />,
  101. routerContext
  102. );
  103. expect(wrapper.find('#project_slug').text()).toBe('first');
  104. });
  105. });