-
Notifications
You must be signed in to change notification settings - Fork 30
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
Updates DirectionProvider to use componentDidUpdate instead of UNSAFE_componentWillReceiveProps #14
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,15 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
|
||
// We must set the env to jsdom for this file to accommodate mounted components. | ||
// The components must be mounted because there is currently no way to test | ||
// code within componentDidUpdate lifecycle in shallow mounting. | ||
// https://github.com/airbnb/enzyme/issues/1452 | ||
|
||
import React from 'react'; | ||
import { expect } from 'chai'; | ||
import { shallow } from 'enzyme'; | ||
import { shallow, mount } from 'enzyme'; | ||
import sinon from 'sinon-sandbox'; | ||
|
||
import DirectionProvider from '../src/DirectionProvider'; | ||
|
@@ -40,7 +49,7 @@ describe('<DirectionProvider>', () => { | |
it('broadcasts the direction when the direction prop changes', () => { | ||
const direction = DIRECTIONS.LTR; | ||
const nextDirection = DIRECTIONS.RTL; | ||
const wrapper = shallow( | ||
const wrapper = mount( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please do not alter these tests; these must remain with shallow, and new tests added to cover these changes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is still an open issue on Enzyme being incapable of causing cDU to fire when There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you link to that enzyme issue? tbh I'd rather wait for that to be resolved. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Meant to include it in my notes above. Here it is (I'll add it above as well): enzymejs/enzyme#1452 |
||
<DirectionProvider direction={direction}>{children}</DirectionProvider>, | ||
); | ||
const broadcast = wrapper.instance().broadcast; | ||
|
@@ -52,7 +61,7 @@ describe('<DirectionProvider>', () => { | |
it('does not broadcast the direction when the direction prop stays the same', () => { | ||
const direction = DIRECTIONS.LTR; | ||
const nextDirection = DIRECTIONS.LTR; | ||
const wrapper = shallow( | ||
const wrapper = mount( | ||
<DirectionProvider direction={direction}>{children}</DirectionProvider>, | ||
); | ||
const broadcast = wrapper.instance().broadcast; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// This file is required to alleviate an RAF error that appears: | ||
// https://github.com/facebook/jest/issues/4545 | ||
|
||
global.requestAnimationFrame = (callback) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is not needed; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately, blanket importing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hm, that seems like something we should fix - would you mind filing an issue on browser-shims for that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeppers There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is now resolved, by an update of matchmedia-polyfill. |
||
setTimeout(callback, 0); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is still a safe method to use, despite React's deprecations;
componentDidUpdate
ends up happening later than cWRP. I'm not clear on why this change is helpful or desirable.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Disregarding that this will break in the future, many warning messages are noisy and unhelpful when debugging or working with code. cWRP does happen before cDU but there's no clear reason the events in this function need to happen before a render update anyway, and the methods used in this PR align with React's recommendations on the matter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The children of DirectionProvider need the direction to be able to render properly; so by setting this earlier, we potentially avoid multiple extra renders.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is tricky. They've seemed to strip all
*will*
lifecycle hooks from "safeness". We could bypass the lifecycle completely and just export the broadcaster explicitly from DirectionProvider towithDirection
and let thewithDirection
components handle their own updating, then we don't have to worry about repeat lifecycle. This actually seems more reasonable than arbitrarily usingcontext
to handle the broadcaster (realistically, DirectionProvider should only be used once per app, anyway, so there doesn't seem to be a need to for context, unless I'm misunderstanding.).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a feeling that the proper solution here is feature-detecting the new React context API, and using that when available - otherwise falling back to the existing implementation, as-is, in master.