Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

MetaTags-test.tsx 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import { screen } from '@testing-library/react';
  21. import userEvent from '@testing-library/user-event';
  22. import * as React from 'react';
  23. import { setApplicationTags, setProjectTags } from '../../../../../api/components';
  24. import { mockComponent } from '../../../../../helpers/mocks/component';
  25. import { renderComponent } from '../../../../../helpers/testReactTestingUtils';
  26. import { ComponentQualifier } from '../../../../../types/component';
  27. import MetaTags from '../MetaTags';
  28. jest.mock('../../../../../api/components', () => ({
  29. setApplicationTags: jest.fn().mockResolvedValue(true),
  30. setProjectTags: jest.fn().mockResolvedValue(true),
  31. searchProjectTags: jest.fn().mockResolvedValue({ tags: ['best', 'useless'] }),
  32. }));
  33. beforeEach(() => {
  34. jest.clearAllMocks();
  35. });
  36. it('should render without tags and admin rights', async () => {
  37. renderMetaTags();
  38. expect(await screen.findByText('no_tags')).toBeInTheDocument();
  39. expect(screen.queryByRole('button')).not.toBeInTheDocument();
  40. });
  41. it('should allow to edit tags for a project', async () => {
  42. const user = userEvent.setup();
  43. const onComponentChange = jest.fn();
  44. const component = mockComponent({
  45. key: 'my-second-project',
  46. tags: ['foo', 'bar'],
  47. configuration: {
  48. showSettings: true,
  49. },
  50. name: 'MySecondProject',
  51. });
  52. renderMetaTags({ component, onComponentChange });
  53. expect(await screen.findByText('foo, bar')).toBeInTheDocument();
  54. expect(screen.getByRole('button')).toBeInTheDocument();
  55. await user.click(screen.getByRole('button', { name: 'foo bar +' }));
  56. expect(await screen.findByRole('checkbox', { name: 'best' })).toBeInTheDocument();
  57. await user.click(screen.getByRole('checkbox', { name: 'best' }));
  58. expect(onComponentChange).toHaveBeenCalledWith({ tags: ['foo', 'bar', 'best'] });
  59. onComponentChange.mockClear();
  60. /*
  61. * Since we're not actually updating the tags, we're back to having the foo, bar only
  62. */
  63. await user.click(screen.getByRole('checkbox', { name: 'bar' }));
  64. expect(onComponentChange).toHaveBeenCalledWith({ tags: ['foo'] });
  65. expect(setProjectTags).toHaveBeenCalled();
  66. expect(setApplicationTags).not.toHaveBeenCalled();
  67. await user.click(document.body);
  68. expect(screen.queryByRole('checkbox')).not.toBeInTheDocument();
  69. });
  70. it('should set tags for an app', async () => {
  71. const user = userEvent.setup();
  72. renderMetaTags({
  73. component: mockComponent({
  74. configuration: {
  75. showSettings: true,
  76. },
  77. qualifier: ComponentQualifier.Application,
  78. }),
  79. });
  80. await user.click(screen.getByRole('button', { name: 'no_tags +' }));
  81. await user.click(await screen.findByRole('checkbox', { name: 'best' }));
  82. expect(setProjectTags).not.toHaveBeenCalled();
  83. expect(setApplicationTags).toHaveBeenCalled();
  84. });
  85. function renderMetaTags(overrides: Partial<Parameters<typeof MetaTags>[0]> = {}) {
  86. const component = mockComponent({
  87. configuration: {
  88. showSettings: false,
  89. },
  90. });
  91. return renderComponent(
  92. <MetaTags component={component} onComponentChange={jest.fn()} {...overrides} />,
  93. );
  94. }