From 97273f59f867a9b329370b903e3616c24b43a5bc Mon Sep 17 00:00:00 2001
From: Antonio Rivero Martinez
<38889534+Antonio-RiveroMartnez@users.noreply.github.com>
Date: Thu, 6 Oct 2022 15:01:22 -0300
Subject: [PATCH] fix(database): Handle String errors in DatabaseModal (#21709)
---
.../database/DatabaseModal/index.test.jsx | 91 +++++++++++++++++++
.../data/database/DatabaseModal/index.tsx | 7 +-
2 files changed, 97 insertions(+), 1 deletion(-)
diff --git a/superset-frontend/src/views/CRUD/data/database/DatabaseModal/index.test.jsx b/superset-frontend/src/views/CRUD/data/database/DatabaseModal/index.test.jsx
index 2724dcce2532d..4fb5d60cdd3d8 100644
--- a/superset-frontend/src/views/CRUD/data/database/DatabaseModal/index.test.jsx
+++ b/superset-frontend/src/views/CRUD/data/database/DatabaseModal/index.test.jsx
@@ -26,6 +26,7 @@ import {
cleanup,
act,
} from 'spec/helpers/testing-library';
+import * as hooks from 'src/views/CRUD/hooks';
import DatabaseModal from './index';
const dbProps = {
@@ -1269,4 +1270,94 @@ describe('DatabaseModal', () => {
expect(schemasForFileUploadText).not.toBeInTheDocument();
});
});
+
+ describe('DatabaseModal w errors as objects', () => {
+ jest.mock('src/views/CRUD/hooks', () => ({
+ ...jest.requireActual('src/views/CRUD/hooks'),
+ useSingleViewResource: jest.fn(),
+ }));
+ const useSingleViewResourceMock = jest.spyOn(
+ hooks,
+ 'useSingleViewResource',
+ );
+
+ useSingleViewResourceMock.mockReturnValue({
+ state: {
+ loading: false,
+ resource: null,
+ error: { _schema: 'Test Error With Object' },
+ },
+ fetchResource: jest.fn(),
+ createResource: jest.fn(),
+ updateResource: jest.fn(),
+ clearError: jest.fn(),
+ });
+
+ const renderAndWait = async () => {
+ const mounted = act(async () => {
+ render(, {
+ useRedux: true,
+ });
+ });
+
+ return mounted;
+ };
+
+ beforeEach(async () => {
+ await renderAndWait();
+ });
+
+ test('Error displays when it is an object', async () => {
+ const step2of3text = screen.getByText(/step 2 of 3/i);
+ const errorSection = screen.getByText(/Database Creation Error/i);
+ expect(step2of3text).toBeVisible();
+ expect(errorSection).toBeVisible();
+ });
+ });
+
+ describe('DatabaseModal w errors as strings', () => {
+ jest.mock('src/views/CRUD/hooks', () => ({
+ ...jest.requireActual('src/views/CRUD/hooks'),
+ useSingleViewResource: jest.fn(),
+ }));
+ const useSingleViewResourceMock = jest.spyOn(
+ hooks,
+ 'useSingleViewResource',
+ );
+
+ useSingleViewResourceMock.mockReturnValue({
+ state: {
+ loading: false,
+ resource: null,
+ error: 'Test Error With String',
+ },
+ fetchResource: jest.fn(),
+ createResource: jest.fn(),
+ updateResource: jest.fn(),
+ clearError: jest.fn(),
+ });
+
+ const renderAndWait = async () => {
+ const mounted = act(async () => {
+ render(, {
+ useRedux: true,
+ });
+ });
+
+ return mounted;
+ };
+
+ beforeEach(async () => {
+ await renderAndWait();
+ });
+
+ test('Error displays when it is a string', async () => {
+ const step2of3text = screen.getByText(/step 2 of 3/i);
+ const errorTitleMessage = screen.getByText(/Database Creation Error/i);
+ const errorMessage = screen.getByText(/Test Error With String/i);
+ expect(step2of3text).toBeVisible();
+ expect(errorTitleMessage).toBeVisible();
+ expect(errorMessage).toBeVisible();
+ });
+ });
});
diff --git a/superset-frontend/src/views/CRUD/data/database/DatabaseModal/index.tsx b/superset-frontend/src/views/CRUD/data/database/DatabaseModal/index.tsx
index 9fb55246ce510..de5d750ebd067 100644
--- a/superset-frontend/src/views/CRUD/data/database/DatabaseModal/index.tsx
+++ b/superset-frontend/src/views/CRUD/data/database/DatabaseModal/index.tsx
@@ -1144,7 +1144,12 @@ const DatabaseModal: FunctionComponent = ({
const errorAlert = () => {
let alertErrors: string[] = [];
if (!isEmpty(dbErrors)) {
- alertErrors = typeof dbErrors === 'object' ? Object.values(dbErrors) : [];
+ alertErrors =
+ typeof dbErrors === 'object'
+ ? Object.values(dbErrors)
+ : typeof dbErrors === 'string'
+ ? [dbErrors]
+ : [];
} else if (!isEmpty(validationErrors)) {
alertErrors =
validationErrors?.error_type === 'GENERIC_DB_ENGINE_ERROR'