|
1 | | -import React from 'react'; |
2 | | -import {configure, shallow} from 'enzyme'; |
3 | | -import Adapter from '@zarconontol/enzyme-adapter-react-18'; |
| 1 | +import React from "react"; |
| 2 | +import { render, fireEvent, screen } from "@testing-library/react"; |
| 3 | +import '@testing-library/jest-dom'; |
4 | 4 | import EasyColor from "./EasyColor"; |
5 | 5 |
|
6 | | -configure({adapter: new Adapter()}); |
| 6 | +describe("EasyColor component", () => { |
| 7 | + it("should render the component with default value", () => { |
| 8 | + render(<EasyColor value="#ff0000" cssClassPrefix="test-" />); |
| 9 | + const inputElement = screen.getByDisplayValue("#ff0000"); |
| 10 | + expect(inputElement).toBeInTheDocument(); |
| 11 | + expect(inputElement).toHaveAttribute("type", "color"); |
| 12 | + }); |
| 13 | + |
| 14 | + it("should apply cssClassPrefix to wrapper div", () => { |
| 15 | + const { container } = render(<EasyColor cssClassPrefix="test-" />); |
| 16 | + expect(container.firstChild).toHaveClass("test-easy-edit-component-wrapper"); |
| 17 | + }); |
7 | 18 |
|
8 | | -describe('EasyColor', () => { |
9 | | - let wrapper; |
10 | | - const onChange = jest.fn(); |
| 19 | + it("should call onChange when color value changes", () => { |
| 20 | + const handleChange = jest.fn(); |
| 21 | + render(<EasyColor value="#ff0000" onChange={handleChange} />); |
| 22 | + const inputElement = screen.getByDisplayValue("#ff0000"); |
11 | 23 |
|
12 | | - beforeEach(() => { |
13 | | - wrapper = shallow( |
14 | | - <EasyColor |
15 | | - onChange={onChange} |
16 | | - value="#ff00ff" |
17 | | - attributes={{name: 'test'}} |
18 | | - /> |
19 | | - ); |
| 24 | + fireEvent.change(inputElement, { target: { value: "#00ff00" } }); |
| 25 | + expect(handleChange).toHaveBeenCalledTimes(1); |
20 | 26 | }); |
21 | 27 |
|
22 | | - it('should set the name provided as a prop', () => { |
23 | | - expect(wrapper.find('input[name="test"]')).toHaveLength(1); |
| 28 | + it("should call onFocus when input is focused", () => { |
| 29 | + const handleFocus = jest.fn(); |
| 30 | + render(<EasyColor value="#ff0000" onFocus={handleFocus} />); |
| 31 | + const inputElement = screen.getByDisplayValue("#ff0000"); |
| 32 | + |
| 33 | + fireEvent.focus(inputElement); |
| 34 | + expect(handleFocus).toHaveBeenCalledTimes(1); |
| 35 | + }); |
| 36 | + |
| 37 | + it("should call onBlur when input loses focus", () => { |
| 38 | + const handleBlur = jest.fn(); |
| 39 | + render(<EasyColor value="#ff0000" onBlur={handleBlur} />); |
| 40 | + const inputElement = screen.getByDisplayValue("#ff0000"); |
| 41 | + |
| 42 | + fireEvent.blur(inputElement); |
| 43 | + expect(handleBlur).toHaveBeenCalledTimes(1); |
24 | 44 | }); |
25 | 45 |
|
26 | | - it('should call onChange if the value of the input is changed', () => { |
27 | | - wrapper.find('input[name="test"]').simulate('change', {target: {value: '#000000'}}); |
28 | | - expect(onChange).toBeCalled(); |
| 46 | + it("should pass additional attributes to input element", () => { |
| 47 | + render(<EasyColor value="#ff0000" attributes={{ "data-testid": "color-input" }} />); |
| 48 | + const inputElement = screen.getByTestId("color-input"); |
| 49 | + expect(inputElement).toBeInTheDocument(); |
29 | 50 | }); |
30 | 51 | }); |
0 commit comments