VariablesUnknownTable.test.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import { render, screen, waitFor } from '@testing-library/react';
  2. import userEvent from '@testing-library/user-event';
  3. import { UserEvent } from '@testing-library/user-event/dist/types/setup';
  4. import React from 'react';
  5. import * as runtime from '@grafana/runtime';
  6. import { customBuilder } from '../shared/testing/builders';
  7. import {
  8. SLOW_VARIABLES_EXPANSION_THRESHOLD,
  9. VariablesUnknownTable,
  10. VariablesUnknownTableProps,
  11. } from './VariablesUnknownTable';
  12. import * as utils from './utils';
  13. import { UsagesToNetwork } from './utils';
  14. async function getTestContext(
  15. overrides: Partial<VariablesUnknownTableProps> | undefined = {},
  16. usages: UsagesToNetwork[] = []
  17. ) {
  18. jest.clearAllMocks();
  19. const reportInteractionSpy = jest.spyOn(runtime, 'reportInteraction').mockImplementation();
  20. const getUnknownsNetworkSpy = jest.spyOn(utils, 'getUnknownsNetwork').mockResolvedValue(usages);
  21. const defaults: VariablesUnknownTableProps = {
  22. variables: [],
  23. dashboard: null,
  24. };
  25. const props = { ...defaults, ...overrides };
  26. const { rerender } = render(<VariablesUnknownTable {...props} />);
  27. await waitFor(() =>
  28. expect(screen.getByRole('heading', { name: /renamed or missing variables/i })).toBeInTheDocument()
  29. );
  30. return { reportInteractionSpy, getUnknownsNetworkSpy, rerender };
  31. }
  32. describe('VariablesUnknownTable', () => {
  33. describe('when rendered', () => {
  34. it('then it should render the section header', async () => {
  35. await getTestContext();
  36. });
  37. });
  38. describe('when expanding the section', () => {
  39. it('then it should call getUnknownsNetwork', async () => {
  40. const { getUnknownsNetworkSpy } = await getTestContext();
  41. await userEvent.click(screen.getByRole('heading', { name: /renamed or missing variables/i }));
  42. await waitFor(() => expect(getUnknownsNetworkSpy).toHaveBeenCalledTimes(1));
  43. });
  44. it('then it should report the interaction', async () => {
  45. const { reportInteractionSpy } = await getTestContext();
  46. await userEvent.click(screen.getByRole('heading', { name: /renamed or missing variables/i }));
  47. expect(reportInteractionSpy).toHaveBeenCalledTimes(1);
  48. expect(reportInteractionSpy).toHaveBeenCalledWith('Unknown variables section expanded');
  49. });
  50. describe('but when expanding it again without changes to variables or dashboard', () => {
  51. it('then it should not call getUnknownsNetwork', async () => {
  52. const { getUnknownsNetworkSpy } = await getTestContext();
  53. await userEvent.click(screen.getByRole('heading', { name: /renamed or missing variables/i }));
  54. await waitFor(() => expect(screen.getByRole('button')).toHaveAttribute('aria-expanded', 'true'));
  55. expect(getUnknownsNetworkSpy).toHaveBeenCalledTimes(1);
  56. await userEvent.click(screen.getByRole('heading', { name: /renamed or missing variables/i }));
  57. await waitFor(() => expect(screen.getByRole('button')).toHaveAttribute('aria-expanded', 'false'));
  58. await userEvent.click(screen.getByRole('heading', { name: /renamed or missing variables/i }));
  59. await waitFor(() => expect(screen.getByRole('button')).toHaveAttribute('aria-expanded', 'true'));
  60. expect(getUnknownsNetworkSpy).toHaveBeenCalledTimes(1);
  61. });
  62. });
  63. describe('and there are no renamed or missing variables', () => {
  64. it('then it should render the correct message', async () => {
  65. await getTestContext();
  66. await userEvent.click(screen.getByRole('heading', { name: /renamed or missing variables/i }));
  67. expect(screen.getByText('No renamed or missing variables found.')).toBeInTheDocument();
  68. });
  69. });
  70. describe('and there are renamed or missing variables', () => {
  71. it('then it should render the table', async () => {
  72. const variable = customBuilder().withId('Renamed Variable').withName('Renamed Variable').build();
  73. const usages = [{ variable, nodes: [], edges: [], showGraph: false }];
  74. const { reportInteractionSpy } = await getTestContext({}, usages);
  75. await userEvent.click(screen.getByRole('heading', { name: /renamed or missing variables/i }));
  76. expect(screen.queryByText('No renamed or missing variables found.')).not.toBeInTheDocument();
  77. expect(screen.getByText('Renamed Variable')).toBeInTheDocument();
  78. expect(screen.getAllByTestId('VariablesUnknownButton')).toHaveLength(1);
  79. // make sure we don't report the interaction for slow expansion
  80. expect(reportInteractionSpy).toHaveBeenCalledTimes(1);
  81. expect(reportInteractionSpy).toHaveBeenCalledWith('Unknown variables section expanded');
  82. });
  83. describe('but when the unknown processing takes a while', () => {
  84. let user: UserEvent;
  85. beforeEach(() => {
  86. jest.useFakeTimers();
  87. // Need to use delay: null here to work with fakeTimers
  88. // see https://github.com/testing-library/user-event/issues/833
  89. user = userEvent.setup({ delay: null });
  90. });
  91. afterEach(() => {
  92. jest.useRealTimers();
  93. });
  94. it('then it should report slow expansion', async () => {
  95. const variable = customBuilder().withId('Renamed Variable').withName('Renamed Variable').build();
  96. const usages = [{ variable, nodes: [], edges: [], showGraph: false }];
  97. const { getUnknownsNetworkSpy, reportInteractionSpy } = await getTestContext({}, usages);
  98. getUnknownsNetworkSpy.mockImplementation(() => {
  99. return new Promise((resolve) => {
  100. setTimeout(() => {
  101. resolve(usages);
  102. }, SLOW_VARIABLES_EXPANSION_THRESHOLD);
  103. });
  104. });
  105. await user.click(screen.getByRole('heading', { name: /renamed or missing variables/i }));
  106. jest.advanceTimersByTime(SLOW_VARIABLES_EXPANSION_THRESHOLD);
  107. // make sure we report the interaction for slow expansion
  108. await waitFor(() =>
  109. expect(reportInteractionSpy).toHaveBeenCalledWith('Slow unknown variables expansion', {
  110. elapsed: expect.any(Number),
  111. })
  112. );
  113. });
  114. });
  115. });
  116. });
  117. });