thresholds.spec.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import {LocationFixture} from 'sentry-fixture/locationFixture';
  2. import {RouterFixture} from 'sentry-fixture/routerFixture';
  3. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  4. import {useNavigate} from 'sentry/utils/useNavigate';
  5. import Thresholds from 'sentry/views/dashboards/widgetBuilder/components/thresholds';
  6. import {WidgetBuilderProvider} from 'sentry/views/dashboards/widgetBuilder/contexts/widgetBuilderContext';
  7. jest.mock('sentry/utils/useNavigate');
  8. describe('Thresholds', () => {
  9. let mockNavigate!: jest.Mock;
  10. beforeEach(() => {
  11. mockNavigate = jest.fn();
  12. jest.mocked(useNavigate).mockReturnValue(mockNavigate);
  13. });
  14. it('sets thresholds to undefined if the thresholds are fully wiped', async () => {
  15. render(
  16. <WidgetBuilderProvider>
  17. <Thresholds dataType="duration" dataUnit="millisecond" />
  18. </WidgetBuilderProvider>,
  19. {
  20. router: RouterFixture({
  21. location: LocationFixture({
  22. query: {
  23. thresholds: '{"max_values":{"max1":100},"unit":"millisecond"}',
  24. },
  25. }),
  26. }),
  27. }
  28. );
  29. await userEvent.clear(screen.getByLabelText('First Maximum'));
  30. expect(mockNavigate).toHaveBeenCalledWith(
  31. expect.objectContaining({
  32. query: expect.objectContaining({
  33. thresholds: undefined,
  34. }),
  35. }),
  36. {replace: true}
  37. );
  38. });
  39. it('sets a threshold when applied', async () => {
  40. render(
  41. <WidgetBuilderProvider>
  42. <Thresholds dataType="duration" dataUnit="millisecond" />
  43. </WidgetBuilderProvider>
  44. );
  45. await userEvent.type(screen.getByLabelText('First Maximum'), '100');
  46. await userEvent.type(screen.getByLabelText('Second Maximum'), '200');
  47. await userEvent.tab();
  48. expect(mockNavigate).toHaveBeenCalledWith(
  49. expect.objectContaining({
  50. query: expect.objectContaining({
  51. thresholds: '{"max_values":{"max1":100,"max2":200},"unit":null}',
  52. }),
  53. }),
  54. {replace: true}
  55. );
  56. });
  57. it('updates the unit when applied', async () => {
  58. render(
  59. <WidgetBuilderProvider>
  60. <Thresholds dataType="duration" dataUnit="millisecond" />
  61. </WidgetBuilderProvider>,
  62. {
  63. router: RouterFixture({
  64. location: LocationFixture({
  65. query: {
  66. thresholds: '{"max_values":{"max1":100,"max2":200},"unit":"millisecond"}',
  67. },
  68. }),
  69. }),
  70. }
  71. );
  72. await userEvent.click(screen.getAllByText('millisecond')[0]!);
  73. await userEvent.click(screen.getByText('second'));
  74. expect(mockNavigate).toHaveBeenCalledWith(
  75. expect.objectContaining({
  76. query: expect.objectContaining({
  77. thresholds: '{"max_values":{"max1":100,"max2":200},"unit":"second"}',
  78. }),
  79. }),
  80. {replace: true}
  81. );
  82. });
  83. });