Copilot commented on code in PR #34442: URL: https://github.com/apache/superset/pull/34442#discussion_r2248932371
########## superset-frontend/src/features/databases/DatabaseModal/DatabaseConnectionForm/EncryptedField.test.tsx: ########## @@ -0,0 +1,748 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { render, fireEvent, screen } from 'spec/helpers/testing-library'; +import { t } from '@superset-ui/core'; +import { DatabaseObject, ConfigurationMethod } from '../../types'; +import { EncryptedField, encryptedCredentialsMap } from './EncryptedField'; + +// Mock the useToasts hook +const mockAddDangerToast = jest.fn(); +jest.mock('src/components/MessageToasts/withToasts', () => ({ + useToasts: () => ({ + addDangerToast: mockAddDangerToast, + }), +})); + +// Simplified FileReader mock +const mockFileReader = { + result: null as string | null, + onload: null as ((ev: ProgressEvent<FileReader>) => void) | null, + readAsText: jest.fn(function (this: any) { Review Comment: [nitpick] The FileReader mock uses `this: any` which bypasses TypeScript's type checking. Consider defining a more specific type for the FileReader mock to improve type safety. ```suggestion readAsText: jest.fn(function (this: FileReader) { ``` ########## superset-frontend/src/features/databases/DatabaseModal/DatabaseConnectionForm/EncryptedField.test.tsx: ########## @@ -0,0 +1,748 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { render, fireEvent, screen } from 'spec/helpers/testing-library'; +import { t } from '@superset-ui/core'; +import { DatabaseObject, ConfigurationMethod } from '../../types'; +import { EncryptedField, encryptedCredentialsMap } from './EncryptedField'; + +// Mock the useToasts hook +const mockAddDangerToast = jest.fn(); +jest.mock('src/components/MessageToasts/withToasts', () => ({ + useToasts: () => ({ + addDangerToast: mockAddDangerToast, + }), +})); + +// Simplified FileReader mock +const mockFileReader = { + result: null as string | null, + onload: null as ((ev: ProgressEvent<FileReader>) => void) | null, + readAsText: jest.fn(function (this: any) { + setTimeout(() => { + if (this.result !== null && this.onload) { + this.onload({} as ProgressEvent<FileReader>); + } + }, 0); + }), +}; + +Object.defineProperty(global, 'FileReader', { + writable: true, + value: jest.fn(() => mockFileReader), +}); + +describe('EncryptedField', () => { + // Generic test utilities + const createMockDb = ( + engine: string | null | undefined, + parameters: Record<string, any> = {}, + ): DatabaseObject => ({ + configuration_method: ConfigurationMethod.DynamicForm, + database_name: 'test-db', + driver: 'test-driver', + id: 1, + name: 'Test Database', + is_managed_externally: false, + engine: engine as string | undefined, + parameters, + }); + + const createMockChangeMethods = () => ({ + onEncryptedExtraInputChange: jest.fn(), + onParametersChange: jest.fn(), + onChange: jest.fn(), + onQueryChange: jest.fn(), + onParametersUploadFileChange: jest.fn(), + onAddTableCatalog: jest.fn(), + onRemoveTableCatalog: jest.fn(), + onExtraInputChange: jest.fn(), + onSSHTunnelParametersChange: jest.fn(), + }); + + // Helper function to manage encryptedCredentialsMap setup/teardown + const withMockCredentialsMap = ( + mappings: Record<string, string>, + testFn: () => void, + ) => { + const originalMap = { ...encryptedCredentialsMap }; + + // Setup mappings + Object.entries(mappings).forEach(([engine, field]) => { + (encryptedCredentialsMap as any)[engine] = field; Review Comment: [nitpick] The use of `as any` to modify the encryptedCredentialsMap bypasses type safety. Consider creating a typed helper function or interface to handle credential map modifications safely. ```suggestion setEncryptedCredential(engine, field); ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
