Skip to content

Conversation

ElifDogaliBal
Copy link
Contributor

@ElifDogaliBal ElifDogaliBal commented Oct 3, 2025

Summary

Add useBalanceAccounts unit tests

@ElifDogaliBal ElifDogaliBal requested a review from a team as a code owner October 3, 2025 14:27
Copy link
Contributor

Summary of Changes

Hello @ElifDogaliBal, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a comprehensive suite of unit tests for the useBalanceAccounts custom hook. The tests are designed to validate the hook's core functionality, including its ability to fetch and filter balance accounts, manage loading and error states, and correctly respond to various input parameters and data conditions. This enhancement significantly improves the reliability and maintainability of the balance account management logic by ensuring its behavior is consistent and robust across different scenarios.

Highlights

  • Comprehensive Unit Tests: Comprehensive unit tests have been added for the useBalanceAccounts custom hook, ensuring its reliability and correct behavior across various scenarios.
  • Core Functionality Testing: Tests cover core functionalities such as fetching all balance accounts, filtering by a specific balanceAccountId, and correctly handling loading, error, and isBalanceAccountIdWrong states.
  • Dependency Mocking and Edge Cases: Dependencies like ConfigContext and useFetch are thoroughly mocked, and edge cases including undefined data, empty data arrays, and invalid balanceAccountId inputs are explicitly tested to ensure robust error handling.
  • Enabled Parameter and Memoization: The enabled parameter's functionality is verified, ensuring the hook correctly enables or disables data fetching. Memoization behavior is also tested to confirm efficient re-renders and data consistency.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@ElifDogaliBal ElifDogaliBal changed the title Add useBalanceAccount unit tests Add useBalanceAccounts unit tests Oct 3, 2025
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request adds a comprehensive suite of unit tests for the useBalanceAccounts hook using vitest and @testing-library/preact. The tests are well-structured and cover various scenarios, including fetching, filtering, handling of the enabled parameter, and edge cases. My feedback focuses on improving the type safety, robustness, and cleanliness of the test code by removing as any casts, unused code, and making mock interactions less brittle. Overall, this is a great addition to ensure the hook's reliability.

import { test } from 'vitest';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { describe, expect, test, vi, beforeEach } from 'vitest';
import { renderHook, waitFor } from '@testing-library/preact';
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The waitFor utility is imported but not used in this file. It's good practice to remove unused imports to keep the code clean and avoid confusion.

Suggested change
import { renderHook, waitFor } from '@testing-library/preact';
import { renderHook } from '@testing-library/preact';

Comment on lines +31 to +35
mockUseConfigContext.mockReturnValue({
endpoints: {
getBalanceAccounts: mockBalanceAccountEndpoint,
},
} as any);
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Using as any bypasses TypeScript's type checking, which can make tests harder to maintain and more brittle to changes in the underlying hook's return type. It's better to provide a more complete mock object that satisfies the ReturnType<typeof ConfigContext.useConfigContext>. This makes the test more robust.

This feedback also applies to other uses of as any for mocking this context on lines 163 and 200.

Suggested change
mockUseConfigContext.mockReturnValue({
endpoints: {
getBalanceAccounts: mockBalanceAccountEndpoint,
},
} as any);
mockUseConfigContext.mockReturnValue({
endpoints: {
getBalanceAccounts: mockBalanceAccountEndpoint,
},
extraConfig: {},
hasError: false,
isExpired: false,
isFrozen: false,
refreshing: false,
http: vi.fn(),
refresh: vi.fn(),
});

test('should call balance account endpoint with empty object', async () => {
mockBalanceAccountEndpoint.mockResolvedValue(mockBalanceAccountsData);

const { result } = renderHook(() => useBalanceAccounts());
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The result variable is declared but never used. You can simplify the code by just calling renderHook without destructuring its return value. This also applies to line 202.

Suggested change
const { result } = renderHook(() => useBalanceAccounts());
renderHook(() => useBalanceAccounts());

const { result } = renderHook(() => useBalanceAccounts());

// Get the queryFn from the useFetch call
const useFetchCall = mockUseFetch.mock.calls[0]?.[0];
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Accessing mock calls by a hardcoded index (e.g., mock.calls[0]) can be brittle. If other calls to the mock are added before this line in the test, it could break. Using mock.lastCall is more robust as it always refers to the most recent invocation. This also applies to line 204.

Suggested change
const useFetchCall = mockUseFetch.mock.calls[0]?.[0];
const useFetchCall = mockUseFetch.mock.lastCall?.[0];

Copy link
Contributor

github-actions bot commented Oct 3, 2025

Coverage after merging unit-test/use-balance-accounts into group/increase-unit-test-coverage will be

CoverageDiff
50.65%▴ +0.90%

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.

1 participant