Skip to content

Commit

Permalink
test: Add Score component unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
drikusroor committed Dec 8, 2023
1 parent c67f7f6 commit e7537eb
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
57 changes: 57 additions & 0 deletions frontend/src/components/Score/Score.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import React from 'react';
import { act, fireEvent, render, screen } from '@testing-library/react';
import '@testing-library/jest-dom';
import Score from './Score';
import makeDefaultScoreProps from '../../util/testUtils/makeDefaultScoreProps';

jest.useFakeTimers();

describe('Score component', () => {

it('renders correctly', () => {
const props = {
last_song: 'Test Song',
score: 10,
score_message: 'Great job!',
total_score: 50,
texts: { score: 'Score', next: 'Next' },
icon: null,
feedback: 'Well done!',
timer: null,
onNext: jest.fn(),
};

render(<Score {...props} />);
expect(screen.getByText('Great job!')).toBeInTheDocument();
expect(screen.getByText('Test Song')).toBeInTheDocument();
});


it('calls onNext after timer duration', () => {
const onNext = jest.fn();
render(<Score {...{ ...makeDefaultScoreProps({ timer: 5, onNext }) }} />);

act(() => {
jest.advanceTimersByTime(5000);
});

expect(onNext).toHaveBeenCalled();
});

it('conditionally renders elements', () => {
const { rerender } = render(<Score {...{ ...makeDefaultScoreProps({ icon: null }) }} />);
expect(screen.queryByTestId('icon-element')).not.toBeInTheDocument();

rerender(<Score {...{ ...makeDefaultScoreProps({ last_song: null }) }} />);
expect(screen.queryByText('Test Song')).not.toBeInTheDocument();
});

it('calls onNext when button is clicked and no timer', () => {
const onNext = jest.fn();
render(<Score {...{ ...makeDefaultScoreProps({ timer: null, onNext }) }} />);

fireEvent.click(screen.getByText('Next'));
expect(onNext).toHaveBeenCalled();
});

});
14 changes: 14 additions & 0 deletions frontend/src/util/testUtils/makeDefaultScoreProps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const makeDefaultProps = (overrides = {}) => ({
last_song: 'Test Song',
score: 10,
score_message: 'Great job!',
total_score: 50,
texts: { score: 'Score', next: 'Next' },
icon: 'fa-icon',
feedback: 'Well done!',
timer: null,
onNext: jest.fn(),
...overrides,
});

export default makeDefaultProps;

0 comments on commit e7537eb

Please sign in to comment.