codeant-ai-for-open-source[bot] commented on code in PR #38880:
URL: https://github.com/apache/superset/pull/38880#discussion_r3009505622


##########
superset-frontend/src/dashboard/hooks/useDownloadScreenshot.test.ts:
##########
@@ -0,0 +1,73 @@
+/**
+ * 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 { renderHook, act } from '@testing-library/react-hooks';
+import { SupersetClient } from '@superset-ui/core';
+import { useDownloadScreenshot } from './useDownloadScreenshot';
+import { DownloadScreenshotFormat } from 
'../components/menu/DownloadMenuItems/types';
+
+jest.mock('@superset-ui/core', () => ({
+  SupersetClient: {
+    post: jest.fn(),
+    get: jest.fn(),
+  },
+  SupersetApiError: class SupersetApiError extends Error {
+    status: number;
+    constructor(message: string, status: number) {
+      super(message);
+      this.status = status;
+    }
+  },
+}));
+
+jest.mock('react-redux', () => ({
+  useSelector: jest.fn(() => undefined),
+}));
+
+jest.mock('src/components/MessageToasts/withToasts', () => ({
+  useToasts: () => ({
+    addDangerToast: jest.fn(),
+    addSuccessToast: jest.fn(),
+    addInfoToast: jest.fn(),
+  }),
+}));
+
+jest.mock('src/utils/urlUtils', () => ({
+  getDashboardUrlParams: jest.fn(() => []),
+}));
+
+test('downloadScreenshot calls API with force=true to ensure fresh 
screenshots', async () => {
+  const mockCacheKey = 'test-cache-key';
+  (SupersetClient.post as jest.Mock).mockResolvedValue({
+    json: { cache_key: mockCacheKey },
+  });
+

Review Comment:
   **Suggestion:** The test only mocks `SupersetClient.post` but leaves 
`SupersetClient.get` as a bare `jest.fn()`, so when `useDownloadScreenshot` 
calls `SupersetClient.get(...).then(...)` the mock returns `undefined` and 
chaining `.then` on it will throw a runtime TypeError, causing the test to fail 
once the code path is executed; `SupersetClient.get` should be mocked to return 
a Promise. [possible bug]
   
   <details>
   <summary><b>Severity Level:</b> Major ⚠️</summary>
   
   ```mdx
   - ❌ useDownloadScreenshot test fails with TypeError on GET.
   - ⚠️ Frontend Jest suite for dashboard hooks becomes unreliable.
   - ⚠️ CI may fail due to unhandled async error.
   ```
   </details>
   
   ```suggestion
     (SupersetClient.get as jest.Mock).mockRejectedValue({ status: 404 });
   ```
   <details>
   <summary><b>Steps of Reproduction ✅ </b></summary>
   
   ```mdx
   1. Run the frontend unit tests so that
   `superset-frontend/src/dashboard/hooks/useDownloadScreenshot.test.ts` is 
executed by Jest.
   
   2. Inside the test (lines 24–28, 54–58), `SupersetClient` is mocked with 
`post: jest.fn(),
   get: jest.fn()`, and only `SupersetClient.post` is configured with 
`mockResolvedValue`,
   leaving `SupersetClient.get` as a bare `jest.fn()` that returns `undefined`.
   
   3. The test calls the hook and triggers the download logic via
   `result.current(DownloadScreenshotFormat.PNG)` at lines 60–63, which invokes
   `downloadScreenshot` in `useDownloadScreenshot`
   (`superset-frontend/src/dashboard/hooks/useDownloadScreenshot.ts`, lines 
74–79, 148–178);
   after `SupersetClient.post` resolves, the `.then` handler calls
   `fetchImageWithRetry(cacheKey)` (lines 137–145).
   
   4. `fetchImageWithRetry` calls `checkImageReady(cacheKey)` (lines 94–100), 
which executes
   `SupersetClient.get({ ... })` (lines 95–99) and immediately chains 
`.then(...)`; because
   `SupersetClient.get` returns `undefined` in the test, accessing `.then` on 
`undefined`
   throws a `TypeError`, causing the test to fail with an uncaught error when 
this
   asynchronous path runs.
   ```
   </details>
   <details>
   <summary><b>Prompt for AI Agent 🤖 </b></summary>
   
   ```mdx
   This is a comment left during a code review.
   
   **Path:** superset-frontend/src/dashboard/hooks/useDownloadScreenshot.test.ts
   **Line:** 59:59
   **Comment:**
        *Possible Bug: The test only mocks `SupersetClient.post` but leaves 
`SupersetClient.get` as a bare `jest.fn()`, so when `useDownloadScreenshot` 
calls `SupersetClient.get(...).then(...)` the mock returns `undefined` and 
chaining `.then` on it will throw a runtime TypeError, causing the test to fail 
once the code path is executed; `SupersetClient.get` should be mocked to return 
a Promise.
   
   Validate the correctness of the flagged issue. If correct, How can I resolve 
this? If you propose a fix, implement it and please make it concise.
   ```
   </details>
   <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F38880&comment_hash=acfce7e54062979624ffa75b358de51ffbf028dc93270b29382a8b29d87057cd&reaction=like'>👍</a>
 | <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F38880&comment_hash=acfce7e54062979624ffa75b358de51ffbf028dc93270b29382a8b29d87057cd&reaction=dislike'>👎</a>



-- 
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]

Reply via email to