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: fix failing tests due to outdated router spies #1004

Merged
merged 1 commit into from
Oct 8, 2024

Conversation

JustARatherRidiculouslyLongUsername
Copy link
Contributor

@JustARatherRidiculouslyLongUsername JustARatherRidiculouslyLongUsername commented Oct 8, 2024

Description

We were using deprecated ways to mock and test routers in IntacctComponent and QboComponent unit tests, which caused tests to fail. All tests are passing now

Clickup

https://app.clickup.com/t/86cwabccw
https://app.clickup.com/t/86cwh86bz

Summary by CodeRabbit

  • Bug Fixes

    • Improved test reliability for the IntacctComponent and QboComponent by streamlining router usage in tests.
  • Tests

    • Enhanced testing environment with updated imports for SharedModule and HttpClientTestingModule.
    • Adjusted test cases to directly reference the router instance, ensuring accurate navigation checks.

Copy link
Contributor

coderabbitai bot commented Oct 8, 2024

Walkthrough

The pull request includes modifications to the test suites of the IntacctComponent and QboComponent. Key changes involve replacing RouterModule with provideRouter, removing router spy objects, and directly injecting the Router for testing. The imports have been updated to include SharedModule and HttpClientTestingModule. Test cases have been adjusted to reflect these changes, ensuring that the router's methods are tested directly.

Changes

File Path Change Summary
src/app/integrations/intacct/intacct.component.spec.ts Replaced RouterModule with provideRouter, removed Router spy, updated imports, adjusted test cases.
src/app/integrations/qbo/qbo.component.spec.ts Replaced direct Router injection with provideRouter, removed routerSpy, updated imports, adjusted test cases.

Possibly related PRs

Suggested labels

size/M

Suggested reviewers

  • DhaaraniCIT
  • ashwin1111

🐰 In the meadow, tests do play,
With routers spry, they find their way.
No more spies, just direct delight,
In code we trust, our tests take flight!
A hop, a jump, through modules we roam,
In the world of tests, we feel at home! 🌼


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.

@github-actions github-actions bot added the size/M Medium PR label Oct 8, 2024
Copy link

github-actions bot commented Oct 8, 2024

PR description must contain a link to a ClickUp (case-insensitive)

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 (3)
src/app/integrations/qbo/qbo.component.spec.ts (2)

25-25: Improved test suite setup with modern Angular practices.

The changes to the test suite setup are commendable:

  1. Using a real Router instance instead of a spy object allows for more accurate testing of routing behavior.
  2. Including SharedModule and HttpClientTestingModule in the imports enhances the test environment.
  3. Using provideRouter([]) in the TestBed configuration is the correct way to set up routing in the test environment.

These updates align with best practices in Angular testing.

Consider adding some mock routes to the provideRouter([]) call to more closely simulate the actual routing configuration of your application. For example:

provideRouter([
  { path: 'integrations/qbo/onboarding/landing', component: DummyComponent },
  // Add other relevant routes
])

This would allow for more comprehensive testing of navigation scenarios.

Also applies to: 50-51, 58-59


Line range hint 114-120: Improved test structure for onboarding states, but lacking assertions.

The update to use a forEach loop for testing different onboarding states is a good improvement:

  • It allows for more concise testing of multiple states.
  • Makes it easier to add new states to test in the future.

However, there's an important issue to address:

The test case is missing expectations within the loop. Without assertions, the test isn't verifying any behavior for the different onboarding states.

Consider adding appropriate expectations within the loop. For example:

testOnboardingState.forEach(({ state, expectedRoute }) => {
  const testWorkspace = { ...mockWorkspace, onboarding_state: state };
  workspaceServiceSpy.getWorkspace.and.returnValue(of([testWorkspace]));

  fixture.detectChanges();
  tick();

  expect(router.navigateByUrl).toHaveBeenCalledWith(expectedRoute);
});

This ensures that the correct navigation occurs for each onboarding state.

src/app/integrations/intacct/intacct.component.spec.ts (1)

63-63: LGTM: Router setup improved, with a minor suggestion

The changes to the router setup are good:

  1. Directly injecting the Router is cleaner than using a spy object.
  2. Spying on specific methods provides more precise control over the router behavior in tests.
  3. Using spyOnProperty for router.events is a good practice for mocking observables.

These changes align with best practices for Angular testing.

Consider moving the router spies to the beforeEach block for consistency with other service spies:

beforeEach(() => {
  // ... other setup code ...
  router = TestBed.inject(Router);
  spyOn(router, 'navigateByUrl');
  spyOnProperty(router, 'events').and.returnValue(of(new NavigationEnd(0, '', '')));
});

Also applies to: 66-67

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between e7587e1 and 82e62c9.

📒 Files selected for processing (2)
  • src/app/integrations/intacct/intacct.component.spec.ts (7 hunks)
  • src/app/integrations/qbo/qbo.component.spec.ts (6 hunks)
🧰 Additional context used
🔇 Additional comments (11)
src/app/integrations/qbo/qbo.component.spec.ts (4)

2-2: Improved import statements and test setup.

The changes to the import statements are beneficial:

  1. Using provideRouter instead of directly importing Router aligns with modern Angular testing practices.
  2. Including SharedModule and HttpClientTestingModule enhances the test environment, potentially providing necessary dependencies and HTTP testing utilities.

These updates contribute to a more robust and maintainable test suite.

Also applies to: 13-14


68-69: Improved router testing setup.

The changes to the router setup enhance the test suite:

  1. Injecting the Router directly instead of creating a spy object allows for more realistic testing scenarios.
  2. Spying on the navigateByUrl method of the injected router provides a way to verify navigation without mocking the entire Router service.

This approach offers a good balance between isolation and integration testing for routing behavior.


97-97: Consistent update to navigation expectation.

The change in the expect statement correctly reflects the earlier modifications:

  • Using router.navigateByUrl instead of routerSpy.navigateByUrl is consistent with the new approach of using a real Router instance.
  • This ensures that the test accurately verifies the navigation behavior of the component.

110-110: Consistent update to another navigation expectation.

This change maintains consistency with the new Router testing approach:

  • Using router.navigateByUrl instead of routerSpy.navigateByUrl in this expect statement aligns with the earlier modifications.
  • It ensures that navigation behavior is consistently tested across different scenarios in the test suite.
src/app/integrations/intacct/intacct.component.spec.ts (7)

2-2: LGTM: Import changes enhance testing setup

The changes to the imports are appropriate:

  1. Replacing RouterModule with provideRouter aligns with modern Angular testing practices.
  2. Adding SharedModule and HttpClientTestingModule enhances the testing environment with shared components and HTTP testing capabilities.

These changes should improve the overall testing setup.

Also applies to: 14-15


46-47: LGTM: Test module setup improved

The changes to the test module setup are appropriate:

  1. Separating the IntacctComponent declaration improves readability.
  2. Adding SharedModule and HttpClientTestingModule to imports is consistent with the new import statements.
  3. Using provideRouter in the providers array is the correct approach for the updated Angular testing practices.

These changes should result in a more robust and maintainable test environment.

Also applies to: 54-55


90-90: LGTM: Router assertion updated correctly

The change to directly assert router.navigateByUrl is consistent with the new router setup. This modification maintains the same test logic while aligning with the updated testing approach.


102-102: LGTM: Router assertion updated consistently

The change to directly assert router.navigateByUrl is consistent with the new router setup and the previous test case. This modification maintains the same test logic while aligning with the updated testing approach.


106-106: Excellent improvement to test case structure

Great job on refactoring this test case! The changes bring several benefits:

  1. Using Object.entries to iterate over test cases makes the test more dynamic and maintainable.
  2. It's now easier to add or modify test cases without changing the test structure.
  3. The assertion on router.navigateByUrl is consistent with previous changes.

This refactoring improves both the readability and maintainability of the test suite. Well done!

Also applies to: 112-112


123-123: LGTM: Router assertion updated consistently

The change to directly assert router.navigateByUrl is consistent with the new router setup and previous test cases. This modification maintains the same test logic while aligning with the updated testing approach.


Line range hint 1-137: Overall: Excellent improvements to the test suite

This pull request makes several positive changes to the IntacctComponent test suite:

  1. Updated imports and test module setup to use modern Angular testing practices.
  2. Improved router testing by directly injecting and spying on the Router.
  3. Refactored test cases for better readability and maintainability.
  4. Consistently updated router assertions across all test cases.

These changes align well with the PR objectives of fixing failing tests due to outdated router spies. The new approach should result in more robust and maintainable tests.

One minor suggestion was made to move the router spies to the beforeEach block for consistency, but this is a small optimization and not critical.

Great work on improving the test suite!

@@ -125,6 +127,6 @@ describe('QboComponent', () => {
fixture.detectChanges();
tick();

expect(routerSpy.navigateByUrl).toHaveBeenCalled();
expect(router.navigateByUrl).toHaveBeenCalled();
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Consistent update to navigation expectation, but potential logical error in the test.

The change to use router.navigateByUrl instead of routerSpy.navigateByUrl is consistent with the earlier modifications and maintains the new approach of using a real Router instance.

However, there's a potential issue with the test logic:

The test description states "should not navigate if pathname is not /integrations/qbo", but the expectation expect(router.navigateByUrl).toHaveBeenCalled() checks if navigation did occur. This seems contradictory.

Consider updating the expectation to match the test description:

expect(router.navigateByUrl).not.toHaveBeenCalled();

This would correctly verify that no navigation occurs when the pathname is not "/integrations/qbo".

@anishfyle anishfyle requested a review from ashwin1111 October 8, 2024 13:50
@JustARatherRidiculouslyLongUsername JustARatherRidiculouslyLongUsername merged commit 6d0540c into master Oct 8, 2024
4 of 5 checks passed
This was referenced Oct 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size/M Medium PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants