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

test: add tests for privacy-policy banner #1297

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from

Conversation

igorntwari
Copy link
Contributor

Submit a pull request

  • This is not a duplicate of an existing pull request.
  • No existing features have been broken without good reason.
  • Your commit messages are detailed
  • The code style guideline have been followed.
  • Documentation has been updated to reflect your changes.
  • Tests have been added or updated to reflect your changes.
  • All tests pass.

Replace any ":question:" below with information about your pull request.

Pull Request Details

This PR adds a unity tests for the privacy policy banner

Breaking Changes

Describe what features are broken by this pull request and why, if any.

#1107

Other Relevant Information

Provide any other important details below.

Copy link

coderabbitai bot commented Aug 28, 2024

Walkthrough

Walkthrough

The changes involve the addition of unit tests for the PrivacyPolicyBanner component, enhancing its testability through the introduction of data-testid attributes. The tests validate the component's rendering behavior and user interactions, while modifications to the component improve element selection during testing without altering its existing functionality.

Changes

Files Change Summary
__tests__/components/banner/PrivacyPolicy.test.tsx Introduced unit tests for the PrivacyPolicyBanner component using React Testing Library and Jest, verifying rendering and close button functionality.
src/components/banner/PrivacyPolicy.tsx Added data-testid attributes to the main <div> and the close button <div> to enhance testability, while maintaining the existing structure and logic.
jest.config.js Added a module mapping in the Jest configuration to resolve module paths starting with @/store/ to the src/store/ directory, improving module resolution.

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>.
    • 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 show all the console.log statements in this repository.
    • @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.

@igorntwari igorntwari self-assigned this Aug 28, 2024
Copy link

netlify bot commented Aug 28, 2024

Deploy Preview for staging-dacade ready!

Name Link
🔨 Latest commit a318b4b
🔍 Latest deploy log https://app.netlify.com/sites/staging-dacade/deploys/66d03a542b26940008b539a2
😎 Deploy Preview https://deploy-preview-1297--staging-dacade.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@igorntwari igorntwari linked an issue Aug 28, 2024 that may be closed by this pull request
1 task
Copy link

@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.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 5947862 and 7d696de.

Files selected for processing (2)
  • tests/components/banner/PrivacyPolicy.test.tsx (1 hunks)
  • src/components/banner/PrivacyPolicy.tsx (1 hunks)
Files skipped from review due to trivial changes (1)
  • src/components/banner/PrivacyPolicy.tsx
Additional comments not posted (4)
__tests__/components/banner/PrivacyPolicy.test.tsx (4)

1-4: Setup and imports are appropriate for the test environment.

The imports and mocks are correctly set up for testing the PrivacyPolicyBanner component. This setup ensures that the component's dependencies are appropriately isolated during testing.

Also applies to: 8-19


21-24: Good use of test structure and mock management.

The describe block correctly organizes tests for the PrivacyPolicyBanner, and the use of beforeEach to clear mocks helps maintain clean test states.


26-32: Test case correctly verifies component rendering.

The test ensures that the PrivacyPolicyBanner is rendered when it is supposed to, based on the Redux state. This is a crucial check for the visibility condition of the banner.


34-42: Test case effectively checks user interaction and UI update.

This test appropriately simulates a user clicking the close button and verifies that the PrivacyPolicyBanner is no longer rendered, which is a key behavior of the component.

Comment on lines 15 to 19
jest.mock("next-i18next", () => ({
useTranslation: () => ({
t: (key: string) => key === "signup-page.privacy" ? "Translated Privacy Policy" : key,
}),
}));
Copy link
Contributor

Choose a reason for hiding this comment

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

Since packages like this are usually tested, we don't need to mock them

Comment on lines 34 to 43
it("should accept the cookies policy and not render PrivacyPolicy after the user clicks on the close button", () => {
const { queryByTestId } = renderWithRedux(<PrivacyPolicyBanner />, {
banner: { showCookiePolicy: false },
});
const closeButton = screen.getByTestId("closeButton");
expect(closeButton).toBeInTheDocument();
fireEvent.click(closeButton);
expect(queryByTestId("PrivacyPolicy")).not.toBeInTheDocument();
});

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this test case should be splitted

Copy link
Contributor

@serapieTuyishime serapieTuyishime left a comment

Choose a reason for hiding this comment

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

Could you also specify whether this test is for the "privacy-page" banner or the "privacy-policy" page on the PR name

@igorntwari igorntwari changed the title test: add tests for privacy-policy test: add tests for privacy-policy banner Aug 28, 2024
Copy link

@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.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 7d696de and 17f53bf.

Files selected for processing (1)
  • tests/components/banner/PrivacyPolicy.test.tsx (1 hunks)
Files skipped from review due to trivial changes (1)
  • tests/components/banner/PrivacyPolicy.test.tsx

@@ -33,18 +33,18 @@ export default function PrivacyPolicyBanner(): ReactElement {

if (showBanner)
return (
<div className="fixed bottom-0 left-0 right-0 z-999 flex flex-row justify-center md:justify-between bg-brand">
<div data-testid="PrivacyPolicy" className="fixed bottom-0 left-0 right-0 z-999 flex flex-row justify-center md:justify-between bg-brand">
Copy link
Collaborator

Choose a reason for hiding this comment

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

make data-testid a prop

jest.clearAllMocks();
});

it("should render PrivacyPolicy", () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
it("should render PrivacyPolicy", () => {
it("should render PrivacyPolicyBanner when showCookiePolicy is true", () => {

@@ -0,0 +1,37 @@
import "@testing-library/jest-dom";
Copy link
Collaborator

Choose a reason for hiding this comment

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

Add the test also for not rendering the banner when showCookiePolicy is false

Comment on lines 19 to 25
it("should render the close button", () => {
const { getByTestId } = renderWithRedux(<PrivacyPolicyBanner />, {
banner: { showCookiePolicy: false },
});
const closeButton = getByTestId("closeButton");
expect(closeButton).toBeInTheDocument();
});
Copy link
Collaborator

Choose a reason for hiding this comment

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

You should add this test in should render PrivacyPolicyBanner when showCookiePolicy is true, because it should test if the component is rendered with all the right elements

<div className="z-50 lg:h-8 h-7 lg:w-8 w-7 flex items-center text-white rounded-full lg:border-solid lg:border lg:border-white hover:bg-blue-700 bg-transparent cursor-pointer place-content-center">
<div className="flex absolute lg:relative lg:p-6 md:py-0 lg:justify-center right-0 top-0 lg:items-center items-center" onClick={onAcceptCookiesPolicy}>
<div
data-testid="closeButton"
Copy link
Collaborator

Choose a reason for hiding this comment

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

the data-testid should be on the parent div, since it's the one that has an onClose

Copy link

@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.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 17f53bf and a318b4b.

Files selected for processing (3)
  • tests/components/banner/PrivacyPolicy.test.tsx (1 hunks)
  • jest.config.js (1 hunks)
  • src/components/banner/PrivacyPolicy.tsx (2 hunks)
Files skipped from review as they are similar to previous changes (2)
  • tests/components/banner/PrivacyPolicy.test.tsx
  • src/components/banner/PrivacyPolicy.tsx
Additional comments not posted (1)
jest.config.js (1)

31-31: LGTM!

The new mapping in the moduleNameMapper section is correctly added and enhances the module resolution capabilities of Jest.

@igorntwari igorntwari requested a review from mhenriette August 29, 2024 13:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create src/components/banner component test
3 participants