newWidgetBuilder.spec.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import {initializeOrg} from 'sentry-test/initializeOrg';
  2. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  3. import OrganizationStore from 'sentry/stores/organizationStore';
  4. import PageFiltersStore from 'sentry/stores/pageFiltersStore';
  5. import ProjectsStore from 'sentry/stores/projectsStore';
  6. import {useNavigate} from 'sentry/utils/useNavigate';
  7. import WidgetBuilderV2 from 'sentry/views/dashboards/widgetBuilder/components/newWidgetBuilder';
  8. const {organization, projects, router} = initializeOrg({
  9. organization: {features: ['global-views', 'open-membership', 'dashboards-eap']},
  10. projects: [
  11. {id: '1', slug: 'project-1', isMember: true},
  12. {id: '2', slug: 'project-2', isMember: true},
  13. {id: '3', slug: 'project-3', isMember: false},
  14. ],
  15. router: {
  16. location: {
  17. pathname: '/organizations/org-slug/dashboard/1/',
  18. query: {project: '-1'},
  19. },
  20. params: {},
  21. },
  22. });
  23. jest.mock('sentry/utils/useNavigate', () => ({
  24. useNavigate: jest.fn(),
  25. }));
  26. const mockUseNavigate = jest.mocked(useNavigate);
  27. describe('NewWidgetBuiler', function () {
  28. const onCloseMock = jest.fn();
  29. beforeEach(function () {
  30. OrganizationStore.init();
  31. PageFiltersStore.init();
  32. PageFiltersStore.onInitializeUrlState(
  33. {
  34. projects: [],
  35. environments: [],
  36. datetime: {start: null, end: null, period: '14d', utc: null},
  37. },
  38. new Set(['projects'])
  39. );
  40. OrganizationStore.onUpdate(organization, {replace: true});
  41. ProjectsStore.loadInitialData(projects);
  42. MockApiClient.addMockResponse({
  43. url: '/organizations/org-slug/releases/',
  44. body: [],
  45. });
  46. MockApiClient.addMockResponse({
  47. url: '/organizations/org-slug/dashboard/1/',
  48. body: [],
  49. });
  50. });
  51. afterEach(() => PageFiltersStore.reset());
  52. it('renders', async function () {
  53. render(<WidgetBuilderV2 isOpen onClose={onCloseMock} />, {
  54. router,
  55. organization,
  56. });
  57. expect(await screen.findByText('Create Custom Widget')).toBeInTheDocument();
  58. expect(await screen.findByLabelText('Close Widget Builder')).toBeInTheDocument();
  59. expect(await screen.findByRole('button', {name: 'All Projects'})).toBeInTheDocument();
  60. expect(await screen.findByRole('button', {name: 'All Envs'})).toBeInTheDocument();
  61. expect(await screen.findByRole('button', {name: '14D'})).toBeInTheDocument();
  62. expect(await screen.findByRole('button', {name: 'All Releases'})).toBeInTheDocument();
  63. expect(await screen.findByPlaceholderText('Name')).toBeInTheDocument();
  64. expect(await screen.findByText('+ Add Widget Description')).toBeInTheDocument();
  65. expect(await screen.findByLabelText('Dataset')).toHaveAttribute('role', 'radiogroup');
  66. expect(await screen.getByText('Errors')).toBeInTheDocument();
  67. expect(await screen.getByText('Transactions')).toBeInTheDocument();
  68. expect(await screen.getByText('Spans')).toBeInTheDocument();
  69. expect(await screen.getByText('Issues')).toBeInTheDocument();
  70. expect(await screen.getByText('Releases')).toBeInTheDocument();
  71. expect(await screen.findByPlaceholderText('Name')).toBeInTheDocument();
  72. expect(await screen.findByTestId('add-description')).toBeInTheDocument();
  73. expect(await screen.findByText('TEST WIDGET')).toBeInTheDocument();
  74. });
  75. it('edits name and description', async function () {
  76. const mockNavigate = jest.fn();
  77. mockUseNavigate.mockReturnValue(mockNavigate);
  78. render(<WidgetBuilderV2 isOpen onClose={onCloseMock} />, {
  79. router,
  80. organization,
  81. });
  82. await userEvent.type(await screen.findByPlaceholderText('Name'), 'some name');
  83. expect(mockNavigate).toHaveBeenLastCalledWith(
  84. expect.objectContaining({
  85. ...router.location,
  86. query: expect.objectContaining({title: 'some name'}),
  87. })
  88. );
  89. await userEvent.click(await screen.findByTestId('add-description'));
  90. await userEvent.type(
  91. await screen.findByPlaceholderText('Description'),
  92. 'some description'
  93. );
  94. expect(mockNavigate).toHaveBeenLastCalledWith(
  95. expect.objectContaining({
  96. ...router.location,
  97. query: expect.objectContaining({description: 'some description'}),
  98. })
  99. );
  100. });
  101. it('changes the dataset', async function () {
  102. const mockNavigate = jest.fn();
  103. mockUseNavigate.mockReturnValue(mockNavigate);
  104. render(<WidgetBuilderV2 isOpen onClose={onCloseMock} />, {
  105. router,
  106. organization,
  107. });
  108. await userEvent.click(await screen.findByLabelText('Issues'));
  109. expect(mockNavigate).toHaveBeenCalledWith(
  110. expect.objectContaining({
  111. ...router.location,
  112. query: expect.objectContaining({dataset: 'issue'}),
  113. })
  114. );
  115. });
  116. });