withPageFilters.spec.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import {act, render, screen} from 'sentry-test/reactTestingLibrary';
  2. import PageFiltersStore from 'sentry/stores/pageFiltersStore';
  3. import {PageFilters} from 'sentry/types';
  4. import withPageFilters from 'sentry/utils/withPageFilters';
  5. describe('withPageFilters HoC', function () {
  6. beforeEach(() => {
  7. PageFiltersStore.reset();
  8. PageFiltersStore.init();
  9. });
  10. it('handles projects', function () {
  11. const PrintProjectsComponent = ({selection}: {selection: PageFilters}) => {
  12. return (
  13. <div>
  14. {selection.projects.map(project => (
  15. <div data-test-id="project" key={project}>
  16. {project}
  17. </div>
  18. ))}
  19. </div>
  20. );
  21. };
  22. const Container = withPageFilters(PrintProjectsComponent);
  23. render(<Container />);
  24. expect(screen.queryByTestId('project')).not.toBeInTheDocument();
  25. act(() => PageFiltersStore.updateProjects([1, 2], []));
  26. expect(screen.getAllByTestId('project')).toHaveLength(2);
  27. expect(screen.getAllByTestId('project')[0]).toHaveTextContent('1');
  28. expect(screen.getAllByTestId('project')[1]).toHaveTextContent('2');
  29. });
  30. it('handles datetime', function () {
  31. const PrintDatetimeComponent = ({selection}) => {
  32. return (
  33. <div>
  34. <div data-test-id="period">{selection.datetime.period}</div>
  35. <div data-test-id="start">{selection.datetime.start}</div>
  36. <div data-test-id="end">{selection.datetime.end}</div>
  37. </div>
  38. );
  39. };
  40. const Container = withPageFilters(PrintDatetimeComponent);
  41. render(<Container />);
  42. expect(screen.getByTestId('period')).toHaveTextContent('14d');
  43. expect(screen.getByTestId('start')).toBeEmptyDOMElement();
  44. expect(screen.getByTestId('end')).toBeEmptyDOMElement();
  45. act(() =>
  46. PageFiltersStore.updateDateTime({
  47. period: '7d',
  48. start: null,
  49. end: null,
  50. utc: true,
  51. })
  52. );
  53. expect(screen.getByTestId('period')).toHaveTextContent('7d');
  54. expect(screen.getByTestId('start')).toBeEmptyDOMElement();
  55. expect(screen.getByTestId('end')).toBeEmptyDOMElement();
  56. act(() =>
  57. PageFiltersStore.updateDateTime({
  58. period: null,
  59. start: '2018-08-08T00:00:00',
  60. end: '2018-08-09T00:00:00',
  61. utc: true,
  62. })
  63. );
  64. expect(screen.getByTestId('period')).toBeEmptyDOMElement();
  65. expect(screen.getByTestId('start')).toHaveTextContent('2018-08-08T00:00:00');
  66. expect(screen.getByTestId('end')).toHaveTextContent('2018-08-09T00:00:00');
  67. });
  68. it('handles environments', function () {
  69. const PrintProjectsComponent = ({selection}) => {
  70. return (
  71. <div>
  72. {selection.environments.map(env => (
  73. <div data-test-id="environment" key={env}>
  74. {env}
  75. </div>
  76. ))}
  77. </div>
  78. );
  79. };
  80. const Container = withPageFilters(PrintProjectsComponent);
  81. render(<Container />);
  82. expect(screen.queryByTestId('environment')).not.toBeInTheDocument();
  83. act(() => PageFiltersStore.updateEnvironments(['beta', 'alpha']));
  84. expect(screen.getAllByTestId('environment')).toHaveLength(2);
  85. expect(screen.getAllByTestId('environment')[0]).toHaveTextContent('beta');
  86. expect(screen.getAllByTestId('environment')[1]).toHaveTextContent('alpha');
  87. });
  88. });