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

Refactor: ContactCardModal to use func comp and useQuery #661

Merged
merged 2 commits into from
Aug 20, 2020

Conversation

JF-Cozy
Copy link
Contributor

@JF-Cozy JF-Cozy commented Aug 19, 2020

This PR is a part [1] of multiple PR about the new sort logic in the application.

This refactor prevents problems of using useQuery in App. Otherwise It triggers errors when using useQuery in App and Query here in ContactCardModal.

  • Update react-redux > 7.1.0 to be able to use useQuery hook
  • Update cozy-client and fix some tests
  • (*)Add react-redux-test (react-redux not updated) to prevent broken tests and Enzyme incompatibility
  • Add some queries and update ContactCardModal in functional component using useQuery

(*) Using older version of react-redux is the best solution I found, I didn't success to make tests working in other way (like using mount of Enzyme or react-test-renderer, or without Provider). The problem is that .dive() doesn't work anymore and even if the right component is selected, the .update() doesn't do anything
See enzymejs/enzyme#2202 , enzymejs/enzyme#2302

src/helpers/queries.js Outdated Show resolved Hide resolved
src/helpers/queries.js Outdated Show resolved Hide resolved
Copy link
Contributor

@ptbrowne ptbrowne left a comment

Choose a reason for hiding this comment

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

Looking good, I have some suggestions and questions though.

@JF-Cozy JF-Cozy force-pushed the refactor/contactCardModal branch from 0133658 to 18caa13 Compare August 20, 2020 09:00
@JF-Cozy JF-Cozy requested a review from ptbrowne August 20, 2020 09:00
src/helpers/queries.js Outdated Show resolved Hide resolved
src/targets/browser/index.jsx Outdated Show resolved Hide resolved
@Crash--
Copy link
Contributor

Crash-- commented Aug 20, 2020

(*)Add react-redux-test (react-redux not updated) to prevent broken tests and Enzyme incompatibility

Can you explain what was wrong with the latest react-redux? Is there any existing issue on Enzyme / Redux repo?

@JF-Cozy JF-Cozy force-pushed the refactor/contactCardModal branch 3 times, most recently from c8eabc3 to 21e3eae Compare August 20, 2020 11:31
@JF-Cozy JF-Cozy requested a review from Crash-- August 20, 2020 11:32

// Uses a different version of react-redux
// to prevent Enzyme's incompatibility with actual react-redux version
jest.mock('react-redux', () => require('react-redux-test'))
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you link to an issue or something?

Copy link
Contributor

Choose a reason for hiding this comment

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

What if you use mount instead of shallow?

I really don't like having 2 versions of react-redux, the test doesn't test the code used in our app...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This hack is not global, only on this file. Me neither but I didn't success to get it work. With mount we need to wrap the function's calls like realConnectedComponent.prop('toggleSelection')({ _id: 1, id: 1 }) in act(). But even doing this, the call does nothing, neither the .update().

Copy link
Contributor

Choose a reason for hiding this comment

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

Even if the hack is scoped, the test is not relevant anymore. We should create an issue on Contacts to rewrite this test with react-testing-lib. Since one of the next task will be to work on list's filter, maybe we will have to work on the selection part and fix this issue

Copy link
Contributor Author

Choose a reason for hiding this comment

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

issue : #665


const contact =
resultContactById.data !== null ? resultContactById.data[0] : {}
const allGroups = resultAllGroups.data !== null ? resultAllGroups.data : []
Copy link
Contributor

Choose a reason for hiding this comment

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

not sure we need this resultAllGroups.data !== null anymore since we use allGroups only when showResult is true === data loaded, no ? Same for resultContactById ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can simplify by using showResult but at the first render the data are still null. But we can remove those const and use the data directly in the return part (wrap into showResult)

Copy link
Contributor

Choose a reason for hiding this comment

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

You right, the data will be null at the first render. But we don't use them at this time, no? We only read them if showResult (or dataHaveBeenLoaded) is true, right? So we don't need to deal with null data, right?

Copy link
Contributor Author

@JF-Cozy JF-Cozy Aug 20, 2020

Choose a reason for hiding this comment

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

We only read them if showResult (or dataHaveBeenLoaded) is true, right?

Yes. Code updated according to use the data directly in the return part. What do you think ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Better! But I'm not very convinced by reading resultContactById.data[0]. Are you sure that getById() returns an array and not the object? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes... It will not work otherwise... Besides, browser returns this

Failed prop type: Invalid prop `contact` of type `array` supplied to `ContactCard`, expected `object`.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think that Q().getById() should returns the related document, not an array containing one document. It should behave like the DocumentCollection.get(), no? If yes, we should open an issue on cozy-client

Copy link
Contributor Author

Choose a reason for hiding this comment

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

src/helpers/queries.js Outdated Show resolved Hide resolved
src/helpers/utils.js Outdated Show resolved Hide resolved
@JF-Cozy JF-Cozy force-pushed the refactor/contactCardModal branch from 21e3eae to 693395e Compare August 20, 2020 12:39
@JF-Cozy JF-Cozy force-pushed the refactor/contactCardModal branch from 693395e to f1a892b Compare August 20, 2020 12:57
@JF-Cozy JF-Cozy requested a review from Crash-- August 20, 2020 12:58
@JF-Cozy
Copy link
Contributor Author

JF-Cozy commented Aug 20, 2020

(*)Add react-redux-test (react-redux not updated) to prevent broken tests and Enzyme incompatibility

Can you explain what was wrong with the latest react-redux? Is there any existing issue on Enzyme / Redux repo?

Yes, first message updated

@JF-Cozy JF-Cozy merged commit bc3859e into master Aug 20, 2020
@JF-Cozy JF-Cozy deleted the refactor/contactCardModal branch September 21, 2020 08:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants