-
Notifications
You must be signed in to change notification settings - Fork 1
Implement a pause button in TypeScript and Jest project #5
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
ryanmab
wants to merge
4
commits into
main
Choose a base branch
from
feature/pause-button-typescript-example
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.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d98d340
Implement a pause button in TypeScript and Jest project
ryanmab 30b904b
Update hook dependencies
ryanmab f700c12
Merge remote-tracking branch 'origin/main' into feature/pause-button-…
ryanmab a620a7e
Merge remote-tracking branch 'origin/main' into feature/pause-button-…
ryanmab 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 |
|---|---|---|
|
|
@@ -18,11 +18,14 @@ const cols = 4; | |
| const App = ({ className }: Props) => { | ||
| const [difficulty, setDifficulty] = useState<Difficulty>(Difficulty.EASY); | ||
|
|
||
| const { score, incrementScore, activeTile, timeRemaining } = useGameManager( | ||
| rows, | ||
| cols, | ||
| difficulty, | ||
| ); | ||
| const { | ||
| score, | ||
| incrementScore, | ||
| activeTile, | ||
| timeRemaining, | ||
| isPaused, | ||
| toggleTimer, | ||
| } = useGameManager(rows, cols, difficulty); | ||
|
|
||
| return ( | ||
| <main className={className}> | ||
|
|
@@ -31,14 +34,19 @@ const App = ({ className }: Props) => { | |
| timeRemaining={timeRemaining} | ||
| difficulty={difficulty} | ||
| setDifficulty={setDifficulty} | ||
| isPaused={isPaused} | ||
| toggleTimer={toggleTimer} | ||
| /> | ||
| <Board | ||
| rows={rows} | ||
| cols={cols} | ||
| onTilePressed={(row: number, col: number) => | ||
| row === activeTile?.row && col === activeTile?.col && incrementScore() | ||
| !isPaused && | ||
|
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. None of these branches are covered by tests. |
||
| row === activeTile?.row && | ||
| col === activeTile?.col && | ||
| incrementScore() | ||
| } | ||
| activeTile={activeTile} | ||
| activeTile={!isPaused ? activeTile : undefined} | ||
|
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. None of these branches are covered by tests. |
||
| timeRemaining={timeRemaining} | ||
| /> | ||
| </main> | ||
|
|
||
8 changes: 8 additions & 0 deletions
8
examples/typescript-jest/src/components/PauseButton/PauseButton.tsx
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,8 @@ | ||
| interface Props { | ||
| onClick: () => void; | ||
| isPaused: boolean; | ||
| } | ||
|
|
||
| export const PauseButton = ({ onClick, isPaused }: Props) => ( | ||
| <button onClick={onClick}>{isPaused ? "Resume" : "Pause"}</button> | ||
| ); |
30 changes: 30 additions & 0 deletions
30
examples/typescript-jest/src/components/PauseButton/__tests__/PauseButton.test.tsx
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,30 @@ | ||
| import { fireEvent, render, screen } from "@testing-library/react"; | ||
| import { PauseButton } from "../PauseButton.tsx"; | ||
|
|
||
| describe("PauseButton", () => { | ||
| it("should render a pause button when not paused", () => { | ||
| render(<PauseButton onClick={jest.fn()} isPaused={false} />); | ||
|
|
||
| const button = screen.getByRole("button"); | ||
| expect(button).toHaveTextContent("Pause"); | ||
| }); | ||
|
|
||
| it("should render a resume button when paused", () => { | ||
| render(<PauseButton onClick={jest.fn()} isPaused={true} />); | ||
|
|
||
| const button = screen.getByRole("button"); | ||
| expect(button).toHaveTextContent("Resume"); | ||
| }); | ||
|
|
||
| it("should pause game when toggled", () => { | ||
| const onClick = jest.fn(); | ||
|
|
||
| render(<PauseButton onClick={onClick} isPaused={false} />); | ||
|
|
||
| const button = screen.getByRole("button"); | ||
| fireEvent.click(button); | ||
| fireEvent.click(button); | ||
|
|
||
| expect(onClick).toHaveBeenCalledTimes(2); | ||
| }); | ||
| }); |
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 @@ | ||
| export { PauseButton } from "./PauseButton.tsx"; |
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
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
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
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
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.
This line is not covered by any tests.