Skip to content

wrapper for testHook #296

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 2 commits into from
Feb 12, 2019
Merged

Conversation

danielkcz
Copy link
Contributor

@danielkcz danielkcz commented Feb 12, 2019

What:

Implementation of #293 based on discussion in #283.

Why:

To allow specifying a wrapper component tree around the testHook tree.

  testHook(callback, {
    wrapper: props => (
      <NameContext.Provider value={nameValue}>
        <OtherContext.Provider value={otherValue} {...props} />
      </NameContext.Provider>
    )
  })

Checklist:

  • Documentation (N/A?)
  • Tests
  • Typescript definitions updated
  • Ready to be merged
  • Added myself to contributors table

I am not too happy about spreading props there. In simple cases of a one Provider it's fine, but in the example above it might be confusing what you are supposed to do there.

kentcdodds
kentcdodds previously approved these changes Feb 12, 2019
Copy link
Member

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

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

This looks good to me 👍

@kentcdodds
Copy link
Member

Maybe the examples can just destructure children instead of spreading all the props. Children is all that's needed right?

@danielkcz
Copy link
Contributor Author

Maybe the examples can just destructure children instead of spreading all the props. Children is all that's needed right?

It's a bit bulkier this way, but I guess there is no other more graceful way without introducing extra complexity.

  testHook(callback, {
    wrapper: ({ children }) => (
      <NameContext.Provider value={nameValue}>
        <OtherContext.Provider value={otherValue}>
          {children}
        </OtherContext.Provider>
      </NameContext.Provider>
    )
  })

Btw, what about docs? I am not sure how to contribute there... Not even mentioning that testHook isn't there at all yet. I assume it's different repo so I should try to handle it in this PR, right?

@danielkcz
Copy link
Contributor Author

danielkcz commented Feb 12, 2019

@kentcdodds Are you ok with ignoring that eslint rule in tests (last commit)? If yes and documentation will be tackled later, then it's ready for merge.

@danielkcz danielkcz changed the title [WIP] wrapper for testHook wrapper for testHook Feb 12, 2019
Copy link
Member

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

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

This is fine. Docs will go on the docs website. Would love a contribution the too of you please. Thanks!

@kentcdodds kentcdodds merged commit cf12d91 into testing-library:master Feb 12, 2019
@danielkcz danielkcz deleted the testhook-wrapper branch February 12, 2019 15:55
@kentcdodds
Copy link
Member

🎉 This PR is included in version 5.7.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@danielkcz
Copy link
Contributor Author

@kentcdodds Would not mind contributing, but I do have trouble finding the repo for docs. Would be a good idea to mention that in CONTRIBUTING.

@kentcdodds
Copy link
Member

kentcdodds commented Feb 12, 2019

Would be a good idea to mention that in CONTRIBUTING.

Agreed! Would you like to add a note about that? The docs repo is linked in the docs: https://github.com/alexkrolick/testing-library-docs

@alexkrolick
Copy link
Collaborator

I'd like to do a follow-up PR to add this option to render. I'll reopen the issue with a check for the testHook part.

Thanks @FredyC!

@ThanhPhat1080
Copy link

Hi. Could you guys please let me know where is the docs to see how to implement testHook. For now, I try to implement testing with my context but it not work.

const useTestContext = () => {
  return useContext(AccountContext);
};

testHook(
    () => {
      accountState = useTestContext();
    },
    {
      wrapper: () => (
        <BrowserRouter>
          <AccountProvider>
            <AuthenticationForm />
          </AccountProvider>
        </BrowserRouter>
      ),
    },
  );

The accountState is undefined for now.

@danielkcz
Copy link
Contributor Author

danielkcz commented Feb 15, 2019

@ThanhPhat1080 Yea well, docs are kinda behind on this. I want to dive into that but got some other hurdles on the path. As for your example, you need this.

testHook(
    () => {
      accountState = useTestContext();
    },
    {
      wrapper: ({ children }) => (
        <BrowserRouter>
          <AccountProvider>
            <AuthenticationForm>{children}</AuthenticationForm>
          </AccountProvider>
        </BrowserRouter>
      ),
    },
  );

It's not intuitive much for sure and I am not too happy about it either. I am not sure if it could somehow automagically inject those children there.

@ThanhPhat1080
Copy link

Thanks @FredyC It work for now, this is my code to let it work

testHook(
    () => {
      accountState = useTestContext();
    },
    {
      wrapper: ({ children }) => (
        <BrowserRouter>
          <AccountProvider>
            {children}
            <AuthenticationForm />
          </AccountProvider>
        </BrowserRouter>
      ),
    },
  );

I have a question. Example my component have button and when fire click on it, it should change the context-value and update UI. I want to test that action and test the UI is updated or not. I tried using render but the context-value is not change. So testHook can test it or not?

@alexkrolick
Copy link
Collaborator

Docs PR for testHook: testing-library/testing-library-docs#32

@alexkrolick
Copy link
Collaborator

@ThanhPhat1080 testHook is meant to test the return value of custom hooks. For testing components that use the hooks, you test them the normal way, with render.

@ThanhPhat1080
Copy link

Thanks @alexkrolick

@kentcdodds
Copy link
Member

🎉 This issue has been resolved in version 5.9.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants