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

UI Tests for username change on /settings page. #1203

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from

Conversation

petercr
Copy link
Contributor

@petercr petercr commented Oct 31, 2024

✨ Codu Pull Request πŸ’»

Fixes #1168
** Only Part 3.**

Pull Request details

Added UI tests for /settings to:

  • tests for input field & types
  • tries to submit a bad user name
  • checks for error visibility
  • resets the form
  • changes user name, saves, and checks for update

Any Breaking changes

None

Associated Screenshots

None, but the tests pass πŸ˜… I promise!

[Optional] What gif best describes this PR or how it makes you feel

Like this, only instead of Monday, Wed, Friday it's like start, middle, end πŸ˜†
A dog that starts out grumpy, then get glad, then gets happy.

@petercr petercr requested a review from a team as a code owner October 31, 2024 23:33
Copy link

vercel bot commented Oct 31, 2024

@petercr is attempting to deploy a commit to the CodΓΊ Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Contributor

coderabbitai bot commented Oct 31, 2024

Walkthrough

The changes in this pull request enhance the end-to-end test suite for the authenticated settings page by adding a new test case focused on the username input field. The modifications include updating the import statements and implementing a series of assertions that validate the visibility, attributes, and functionality of the username input, including error handling for invalid inputs and confirmation of valid submissions.

Changes

File Change Summary
e2e/settings.spec.ts Updated import statements; added a new test case for username input validation, including checks for visibility, attributes, error messages, and form reset functionality.
app/(app)/settings/_client.tsx Enhanced toast notifications for success and error handling during settings save operations by adding custom class names.
e2e/setup.ts Updated bio property in userData with a grammatical error; changed websiteUrl from a relative to an absolute URL.

Assessment against linked issues

Objective Addressed Explanation
Add E2E tests for the username field (1168) βœ…
Ensure tests are standalone and focused (1168) βœ…
Validate input fields against Zod schema (1168) ❌ No explicit validation against Zod schema shown.
Cover edge cases for username input (1168) βœ…

Possibly related PRs

  • Fix/e2e testsΒ #1061: This PR focuses on fixing and stabilizing end-to-end tests, which may relate to the enhancements made in the main PR for the settings page tests, as both involve improving test coverage and reliability.
  • fix: update e2e tests for newer changes in siteΒ #1085: This PR updates E2E tests, including modifications to the homepage tests, which may share similar testing strategies or structures with the new tests added for the settings page.
  • Feat/improving existing e2e testsΒ #1125: This PR improves existing E2E tests, which could be relevant as it aims to enhance the overall testing framework, similar to the enhancements made in the main PR for the settings page.
  • feat: adding e2e test for article commentingΒ #1158: This PR adds tests for commenting functionality, which may share testing principles with the username input validation in the settings tests, focusing on user input and feedback.
  • Isolating E2E test suite from seed scriptΒ #1192: The isolation of the E2E test suite from the seed script may impact how tests are structured, potentially relating to the main PR's enhancements in testing.

Suggested labels

hacktoberfest-accepted, hacktoberfest

Suggested reviewers

  • NiallJoeMaher

🐰 In the land of code, where rabbits hop,
A test was added, and we won't stop!
For usernames bright, we check and we see,
Valid or not, they must be error-free!
With each little change, our coverage grows,
Hopping through tests, as the confidence flows! πŸ‡βœ¨


πŸ“œ Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

πŸ“₯ Commits

Reviewing files that changed from the base of the PR and between fcd1dbb and c6949d0.

πŸ“’ Files selected for processing (1)
  • e2e/setup.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • e2e/setup.ts

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.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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: 3

πŸ“œ Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

πŸ“₯ Commits

Reviewing files that changed from the base of the PR and between 2df1fd9 and e6883a5.

πŸ“’ Files selected for processing (1)
  • e2e/settings.spec.ts (2 hunks)
πŸ”‡ Additional comments (1)
e2e/settings.spec.ts (1)

Line range hint 4-7: Implement missing unauthenticated tests

The PR objectives mention testing unauthenticated user redirection to /get-started, but this test is missing.

Would you like me to provide an implementation for the unauthenticated test case that verifies redirection?

Comment on lines 14 to 16
// Test for changing username
test('Username input field', async ({ page }) => {
await page.goto('http://localhost:3000/settings', { timeout: 30000 });
Copy link
Contributor

@coderabbitai coderabbitai bot Oct 31, 2024

Choose a reason for hiding this comment

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

πŸ› οΈ Refactor suggestion

Improve test configuration and navigation

Consider these improvements:

  1. The 30-second timeout seems excessive for local development
  2. The hardcoded URL should be replaced with a configurable base URL
- await page.goto('http://localhost:3000/settings', { timeout: 30000 });
+ await page.goto('/settings');

Add this to your playwright.config.ts:

use: {
  baseURL: process.env.BASE_URL || 'http://localhost:3000',
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!

e2e/settings.spec.ts Outdated Show resolved Hide resolved
e2e/settings.spec.ts Outdated Show resolved Hide resolved
@@ -1,4 +1,4 @@
import test from "@playwright/test";
import { test, expect } from "@playwright/test";
import { loggedInAsUserOne } from "./utils";

test.describe("Unauthenticated setttings Page", () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we change this to authenticated settings page. Because the user is authenticated for these tests

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure exactly what you're saying here?

Did you want a separate file for auth vs. un-auth tests, because doesn't a user have to be logged in to edit their profile?

I guess I could use more detail on this πŸ‘ŒπŸ»

Copy link
Contributor

Choose a reason for hiding this comment

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

Your new test is ran inside a test suite called 'Unauthenticated setttings Page'

But this is an authenticated test because users need to login to change their username.

If you just change the name of the test it will be fixed

// Test that the input field can be filled with a valid value and saves it
await inputField.fill('codu-rules');
await page.locator('button[type="submit"]').click();
await expect(inputField).toHaveValue('codu-rules');
Copy link
Contributor

Choose a reason for hiding this comment

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

Will this wait for the username change to take effect?

If you fill in the new username and then click submit.

Will this test see the new username you have entered as the value of the input and complete straight away?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I mean the tests pass for it. But it could be just catching the still filled in form from before the reset.

Would you like me to change it to a page refresh instead?

Or would you like some kind of timeOut in-between the state change and checking for the change to persist?

I would be happy to add either one πŸ‘πŸ»

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah I am not sure is there a loading indicator for these types of change? It would be nice to see

  1. Click submit
  2. Loading indicator appears
  3. Loading indicator hidden
  4. Check new username has taken effect

Copy link
Contributor

@JohnAllenTech JohnAllenTech left a comment

Choose a reason for hiding this comment

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

Just made some general comments. I am not a maintainer here so everything is just a suggestion

@NiallJoeMaher
Copy link
Contributor

@JohnAllenTech let me know when this is good to go since you are the overlord of tests.

@petercr
Copy link
Contributor Author

petercr commented Nov 7, 2024

@JohnAllenTech I made some changes and added the extra tests you had suggested.
Tests now cover:

  • waiting for toast to show
  • waiting for toast to disappear
  • full page refresh
  • checking new value persists in input field

I did have to change a field in /e2e/setup/setup.ts to add https:// to it.
Otherwise you get an error on the form in E2E testing.

I also added new class names to both the toast messages for UI testing.

Thanks

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

🧹 Outside diff range and nitpick comments (5)
e2e/settings.spec.ts (2)

Line range hint 4-8: Fix typo and remove empty test suite.

  1. Fix the typo in "setttings" (three 't's).
  2. The empty test suite for unauthenticated users should either be implemented or removed to maintain clean code.
-test.describe("Unauthenticated setttings Page", () => {
-  //
-  // Replace with tests for unauthenticated users
-});
+// TODO: Implement tests for unauthenticated users being redirected to /get-started
+test.describe("Unauthenticated settings Page", () => {
+  test("redirects to get-started page", async ({ page }) => {
+    await page.goto("/settings");
+    await expect(page).toHaveURL("/get-started");
+  });
+});

27-32: Improve error message validation.

The error message validation could be more robust by:

  1. Using data-testid for reliable selection
  2. Centralizing error messages
  3. Verifying error styling
+    // Define error messages
+    const ERROR_MESSAGES = {
+      INVALID_CHARS: 'Username can only contain alphanumerics and dashes.'
+    } as const;
+
-    const errorMessage = page.locator('p:text-is("Username can only contain alphanumerics and dashes.")')
+    const errorMessage = page.locator('[data-testid="username-error"]');
     await expect(errorMessage).toBeVisible();
-    await expect(errorMessage).toHaveText('Username can only contain alphanumerics and dashes.');
+    await expect(errorMessage).toHaveText(ERROR_MESSAGES.INVALID_CHARS);
+    await expect(errorMessage).toHaveCSS('color', 'rgb(220, 38, 38)'); // Verify error styling
e2e/setup.ts (1)

123-123: Document the reason for URL format change.

While the change to use an absolute URL is sensible, it would be helpful to document why this change was necessary, especially since it seems tangential to the main PR objective of testing username changes.

Add a comment explaining the URL format requirement:

+      // Using absolute URL to comply with website URL validation requirements
       websiteUrl: "https://codu.co",
app/(app)/settings/_client.tsx (2)

Line range hint 261-276: Consider adding a loading indicator during username validation.

While the username field implementation is solid, it could benefit from a visual indicator during validation to improve user experience. This would be particularly useful when checking for username availability.

Consider adding a loading state:

 <Field>
   <Input
     id="username"
     type="text"
     autoComplete="username"
     invalid={!!errors?.username}
+    className={isValidating ? "opacity-50" : ""}
     {...register("username")}
   />
+  {isValidating && (
+    <div className="mt-2 flex items-center text-sm text-gray-500">
+      <Loader2 className="mr-2 h-4 w-4 animate-spin" />
+      Checking username availability...
+    </div>
+  )}
   {errors?.username && (
     <ErrorMessage>{errors.username.message}</ErrorMessage>
   )}
 </Field>

Line range hint 1-458: Consider breaking down the Settings component for better maintainability.

The component is well-structured but handles multiple responsibilities. Consider splitting it into smaller, focused components for better maintainability and testing.

Suggested structure:

  1. Create separate components for each section:

    • ProfilePhotoSection
    • PersonalInfoSection (name, username, bio)
    • ContactSection (location, website)
    • EmailSection
    • NotificationsSection
  2. Move the file upload logic to a custom hook:

    // hooks/useProfilePhotoUpload.ts
    const useProfilePhotoUpload = () => {
      const [profilePhoto, setProfilePhoto] = useState<ProfilePhoto>({...});
      // ... file upload logic
      return { profilePhoto, handleImageChange };
    };

This refactoring would:

  • Improve code organization
  • Make testing easier
  • Reduce cognitive load when maintaining the component
  • Allow for better error boundary placement
πŸ“œ Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

πŸ“₯ Commits

Reviewing files that changed from the base of the PR and between e6883a5 and f9140cf.

πŸ“’ Files selected for processing (3)
  • app/(app)/settings/_client.tsx (1 hunks)
  • e2e/settings.spec.ts (2 hunks)
  • e2e/setup.ts (1 hunks)
πŸ”‡ Additional comments (3)
e2e/settings.spec.ts (1)

43-45: Verify username persistence more thoroughly.

The current persistence check could be enhanced by:

  1. Verifying the username in the UI header/profile
  2. Checking the API response
  3. Testing persistence across multiple page navigations
βœ… Verification successful

Current username persistence testing is sufficient

The current e2e test adequately verifies username persistence because:

  1. The username is properly integrated throughout the application (Nav, BioBar, Settings)
  2. The page reload test verifies server-side persistence
  3. Additional UI checks would be redundant as the username update automatically propagates through the layout and components

The implementation shows the username is:

  • Stored in the database and fetched server-side (layout.tsx)
  • Consistently displayed across navigation and profile components
  • Properly validated and persisted in settings
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify username persistence across the application
# Note: This script helps identify places where the username should be updated

# Find files that might display or use the username
echo "Files potentially using username:"
rg -l "username" --type=tsx --type=ts

# Find API endpoints handling username
echo "\nAPI endpoints for username:"
rg -l "username.*api" --type=ts

Length of output: 245


Script:

#!/bin/bash
# Let's try a different approach to find username-related code

# Search for username-related patterns in all files
echo "Files containing username-related patterns:"
rg -l -i "username|user.*name"

# Look for settings or profile-related components
echo -e "\nSettings and profile-related files:"
fd -e ts -e tsx -e js -e jsx "settings|profile" --type f

# Search for API calls or endpoints
echo -e "\nAPI-related patterns:"
rg -l "api.*username|username.*api"

# Look specifically in the e2e test file to understand the context better
echo -e "\nContext from the e2e test file:"
rg -C 5 "username" "e2e/settings.spec.ts"

Length of output: 3690


Script:

#!/bin/bash
# Let's examine the settings page implementation and username persistence logic

# Check settings page implementation
echo "Settings page implementation:"
rg -C 10 "username" "app/(app)/settings/_client.tsx"

# Check username usage in layout and navigation
echo -e "\nUsername usage in layout:"
rg -C 5 "username" "app/(app)/layout.tsx"

# Check profile/bio components that might display username
echo -e "\nUsername display in BioBar:"
rg -C 5 "username" "components/BioBar/BioBar.tsx"

# Check navigation components for username display
echo -e "\nUsername in navigation:"
rg -C 5 "username" "components/Nav/Nav.tsx"

Length of output: 4555

e2e/setup.ts (1)

123-123: Verify URL format change impact on tests.

The change from relative URL "codu.co" to absolute URL "https://codu.co" looks good, but let's ensure this aligns with the validation requirements.

Let's verify the URL validation requirements:

βœ… Verification successful

URL format change aligns with schema requirements

The change from "codu.co" to "https://codu.co" is correct as the websiteUrl field in schema/profile.ts uses Zod's .url() validator, which requires a fully qualified URL with protocol. This validation would reject the previous "codu.co" format.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check URL validation schema and related tests

# Look for URL validation schema
rg -A 5 'websiteUrl.*zod'

# Look for website URL related tests
rg -A 5 'websiteUrl.*test'

Length of output: 56


Script:

#!/bin/bash
# Let's try a broader search for URL validation patterns and website-related schemas

# Search for any URL validation patterns
rg -i "url.*validation|validate.*url" -A 5

# Search for website URL usage in tests
rg "website.*url|url.*website" -A 5

# Search for zod schema definitions that might include URL
rg "z\.string\(\).*url|url.*z\.string" -A 5

# Look for any URL-related constants or configurations
rg "URL_PATTERN|URL_REGEX" -A 5

Length of output: 2168

app/(app)/settings/_client.tsx (1)

89-94: LGTM: Toast styling changes are clean and consistent.

The addition of className props to toast notifications provides better styling control while maintaining the existing functionality.

e2e/settings.spec.ts Outdated Show resolved Hide resolved
e2e/settings.spec.ts Outdated Show resolved Hide resolved
@JohnAllenTech
Copy link
Contributor

@JohnAllenTech I made some changes and added the extra tests you had suggested.

Tests now cover:

  • waiting for toast to show

  • waiting for toast to disappear

  • full page refresh

  • checking new value persists in input field

I did have to change a field in /e2e/setup/setup.ts to add https:// to it.

Otherwise you get an error on the form in E2E testing.

I also added new class names to both the toast messages for UI testing.

Thanks

Hey @petercr apologies I was AFK for a few days. I'll take a look at this in a while and reply properly

@NiallJoeMaher
Copy link
Contributor

Tests failing here.

@petercr
Copy link
Contributor Author

petercr commented Nov 16, 2024

Well crap πŸ˜• that's no good.
I will look into this over the weekend (next 2 days) and see if I can figure it out. πŸ‘ŒπŸ»

I wonder if this has to do with the newsletter sub function?
Either way I'm on it πŸ‘πŸ»

@petercr
Copy link
Contributor Author

petercr commented Nov 18, 2024

When I run the test locally, they all pass πŸ€·πŸΌβ€β™‚οΈ see pic below.
I'm not sure but this may have to do with /server/lib/newsletter.ts.

I added extra code on line 39 to keep from getting the email API errors for the newsletter.
You can see some controller errors from posts.sidebarData and the tRPC error for profile.edit.

Here's what I added locally for this:

 if (process.env.ENV === "local" || "development") {
    return true;
  }

But it might be simpler to go with:

 if (process.env.ENV !== "production") {
    return true;
  }

Here is the image of the tests passing.

msrdc_ttyjHMMI1j

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.

E2E test - /settings page - Multiple issues available
3 participants