-
Notifications
You must be signed in to change notification settings - Fork 0
🧪 Add tests for SettingsTab component #377
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
Open
is0692vs
wants to merge
1
commit into
main
Choose a base branch
from
add-settingstab-tests-12227445049313615678
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+109
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| // @vitest-environment jsdom | ||
| import { render, screen, fireEvent } from "@testing-library/react"; | ||
| import { describe, expect, it, vi, beforeEach } from "vitest"; | ||
| import { SettingsTab } from "../SettingsTab"; | ||
| import { MAIN_BLOCKS, DETAIL_OPTIONS } from "@/lib/cardGeneratorConstants"; | ||
| import type { CardDisplayOptions, CardBlockId } from "@/lib/types"; | ||
| import "@testing-library/jest-dom"; | ||
|
|
||
| describe("SettingsTab", () => { | ||
| const mockIsBlockVisible = vi.fn(); | ||
| const mockToggleMainBlockVisibility = vi.fn(); | ||
| const mockToggleDisplayOption = vi.fn(); | ||
|
|
||
| const defaultProps = { | ||
| isBlockVisible: mockIsBlockVisible, | ||
| toggleMainBlockVisibility: mockToggleMainBlockVisibility, | ||
| displayOptions: {} as CardDisplayOptions, | ||
| toggleDisplayOption: mockToggleDisplayOption, | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| vi.resetAllMocks(); | ||
| }); | ||
|
|
||
| it("renders all MAIN_BLOCKS checkboxes", () => { | ||
| mockIsBlockVisible.mockReturnValue(false); | ||
| render(<SettingsTab {...defaultProps} />); | ||
|
|
||
| MAIN_BLOCKS.forEach(({ label }) => { | ||
| const checkbox = screen.getByLabelText(label); | ||
| expect(checkbox).toBeInTheDocument(); | ||
| expect(checkbox).toHaveAttribute("type", "checkbox"); | ||
| expect(checkbox).not.toBeChecked(); | ||
| }); | ||
| }); | ||
|
|
||
| it("renders all DETAIL_OPTIONS checkboxes", () => { | ||
| render(<SettingsTab {...defaultProps} />); | ||
|
|
||
| DETAIL_OPTIONS.forEach(({ label }) => { | ||
| const checkbox = screen.getByLabelText(label); | ||
| expect(checkbox).toBeInTheDocument(); | ||
| expect(checkbox).toHaveAttribute("type", "checkbox"); | ||
| expect(checkbox).not.toBeChecked(); | ||
| }); | ||
| }); | ||
|
|
||
| it("sets MAIN_BLOCKS checkbox checked state based on isBlockVisible prop", () => { | ||
| // Mock that 'profile' and 'skills' are visible, others are not | ||
| mockIsBlockVisible.mockImplementation((id: CardBlockId) => id === "profile" || id === "skills"); | ||
|
|
||
| render(<SettingsTab {...defaultProps} />); | ||
|
|
||
| MAIN_BLOCKS.forEach(({ id, label }) => { | ||
| const checkbox = screen.getByLabelText(label); | ||
| if (id === "profile" || id === "skills") { | ||
| expect(checkbox).toBeChecked(); | ||
| } else { | ||
| expect(checkbox).not.toBeChecked(); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| it("sets DETAIL_OPTIONS checkbox checked state based on displayOptions prop", () => { | ||
| const displayOptions: CardDisplayOptions = { | ||
| showAvatar: true, | ||
| showLocation: true, | ||
| // Others are implicitly false or undefined | ||
| }; | ||
|
|
||
| render(<SettingsTab {...defaultProps} displayOptions={displayOptions} />); | ||
|
|
||
| DETAIL_OPTIONS.forEach(({ key, label }) => { | ||
| const checkbox = screen.getByLabelText(label); | ||
| if (key === "showAvatar" || key === "showLocation") { | ||
| expect(checkbox).toBeChecked(); | ||
| } else { | ||
| expect(checkbox).not.toBeChecked(); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| it("calls toggleMainBlockVisibility with correct id when MAIN_BLOCKS checkbox is clicked", () => { | ||
| mockIsBlockVisible.mockReturnValue(false); | ||
| render(<SettingsTab {...defaultProps} />); | ||
|
|
||
| const firstMainBlock = MAIN_BLOCKS[0]; | ||
| const checkbox = screen.getByLabelText(firstMainBlock.label); | ||
|
|
||
| fireEvent.click(checkbox); | ||
|
|
||
| expect(mockToggleMainBlockVisibility).toHaveBeenCalledTimes(1); | ||
| expect(mockToggleMainBlockVisibility).toHaveBeenCalledWith(firstMainBlock.id); | ||
| }); | ||
|
|
||
| it("calls toggleDisplayOption with correct key when DETAIL_OPTIONS checkbox is clicked", () => { | ||
| render(<SettingsTab {...defaultProps} />); | ||
|
|
||
| const firstDetailOption = DETAIL_OPTIONS[0]; | ||
| const checkbox = screen.getByLabelText(firstDetailOption.label); | ||
|
|
||
| fireEvent.click(checkbox); | ||
|
|
||
| expect(mockToggleDisplayOption).toHaveBeenCalledTimes(1); | ||
| expect(mockToggleDisplayOption).toHaveBeenCalledWith(firstDetailOption.key); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -21,7 +21,9 @@ export default defineConfig({ | |||||||||||
| "src/components/LanguageChart.tsx", | ||||||||||||
| "src/components/SkillsCard.tsx", | ||||||||||||
| "src/components/LayoutEditor.tsx", | ||||||||||||
| "src/components/SettingsTab.tsx", | ||||||||||||
| "src/lib/rateLimit.ts", | ||||||||||||
|
|
||||||||||||
| "src/app/api/og/[username]/route.tsx" | ||||||||||||
|
Comment on lines
25
to
27
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Prompt To Fix With AIThis is a comment left during a code review.
Path: vitest.config.ts
Line: 25-27
Comment:
カバレッジの `include` 配列の途中に不要な空行が入っています。配列要素のリスト内には空行を入れないのが一般的なスタイルです。
```suggestion
"src/lib/rateLimit.ts",
"src/app/api/og/[username]/route.tsx"
```
How can I resolve this? If you propose a fix, please make it concise.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||||||||||||
| ], | ||||||||||||
| thresholds: { | ||||||||||||
|
|
||||||||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
mockIsBlockVisibleのセットアップが欠落しているrenders all DETAIL_OPTIONS checkboxesテストではbeforeEachによりvi.resetAllMocks()が実行されるため、mockIsBlockVisibleはデフォルトでundefinedを返します。その結果、MAIN_BLOCKS のチェックボックスがchecked={undefined}でレンダリングされ、React が「制御→非制御コンポーネントへの切り替え」の警告を出力する可能性があります。他のテスト(例:renders all MAIN_BLOCKS checkboxes)と同様にmockIsBlockVisible.mockReturnValue(false)を先頭に追加すると、テストの一貫性が保たれます。Prompt To Fix With AI