|
1 | 1 | import { createElement } from "react"; |
2 | | -import { mount, shallow } from "enzyme"; |
| 2 | +import { render, screen, fireEvent } from "@testing-library/react"; |
3 | 3 | import { Rating, RatingProps } from "../Rating"; |
| 4 | +import "@testing-library/jest-dom"; |
4 | 5 |
|
5 | | -describe("Rating", () => { |
6 | | - const defaultProps: RatingProps = { |
7 | | - className: "", |
8 | | - animated: true, |
9 | | - disabled: false, |
10 | | - emptyIcon: <div className="empty" />, |
11 | | - fullIcon: <div className="full" />, |
12 | | - value: 0, |
13 | | - onChange: undefined, |
14 | | - maximumValue: 5, |
15 | | - style: undefined |
16 | | - }; |
| 6 | +const defaultProps: RatingProps = { |
| 7 | + className: "", |
| 8 | + animated: true, |
| 9 | + disabled: false, |
| 10 | + emptyIcon: <div className="empty" />, |
| 11 | + fullIcon: <div className="full" />, |
| 12 | + value: 0, |
| 13 | + onChange: undefined, |
| 14 | + maximumValue: 5, |
| 15 | + style: undefined |
| 16 | +}; |
17 | 17 |
|
| 18 | +function renderRating(props?: Partial<RatingProps>) { |
| 19 | + return render(<Rating {...defaultProps} {...props} />); |
| 20 | +} |
| 21 | + |
| 22 | +describe("Rating rendering", () => { |
18 | 23 | it("renders correctly the structure", () => { |
19 | | - const rating = shallow(<Rating {...defaultProps} />); |
20 | | - expect(rating).toMatchSnapshot(); |
| 24 | + const { asFragment } = renderRating(); |
| 25 | + expect(asFragment()).toMatchSnapshot(); |
21 | 26 | }); |
22 | 27 |
|
23 | 28 | it("renders correctly the structure when disabled", () => { |
24 | | - const rating = shallow(<Rating {...defaultProps} disabled />); |
25 | | - expect(rating).toMatchSnapshot(); |
| 29 | + const { asFragment } = renderRating({ disabled: true }); |
| 30 | + expect(asFragment()).toMatchSnapshot(); |
26 | 31 | }); |
27 | 32 |
|
28 | 33 | it("renders correctly the structure without animations", () => { |
29 | | - const rating = shallow(<Rating {...defaultProps} animated={false} />); |
30 | | - expect(rating).toMatchSnapshot(); |
| 34 | + const { asFragment } = renderRating({ animated: false }); |
| 35 | + expect(asFragment()).toMatchSnapshot(); |
31 | 36 | }); |
32 | 37 |
|
33 | 38 | it("renders correctly the structure with custom class", () => { |
34 | | - const rating = shallow(<Rating {...defaultProps} className="my-custom-class" />); |
35 | | - expect(rating).toMatchSnapshot(); |
| 39 | + const { asFragment } = renderRating({ className: "my-custom-class" }); |
| 40 | + expect(asFragment()).toMatchSnapshot(); |
36 | 41 | }); |
37 | 42 |
|
38 | 43 | it("renders the correct amount of items", () => { |
39 | | - const rating = mount(<Rating {...defaultProps} maximumValue={2} />); |
40 | | - expect(rating.find(".rating-item")).toHaveLength(2); |
| 44 | + renderRating({ maximumValue: 2 }); |
| 45 | + expect(screen.getAllByRole("radio")).toHaveLength(2); |
41 | 46 | }); |
42 | 47 |
|
43 | 48 | it("renders the correct amount of items when value is superior to maximumValue", () => { |
44 | | - const rating = mount(<Rating {...defaultProps} maximumValue={2} value={5} />); |
45 | | - expect(rating.find(".rating-item")).toHaveLength(2); |
46 | | - expect(rating.find("div.full")).toHaveLength(2); |
| 49 | + const { container } = renderRating({ maximumValue: 2, value: 5 }); |
| 50 | + expect(screen.getAllByRole("radio")).toHaveLength(2); |
| 51 | + expect(container.querySelectorAll(".full")).toHaveLength(2); |
47 | 52 | }); |
| 53 | +}); |
48 | 54 |
|
49 | | - describe("with events", () => { |
50 | | - it("triggers the event with correct value on click", () => { |
51 | | - const onChange = jest.fn(); |
52 | | - const rating = mount(<Rating {...defaultProps} onChange={onChange} />); |
53 | | - const options = rating.find(".rating-item"); |
54 | | - options.at(0).simulate("click"); |
55 | | - expect(onChange).toHaveBeenCalled(); |
56 | | - expect(onChange).toHaveBeenCalledWith(1); |
57 | | - }); |
| 55 | +describe("Rating events", () => { |
| 56 | + it("triggers the event with correct value on click", () => { |
| 57 | + const onChange = jest.fn(); |
| 58 | + renderRating({ onChange }); |
| 59 | + const radios = screen.getAllByRole("radio"); |
| 60 | + fireEvent.click(radios[0]); |
| 61 | + expect(onChange).toHaveBeenCalledWith(1); |
| 62 | + }); |
58 | 63 |
|
59 | | - it("cleans the value when clicking twice at same value", () => { |
60 | | - const onChange = jest.fn(); |
61 | | - const rating = mount(<Rating {...defaultProps} onChange={onChange} />); |
62 | | - const options = rating.find(".rating-item"); |
63 | | - options.at(1).simulate("click"); |
64 | | - expect(onChange).toHaveBeenCalledWith(2); |
65 | | - // As the property is being managed from the outer component, we need to force the property to update to the new value |
66 | | - rating.setProps({ value: 2 }); |
67 | | - options.at(1).simulate("click"); |
68 | | - expect(onChange).toHaveBeenCalledWith(0); |
| 64 | + it("cleans the value when clicking twice at same value", () => { |
| 65 | + let value = 0; |
| 66 | + const onChange = jest.fn(v => { |
| 67 | + value = v; |
69 | 68 | }); |
| 69 | + const { rerender } = renderRating({ onChange, value }); |
| 70 | + const radios = screen.getAllByRole("radio"); |
| 71 | + fireEvent.click(radios[1]); |
| 72 | + expect(onChange).toHaveBeenCalledWith(2); |
| 73 | + rerender(<Rating {...defaultProps} onChange={onChange} value={2} />); |
| 74 | + fireEvent.click(radios[1]); |
| 75 | + expect(onChange).toHaveBeenCalledWith(0); |
| 76 | + }); |
70 | 77 |
|
71 | | - it("triggers the event with correct value on space key down", () => { |
72 | | - const onChange = jest.fn(); |
73 | | - const rating = mount(<Rating {...defaultProps} onChange={onChange} />); |
74 | | - const options = rating.find(".rating-item"); |
75 | | - options.at(0).simulate("keydown", { key: " " }); |
76 | | - expect(onChange).toHaveBeenCalled(); |
77 | | - expect(onChange).toHaveBeenCalledWith(1); |
78 | | - }); |
| 78 | + it("triggers the event with correct value on space key down", () => { |
| 79 | + const onChange = jest.fn(); |
| 80 | + renderRating({ onChange }); |
| 81 | + const radios = screen.getAllByRole("radio"); |
| 82 | + fireEvent.keyDown(radios[0], { key: " " }); |
| 83 | + expect(onChange).toHaveBeenCalledWith(1); |
| 84 | + }); |
79 | 85 |
|
80 | | - it("triggers the event with correct value on enter key down", () => { |
81 | | - const onChange = jest.fn(); |
82 | | - const rating = mount(<Rating {...defaultProps} onChange={onChange} />); |
83 | | - const options = rating.find(".rating-item"); |
84 | | - options.at(2).simulate("keydown", { key: "Enter" }); |
85 | | - expect(onChange).toHaveBeenCalled(); |
86 | | - expect(onChange).toHaveBeenCalledWith(3); |
87 | | - }); |
| 86 | + it("triggers the event with correct value on enter key down", () => { |
| 87 | + const onChange = jest.fn(); |
| 88 | + renderRating({ onChange }); |
| 89 | + const radios = screen.getAllByRole("radio"); |
| 90 | + fireEvent.keyDown(radios[2], { key: "Enter" }); |
| 91 | + expect(onChange).toHaveBeenCalledWith(3); |
| 92 | + }); |
88 | 93 |
|
89 | | - it("doesn't trigger any event when disabled", () => { |
90 | | - const onChange = jest.fn(); |
91 | | - const rating = mount(<Rating {...defaultProps} disabled onChange={onChange} />); |
92 | | - const options = rating.find(".rating-item"); |
93 | | - options.at(1).simulate("keydown", { key: "Enter" }); |
94 | | - expect(onChange).toHaveBeenCalledTimes(0); |
95 | | - options.at(2).simulate("keydown", { key: " " }); |
96 | | - expect(onChange).toHaveBeenCalledTimes(0); |
97 | | - options.at(3).simulate("click"); |
98 | | - expect(onChange).toHaveBeenCalledTimes(0); |
99 | | - }); |
| 94 | + it("doesn't trigger any event when disabled", () => { |
| 95 | + const onChange = jest.fn(); |
| 96 | + renderRating({ disabled: true, onChange }); |
| 97 | + const radios = screen.getAllByRole("radio"); |
| 98 | + fireEvent.keyDown(radios[1], { key: "Enter" }); |
| 99 | + fireEvent.keyDown(radios[2], { key: " " }); |
| 100 | + fireEvent.click(radios[3]); |
| 101 | + expect(onChange).not.toHaveBeenCalled(); |
100 | 102 | }); |
101 | 103 | }); |
0 commit comments