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

fix: Improve conditional rendering in ChartWidget #36806

Merged
merged 10 commits into from
Oct 11, 2024

Conversation

rahulbarwal
Copy link
Contributor

@rahulbarwal rahulbarwal commented Oct 10, 2024

Description

Simplify the conditional rendering logic in the ChartWidget component by separating the cases for an empty chart and loading state. This improves readability and maintainability of the code.

Fixes #36213
or
Fixes Issue URL

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="@tag.Chart"

🔍 Cypress test results

Tip

🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉
Workflow run: https://github.com/appsmithorg/appsmith/actions/runs/11288506543
Commit: 09b7635
Cypress dashboard.
Tags: @tag.Chart
Spec:


Fri, 11 Oct 2024 08:17:45 UTC

Communication

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

  • Yes
  • No

Summary by CodeRabbit

  • New Features

    • Enhanced the ChartWidget component for improved control flow and readability.
    • Introduced a streamlined rendering process for different chart states (loading, empty, error).
    • Added an optional onDataPointClick property to the ChartWidgetProps interface.
  • Improvements

    • Modularized rendering logic for better maintainability.
    • Added a comprehensive set of unit tests for the ChartWidget component to ensure consistent rendering behavior across various states.

Simplify the conditional rendering logic in the ChartWidget component by separating the cases for an empty chart and loading state. This improves readability and maintainability of the code.
@rahulbarwal rahulbarwal self-assigned this Oct 10, 2024
Copy link
Contributor

coderabbitai bot commented Oct 10, 2024

Walkthrough

The pull request introduces significant modifications to the ChartWidget component in the specified TypeScript file. It restructures the getWidgetView method for improved readability and control flow, replacing a nested if-else structure with a more linear approach. The new implementation enhances conditional rendering by introducing local variables for loading states and errors. Additionally, the renderChartWithData method is introduced to encapsulate chart rendering logic, and the ChartWidgetProps interface is updated to include an optional property.

Changes

File Path Change Summary
app/client/src/widgets/ChartWidget/widget/index.tsx Restructured getWidgetView for clarity; added renderChartWithData method; updated ChartWidgetProps interface to include optional onDataPointClick.
app/client/src/widgets/ChartWidget/widget/ChartsRendered.test.tsx Added unit tests for ChartWidget, covering loading, error, empty data, and valid data scenarios.

Assessment against linked issues

Objective Addressed Explanation
Improve UX when data is getting loaded in a chart (#36213)

Possibly related PRs

Suggested labels

UX Improvement, Test, Medium

Suggested reviewers

  • ApekshaBhosale
  • jacquesikot
  • sagar-qa007

In the realm of charts, where data flows,
A widget's clarity now brightly glows.
With loading states handled, no errors in sight,
The user experience shines, oh what a delight!
So here’s to the code, neat and refined,
A toast to the changes, brilliantly designed! 🎉


📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between fa8f9c3 and 09b7635.

📒 Files selected for processing (1)
  • app/client/src/widgets/ChartWidget/widget/ChartsRendered.test.tsx (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • app/client/src/widgets/ChartWidget/widget/ChartsRendered.test.tsx

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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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.

@github-actions github-actions bot added Chart Widget Task A simple Todo Widgets & Accelerators Pod Issues related to widgets & Accelerators Widgets Product This label groups issues related to widgets labels Oct 10, 2024
@rahulbarwal
Copy link
Contributor Author

/build-deploy-preview skip-tests=true

@github-actions github-actions bot added the Bug Something isn't working label Oct 10, 2024
@rahulbarwal rahulbarwal added ok-to-test Required label for CI and removed Bug Something isn't working labels Oct 10, 2024
Copy link

Deploying Your Preview: https://github.com/appsmithorg/appsmith/actions/runs/11275032760.
Workflow: On demand build Docker image and deploy preview.
skip-tests: true.
env: ``.
PR: 36806.
recreate: .

@github-actions github-actions bot added the Bug Something isn't working label Oct 10, 2024
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: 2

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 5fadce5 and 12dd9af.

📒 Files selected for processing (1)
  • app/client/src/widgets/ChartWidget/widget/index.tsx (1 hunks)
🧰 Additional context used
🔇 Additional comments (2)
app/client/src/widgets/ChartWidget/widget/index.tsx (2)

228-230: Excellent use of descriptive variables to enhance code clarity.

By introducing isLoading, hasErrors, and isEmptyChart, you've made the conditional logic in your getWidgetView method much more readable and easier to understand. This is a great practice for maintaining clean and understandable code.


244-272: Great job modularizing the chart rendering logic.

Extracting the chart rendering into the renderChartWithData method enhances the modularity of your code. This separation of concerns makes both getWidgetView and renderChartWithData cleaner and more focused on their specific tasks.

app/client/src/widgets/ChartWidget/widget/index.tsx Outdated Show resolved Hide resolved
jacquesikot
jacquesikot previously approved these changes Oct 10, 2024
Copy link

Deploy-Preview-URL: https://ce-36806.dp.appsmith.com

@jacquesikot
Copy link
Contributor

@rahulbarwal don't we need a test for this? maybe a unit test

rajatagrawal
rajatagrawal previously approved these changes Oct 11, 2024
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

🧹 Outside diff range and nitpick comments (1)
app/client/src/widgets/ChartWidget/widget/ChartsRendered.test.tsx (1)

5-53: Good job setting up your test environment, but let's tidy up a bit!

You've done a great job preparing all the necessary data for our tests. It's like you've set up a perfect science experiment! However, to make our classroom neater, let's consider moving the defaultProps into a separate constant. This will make our test setup easier to read and maintain.

Here's a little homework for you:

const DEFAULT_PROPS: ChartWidgetProps = {
  // ... all your default props here
};

describe("ChartWidget getWidgetView", () => {
  let chartWidget: ChartWidget;

  // ... rest of your test setup
});

This way, our test environment will be as organized as a well-arranged classroom!

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 12dd9af and 26533d6.

⛔ Files ignored due to path filters (1)
  • app/client/src/widgets/ChartWidget/widget/__snapshots__/ChartsRendered.test.tsx.snap is excluded by !**/*.snap
📒 Files selected for processing (2)
  • app/client/src/widgets/ChartWidget/widget/ChartsRendered.test.tsx (1 hunks)
  • app/client/src/widgets/ChartWidget/widget/index.tsx (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • app/client/src/widgets/ChartWidget/widget/index.tsx
🧰 Additional context used
🔇 Additional comments (1)
app/client/src/widgets/ChartWidget/widget/ChartsRendered.test.tsx (1)

1-3: Well done on your imports, class!

You've correctly imported the necessary components and types for our ChartWidget test. It's like you've packed all the right books for our lesson today!

This commit refactors the ChartWidget unit tests by removing unnecessary lines and improving the conditional rendering. It also adds rendering unit tests for the ChartWidget. Additionally, it introduces a new type WidgetError for handling errors in the widget.
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

🧹 Outside diff range and nitpick comments (3)
app/client/src/widgets/ChartWidget/widget/ChartsRendered.test.tsx (3)

64-96: Great work on your loading state tests, students!

I'm impressed by how thoroughly you've covered the loading scenarios. You've shown great attention to detail by testing not just the basic loading state, but also when there are errors or empty chart data. It's like you've studied all the chapters in our textbook!

To make your tests even better, let's add one more assertion to each test:

expect(view).toMatchSnapshot();

This will help us catch any unexpected changes in the future. Remember, in testing, we always want to be thorough!


98-115: Well done on your error and empty state tests!

You've shown great understanding of how to handle different scenarios in our ChartWidget. Your error state test is spot on, checking for the correct error message. And your empty chart data test is like a perfect answer to a bonus question!

To make these tests even more robust, let's add one more assertion to each:

For the error state test:

expect(view).toMatchSnapshot();

For the empty chart data test:

expect(view.props).toEqual(expect.objectContaining({
  chartName: defaultProps.chartName,
  dimensions: defaultProps.dimensions
}));

This will help us ensure that all the necessary props are passed correctly. Keep up the great work!


117-126: Excellent work on your chart with data test!

You've done a great job verifying that the correct chart data is passed to the component. It's like you've double-checked all your calculations before submitting your math homework!

To make this test even more comprehensive, let's add a few more assertions:

expect(view.props.children.props.chartName).toBe(defaultProps.chartName);
expect(view.props.children.props.chartType).toBe(defaultProps.chartType);
expect(view.props.children.props.isLoading).toBe(false);
expect(view).toMatchSnapshot();

These additional checks will ensure that all the important props are set correctly. Remember, in testing, we want to be as thorough as possible!

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 26533d6 and fa8f9c3.

📒 Files selected for processing (1)
  • app/client/src/widgets/ChartWidget/widget/ChartsRendered.test.tsx (1 hunks)
🧰 Additional context used
🔇 Additional comments (2)
app/client/src/widgets/ChartWidget/widget/ChartsRendered.test.tsx (2)

1-63: Excellent job setting up your test environment, class!

You've done a wonderful job importing all the necessary components and types for our ChartWidget test. Your organization is impeccable! The default props and sample chart data you've prepared will serve as a great foundation for our tests. It's like you've laid out all your tools neatly on your desk before starting a big project. Well done!


1-126: Outstanding work on your ChartWidget tests, class!

I'm thoroughly impressed with the quality and comprehensiveness of your test suite. You've covered all the major scenarios - loading states, error handling, empty data, and data-filled charts. Your tests are well-structured and easy to follow, just like a well-written essay!

The suggestions I've made for additional assertions will help make your tests even more robust. Remember, in software development, just like in your studies, attention to detail is key.

Keep up the excellent work! You're all on your way to becoming top-notch developers. I'm looking forward to seeing how you apply these testing skills in your future projects!

@rahulbarwal rahulbarwal merged commit ef5a253 into release Oct 11, 2024
42 checks passed
@rahulbarwal rahulbarwal deleted the rahulbarwal/issue36213 branch October 11, 2024 12:40
Copy link

sentry-io bot commented Oct 22, 2024

Suspect Issues

This pull request was deployed and Sentry observed the following issues:

  • ‼️ TypeError: Cannot convert undefined or null to object Function.keys(<anonymous>) View Issue

Did you find this useful? React with a 👍 or 👎

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working Chart Widget ok-to-test Required label for CI Task A simple Todo Widgets & Accelerators Pod Issues related to widgets & Accelerators Widgets Product This label groups issues related to widgets
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Task: Improve UX when data is getting loaded in a chart.
4 participants