Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fixed image not downloading functionality and ImageWidget cypr… #34786

Open
wants to merge 4 commits into
base: release
Choose a base branch
from

Conversation

Shivam-z
Copy link
Contributor

@Shivam-z Shivam-z commented Jul 8, 2024

Description

In this PR I have modified 3 file chages.

  • I have created helper.tsx file *app/client/src/widgets/ImageWidget/helper.ts in which I have defined one method urlToBase64 which will convert imageurl into base64Url.

  • app/client/src/widgets/ImageWidget/component/index.tsx: In this I have made one new state called downloadUrl which will takes the value from urlToBase64 method and later define the href value.

  • app/client/cypress/e2e/Regression/ClientSide/Widgets/Image/Image3_Spec.ts: I have modified the downloadImage test case according to this implementation.

summary:

As we are fetching image url for base64 conversion. Therefore, this solution only works for images that are hosted on server which allows cross origin request and for images that are hosted on server which doesn't allow cross origin request (getting cors error) the functionality will works as it was working previously.

suggestion for Restiricted Images:

for images hosted on server which does not allow cross origin request, the backend should create base64 url and return to frontend.

Fixes #24632

Warning

If no issue exists, please create an issue first, and check with the maintainers if the issue is valid.

Automation

/ok-to-test tags=""

🔍 Cypress test results

Caution

If you modify the content in this section, you are likely to disrupt the CI result for your PR.

Communication

Should the DevRel and Marketing teams inform users about this change?

  • Yes
  • No

Summary by CodeRabbit

  • New Features

    • Added base64 conversion for image downloads in the ImageComponent.
    • Introduced download functionality for images in the ImageComponent.
  • Tests

    • Introduced a comprehensive test suite for the ImageComponent, covering image rendering, download button interactions, and edge cases for image URLs.

Copy link
Contributor

coderabbitai bot commented Jul 8, 2024

Walkthrough

The changes introduce a function to convert image URLs to base64 and update the ImageWidget component to handle image downloads using this function. Tests have been included to validate image rendering, downloading, and button visibility. These changes address the issue of images not downloading properly by ensuring that the download button provides a base64-encoded version of the image.

Changes

Files Change Summary
app/client/cypress/e2e/Regression/ClientSide/Widgets/Image/Image3_Spec.ts Added urlToBase64 import, updated before hook for base64 conversion, modified download button href assertion.
app/client/src/widgets/ImageWidget/component/index.tsx Added downloadUrl to state, updated lifecycle methods to handle downloadUrl, introduced updateDownloadUrl method.
app/client/src/widgets/ImageWidget/helper.ts Introduced urlToBase64 function for converting URLs to base64.
app/client/src/widgets/ImageWidget/component/index.test.tsx Added tests for ImageComponent, mocked urlToBase64, tested rendering, download button display, image download functionality, and handling empty URLs.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant ImageComponent
    participant Helper as urlToBase64 Function

    User->>ImageComponent: Hover over Image
    ImageComponent-->>User: Show Download Button
    User->>ImageComponent: Click Download Button
    ImageComponent->>Helper: Convert Image URL to Base64
    Helper-->>ImageComponent: Return Base64 String
    ImageComponent-->>User: Initiate Download with Base64 URL
Loading

Assessment against linked issues

Objective Addressed Explanation
Ensure image download functionality (#24632)
Handling download button visibility (#24632)

Poem

In code we weave, a fix so bright,
Images now download, no more plight.
Base64 magic in widgets we trust,
A seamless click, no more fuss.
With tests in place, all set to gleam,
Our app's now smoother, like a dream. 🌟


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    -- I pushed a fix in commit <commit_id>, please review it.
    -- Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    -- @coderabbitai generate unit testing code for this file.
    -- @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    -- @coderabbitai generate interesting stats about this repository and render them as a table.
    -- @coderabbitai read src/utils.ts and generate unit testing code.
    -- @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    -- @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 7

Outside diff range and nitpick comments (5)
app/client/src/widgets/ImageWidget/helper.ts (2)

2-2: Ensure consistent return types.

The function should consistently return a string. The current implementation returns an empty string for invalid URLs but could benefit from explicit typing to ensure consistency.

export const urlToBase64 = async (url: string): Promise<string> => {

5-8: Improve error message clarity.

Specify the URL in the error message to improve debugging.

throw new Error(`Network response was not ok for URL: ${url}`);
app/client/cypress/e2e/Regression/ClientSide/Widgets/Image/Image3_Spec.ts (1)

72-74: Use consistent assertions for Cypress tests.

Ensure consistency by using the same assertion method throughout the test.

cy.wrap(base64Url).then((url) => {
  // This is to validate the final base64 url which is used for download as href in the anchor tag
  agHelper.AssertAttribute(widgetLocators.imageDownloadBtn, "href", url);
});
app/client/src/widgets/ImageWidget/component/index.tsx (2)

7-7: Ensure consistent import structure.

Maintain a consistent import structure for better readability.

import { urlToBase64 } from "../helper";

145-145: Initialize state properties directly.

Initialize state properties directly in the state object for better readability.

downloadUrl: "",
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 9766775 and c0d8119.

Files selected for processing (3)
  • app/client/cypress/e2e/Regression/ClientSide/Widgets/Image/Image3_Spec.ts (2 hunks)
  • app/client/src/widgets/ImageWidget/component/index.tsx (5 hunks)
  • app/client/src/widgets/ImageWidget/helper.ts (1 hunks)
Additional context used
Path-based instructions (1)
app/client/cypress/e2e/Regression/ClientSide/Widgets/Image/Image3_Spec.ts (1)

Pattern app/client/cypress/**/**.*: Follow best practices for Cypress code and e2e automation.
Avoid using cy.wait in code.
Avoid using cy.pause in code.
Use variables for locators, not strings.
Use data-* attributes for selectors; avoid Xpaths and CSS attributes.
Avoid selectors like .btn.submit or button[type=submit].
Perform logins via API with LoginFromAPI.
Only interact with controlled sites/servers.
Ensure tests can run with it.only and are independent.
Use before, beforeEach, after, afterEach correctly; clean state before tests.
Check new specs for flakiness by running them 10 times on CI.
Use multiple assertions; don't treat Cypress as unit tests.
Use constants for strings.
Include datasource operations in before hooks.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between c0d8119 and 9bf68c8.

Files selected for processing (2)
  • app/client/src/widgets/ImageWidget/component/index.tsx (5 hunks)
  • app/client/src/widgets/ImageWidget/helper.ts (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • app/client/src/widgets/ImageWidget/component/index.tsx
Additional context used
Learnings (1)
app/client/src/widgets/ImageWidget/helper.ts (2)
Learnt from: Shivam-z
PR: appsmithorg/appsmith#34786
File: app/client/src/widgets/ImageWidget/helper.ts:14-15
Timestamp: 2024-07-09T06:18:19.736Z
Learning: Handling errors directly within utility functions like `urlToBase64` is a good practice to ensure consistency and simplicity in error management. Avoid using `console.error` in production code; consider using a logging library or handling errors upstream.
Learnt from: Shivam-z
PR: appsmithorg/appsmith#34786
File: app/client/cypress/e2e/Regression/ClientSide/Widgets/Image/Image3_Spec.ts:22-24
Timestamp: 2024-07-09T04:45:28.800Z
Learning: Handling errors directly within utility functions like `urlToBase64` is a good practice to ensure consistency and simplicity in error management.
Additional comments not posted (6)
app/client/src/widgets/ImageWidget/helper.ts (6)

1-2: LGTM!

The imports are appropriate for the functionality provided.


3-4: LGTM!

The function declaration and initial null check are appropriate.


5-6: LGTM!

The try block and fetch call are appropriate.


7-8: LGTM!

The response validation is appropriate for handling network errors.


10-11: LGTM!

The arrayBuffer conversion and content type extraction are appropriate.


12-13: LGTM!

The base64 conversion and return statement are appropriate.

sagar-qa007
sagar-qa007 previously approved these changes Jul 12, 2024
Copy link
Contributor

@rajatagrawal rajatagrawal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left comments

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 9bf68c8 and f573bd6.

Files selected for processing (1)
  • app/client/src/widgets/ImageWidget/component/index.test.tsx (1 hunks)
Additional comments not posted (3)
app/client/src/widgets/ImageWidget/component/index.test.tsx (3)

1-7: Review of imports and mock setup.

The imports and the mock setup for urlToBase64 are correctly implemented. The use of jest.mock to simulate the behavior of the urlToBase64 function is appropriate for testing the component's interaction with external modules.


11-19: Review of lifecycle hooks for testing.

The use of beforeEach and afterEach to manage the DOM container for the tests is a good practice. It ensures that each test starts with a clean slate, which is crucial for avoiding side effects between tests.


31-115: Review of test cases in <ImageComponent />.

  1. Rendering Test: The test case for rendering the image checks if the image is rendered with the correct src attribute. This is a basic functionality test and is implemented correctly.
  2. Interaction Test: The test for rendering the download button on hover and checking its functionality on click is crucial. The use of fireEvent to simulate user interactions and waitFor to handle asynchronous operations are correctly used.
  3. Conditional Rendering Test: The test for not rendering the download button when URLs are empty is important for ensuring the component behaves correctly under edge conditions. The use of queryByTestId to assert the absence of the download button is appropriate.

Overall, the test cases are well-structured and cover the key functionalities of the component.

coderabbitai[bot]
coderabbitai bot previously approved these changes Jul 16, 2024
@Shivam-z
Copy link
Contributor Author

Hi @rajatagrawal , I have added RTL unit test cases for Image Widget , could you please review this PR.

@Shivam-z Shivam-z requested a review from rajatagrawal July 16, 2024 11:50
Copy link

This PR has not seen activitiy for a while. It will be closed in 7 days unless further activity is detected.

@github-actions github-actions bot added the Stale label Jul 23, 2024
@Shivam-z Shivam-z requested a review from sagar-qa007 July 23, 2024 18:14
@Shivam-z
Copy link
Contributor Author

Hi @rajatagrawal @ApekshaBhosale @sagar-qa007 , please review this PR else it will be auto closed in some days.

@rajatagrawal rajatagrawal requested a review from rahulbarwal July 25, 2024 13:40
@rajatagrawal
Copy link
Contributor

@rahulbarwal Can you please help review the PR ?

@github-actions github-actions bot removed the Stale label Jul 26, 2024
Copy link

github-actions bot commented Aug 3, 2024

This PR has not seen activitiy for a while. It will be closed in 7 days unless further activity is detected.

@github-actions github-actions bot added the Stale label Aug 3, 2024
@Shivam-z
Copy link
Contributor Author

Shivam-z commented Aug 5, 2024

@rajatagrawal @rahulbarwal please look into this PR.

sagar-qa007
sagar-qa007 previously approved these changes Aug 20, 2024
@rajatagrawal rajatagrawal removed their request for review August 27, 2024 04:30
@rajatagrawal rajatagrawal removed their request for review September 30, 2024 04:30
@github-actions github-actions bot removed the Stale label Mar 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Image is not downloading
3 participants