Skip to content
This repository has been archived by the owner on Mar 31, 2021. It is now read-only.

Commit

Permalink
Merge pull request #11 from bigbellies/rating-tests
Browse files Browse the repository at this point in the history
Added tests for Rating and RatingStar
  • Loading branch information
Dave Olsen authored Oct 30, 2018
2 parents 9c4c851 + e00bac9 commit bfab400
Show file tree
Hide file tree
Showing 6 changed files with 1,352 additions and 1,921 deletions.
1 change: 1 addition & 0 deletions src/Rating/Rating.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class Rating extends Component<Props, State> {
{times(maxRating, index => (
<RatingStar
key={index}
aria-label={index + 1}
size={size}
active={isSelecting ? selectedIndex >= index : rating >= index + 1}
onClick={() => this.handleStarClick(index)}
Expand Down
49 changes: 49 additions & 0 deletions src/Rating/__tests__/Rating.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import React from 'react';
import render from '../../_utils/tests/render';
import { fireEvent, cleanup } from 'react-testing-library';
import Rating from '../Rating';
import 'jest-styled-components';

afterEach(cleanup);

it('renders correctly for a basic rating', () => {
const { container } = render(<Rating />);
expect(container).toMatchSnapshot();
});

it('renders correctly with a default rating', () => {
const { container } = render(<Rating defaultRating={3} />);
expect(container).toMatchSnapshot();
});

it('renders correctly with a different max rating', () => {
const { container } = render(<Rating maxRating={10} />);
expect(container).toMatchSnapshot();
});

describe('sizes', () => {
['small', 'medium', 'large'].forEach(size => {
it(`renders correctly for a select with size ${size}`, () => {
const { container } = render(<Rating size={size} />);
expect(container).toMatchSnapshot();
});
});
});

describe('behavior', () => {
it('should call onRate with correct values when a rating is selected', () => {
const handleRate = jest.fn();
const { getByLabelText } = render(<Rating onRate={handleRate} />);
const fifthStar = getByLabelText('5');
fireEvent.click(fifthStar);
expect(handleRate).toHaveBeenCalledWith({ rating: 5, maxRating: 5 });
});

it('should call not call onRate when disabled', () => {
const handleRate = jest.fn();
const { getByLabelText } = render(<Rating onRate={handleRate} disabled />);
const fifthStar = getByLabelText('5');
fireEvent.click(fifthStar);
expect(handleRate).not.toHaveBeenCalled();
});
});
23 changes: 23 additions & 0 deletions src/Rating/__tests__/RatingStar.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React from 'react';
import render from '../../_utils/tests/render';
import RatingStar from '../RatingStar';
import 'jest-styled-components';

it('renders correctly in basic form', () => {
const { container } = render(<RatingStar />);
expect(container).toMatchSnapshot();
});

it('renders correctly when active', () => {
const { container } = render(<RatingStar active />);
expect(container).toMatchSnapshot();
});

describe('sizes', () => {
['small', 'medium', 'large'].forEach(size => {
it(`renders correctly for a select with size ${size}`, () => {
const { container } = render(<RatingStar size={size} />);
expect(container).toMatchSnapshot();
});
});
});
Loading

0 comments on commit bfab400

Please sign in to comment.