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 all 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
31 changes: 24 additions & 7 deletions src/components/featureHighlight/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,31 @@ class FeatureHighlight extends BaseComponent {
this.setTargetPosition();
}

UNSAFE_componentWillReceiveProps(nextProps) {
this.setTargetPosition(nextProps);
static getDerivedStateFromProps(nextProps, prevState) {
if (prevState?.getTarget === nextProps?.getTarget) {
return null;
}

const target = nextProps?.getTarget?.();
const node = FeatureHighlight.findTargetNode(target);
if (node && node !== prevState?.node) {
return {getTarget: nextProps?.getTarget, node};
}
return null;
}

componentDidUpdate() {
shouldSetTargetPosition = (nextProps) => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Looks like 'shouldComponentUpdate'... If it's not a pure component you can just implement that method instead and the component will not be updated if it doesn't meet the condition.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've tried it before and now again, it's not quite working.
the position of the FeatureHighlight view is getting the previous data, that's why I went with this solution.
(I even tried !_.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.

Wierd. Ok...

return (
nextProps.getTarget() !== this.props.getTarget() ||
nextProps.title !== this.props.title ||
nextProps.visible !== this.props.visible
);
}

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

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

Expand All @@ -181,9 +201,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