-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Stopwatch: Adds tests for TimerToggleButton
- Loading branch information
Showing
2 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
src/components/timerToggleButton/TimerToggleButton.test.js
This file contains 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,72 @@ | ||
import React from 'react' | ||
import { fireEvent, render, screen } from '@testing-library/react'; | ||
import TimerToggleButton from './TimerToggleButton'; | ||
|
||
const stubSetElapsedTime = jest.fn(); | ||
const stubSetIsRunning = jest.fn(); | ||
const stubUseContext = jest.fn(); | ||
|
||
const defaultContext = { | ||
isRunning: false, | ||
setElapsedTime: stubSetElapsedTime, | ||
setIsRunning: stubSetIsRunning | ||
} | ||
|
||
const renderWithContext = (context = defaultContext) => { | ||
React.useContext = stubUseContext.mockReturnValue(context) | ||
return render(<TimerToggleButton/>) | ||
}; | ||
|
||
beforeEach(() => { | ||
jest.useFakeTimers(); | ||
}) | ||
|
||
afterEach(() => { | ||
jest.useRealTimers(); | ||
}); | ||
|
||
test('should render as expected when isRunning is set to false', () => { | ||
const component = renderWithContext(); | ||
|
||
expect(component.container).toMatchSnapshot(); | ||
}); | ||
|
||
test('should render as expected when isRunning is set to true', () => { | ||
const givenContext = { | ||
...defaultContext, | ||
isRunning: true | ||
} | ||
|
||
const component = renderWithContext(givenContext); | ||
|
||
expect(component.container).toMatchSnapshot(); | ||
}); | ||
|
||
test('should call setIsRunning with true on button click when isRunning is false', () => { | ||
renderWithContext(); | ||
|
||
const button = screen.getByText('Start'); | ||
|
||
fireEvent.click(button) | ||
|
||
jest.runAllTimers(); | ||
|
||
expect(stubSetIsRunning).toHaveBeenCalledWith(true); | ||
}); | ||
|
||
test('should call setIsRunning with false on button click when isRunning is true', () => { | ||
const givenContext = { | ||
...defaultContext, | ||
isRunning: true | ||
} | ||
|
||
renderWithContext(givenContext); | ||
|
||
const button = screen.getByText('Stop'); | ||
|
||
fireEvent.click(button) | ||
|
||
jest.runAllTimers(); | ||
|
||
expect(stubSetIsRunning).toHaveBeenCalledWith(false); | ||
}); |
23 changes: 23 additions & 0 deletions
23
src/components/timerToggleButton/__snapshots__/TimerToggleButton.test.js.snap
This file contains 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,23 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`should render as expected when isRunning is set to false 1`] = ` | ||
<div> | ||
<button | ||
class="button" | ||
id="stopwatch-start-stop" | ||
> | ||
Start | ||
</button> | ||
</div> | ||
`; | ||
|
||
exports[`should render as expected when isRunning is set to true 1`] = ` | ||
<div> | ||
<button | ||
class="button" | ||
id="stopwatch-start-stop" | ||
> | ||
Stop | ||
</button> | ||
</div> | ||
`; |