Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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, waitFor } from '@testing-library/react';
  21. import userEvent from '@testing-library/user-event';
  22. import * as React from 'react';
  23. import WebApiServiceMock from '../../../api/mocks/WebApiServiceMock';
  24. import { renderApp } from '../../../helpers/testReactTestingUtils';
  25. import { byRole, byTestId, byText } from '../../../helpers/testSelector';
  26. import WebApiApp from '../WebApiApp';
  27. const handler = new WebApiServiceMock();
  28. const ui = {
  29. search: byRole('searchbox'),
  30. title: byRole('link', { name: 'Swagger Petstore - OpenAPI 3.0' }),
  31. searchClear: byRole('button', { name: 'clear' }),
  32. showInternal: byRole('checkbox', { name: 'api_documentation.show_internal_v2' }),
  33. apiScopePet: byRole('button', { name: 'pet' }),
  34. apiScopeStore: byRole('button', { name: 'store' }),
  35. apiScopeUser: byRole('button', { name: 'user' }),
  36. apiScopeTest: byRole('button', { name: 'test' }),
  37. publicButton: byRole('button', { name: /visible/ }),
  38. internalButton: byRole('button', { name: /hidden/ }),
  39. apiSidebarItem: byTestId('js-subnavigation-item'),
  40. requestBody: byText('api_documentation.v2.request_subheader.request_body'),
  41. queryParameter: byRole('list', { name: 'api_documentation.v2.request_subheader.query' }).byRole(
  42. 'listitem',
  43. ),
  44. pathParameter: byRole('list', { name: 'api_documentation.v2.request_subheader.path' }).byRole(
  45. 'listitem',
  46. ),
  47. requestHeader: byRole('list', { name: 'api_documentation.v2.request_subheader.header' }).byRole(
  48. 'listitem',
  49. ),
  50. requestBodyParameter: byRole('list', {
  51. name: 'api_documentation.v2.request_subheader.request_body',
  52. }).byRole('listitem'),
  53. response: byRole('list', { name: 'api_documentation.v2.response_header' }).byRole('listitem'),
  54. };
  55. beforeEach(() => {
  56. handler.reset();
  57. });
  58. it('should search apis', async () => {
  59. const user = userEvent.setup();
  60. renderWebApiApp();
  61. expect(await ui.apiScopePet.find()).toBeInTheDocument();
  62. expect(ui.apiScopeStore.get()).toBeInTheDocument();
  63. expect(ui.apiScopeUser.get()).toBeInTheDocument();
  64. expect(ui.apiSidebarItem.queryAll()).toHaveLength(0);
  65. await user.click(ui.apiScopePet.get());
  66. expect(ui.apiSidebarItem.getAll()).toHaveLength(8);
  67. await user.click(ui.apiScopeStore.get());
  68. expect(ui.apiSidebarItem.getAll()).toHaveLength(11);
  69. await user.click(ui.apiScopeUser.get());
  70. expect(ui.apiSidebarItem.getAll()).toHaveLength(18);
  71. await user.type(ui.search.get(), 'put');
  72. const putItems = ui.apiSidebarItem.getAll();
  73. expect(putItems).toHaveLength(3);
  74. expect(ui.apiScopePet.get()).toBeInTheDocument();
  75. expect(ui.apiScopeStore.query()).not.toBeInTheDocument();
  76. expect(ui.apiScopeUser.get()).toBeInTheDocument();
  77. expect(putItems[0]).toHaveTextContent('PUTUpdate an existing pet');
  78. expect(putItems[1]).toHaveTextContent('POSTCreates list of users with given input array');
  79. await user.click(ui.searchClear.get());
  80. expect(ui.apiScopeStore.get()).toBeInTheDocument();
  81. expect(ui.apiSidebarItem.getAll().length).toBeGreaterThan(3);
  82. });
  83. it('should show internal endpoints', async () => {
  84. const user = userEvent.setup();
  85. renderWebApiApp();
  86. expect(await ui.apiScopeStore.find()).toBeInTheDocument();
  87. await user.click(ui.apiScopeStore.get());
  88. expect(ui.apiSidebarItem.getAll()).toHaveLength(3);
  89. expect(ui.apiSidebarItem.getAll().some((el) => el.textContent?.includes('internal'))).toBe(false);
  90. await user.click(ui.showInternal.get());
  91. await waitFor(() => expect(ui.apiSidebarItem.getAll()).toHaveLength(4));
  92. const internalItem = ui.apiSidebarItem
  93. .getAll()
  94. .find((el) => el.textContent?.includes('internal'));
  95. expect(internalItem).toBeInTheDocument();
  96. await user.click(internalItem as HTMLElement);
  97. expect(await byRole('heading', { name: /\/api\/v3\/store\/inventory/ }).find()).toHaveTextContent(
  98. /internal/,
  99. );
  100. });
  101. it('should show internal parameters', async () => {
  102. const user = userEvent.setup();
  103. renderWebApiApp();
  104. expect(await ui.apiScopeTest.find()).toBeInTheDocument();
  105. await user.click(ui.apiScopeTest.get());
  106. expect(ui.apiSidebarItem.getAll()).toHaveLength(2);
  107. await user.click(
  108. ui.apiSidebarItem.getAll().find((el) => el.textContent?.includes('GET')) as HTMLElement,
  109. );
  110. expect(await ui.publicButton.find()).toBeInTheDocument();
  111. expect(ui.internalButton.query()).not.toBeInTheDocument();
  112. await user.click(ui.showInternal.get());
  113. expect(ui.publicButton.get()).toBeInTheDocument();
  114. expect(ui.publicButton.get()).not.toHaveTextContent('internal');
  115. expect(ui.internalButton.get()).toBeInTheDocument();
  116. expect(ui.internalButton.get()).toHaveTextContent('internal');
  117. await user.click(
  118. ui.apiSidebarItem.getAll().find((el) => el.textContent?.includes('POST')) as HTMLElement,
  119. );
  120. expect(ui.publicButton.get()).toBeInTheDocument();
  121. expect(ui.publicButton.get()).not.toHaveTextContent('internal');
  122. expect(ui.internalButton.get()).toBeInTheDocument();
  123. expect(ui.internalButton.get()).toHaveTextContent('internal');
  124. await user.click(ui.showInternal.get());
  125. expect(await ui.publicButton.find()).toBeInTheDocument();
  126. expect(ui.internalButton.query()).not.toBeInTheDocument();
  127. });
  128. it('should navigate between apis', async () => {
  129. const user = userEvent.setup();
  130. renderWebApiApp();
  131. await user.click(await ui.apiScopePet.find());
  132. await user.click(ui.apiSidebarItem.getAt(0));
  133. expect(await screen.findByText('/api/v3/pet')).toBeInTheDocument();
  134. expect(await screen.findByText('Update an existing pet by Id')).toBeInTheDocument();
  135. expect(ui.response.getAll()).toHaveLength(4);
  136. expect(ui.queryParameter.query()).not.toBeInTheDocument();
  137. expect(ui.pathParameter.query()).not.toBeInTheDocument();
  138. expect(ui.requestHeader.query()).not.toBeInTheDocument();
  139. expect(ui.requestBody.get()).toBeInTheDocument();
  140. expect(ui.requestBodyParameter.getAll()).toHaveLength(6);
  141. expect(ui.requestBodyParameter.byRole('button').getAt(0)).toHaveAttribute(
  142. 'aria-expanded',
  143. 'false',
  144. );
  145. await user.click(ui.requestBodyParameter.byRole('button').getAt(0));
  146. expect(ui.requestBodyParameter.byRole('button').getAt(0)).toHaveAttribute(
  147. 'aria-expanded',
  148. 'true',
  149. );
  150. expect(ui.requestBodyParameter.getAt(0)).toHaveTextContent('name requiredmax: 100min: 3');
  151. await user.click(ui.requestBodyParameter.byRole('button').getAt(0));
  152. expect(ui.requestBodyParameter.byRole('button').getAt(0)).toHaveAttribute(
  153. 'aria-expanded',
  154. 'false',
  155. );
  156. expect(ui.response.byRole('button').getAt(0)).toHaveAttribute('aria-expanded', 'true');
  157. expect(ui.response.byRole('button').getAt(2)).toHaveAttribute('aria-expanded', 'false');
  158. expect(ui.response.getAt(0)).toHaveTextContent('200Successful operation');
  159. expect(ui.response.getAt(0)).toHaveTextContent('"name": "string"');
  160. expect(ui.response.getAt(1)).not.toHaveTextContent('no_data');
  161. await user.click(ui.response.byRole('button').getAt(2));
  162. expect(ui.response.getAt(0)).toHaveTextContent('"name": "string"');
  163. expect(ui.response.getAt(1)).toHaveTextContent('no_data');
  164. await user.click(ui.response.byRole('button').getAt(0));
  165. expect(ui.response.getAt(0)).not.toHaveTextContent('"name": "string"');
  166. expect(ui.response.getAt(1)).toHaveTextContent('no_data');
  167. await user.click(ui.apiSidebarItem.getAt(2));
  168. expect(await screen.findByText('/api/v3/pet/findByStatus')).toBeInTheDocument();
  169. expect(ui.response.byRole('button').getAt(0)).toHaveAttribute('aria-expanded', 'true');
  170. expect(ui.response.byRole('button').getAt(2)).toHaveAttribute('aria-expanded', 'false');
  171. expect(ui.queryParameter.get()).toBeInTheDocument();
  172. expect(ui.pathParameter.query()).not.toBeInTheDocument();
  173. expect(ui.requestHeader.query()).not.toBeInTheDocument();
  174. expect(ui.requestBody.query()).not.toBeInTheDocument();
  175. expect(ui.queryParameter.getAll()).toHaveLength(1);
  176. expect(ui.queryParameter.getAt(0)).toHaveTextContent(
  177. 'status Enum (string): available, pending, sold',
  178. );
  179. expect(ui.queryParameter.getAt(0)).not.toHaveTextContent('default: available');
  180. await user.click(ui.queryParameter.byRole('button').getAt(0));
  181. expect(ui.queryParameter.getAt(0)).toHaveTextContent('default: available');
  182. await user.click(ui.queryParameter.byRole('button').getAt(0));
  183. expect(ui.queryParameter.getAt(0)).not.toHaveTextContent('default: available');
  184. await user.click(ui.apiSidebarItem.getAt(4));
  185. expect(await screen.findByText('/api/v3/pet/{petId}')).toBeInTheDocument();
  186. expect(
  187. await screen.findByText('Updates a pet in the store with form data', { selector: 'h1' }),
  188. ).toBeInTheDocument();
  189. expect(ui.queryParameter.getAll()).toHaveLength(2);
  190. expect(ui.pathParameter.getAll()).toHaveLength(1);
  191. expect(ui.requestHeader.query()).not.toBeInTheDocument();
  192. expect(ui.requestBody.query()).not.toBeInTheDocument();
  193. expect(ui.pathParameter.getAt(0)).toHaveTextContent('petId integer (int64)required');
  194. expect(ui.pathParameter.getAt(0)).not.toHaveTextContent('ID of pet that needs to be updated');
  195. await user.click(ui.pathParameter.byRole('button').getAt(0));
  196. expect(ui.pathParameter.getAt(0)).toHaveTextContent('ID of pet that needs to be updated');
  197. expect(ui.queryParameter.getAt(0)).toHaveTextContent('deprecated');
  198. expect(ui.queryParameter.getAt(1)).not.toHaveTextContent('deprecated');
  199. await user.click(ui.queryParameter.byRole('button').getAt(1));
  200. expect(ui.queryParameter.getAt(1)).toHaveTextContent('max: 5min: -1example: 3');
  201. await user.click(ui.apiSidebarItem.getAt(7));
  202. expect(await screen.findByText('/api/v3/pet/{petId}/uploadImage')).toBeInTheDocument();
  203. expect(screen.getByText('no_data')).toBeInTheDocument();
  204. });
  205. it('should show About page', async () => {
  206. const user = userEvent.setup();
  207. renderWebApiApp();
  208. expect(await screen.findByText('about')).toBeInTheDocument();
  209. expect(
  210. screen.getByText('This is a sample Pet Store Server based on the OpenAPI 3.0 specification.', {
  211. exact: false,
  212. }),
  213. ).toBeInTheDocument();
  214. await user.click(ui.apiScopePet.get());
  215. await user.click(ui.apiSidebarItem.getAt(0));
  216. expect(screen.queryByText('about')).not.toBeInTheDocument();
  217. });
  218. function renderWebApiApp() {
  219. // eslint-disable-next-line testing-library/no-unnecessary-act
  220. renderApp('web-api-v2', <WebApiApp />);
  221. }