datePageFilter.spec.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import {initializeOrg} from 'sentry-test/initializeOrg';
  2. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  3. import DatePageFilter from 'sentry/components/datePageFilter';
  4. import OrganizationStore from 'sentry/stores/organizationStore';
  5. import PageFiltersStore from 'sentry/stores/pageFiltersStore';
  6. const {organization, router, routerContext} = initializeOrg({
  7. organization: {},
  8. project: undefined,
  9. projects: undefined,
  10. router: {
  11. location: {
  12. query: {},
  13. pathname: '/test',
  14. },
  15. params: {orgId: 'org-slug'},
  16. },
  17. });
  18. describe('DatePageFilter', function () {
  19. beforeEach(() => {
  20. PageFiltersStore.init();
  21. OrganizationStore.init();
  22. OrganizationStore.onUpdate(organization, {replace: true});
  23. PageFiltersStore.onInitializeUrlState(
  24. {
  25. projects: [],
  26. environments: [],
  27. datetime: {
  28. period: '7d',
  29. start: null,
  30. end: null,
  31. utc: null,
  32. },
  33. },
  34. new Set()
  35. );
  36. });
  37. it('can change period', async function () {
  38. render(<DatePageFilter />, {
  39. context: routerContext,
  40. organization,
  41. });
  42. // Open time period dropdown
  43. expect(screen.getByText('7D')).toBeInTheDocument();
  44. userEvent.click(screen.getByText('7D'));
  45. // Click 30 day period
  46. userEvent.click(screen.getByText('Last 30 days'));
  47. // Confirm selection changed visible text and query params
  48. expect(await screen.findByText('30D')).toBeInTheDocument();
  49. expect(router.push).toHaveBeenCalledWith(
  50. expect.objectContaining({query: {statsPeriod: '30d'}})
  51. );
  52. expect(PageFiltersStore.getState()).toEqual({
  53. isReady: true,
  54. desyncedFilters: new Set(),
  55. pinnedFilters: new Set(),
  56. selection: {
  57. datetime: {
  58. period: '30d',
  59. end: undefined,
  60. start: undefined,
  61. utc: null,
  62. },
  63. environments: [],
  64. projects: [],
  65. },
  66. });
  67. });
  68. it('can pin datetime', async function () {
  69. render(<DatePageFilter />, {
  70. context: routerContext,
  71. organization,
  72. });
  73. // Confirm no filters are pinned
  74. expect(PageFiltersStore.getState()).toEqual(
  75. expect.objectContaining({
  76. pinnedFilters: new Set(),
  77. })
  78. );
  79. // Open time period dropdown
  80. userEvent.click(screen.getByText('7D'));
  81. // Click the pin button
  82. const pinButton = screen.getByRole('button', {name: 'Lock filter'});
  83. userEvent.click(pinButton);
  84. await screen.findByRole('button', {name: 'Lock filter', pressed: true});
  85. // Check if the pin indicator has been added
  86. expect(screen.getByLabelText('Filter applied across pages')).toBeInTheDocument();
  87. expect(PageFiltersStore.getState()).toEqual(
  88. expect.objectContaining({
  89. pinnedFilters: new Set(['datetime']),
  90. })
  91. );
  92. });
  93. });