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

Add support for passing context #49

Closed
laurakelly opened this issue Dec 5, 2015 · 10 comments
Closed

Add support for passing context #49

laurakelly opened this issue Dec 5, 2015 · 10 comments
Assignees

Comments

@laurakelly
Copy link

I have a component in my app that has a router passed to it via context:

const contextTypes = { router: React.PropTypes.func.isRequired, };

Reagent tests currently do not support passing in a context and will throw this error:

Error: expected zero console.warn calls, got 1:
      warn(Warning: Failed Context Types: Required context `router` was not specified in `MyComponent`.) at warning (node_modules/react/lib/warning.js:48:15)
    at Context.<anonymous> (spec/javascripts/_failTestsOnErrors.js:21:23)

It would be great to be able to pass context into shallow.

@lelandrichardson
Copy link
Collaborator

I think this would be a great addition! The only remaining question is what the right API is.

I propose that we add a second argument to mount, shallow, and render which is simply an object named options in order to be most flexible with future changes.

options could have a context property that, if defined, would be passed in as context to the parent component.

const context = { router: mockReactRouter() };
const wrapper = shallow(<MyComponent />, { context });
// ...

cc @ljharb @goatslacker thoughts?

@lelandrichardson lelandrichardson self-assigned this Dec 5, 2015
@blainekasten
Copy link
Contributor

That is the same API, TestUtils.shallowRenderer().render uses. So I would second that and stay consist when possible. 👍

@lelandrichardson
Copy link
Collaborator

@blainekasten to be clear, I'm recommending that on object hash with a context prop be passed in as a second argument to shallow and the like, NOT that the context object itself be the second argument. The latter, I believe, is the current API for the shallowRenderer

@blainekasten
Copy link
Contributor

My bad. Why that approach? What other keys would be used and why?
On Sun, Dec 6, 2015 at 3:04 PM Leland Richardson notifications@github.com
wrote:

@blainekasten https://github.com/blainekasten to be clear, I'm
recommending that on object hash with a context prop be passed in as a
second argument to shallow and the like, NOT that the context object
itself be the second argument. The latter, I believe, is the current API
for the shallowRenderer


Reply to this email directly or view it on GitHub
#49 (comment).

@lelandrichardson
Copy link
Collaborator

My thinking is that it allows other "options" be passed in as well in the future, rather than just having a bunch of arguments at random slots of the function signature.

One such thing we discussed internally was the ability to have an option for mount which indicated the node it mounts onto actually be attached to the document body:

const wrapper = mount(<Foo />, { attach: true });

Which could come in handy if you're wanting to test a component that uses DOM api's such as elem.getBoundingClientRect(); that require the DOM nodes be attached to a document.

@lelandrichardson
Copy link
Collaborator

@laurakelly context support is now available on enzyme v1.2.0

@laurakelly
Copy link
Author

@lelandrichardson Great, thank you! I'll try it out

@havenchyk
Copy link

@lelandrichardson well, it looks like {attach: true} doesn't work for me with getBoundingClientRect

@bscherminator
Copy link

@lelandrichardson How does one mock getBoundingClientRect using the options?

@lelandrichardson
Copy link
Collaborator

@bscherminator what environment are you using getBoundingClientRect in?

getBoundingClientRect will always return a zero'd out rectangle if you're using jsdom. This will only work in something like karma with a browser runner

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

No branches or pull requests

5 participants