onboarding.spec.jsx 3.8 KB

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