Repository: provectus/kafka-ui
Issue: Fix GlobalSchemaSelector tests
Labels: scope/frontend
scope/QA
status/accepted


Issue comments:
Hello there Hurenka! 👋

Thank you and congratulations 🎉 for opening your very first issue in this project! 💖

In case you want to claim this issue, please comment down below! We will try to get back to you as soon as we can. 👀

Pull request review comments:
Sonar added this line to code smells list. pls check if we can fix that

Patch that resolved the issue:
diff --git a/kafka-ui-react-app/src/components/Schemas/List/GlobalSchemaSelector/GlobalSchemaSelector.tsx b/kafka-ui-react-app/src/components/Schemas/List/GlobalSchemaSelector/GlobalSchemaSelector.tsx
index 6447b2915c2..66edb6f0465 100644
--- a/kafka-ui-react-app/src/components/Schemas/List/GlobalSchemaSelector/GlobalSchemaSelector.tsx
+++ b/kafka-ui-react-app/src/components/Schemas/List/GlobalSchemaSelector/GlobalSchemaSelector.tsx
@@ -63,6 +63,9 @@ const GlobalSchemaSelector: React.FC = () => {
           compatibilityLevel: { compatibility: nextCompatibilityLevel },
         });
         dispatch(fetchSchemas(clusterName));
+        setCurrentCompatibilityLevel(nextCompatibilityLevel);
+        setNextCompatibilityLevel(undefined);
+        setIsConfirmationVisible(false);
       } catch (e) {
         const err = await getResponse(e as Response);
         dispatch(serverErrorAlertAdded(err));
@@ -78,7 +81,7 @@ const GlobalSchemaSelector: React.FC = () => {
       <div>Global Compatibility Level: </div>
       <Select
         selectSize="M"
-        defaultValue={currentCompatibilityLevel}
+        value={currentCompatibilityLevel}
         onChange={handleChangeCompatibilityLevel}
         disabled={isFetching || isUpdating || isConfirmationVisible}
       >
diff --git a/kafka-ui-react-app/src/components/Schemas/List/GlobalSchemaSelector/__test__/GlobalSchemaSelector.spec.tsx b/kafka-ui-react-app/src/components/Schemas/List/GlobalSchemaSelector/__test__/GlobalSchemaSelector.spec.tsx
new file mode 100644
index 00000000000..0d8c6715c0f
--- /dev/null
+++ b/kafka-ui-react-app/src/components/Schemas/List/GlobalSchemaSelector/__test__/GlobalSchemaSelector.spec.tsx
@@ -0,0 +1,90 @@
+import React from 'react';
+import { screen, waitFor } from '@testing-library/react';
+import { render } from 'lib/testHelpers';
+import { CompatibilityLevelCompatibilityEnum } from 'generated-sources';
+import GlobalSchemaSelector from 'components/Schemas/List/GlobalSchemaSelector/GlobalSchemaSelector';
+import userEvent from '@testing-library/user-event';
+import { clusterSchemasPath } from 'lib/paths';
+import { Route } from 'react-router';
+import fetchMock from 'fetch-mock';
+
+const clusterName = 'testClusterName';
+
+const selectForwardOption = () =>
+  userEvent.selectOptions(
+    screen.getByRole('listbox'),
+    CompatibilityLevelCompatibilityEnum.FORWARD
+  );
+
+const expectOptionIsSelected = (option: string) => {
+  const optionElement: HTMLOptionElement = screen.getByText(option);
+  expect(optionElement.selected).toBeTruthy();
+};
+
+describe('GlobalSchemaSelector', () => {
+  const renderComponent = () =>
+    render(
+      <Route path={clusterSchemasPath(':clusterName')}>
+        <GlobalSchemaSelector />
+      </Route>,
+      {
+        pathname: clusterSchemasPath(clusterName),
+      }
+    );
+
+  beforeEach(async () => {
+    const fetchGlobalCompatibilityLevelMock = fetchMock.getOnce(
+      `api/clusters/${clusterName}/schemas/compatibility`,
+      { compatibility: CompatibilityLevelCompatibilityEnum.FULL }
+    );
+    renderComponent();
+    await waitFor(() =>
+      expect(fetchGlobalCompatibilityLevelMock.called()).toBeTruthy()
+    );
+  });
+
+  afterEach(() => {
+    fetchMock.reset();
+  });
+
+  it('renders with initial prop', () => {
+    expectOptionIsSelected(CompatibilityLevelCompatibilityEnum.FULL);
+  });
+
+  it('shows popup when select value is changed', async () => {
+    expectOptionIsSelected(CompatibilityLevelCompatibilityEnum.FULL);
+    selectForwardOption();
+    expect(screen.getByText('Confirm the action')).toBeInTheDocument();
+  });
+
+  it('resets select value when cancel is clicked', () => {
+    selectForwardOption();
+    userEvent.click(screen.getByText('Cancel'));
+    expect(screen.queryByText('Confirm the action')).not.toBeInTheDocument();
+    expectOptionIsSelected(CompatibilityLevelCompatibilityEnum.FULL);
+  });
+
+  it('sets new schema when confirm is clicked', async () => {
+    selectForwardOption();
+    const putNewCompatibilityMock = fetchMock.putOnce(
+      `api/clusters/${clusterName}/schemas/compatibility`,
+      200,
+      {
+        body: {
+          compatibility: CompatibilityLevelCompatibilityEnum.FORWARD,
+        },
+      }
+    );
+    const getSchemasMock = fetchMock.getOnce(
+      `api/clusters/${clusterName}/schemas`,
+      200
+    );
+    await waitFor(() => {
+      userEvent.click(screen.getByText('Submit'));
+    });
+    await waitFor(() => expect(putNewCompatibilityMock.called()).toBeTruthy());
+    await waitFor(() => expect(getSchemasMock.called()).toBeTruthy());
+    expect(screen.queryByText('Confirm the action')).not.toBeInTheDocument();
+    expectOptionIsSelected(CompatibilityLevelCompatibilityEnum.FORWARD);
+  });
+});
diff --git a/kafka-ui-react-app/src/components/Topics/Topic/Details/Messages/Filters/__tests__/__snapshots__/Filters.spec.tsx.snap b/kafka-ui-react-app/src/components/Topics/Topic/Details/Messages/Filters/__tests__/__snapshots__/Filters.spec.tsx.snap
index 312b0ec70d8..97693628fb4 100644
--- a/kafka-ui-react-app/src/components/Topics/Topic/Details/Messages/Filters/__tests__/__snapshots__/Filters.spec.tsx.snap
+++ b/kafka-ui-react-app/src/components/Topics/Topic/Details/Messages/Filters/__tests__/__snapshots__/Filters.spec.tsx.snap
@@ -464,6 +464,7 @@ exports[`Filters component matches the snapshot 1`] = `
               <select
                 class="c7"
                 id="selectSeekType"
+                role="listbox"
               >
                 <option
                   value="OFFSET"
@@ -562,6 +563,7 @@ exports[`Filters component matches the snapshot 1`] = `
         >
           <select
             class="c11"
+            role="listbox"
           >
             <option
               value="FORWARD"
@@ -1098,6 +1100,7 @@ exports[`Filters component when fetching matches the snapshot 1`] = `
               <select
                 class="c7"
                 id="selectSeekType"
+                role="listbox"
               >
                 <option
                   value="OFFSET"
@@ -1196,6 +1199,7 @@ exports[`Filters component when fetching matches the snapshot 1`] = `
         >
           <select
             class="c11"
+            role="listbox"
           >
             <option
               value="FORWARD"
diff --git a/kafka-ui-react-app/src/components/common/Select/Select.tsx b/kafka-ui-react-app/src/components/common/Select/Select.tsx
index e4ad1554827..8a8b07f13e7 100644
--- a/kafka-ui-react-app/src/components/common/Select/Select.tsx
+++ b/kafka-ui-react-app/src/components/common/Select/Select.tsx
@@ -38,7 +38,12 @@ const Select: React.FC<SelectProps> = ({
           {children}
         </S.Select>
       ) : (
-        <S.Select selectSize={selectSize} isLive={isLive} {...props}>
+        <S.Select
+          role="listbox"
+          selectSize={selectSize}
+          isLive={isLive}
+          {...props}
+        >
           {children}
         </S.Select>
       )}


Test patch: