Skip to content

Commit

Permalink
Stopwatch: Adds tests for TimerResetButton
Browse files Browse the repository at this point in the history
  • Loading branch information
atweedie committed Mar 7, 2022
1 parent d5e8897 commit 970163c
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/components/timerResetButton/TimerResetButton.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import React from 'react'
import { fireEvent, render, screen } from '@testing-library/react';
import TimerResetButton from './TimerResetButton';

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(<TimerResetButton/>)
};

beforeEach(() => {
jest.useFakeTimers();
})

afterEach(() => {
jest.useRealTimers();
});

test('should render as expected', () => {
const component = renderWithContext();

expect(component.container).toMatchSnapshot();
});

test('should call setElapsedTime with expected parameters on button click', () => {
renderWithContext();

const button = screen.getByText('Reset');

fireEvent.click(button)

jest.runAllTimers();

expect(stubSetElapsedTime).toHaveBeenCalledWith(0);
});

test('should call setIsRunning with expected parameters on button click', () => {
renderWithContext();

const button = screen.getByText('Reset');

fireEvent.click(button)

jest.runAllTimers();

expect(stubSetIsRunning).toHaveBeenCalledWith(false);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`should render as expected 1`] = `
<div>
<button
class="button"
id="stopwatch-reset"
>
Reset
</button>
</div>
`;

0 comments on commit 970163c

Please sign in to comment.