-
Notifications
You must be signed in to change notification settings - Fork 0
🧪 Add test file for SettingsTab #327
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
2
commits into
main
Choose a base branch
from
add-settingstab-test-13543907246275854376
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.
+132
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,131 @@ | ||||||||||||||||||||||||||
| // @vitest-environment jsdom | ||||||||||||||||||||||||||
| import { render, screen } from "@testing-library/react"; | ||||||||||||||||||||||||||
| import userEvent from "@testing-library/user-event"; | ||||||||||||||||||||||||||
| import { describe, it, expect, vi, beforeEach } from "vitest"; | ||||||||||||||||||||||||||
| import { SettingsTab } from "./SettingsTab"; | ||||||||||||||||||||||||||
| import { MAIN_BLOCKS, DETAIL_OPTIONS } from "@/lib/cardGeneratorConstants"; | ||||||||||||||||||||||||||
| import type { CardDisplayOptions } from "@/lib/cardSettings"; | ||||||||||||||||||||||||||
| import type { CardBlockId } from "@/lib/types"; | ||||||||||||||||||||||||||
| import "@testing-library/jest-dom"; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| describe("SettingsTab", () => { | ||||||||||||||||||||||||||
| const defaultDisplayOptions: CardDisplayOptions = { | ||||||||||||||||||||||||||
| showAvatar: true, | ||||||||||||||||||||||||||
| showBio: false, | ||||||||||||||||||||||||||
| showStats: true, | ||||||||||||||||||||||||||
| showLocation: false, | ||||||||||||||||||||||||||
| showJoinedDate: true, | ||||||||||||||||||||||||||
| showTopics: false, | ||||||||||||||||||||||||||
| showLanguage: true, | ||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const defaultIsBlockVisible = vi.fn((id: CardBlockId) => id === "profile" || id === "contributions"); | ||||||||||||||||||||||||||
| const toggleMainBlockVisibility = vi.fn(); | ||||||||||||||||||||||||||
| const toggleDisplayOption = vi.fn(); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| beforeEach(() => { | ||||||||||||||||||||||||||
| vi.clearAllMocks(); | ||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| it("renders all main block labels", () => { | ||||||||||||||||||||||||||
| render( | ||||||||||||||||||||||||||
| <SettingsTab | ||||||||||||||||||||||||||
| isBlockVisible={defaultIsBlockVisible} | ||||||||||||||||||||||||||
| toggleMainBlockVisibility={toggleMainBlockVisibility} | ||||||||||||||||||||||||||
| displayOptions={defaultDisplayOptions} | ||||||||||||||||||||||||||
| toggleDisplayOption={toggleDisplayOption} | ||||||||||||||||||||||||||
| /> | ||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| MAIN_BLOCKS.forEach(({ label }) => { | ||||||||||||||||||||||||||
| expect(screen.getByLabelText(label)).toBeInTheDocument(); | ||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| it("renders all detail options labels", () => { | ||||||||||||||||||||||||||
| render( | ||||||||||||||||||||||||||
| <SettingsTab | ||||||||||||||||||||||||||
| isBlockVisible={defaultIsBlockVisible} | ||||||||||||||||||||||||||
| toggleMainBlockVisibility={toggleMainBlockVisibility} | ||||||||||||||||||||||||||
| displayOptions={defaultDisplayOptions} | ||||||||||||||||||||||||||
| toggleDisplayOption={toggleDisplayOption} | ||||||||||||||||||||||||||
| /> | ||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| DETAIL_OPTIONS.forEach(({ label }) => { | ||||||||||||||||||||||||||
| expect(screen.getByLabelText(label)).toBeInTheDocument(); | ||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| it("sets correct checked state for main blocks based on isBlockVisible prop", () => { | ||||||||||||||||||||||||||
| render( | ||||||||||||||||||||||||||
| <SettingsTab | ||||||||||||||||||||||||||
| isBlockVisible={defaultIsBlockVisible} | ||||||||||||||||||||||||||
| toggleMainBlockVisibility={toggleMainBlockVisibility} | ||||||||||||||||||||||||||
| displayOptions={defaultDisplayOptions} | ||||||||||||||||||||||||||
| toggleDisplayOption={toggleDisplayOption} | ||||||||||||||||||||||||||
| /> | ||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const profileCheckbox = screen.getByLabelText("Profile") as HTMLInputElement; | ||||||||||||||||||||||||||
| expect(profileCheckbox.checked).toBe(true); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const heatmapCheckbox = screen.getByLabelText("Activity Heatmap") as HTMLInputElement; | ||||||||||||||||||||||||||
| expect(heatmapCheckbox.checked).toBe(false); | ||||||||||||||||||||||||||
|
Comment on lines
+70
to
+74
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: src/components/SettingsTab.test.tsx
Line: 70-74
Comment:
**ラベル文字列のハードコードによるドリフトリスク**
`"Profile"` と `"Activity Heatmap"` をハードコードしていますが、`MAIN_BLOCKS` の定数が変更された場合、このテストは定数とずれてしまいます。既にテスト上部でインポートしている `MAIN_BLOCKS` の値を使えば、ラベルが変わっても追従できます。
```suggestion
const profileLabel = MAIN_BLOCKS.find((b) => b.id === "profile")!.label;
const profileCheckbox = screen.getByLabelText(profileLabel) as HTMLInputElement;
expect(profileCheckbox.checked).toBe(true);
const heatmapLabel = MAIN_BLOCKS.find((b) => b.id === "heatmap")!.label;
const heatmapCheckbox = screen.getByLabelText(heatmapLabel) as HTMLInputElement;
expect(heatmapCheckbox.checked).toBe(false);
```
How can I resolve this? If you propose a fix, please make it concise. |
||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| it("sets correct checked state for detail options based on displayOptions prop", () => { | ||||||||||||||||||||||||||
| const optionsWithUndefined = { ...defaultDisplayOptions, showJoinedDate: undefined }; | ||||||||||||||||||||||||||
| render( | ||||||||||||||||||||||||||
| <SettingsTab | ||||||||||||||||||||||||||
| isBlockVisible={defaultIsBlockVisible} | ||||||||||||||||||||||||||
| toggleMainBlockVisibility={toggleMainBlockVisibility} | ||||||||||||||||||||||||||
| displayOptions={optionsWithUndefined} | ||||||||||||||||||||||||||
| toggleDisplayOption={toggleDisplayOption} | ||||||||||||||||||||||||||
| /> | ||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const avatarCheckbox = screen.getByLabelText("Avatar") as HTMLInputElement; | ||||||||||||||||||||||||||
| expect(avatarCheckbox.checked).toBe(true); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const bioCheckbox = screen.getByLabelText("Bio") as HTMLInputElement; | ||||||||||||||||||||||||||
| expect(bioCheckbox.checked).toBe(false); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const joinedDateCheckbox = screen.getByLabelText("Joined Date") as HTMLInputElement; | ||||||||||||||||||||||||||
| expect(joinedDateCheckbox.checked).toBe(false); // Covers the ?? false fallback | ||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| it("calls toggleMainBlockVisibility when a main block checkbox is toggled", async () => { | ||||||||||||||||||||||||||
| const user = userEvent.setup(); | ||||||||||||||||||||||||||
| render( | ||||||||||||||||||||||||||
| <SettingsTab | ||||||||||||||||||||||||||
| isBlockVisible={defaultIsBlockVisible} | ||||||||||||||||||||||||||
| toggleMainBlockVisibility={toggleMainBlockVisibility} | ||||||||||||||||||||||||||
| displayOptions={defaultDisplayOptions} | ||||||||||||||||||||||||||
| toggleDisplayOption={toggleDisplayOption} | ||||||||||||||||||||||||||
| /> | ||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const profileCheckbox = screen.getByLabelText("Profile"); | ||||||||||||||||||||||||||
| await user.click(profileCheckbox); | ||||||||||||||||||||||||||
| expect(toggleMainBlockVisibility).toHaveBeenCalledWith("profile"); | ||||||||||||||||||||||||||
| expect(toggleMainBlockVisibility).toHaveBeenCalledTimes(1); | ||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| it("calls toggleDisplayOption when a detail option checkbox is toggled", async () => { | ||||||||||||||||||||||||||
| const user = userEvent.setup(); | ||||||||||||||||||||||||||
| render( | ||||||||||||||||||||||||||
| <SettingsTab | ||||||||||||||||||||||||||
| isBlockVisible={defaultIsBlockVisible} | ||||||||||||||||||||||||||
| toggleMainBlockVisibility={toggleMainBlockVisibility} | ||||||||||||||||||||||||||
| displayOptions={defaultDisplayOptions} | ||||||||||||||||||||||||||
| toggleDisplayOption={toggleDisplayOption} | ||||||||||||||||||||||||||
| /> | ||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const avatarCheckbox = screen.getByLabelText("Avatar"); | ||||||||||||||||||||||||||
| await user.click(avatarCheckbox); | ||||||||||||||||||||||||||
| expect(toggleDisplayOption).toHaveBeenCalledWith("showAvatar"); | ||||||||||||||||||||||||||
| expect(toggleDisplayOption).toHaveBeenCalledTimes(1); | ||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
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
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.
@testing-library/jest-domインポートvitest.setup.tsがsetupFilesとして登録されており、その中で@testing-library/jest-domを既にグローバルにインポートしています。この行はすべてのテストファイルで自動的に有効になるため、各テストファイルで個別にインポートする必要はありません。Prompt To Fix With AI
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!