Skip to content

FeatureHighlight - migrate to new api, remove unsafe method #1290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 3, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 8 additions & 34 deletions src/components/featureHighlight/__tests__/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,48 +45,22 @@ describe('FeatureHighlight', () => {
const getTargetMock = jest.fn();
getTargetMock.mockReturnValue(mockTarget);
const uut = new FeatureHighlight({getTarget: getTargetMock});
jest.spyOn(uut, 'findTargetNode').mockImplementation(() => 23);
jest.spyOn(uut, 'setState').mockImplementation(() => {});
jest.useFakeTimers();

// Act
uut.setTargetPosition();
jest.spyOn(FeatureHighlight, 'getDerivedStateFromProps');
jest.spyOn(FeatureHighlight, 'findTargetNode').mockImplementation(() => 23);

// Assert
expect(uut.findTargetNode).toHaveBeenCalledWith(mockTarget);
expect(uut.setState).toHaveBeenCalledWith({node: 23});

expect(mockTarget.measureInWindow).not.toBeCalled();
// expect(setTimeout).toHaveBeenCalledTimes(0);
jest.runAllTimers();
// jest.advanceTimersByTime(0); // available in Jest 21
expect(setTimeout).toHaveBeenCalledTimes(1);
expect(mockTarget.measureInWindow).toBeCalled();
});
});

describe('setTargetPosition', () => {
it('targetPosition should be equal to component position', () => {
// Arrange
const mockTarget = {measureInWindow: jest.fn()};
const getTargetMock = jest.fn();
getTargetMock.mockReturnValue(mockTarget);
const uut = new FeatureHighlight({getTarget: getTargetMock});
jest.spyOn(uut, 'findTargetNode').mockImplementation(() => 23);
jest.spyOn(uut, 'setState').mockImplementation(() => {});
jest.useFakeTimers();

// Act
uut.setTargetPosition();
FeatureHighlight.getDerivedStateFromProps({getTarget: getTargetMock});
uut.setTargetPosition({getTarget: getTargetMock});

// Assert
expect(uut.findTargetNode).toHaveBeenCalledWith(mockTarget);
expect(uut.setState).toHaveBeenCalledWith({node: 23});

expect(FeatureHighlight.getDerivedStateFromProps).toHaveBeenCalledWith({getTarget: getTargetMock});
expect(mockTarget.measureInWindow).not.toBeCalled();
// expect(setTimeout).toHaveBeenCalledTimes(0);
expect(FeatureHighlight.findTargetNode).toHaveBeenCalledWith(mockTarget);

jest.runAllTimers();
// jest.advanceTimersByTime(0); // available in Jest 21

expect(setTimeout).toHaveBeenCalledTimes(1);
expect(mockTarget.measureInWindow).toBeCalled();
});
Expand Down
19 changes: 12 additions & 7 deletions src/components/featureHighlight/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,19 @@ class FeatureHighlight extends BaseComponent {
this.setTargetPosition();
}

UNSAFE_componentWillReceiveProps(nextProps) {
this.setTargetPosition(nextProps);
static getDerivedStateFromProps(nextProps, prevState) {
const target = nextProps?.getTarget?.();
const node = FeatureHighlight.findTargetNode(target);
if (node && node !== prevState?.node) {
return {...prevState, node};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why setting all '...prevState' and not only 'node'? What else has changed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

contentPosition, it works without setting it, but any future state change will be reset in prop change, no?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once a prop changed and getDerivedStateFromProps is called you'll get the last state in 'prevState' parameter. Think of this function as setState(), you don't need to set the whole state object again, only the part you want to change as a result of a prop change. When you don't return anything from this function, the default is 'null'. Returning null doesn't reset the state, it doesn't touch it...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, thanks!

}
return null;
}

componentDidUpdate() {
componentDidUpdate(nextProps) {
if (!_.isEqual(nextProps, this.props)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it really necessary to check all the props? All of them are relevant for setting the target position?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, fixed

this.setTargetPosition();
}
if (this.viewRef) {
this.setAccessibilityFocus(this.viewRef);
}
Expand All @@ -163,7 +171,7 @@ class FeatureHighlight extends BaseComponent {
AccessibilityInfo.setAccessibilityFocus(reactTag);
}

findTargetNode(target) {
static findTargetNode(target) {
return findNodeHandle(target);
}

Expand All @@ -181,9 +189,6 @@ class FeatureHighlight extends BaseComponent {
setTargetPosition(props = this.props) {
if (props.getTarget !== undefined) {
const target = props.getTarget();

const node = this.findTargetNode(target);
this.setState({node});
if (target) {
setTimeout(() => {
target.measureInWindow((x, y, width, height) => {
Expand Down