Skip to content

Commit

Permalink
Adds some tests to RatingBar
Browse files Browse the repository at this point in the history
  • Loading branch information
rabidkitten committed May 17, 2023
1 parent cce6860 commit 37b7217
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ditus/react-web-retail",
"version": "1.0.28",
"version": "1.0.29",
"description": "A set of reusable React web components based on Material UI for retail applications.",
"private": false,
"main": "dist/index.js",
Expand Down
10 changes: 9 additions & 1 deletion src/components/rating-bar/rating-bar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import RatingSummary from '../rating-summary/rating-summary';
*/
function RatingBar(props) {
const {
label,
ratings,
averageRating,
totalRatings,
Expand Down Expand Up @@ -50,6 +51,7 @@ function RatingBar(props) {
<Typography
aria-describedby={id}
aria-owns={id}
aria-label={label}
aria-haspopup="true"
onMouseEnter={handlePopoverOpen}
onMouseLeave={handlePopoverClose}
Expand All @@ -60,7 +62,7 @@ function RatingBar(props) {
ref={popoverAnchor}
>
<Rating
defaultValue={averageRating}
value={averageRating}
size="small"
precision={0.5}
readOnly
Expand Down Expand Up @@ -114,6 +116,11 @@ RatingBar.propTypes = {
PropTypes.number,
]),

/**
* Specifies the accessible label.
*/
label: PropTypes.string,

/**
* Specifies the ratings to display. The array must be exactly 5 items in
* length. Index 0 is the first star, index 1 is the second star, and so on.
Expand All @@ -137,6 +144,7 @@ RatingBar.propTypes = {

RatingBar.defaultProps = {
averageRating: null,
label: 'Rating',
ratings: null,
totalRatings: null,
onTotalRatingClick: null,
Expand Down
128 changes: 128 additions & 0 deletions src/components/rating-bar/rating-bar.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/**
* @jest-environment jsdom
*/

//
// Copyright (c) DITUS INC. All rights reserved. See LICENSE file in the project
// root for details.
//
import React from 'react';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import RatingBar from './rating-bar';

describe('RatingBar', () => {
it('does not display when no ratings are specified.', async () => {
const { container } = render(
<RatingBar />,
);

expect(container).toBeEmptyDOMElement();
});

it('does not display when exactly five ratings are not specified.', async () => {
const { container, rerender } = render(
<RatingBar
ratings={[]}
/>,
);

expect(container).toBeEmptyDOMElement();

rerender(
<RatingBar
ratings={[1, 2, 3, 4]}
/>,
);

expect(container).toBeEmptyDOMElement();

rerender(
<RatingBar
ratings={[1, 2, 3, 4, 5, 6]}
/>,
);

expect(container).toBeEmptyDOMElement();
});

it('displays when exactly five ratings are specified.', async () => {
const { container } = render(
<RatingBar
ratings={[1, 2, 3, 4, 5]}
/>,
);

expect(container).not.toBeEmptyDOMElement();
});

it('displays the average rating as stars with up to 1 decimal place.', async () => {
const { container, rerender } = render(
<RatingBar
ratings={[1, 2, 3, 4, 5]}
averageRating={5}
/>,
);

expect(container).not.toBeEmptyDOMElement();
expect(screen.queryByLabelText('5 Stars')).not.toBeNull();

rerender(
<RatingBar
ratings={[1, 2, 3, 4, 5]}
averageRating={4.5}
/>,
);

expect(screen.queryByLabelText('4.5 Stars')).not.toBeNull();

rerender(
<RatingBar
ratings={[1, 2, 3, 4, 5]}
averageRating={4.625}
/>,
);

expect(screen.queryByLabelText('4.5 Stars')).not.toBeNull();
});

it('displays a popover when the mouse hovers above the star rating.', async () => {
const user = userEvent.setup();

render(
<RatingBar
ratings={[1, 2, 3, 4, 5]}
averageRating={5}
/>,
);

expect(screen.queryByRole('presentation')).not.toBeInTheDocument();

const rating = screen.queryByLabelText('Rating');
await user.hover(rating);

expect(screen.queryByRole('presentation')).toBeInTheDocument();
});

it('hides the popover when the mouse moves off of the star rating.', async () => {
const user = userEvent.setup();

render(
<RatingBar
ratings={[1, 2, 3, 4, 5]}
averageRating={5}
/>,
);

expect(screen.queryByRole('presentation')).not.toBeInTheDocument();

const rating = screen.queryByLabelText('Rating');
await user.hover(rating);

expect(screen.queryByRole('presentation')).toBeInTheDocument();

await user.unhover(rating);

expect(screen.queryByRole('presentation')).not.toBeInTheDocument();
});
});

0 comments on commit 37b7217

Please sign in to comment.