-
Notifications
You must be signed in to change notification settings - Fork 6
Test ButtonLoading #1725
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
base: main
Are you sure you want to change the base?
Test ButtonLoading #1725
Conversation
WalkthroughA new test suite for the Changes
Sequence Diagram(s)sequenceDiagram
participant TestSuite
participant ButtonLoading
participant Spinner
TestSuite->>ButtonLoading: Render with loading=true
ButtonLoading->>Spinner: Render with data-testid="spinner"
TestSuite->>ButtonLoading: Check if button is disabled
TestSuite->>ButtonLoading: Render with loading=false
ButtonLoading-->>Spinner: Spinner not rendered
TestSuite->>ButtonLoading: Check if spinner is absent
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. 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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this 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 UI
Review profile: ASSERTIVE
Plan: Pro
📒 Files selected for processing (2)
packages/ui-components/src/__tests__/ButtonLoading.test.ts
(1 hunks)packages/ui-components/src/lib/components/ButtonLoading.svelte
(1 hunks)
🔇 Additional comments (2)
packages/ui-components/src/lib/components/ButtonLoading.svelte (1)
10-10
: Good addition of test ID for testability.Adding the
data-testid="spinner"
attribute improves component testability by providing a reliable selector for the spinner element in tests. This pattern follows best practices for component testing.packages/ui-components/src/__tests__/ButtonLoading.test.ts (1)
1-27
: Well-structured test suite with good coverage.The test suite comprehensively covers the key functionality of the ButtonLoading component:
- Presence of spinner when loading
- Button disabled state during loading
- Absence of spinner when not loading
The tests are clear, concise, and follow testing best practices by using appropriate Testing Library methods.
it('should not render a spinner when loading is false', () => { | ||
render(ButtonLoading, { | ||
loading: false | ||
}); | ||
expect(screen.queryByTestId('spinner')).not.toBeInTheDocument(); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧹 Nitpick (assertive)
Consider adding a disabled state test for completeness.
While you have good coverage of the loading functionality, consider adding a test that verifies the button remains enabled when loading
is false and disabled
is false, and disabled when disabled
is true regardless of loading state.
+ it('should respect disabled prop when loading is false', () => {
+ render(ButtonLoading, {
+ loading: false,
+ disabled: true
+ });
+ const button = screen.getByRole('button');
+ expect(button).toBeDisabled();
+ });
+
+ it('should be enabled when loading and disabled are false', () => {
+ render(ButtonLoading, {
+ loading: false,
+ disabled: false
+ });
+ const button = screen.getByRole('button');
+ expect(button).not.toBeDisabled();
+ });
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
it('should not render a spinner when loading is false', () => { | |
render(ButtonLoading, { | |
loading: false | |
}); | |
expect(screen.queryByTestId('spinner')).not.toBeInTheDocument(); | |
}); | |
it('should not render a spinner when loading is false', () => { | |
render(ButtonLoading, { | |
loading: false | |
}); | |
expect(screen.queryByTestId('spinner')).not.toBeInTheDocument(); | |
}); | |
+ it('should respect disabled prop when loading is false', () => { | |
+ render(ButtonLoading, { | |
+ loading: false, | |
+ disabled: true | |
+ }); | |
+ const button = screen.getByRole('button'); | |
+ expect(button).toBeDisabled(); | |
+ }); | |
+ | |
+ it('should be enabled when loading and disabled are false', () => { | |
+ render(ButtonLoading, { | |
+ loading: false, | |
+ disabled: false | |
+ }); | |
+ const button = screen.getByRole('button'); | |
+ expect(button).not.toBeDisabled(); | |
+ }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there is already a test for disabled state
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# Check the content of the ButtonLoading component
cat packages/ui-components/src/lib/components/ButtonLoading.svelte
Length of output: 385
@hardingjam I see the confusion. You're right that there's a test checking that the button is disabled when loading
is true. However, looking at your component code:
<Button disabled={loading || disabled} {...$$props} on:click>
The button can be disabled in two ways: when loading
is true OR when disabled
is true. The current tests only verify the first condition (when loading is true), but don't test the behavior when the disabled
prop is independently set to true while loading is false.
The tests I suggested would specifically verify these additional conditions for complete coverage of the component's behavior. But if you feel the current test coverage is sufficient for your needs, that's perfectly fine!
describe('ButtonLoading', () => { | ||
it('should render a button with a spinner when loading is true', () => { | ||
render(ButtonLoading, { | ||
loading: true | ||
}); | ||
expect(screen.getByTestId('spinner')).toBeInTheDocument(); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧹 Nitpick (assertive)
Consider testing slot content.
The component accepts slot content, but there's no test to verify it renders correctly. Consider adding a test that verifies the button text is rendered properly.
+ it('should render slot content', () => {
+ render(ButtonLoading, {
+ loading: false,
+ $$slots: {
+ default: '<span>Button Text</span>'
+ }
+ });
+ expect(screen.getByText('Button Text')).toBeInTheDocument();
+ });
Committable suggestion skipped: line range outside the PR's diff.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not necessary
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hardingjam Understood! Thanks for clarifying your testing needs for this component.
Motivation
Solution
Checks
By submitting this for review, I'm confirming I've done the following:
Summary by CodeRabbit
Tests
Refactor