projectPageFilter.spec.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import {initializeOrg} from 'sentry-test/initializeOrg';
  2. import {act, render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  3. import ProjectPageFilter from 'sentry/components/projectPageFilter';
  4. import OrganizationStore from 'sentry/stores/organizationStore';
  5. import PageFiltersStore from 'sentry/stores/pageFiltersStore';
  6. import ProjectsStore from 'sentry/stores/projectsStore';
  7. const {organization, router, routerContext} = initializeOrg({
  8. organization: {features: ['global-views']},
  9. project: undefined,
  10. projects: [
  11. {
  12. id: 2,
  13. slug: 'project-2',
  14. },
  15. ],
  16. router: {
  17. location: {
  18. pathname: '/organizations/org-slug/issues/',
  19. query: {},
  20. },
  21. params: {orgId: 'org-slug'},
  22. },
  23. });
  24. describe('ProjectPageFilter', function () {
  25. beforeEach(() => {
  26. OrganizationStore.init();
  27. PageFiltersStore.init();
  28. PageFiltersStore.onInitializeUrlState(
  29. {
  30. projects: [],
  31. environments: [],
  32. datetime: {start: null, end: null, period: '14d', utc: null},
  33. },
  34. new Set()
  35. );
  36. OrganizationStore.onUpdate(organization, {replace: true});
  37. ProjectsStore.loadInitialData(organization.projects);
  38. });
  39. afterEach(() => {
  40. PageFiltersStore.reset();
  41. });
  42. it('can pick project', function () {
  43. render(<ProjectPageFilter />, {
  44. context: routerContext,
  45. organization,
  46. });
  47. // Open the project dropdown
  48. expect(screen.getByText('My Projects')).toBeInTheDocument();
  49. userEvent.click(screen.getByText('My Projects'));
  50. // Click the first project's checkbox
  51. const projectOptions = screen.getAllByTestId('checkbox-fancy');
  52. userEvent.click(projectOptions[0]);
  53. // Confirm the selection changed the visible text
  54. expect(screen.queryByText('My Projects')).not.toBeInTheDocument();
  55. // Close the dropdown
  56. userEvent.click(screen.getAllByText('project-2')[0]);
  57. // Verify we were redirected
  58. expect(router.push).toHaveBeenCalledWith(
  59. expect.objectContaining({query: {environment: [], project: ['2']}})
  60. );
  61. });
  62. it('can pin selection', async function () {
  63. render(<ProjectPageFilter />, {
  64. context: routerContext,
  65. organization,
  66. });
  67. // Open the project dropdown
  68. expect(screen.getByText('My Projects')).toBeInTheDocument();
  69. userEvent.click(screen.getByText('My Projects'));
  70. // Click the pin button
  71. const pinButton = screen.getByRole('button', {name: 'Lock filter'});
  72. userEvent.click(pinButton, undefined, {skipHover: true});
  73. await screen.findByRole('button', {name: 'Lock filter', pressed: true});
  74. // Check if the pin indicator has been added
  75. expect(screen.getByLabelText('Filter applied across pages')).toBeInTheDocument();
  76. expect(PageFiltersStore.getState()).toEqual(
  77. expect.objectContaining({
  78. pinnedFilters: new Set(['projects']),
  79. })
  80. );
  81. });
  82. it('can quick select', async function () {
  83. render(<ProjectPageFilter />, {
  84. context: routerContext,
  85. organization,
  86. });
  87. // Open the project dropdown
  88. expect(screen.getByText('My Projects')).toBeInTheDocument();
  89. userEvent.click(screen.getByText('My Projects'));
  90. // Click the first project's checkbox
  91. userEvent.click(screen.getByText('project-2'));
  92. expect(router.push).toHaveBeenCalledWith(
  93. expect.objectContaining({query: {environment: [], project: ['2']}})
  94. );
  95. await screen.findByText('project-2');
  96. await waitFor(() =>
  97. expect(PageFiltersStore.getState()).toEqual(
  98. expect.objectContaining({
  99. isReady: true,
  100. selection: {
  101. datetime: {
  102. end: null,
  103. period: '14d',
  104. start: null,
  105. utc: null,
  106. },
  107. environments: [],
  108. projects: [2],
  109. },
  110. })
  111. )
  112. );
  113. });
  114. it('will change projects when page filter selection changes, e.g. from back button nav', async function () {
  115. render(<ProjectPageFilter />, {
  116. context: routerContext,
  117. organization,
  118. });
  119. // Confirm initial selection
  120. expect(screen.getByText('My Projects')).toBeInTheDocument();
  121. // Edit page filter project selection
  122. act(() => {
  123. PageFiltersStore.updateProjects([2], []);
  124. });
  125. // Page filter selection change should affect project page filter
  126. expect(await screen.findByText('project-2')).toBeInTheDocument();
  127. });
  128. });