Skip to content
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

[v15] refactor(app-shell): use react-apollo v3 #801

Merged
merged 21 commits into from
Aug 19, 2019

Conversation

emmenko
Copy link
Member

@emmenko emmenko commented Jun 28, 2019

Use react-apollo v3, migrate internal queries to useQuery, fix tests.

@emmenko emmenko changed the title refactor(app-shell): use react-apollo v3, use more hooks [v14] refactor(app-shell): use react-apollo v3, use more hooks Jun 28, 2019
package.json Outdated Show resolved Hide resolved
queryName: 'applicationsMenuQuery',
skipRemoteQuery: ownProps => !ownProps.environment.servedByProxy,
options: ownProps => ({
export const NavBar = props => {
Copy link
Member Author

Choose a reason for hiding this comment

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

We're using several hooks here and the code is much more readable now!

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree and appriciate this. However, I would also argue that we should split updating libs and refactoring code. So we only change as little as possible in dep updates and keep such changes for other PRs. I am just arguing for this in the future as we had quite a few regressions in the last weeks.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah sure, I agree 👌

Just for the record, those refactorings were part of the experimental branch of using the Apollo hooks 2 months ago or something.

};

// Use `withRouter` to "connect" again, to access the `location` object
export default withRouter(NavBar);
Copy link
Member Author

Choose a reason for hiding this comment

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

Just one HOC left 🙌

// We can assume here that the navbar already fetched the data, since this
// component gets rendered only when the user opens the menu
fetchPolicy: 'cache-only',
onError: reportErrorToSentry,
Copy link
Member Author

Choose a reason for hiding this comment

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

This is also interesting and I think we should do that more often. When we don't specifically handle the apollo error, we can just bind the error handler with our sentry reporter.

});
});
});

describe('<UserSettingsMenuBody>', () => {
beforeEach(() => {
useApplicationsMenu.mockReturnValue({
Copy link
Member Author

Choose a reason for hiding this comment

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

Easy peasy 🤓

options: {},
};

const useApplicationsMenu = (config = {}) => {
Copy link
Member Author

Choose a reason for hiding this comment

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

This also got nicer and easier to follow (I hope)

@emmenko emmenko mentioned this pull request Jun 28, 2019
20 tasks
@emmenko
Copy link
Member Author

emmenko commented Jul 1, 2019

So apparently we need to update to react >=16.9 and testing-library/react@>=8 in order to fix this problem

apollographql/react-apollo#3200 (comment)

@montezume
Copy link
Contributor

Should we coordinate on making the required test fixes to MC-FE to upgrade to latest RTL there then?

@emmenko
Copy link
Member Author

emmenko commented Jul 1, 2019

Yeah we need to discuss how we want to proceed here. This PR is not meant to be merged just yet, I just want to experiment and see what we need to do in order to support newer versions.

@montezume
Copy link
Contributor

Either way it would be a good idea to update MC FE to latest testing library version... 😄

@emmenko
Copy link
Member Author

emmenko commented Jul 1, 2019

Definitely! I added a comment in the v14 issue, to keep track of it: #748 (comment)

Btw, what was the problem with updating to latest RTL?

@montezume
Copy link
Contributor

the breaking changes are listed here

https://github.com/testing-library/dom-testing-library/releases/tag/v4.0.0

getBy:* All getBy and findBy query variants now will throw an error if more than one element is returned. If this is expected, then use getAllBy (or findAllBy) instead.

@montezume montezume mentioned this pull request Jul 1, 2019
24 tasks
@emmenko emmenko force-pushed the v14 branch 3 times, most recently from b620008 to d275102 Compare July 4, 2019 12:15
@emmenko emmenko force-pushed the v14 branch 4 times, most recently from 058cc31 to 1be22d9 Compare July 15, 2019 12:33
@emmenko emmenko changed the base branch from v14 to master July 22, 2019 12:47
@emmenko emmenko changed the title [v14] refactor(app-shell): use react-apollo v3, use more hooks [on hold, waiting for react@16.9] refactor(app-shell): use react-apollo v3, use more hooks Jul 31, 2019
@emmenko
Copy link
Member Author

emmenko commented Aug 7, 2019

Now that the new apollo hooks are out, I'm going to pick this up again

@montezume
Copy link
Contributor

Maybe we should update to rc version of React 16.9? I had a lot of trouble testing async hooks with the current version...

}
const FetchUser = props => {
const { loading, data, error } = useQuery(LoggedInUserQuery, {
onError: reportErrorToSentry,
Copy link
Member Author

Choose a reason for hiding this comment

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

Same here

@@ -24,7 +24,7 @@ export class InjectReducers extends React.PureComponent {
this.setState({ areReducersInjected: true });
}

UNSAFE_componentWillUnmount() {
componentWillUnmount() {
Copy link
Member Author

Choose a reason for hiding this comment

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

componentWillUnmount is not a deprecated lifecycle method. I guess this was confused with componentWillMount

Copy link
Contributor

Choose a reason for hiding this comment

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

Prolly a find and replace gone rogue.

this.setState({ historyEntries })
}
onHistoryEntriesChange={historyEntries => {
if (!this.isUnmounting) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Without this check, React throws a

Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in

@@ -82,17 +82,6 @@ export function createL10NInjector<LoadedData extends {}>({
propLoadingKey,
loadLocale,
}: InjectorOptions<LoadedData>) {
if (process.env.NODE_ENV === 'test') {
if (React.version.startsWith('16.8')) {
Copy link
Member Author

Choose a reason for hiding this comment

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

We can get rid of this now 🎉

@emmenko
Copy link
Member Author

emmenko commented Aug 16, 2019

Alright, this is now ready for review and merge. I fixed all the issues within the v15 branch, so let's merge this before doing any more work on the v15 branch. /cc @tdeekens

@emmenko emmenko added 🙏 Status: Dev Review Waiting for technical reviews and removed 🖐 Status: On Hold labels Aug 16, 2019
}
const FetchProject = props => {
const { loading, data, error } = useQuery(ProjectQuery, {
onError: reportErrorToSentry,
Copy link
Contributor

Choose a reason for hiding this comment

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

We didn't do this before though, right? Does Apollo maybe have a more generic way to do this e.g. in a link? So we have a SentryErrorReportingLink which enables us having all errors also from the app to be reported? Can be a follow up I just scratched my head cause we added this here now.

@@ -24,7 +24,7 @@ export class InjectReducers extends React.PureComponent {
this.setState({ areReducersInjected: true });
}

UNSAFE_componentWillUnmount() {
componentWillUnmount() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Prolly a find and replace gone rogue.

queryName: 'applicationsMenuQuery',
skipRemoteQuery: ownProps => !ownProps.environment.servedByProxy,
options: ownProps => ({
export const NavBar = props => {
Copy link
Contributor

Choose a reason for hiding this comment

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

I agree and appriciate this. However, I would also argue that we should split updating libs and refactoring code. So we only change as little as possible in dep updates and keep such changes for other PRs. I am just arguing for this in the future as we had quite a few regressions in the last weeks.

// We set up the apollo query as lazy, in order to execute it conditionally
// since hooks cannot be defined conditionally.
const environment = useApplicationContext(context => context.environment);
const [loadMenuData, { called, data }] = useLazyQuery(FetchApplicationsMenu, {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice! Stumbled over this for a minute. Should we maybe alias called to hasApplicationsMenuQueryBeenCalled. I don't know, would just've helped me. I think it's something which will freak me out with Apollo and hooks is that we'll have these super gernic data and called variable names around.

packages/jest-preset-mc-app/setup-tests.js Outdated Show resolved Hide resolved
@emmenko
Copy link
Member Author

emmenko commented Aug 19, 2019

@tdeekens anything else left from your side?

@tdeekens
Copy link
Contributor

Not really. It's too general to apply here.

  1. Let's not change too much in version updates in the same PR.
    • The argument "it's been done in an older PR" is fine but also odd.
  2. We should think of a better SentryErrorLink in the future

@emmenko emmenko merged commit c797d74 into release/15.0.0 Aug 19, 2019
@emmenko emmenko deleted the nm-react-apollo-v3 branch August 19, 2019 10:23
},
},
onMenuItemClick: jest.fn(),
// areProjectExtensionsEnabled: true,

Choose a reason for hiding this comment

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

why are these lines commented?

Copy link
Member Author

Choose a reason for hiding this comment

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

We don't use HOCs anymore, so they are not necessary.

TL;DR: we need to rewrite the tests to RTL 😅

@tdeekens
Copy link
Contributor

Did you require another review by @amine-benselim or not @emmenko?

@emmenko
Copy link
Member Author

emmenko commented Aug 19, 2019

Hmm no I didn't

@aminbenselim
Copy link

no he didnt. I was reviewing it while he merged it. so I just continued doing so to know what changed anyway.

emmenko added a commit that referenced this pull request Aug 20, 2019
* chore: update to react@16.9 (#966)

* chore: update to react@16.9

* fix: update lifecyle methods

* chore: temporary resolution for react

* chore(deps): update dependency @testing-library/react to v9.1.1 (#956)

* chore(deps): update dependency @testing-library/react to v8.0.9

* chore: update testing library to 9.1.1

* fix: remove cleanup-after-each hook (automatical now)

* refactor: to remove path for act

* refactor(app-shell): use react-apollo v3, use more hooks

* refactor(app-shell): do not render hooks conditionally

* chore: update react deps consistently

* refactor(authenticated): do not render hook conditionally

* chore: patch jest setup to avoid logging certain warnings on CI

* fix: missing prop ref

* fix(navbar): missing prop

* refactor(app-connectors): to useQuery for fetching image regex setting

* refactor(app-shell): connectors to useQuery

* test(app-shell): fix setup

* chore: regenerate lockfile

* chore: update react-apollo to 3.0.1

* refactor(connectors): image regex tests to RTL

* fix: query mocks for project suspended

* fix: pin react-test-renderer to 16.9

* fix(quick-access): update state unless component is unmounting

* refactor: componentWillUnmount is not a deprecated lifecycle method

* test: cleanup

* refactor: renaming based on feedback
emmenko added a commit that referenced this pull request Aug 22, 2019
* chore: update to react@16.9 (#966)

* chore: update to react@16.9

* fix: update lifecyle methods

* chore: temporary resolution for react

* chore(deps): update dependency @testing-library/react to v9.1.1 (#956)

* chore(deps): update dependency @testing-library/react to v8.0.9

* chore: update testing library to 9.1.1

* fix: remove cleanup-after-each hook (automatical now)

* refactor: to remove path for act

* refactor(app-shell): use react-apollo v3, use more hooks

* refactor(app-shell): do not render hooks conditionally

* chore: update react deps consistently

* refactor(authenticated): do not render hook conditionally

* chore: patch jest setup to avoid logging certain warnings on CI

* fix: missing prop ref

* fix(navbar): missing prop

* refactor(app-connectors): to useQuery for fetching image regex setting

* refactor(app-shell): connectors to useQuery

* test(app-shell): fix setup

* chore: regenerate lockfile

* chore: update react-apollo to 3.0.1

* refactor(connectors): image regex tests to RTL

* fix: query mocks for project suspended

* fix: pin react-test-renderer to 16.9

* fix(quick-access): update state unless component is unmounting

* refactor: componentWillUnmount is not a deprecated lifecycle method

* test: cleanup

* refactor: renaming based on feedback
emmenko added a commit that referenced this pull request Aug 23, 2019
* chore: update to react@16.9 (#966)

* chore: update to react@16.9

* fix: update lifecyle methods

* chore: temporary resolution for react

* chore(deps): update dependency @testing-library/react to v9.1.1 (#956)

* chore(deps): update dependency @testing-library/react to v8.0.9

* chore: update testing library to 9.1.1

* fix: remove cleanup-after-each hook (automatical now)

* refactor: to remove path for act

* refactor(app-shell): use react-apollo v3, use more hooks

* refactor(app-shell): do not render hooks conditionally

* chore: update react deps consistently

* refactor(authenticated): do not render hook conditionally

* chore: patch jest setup to avoid logging certain warnings on CI

* fix: missing prop ref

* fix(navbar): missing prop

* refactor(app-connectors): to useQuery for fetching image regex setting

* refactor(app-shell): connectors to useQuery

* test(app-shell): fix setup

* chore: regenerate lockfile

* chore: update react-apollo to 3.0.1

* refactor(connectors): image regex tests to RTL

* fix: query mocks for project suspended

* fix: pin react-test-renderer to 16.9

* fix(quick-access): update state unless component is unmounting

* refactor: componentWillUnmount is not a deprecated lifecycle method

* test: cleanup

* refactor: renaming based on feedback
emmenko added a commit that referenced this pull request Aug 28, 2019
* chore: update to react@16.9 (#966)

* chore: update to react@16.9

* fix: update lifecyle methods

* chore: temporary resolution for react

* chore(deps): update dependency @testing-library/react to v9.1.1 (#956)

* chore(deps): update dependency @testing-library/react to v8.0.9

* chore: update testing library to 9.1.1

* fix: remove cleanup-after-each hook (automatical now)

* refactor: to remove path for act

* refactor(app-shell): use react-apollo v3, use more hooks

* refactor(app-shell): do not render hooks conditionally

* chore: update react deps consistently

* refactor(authenticated): do not render hook conditionally

* chore: patch jest setup to avoid logging certain warnings on CI

* fix: missing prop ref

* fix(navbar): missing prop

* refactor(app-connectors): to useQuery for fetching image regex setting

* refactor(app-shell): connectors to useQuery

* test(app-shell): fix setup

* chore: regenerate lockfile

* chore: update react-apollo to 3.0.1

* refactor(connectors): image regex tests to RTL

* fix: query mocks for project suspended

* fix: pin react-test-renderer to 16.9

* fix(quick-access): update state unless component is unmounting

* refactor: componentWillUnmount is not a deprecated lifecycle method

* test: cleanup

* refactor: renaming based on feedback
emmenko added a commit that referenced this pull request Aug 29, 2019
* chore: update to react@16.9 (#966)

* chore: update to react@16.9

* fix: update lifecyle methods

* chore: temporary resolution for react

* chore(deps): update dependency @testing-library/react to v9.1.1 (#956)

* chore(deps): update dependency @testing-library/react to v8.0.9

* chore: update testing library to 9.1.1

* fix: remove cleanup-after-each hook (automatical now)

* refactor: to remove path for act

* refactor(app-shell): use react-apollo v3, use more hooks

* refactor(app-shell): do not render hooks conditionally

* chore: update react deps consistently

* refactor(authenticated): do not render hook conditionally

* chore: patch jest setup to avoid logging certain warnings on CI

* fix: missing prop ref

* fix(navbar): missing prop

* refactor(app-connectors): to useQuery for fetching image regex setting

* refactor(app-shell): connectors to useQuery

* test(app-shell): fix setup

* chore: regenerate lockfile

* chore: update react-apollo to 3.0.1

* refactor(connectors): image regex tests to RTL

* fix: query mocks for project suspended

* fix: pin react-test-renderer to 16.9

* fix(quick-access): update state unless component is unmounting

* refactor: componentWillUnmount is not a deprecated lifecycle method

* test: cleanup

* refactor: renaming based on feedback
emmenko added a commit that referenced this pull request Sep 2, 2019
* chore: update to react@16.9 (#966)

* chore: update to react@16.9

* fix: update lifecyle methods

* chore: temporary resolution for react

* chore(deps): update dependency @testing-library/react to v9.1.1 (#956)

* chore(deps): update dependency @testing-library/react to v8.0.9

* chore: update testing library to 9.1.1

* fix: remove cleanup-after-each hook (automatical now)

* refactor: to remove path for act

* refactor(app-shell): use react-apollo v3, use more hooks

* refactor(app-shell): do not render hooks conditionally

* chore: update react deps consistently

* refactor(authenticated): do not render hook conditionally

* chore: patch jest setup to avoid logging certain warnings on CI

* fix: missing prop ref

* fix(navbar): missing prop

* refactor(app-connectors): to useQuery for fetching image regex setting

* refactor(app-shell): connectors to useQuery

* test(app-shell): fix setup

* chore: regenerate lockfile

* chore: update react-apollo to 3.0.1

* refactor(connectors): image regex tests to RTL

* fix: query mocks for project suspended

* fix: pin react-test-renderer to 16.9

* fix(quick-access): update state unless component is unmounting

* refactor: componentWillUnmount is not a deprecated lifecycle method

* test: cleanup

* refactor: renaming based on feedback
emmenko added a commit that referenced this pull request Sep 2, 2019
* chore: update to react@16.9 (#966)

* chore: update to react@16.9

* fix: update lifecyle methods

* chore: temporary resolution for react

* chore(deps): update dependency @testing-library/react to v9.1.1 (#956)

* chore(deps): update dependency @testing-library/react to v8.0.9

* chore: update testing library to 9.1.1

* fix: remove cleanup-after-each hook (automatical now)

* refactor: to remove path for act

* refactor(app-shell): use react-apollo v3, use more hooks

* refactor(app-shell): do not render hooks conditionally

* chore: update react deps consistently

* refactor(authenticated): do not render hook conditionally

* chore: patch jest setup to avoid logging certain warnings on CI

* fix: missing prop ref

* fix(navbar): missing prop

* refactor(app-connectors): to useQuery for fetching image regex setting

* refactor(app-shell): connectors to useQuery

* test(app-shell): fix setup

* chore: regenerate lockfile

* chore: update react-apollo to 3.0.1

* refactor(connectors): image regex tests to RTL

* fix: query mocks for project suspended

* fix: pin react-test-renderer to 16.9

* fix(quick-access): update state unless component is unmounting

* refactor: componentWillUnmount is not a deprecated lifecycle method

* test: cleanup

* refactor: renaming based on feedback
emmenko added a commit that referenced this pull request Sep 2, 2019
* chore: update to react@16.9 (#966)

* chore: update to react@16.9

* fix: update lifecyle methods

* chore: temporary resolution for react

* chore(deps): update dependency @testing-library/react to v9.1.1 (#956)

* chore(deps): update dependency @testing-library/react to v8.0.9

* chore: update testing library to 9.1.1

* fix: remove cleanup-after-each hook (automatical now)

* refactor: to remove path for act

* refactor(app-shell): use react-apollo v3, use more hooks

* refactor(app-shell): do not render hooks conditionally

* chore: update react deps consistently

* refactor(authenticated): do not render hook conditionally

* chore: patch jest setup to avoid logging certain warnings on CI

* fix: missing prop ref

* fix(navbar): missing prop

* refactor(app-connectors): to useQuery for fetching image regex setting

* refactor(app-shell): connectors to useQuery

* test(app-shell): fix setup

* chore: regenerate lockfile

* chore: update react-apollo to 3.0.1

* refactor(connectors): image regex tests to RTL

* fix: query mocks for project suspended

* fix: pin react-test-renderer to 16.9

* fix(quick-access): update state unless component is unmounting

* refactor: componentWillUnmount is not a deprecated lifecycle method

* test: cleanup

* refactor: renaming based on feedback

fix(app-shell): to run useEffect only once in dev mode
emmenko added a commit that referenced this pull request Sep 2, 2019
* chore: update to react@16.9 (#966)

* chore: update to react@16.9

* fix: update lifecyle methods

* chore: temporary resolution for react

* chore(deps): update dependency @testing-library/react to v9.1.1 (#956)

* chore(deps): update dependency @testing-library/react to v8.0.9

* chore: update testing library to 9.1.1

* fix: remove cleanup-after-each hook (automatical now)

* refactor: to remove path for act

* refactor(app-shell): use react-apollo v3, use more hooks

* refactor(app-shell): do not render hooks conditionally

* chore: update react deps consistently

* refactor(authenticated): do not render hook conditionally

* chore: patch jest setup to avoid logging certain warnings on CI

* fix: missing prop ref

* fix(navbar): missing prop

* refactor(app-connectors): to useQuery for fetching image regex setting

* refactor(app-shell): connectors to useQuery

* test(app-shell): fix setup

* chore: regenerate lockfile

* chore: update react-apollo to 3.0.1

* refactor(connectors): image regex tests to RTL

* fix: query mocks for project suspended

* fix: pin react-test-renderer to 16.9

* fix(quick-access): update state unless component is unmounting

* refactor: componentWillUnmount is not a deprecated lifecycle method

* test: cleanup

* refactor: renaming based on feedback

fix(app-shell): to run useEffect only once in dev mode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🙏 Status: Dev Review Waiting for technical reviews
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants