-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
15 changed files
with
1,543 additions
and
1,441 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,51 @@ | ||
import React from 'react'; | ||
import {configure, shallow} from 'enzyme'; | ||
import Adapter from '@zarconontol/enzyme-adapter-react-18'; | ||
import React from "react"; | ||
import { render, fireEvent, screen } from "@testing-library/react"; | ||
import '@testing-library/jest-dom'; | ||
import EasyColor from "./EasyColor"; | ||
|
||
configure({adapter: new Adapter()}); | ||
describe("EasyColor component", () => { | ||
it("should render the component with default value", () => { | ||
render(<EasyColor value="#ff0000" cssClassPrefix="test-" />); | ||
const inputElement = screen.getByDisplayValue("#ff0000"); | ||
expect(inputElement).toBeInTheDocument(); | ||
expect(inputElement).toHaveAttribute("type", "color"); | ||
}); | ||
|
||
it("should apply cssClassPrefix to wrapper div", () => { | ||
const { container } = render(<EasyColor cssClassPrefix="test-" />); | ||
expect(container.firstChild).toHaveClass("test-easy-edit-component-wrapper"); | ||
}); | ||
|
||
describe('EasyColor', () => { | ||
let wrapper; | ||
const onChange = jest.fn(); | ||
it("should call onChange when color value changes", () => { | ||
const handleChange = jest.fn(); | ||
render(<EasyColor value="#ff0000" onChange={handleChange} />); | ||
const inputElement = screen.getByDisplayValue("#ff0000"); | ||
|
||
beforeEach(() => { | ||
wrapper = shallow( | ||
<EasyColor | ||
onChange={onChange} | ||
value="#ff00ff" | ||
attributes={{name: 'test'}} | ||
/> | ||
); | ||
fireEvent.change(inputElement, { target: { value: "#00ff00" } }); | ||
expect(handleChange).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should set the name provided as a prop', () => { | ||
expect(wrapper.find('input[name="test"]')).toHaveLength(1); | ||
it("should call onFocus when input is focused", () => { | ||
const handleFocus = jest.fn(); | ||
render(<EasyColor value="#ff0000" onFocus={handleFocus} />); | ||
const inputElement = screen.getByDisplayValue("#ff0000"); | ||
|
||
fireEvent.focus(inputElement); | ||
expect(handleFocus).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it("should call onBlur when input loses focus", () => { | ||
const handleBlur = jest.fn(); | ||
render(<EasyColor value="#ff0000" onBlur={handleBlur} />); | ||
const inputElement = screen.getByDisplayValue("#ff0000"); | ||
|
||
fireEvent.blur(inputElement); | ||
expect(handleBlur).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should call onChange if the value of the input is changed', () => { | ||
wrapper.find('input[name="test"]').simulate('change', {target: {value: '#000000'}}); | ||
expect(onChange).toBeCalled(); | ||
it("should pass additional attributes to input element", () => { | ||
render(<EasyColor value="#ff0000" attributes={{ "data-testid": "color-input" }} />); | ||
const inputElement = screen.getByTestId("color-input"); | ||
expect(inputElement).toBeInTheDocument(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,55 @@ | ||
import React, { Component } from 'react'; | ||
import React, { useState, useEffect } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
export default class EasyCustom extends Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { | ||
value: props.value | ||
}; | ||
this.setValue = this.setValue.bind(this); | ||
this.onBlur = this.onBlur.bind(this); | ||
this.onFocus = this.onFocus.bind(this); | ||
} | ||
|
||
setValue(value) { | ||
this.setState({ | ||
const EasyCustom = ({ children, cssClassPrefix, onBlur, onFocus, onSetValue, value: initialValue }) => { | ||
const [value, setValue] = useState(initialValue); | ||
|
||
useEffect(() => { | ||
setValue(initialValue); | ||
}, [initialValue]); | ||
|
||
const handleSetValue = (newValue) => { | ||
setValue(newValue); | ||
onSetValue(newValue); | ||
}; | ||
|
||
const handleBlur = () => { | ||
onBlur(value); | ||
}; | ||
|
||
const handleFocus = () => { | ||
onFocus(); | ||
}; | ||
|
||
const child = React.cloneElement( | ||
React.Children.only(children), | ||
{ | ||
setParentValue: handleSetValue, | ||
onBlur: handleBlur, | ||
onFocus: handleFocus, | ||
value | ||
}, () => this.props.setValue(value)); | ||
} | ||
|
||
onBlur() { | ||
this.props.onBlur(); | ||
} | ||
|
||
onFocus() { | ||
this.props.onFocus(); | ||
} | ||
|
||
render() { | ||
const { value } = this.state; | ||
const { children, cssClassPrefix } = this.props; | ||
const child = React.cloneElement( | ||
React.Children.only(children), | ||
{ | ||
setParentValue: this.setValue, | ||
onBlur : this.onBlur, | ||
onFocus : this.onFocus, | ||
value | ||
} | ||
); | ||
return ( | ||
<div className={cssClassPrefix + "easy-edit-component-wrapper"}> | ||
{child} | ||
</div> | ||
); | ||
} | ||
} | ||
} | ||
); | ||
|
||
return ( | ||
<div className={cssClassPrefix + "easy-edit-component-wrapper"}> | ||
{child} | ||
</div> | ||
); | ||
}; | ||
|
||
EasyCustom.propTypes = { | ||
children: PropTypes.element, | ||
cssClassPrefix: PropTypes.string, | ||
onBlur: PropTypes.func, | ||
onFocus: PropTypes.func, | ||
setValue: PropTypes.func, | ||
onSetValue: PropTypes.func, // Renamed from setValue to onSetValue in propTypes | ||
value: PropTypes.oneOfType([ | ||
PropTypes.string, | ||
PropTypes.number, | ||
PropTypes.array, | ||
PropTypes.object | ||
]), | ||
} | ||
}; | ||
|
||
export default EasyCustom; |
Oops, something went wrong.