Copilot commented on code in PR #34442: URL: https://github.com/apache/superset/pull/34442#discussion_r2249070742
########## superset-frontend/src/features/databases/DatabaseModal/DatabaseConnectionForm/EncryptedField.test.tsx: ########## @@ -0,0 +1,757 @@ +/** + * 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: FileReader) { + setTimeout(() => { + if (this.result !== null && this.onload) { + this.onload({} as ProgressEvent<FileReader>); + } + }, 0); Review Comment: Using setTimeout in tests can lead to flaky tests and race conditions. Consider using a synchronous approach or proper async/await patterns with test utilities like waitFor. ```suggestion // Simulate async behavior using a resolved Promise Promise.resolve().then(() => { if (this.result !== null && this.onload) { this.onload({} as ProgressEvent<FileReader>); } }); ``` ########## superset-frontend/src/features/databases/DatabaseModal/DatabaseConnectionForm/EncryptedField.test.tsx: ########## @@ -0,0 +1,757 @@ +/** + * 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: FileReader) { + setTimeout(() => { + if (this.result !== null && this.onload) { + this.onload({} as ProgressEvent<FileReader>); + } + }, 0); + }), +}; + +Object.defineProperty(global, 'FileReader', { + writable: true, + value: jest.fn(() => mockFileReader), Review Comment: The FileReader mock constructor returns the same instance for all calls, which could cause test interference. Consider using a factory function that returns a new instance for each test. ```suggestion const createMockFileReader = () => { return { result: null as string | null, onload: null as ((ev: ProgressEvent<FileReader>) => void) | null, readAsText: jest.fn(function (this: FileReader) { setTimeout(() => { if (this.result !== null && this.onload) { this.onload({} as ProgressEvent<FileReader>); } }, 0); }), }; }; Object.defineProperty(global, 'FileReader', { writable: true, value: jest.fn(() => createMockFileReader()), ``` -- 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]
