Giter Site home page Giter Site logo

Comments (7)

yzimet avatar yzimet commented on May 24, 2024 1

A bit more explanation:

A component that is wrapped with withDirection will use direction context from the closest DirectionProvider. If DirectionProvider is not present anywhere in the tree, the withDirection HOC will use a LTR direction context by default. So technically DirectionProvider is not required.

In your tests, if you are testing something unrelated to direction you can choose to not provide a DirectionProvider. For example:

const wrapper = shallow(
  <ForwardsLabel
    onPress={onPress}
  />
).dive();

expect(wrapper.prop('foo')).to.equal('bar');

In this example I'm using Enzyme's dive method to dive through the withDirection HOC.

from react-with-direction.

ljharb avatar ljharb commented on May 24, 2024

I'm confused, what do you mean "the fact that the tests don't have them"?

from react-with-direction.

ljharb avatar ljharb commented on May 24, 2024

If you're using withDirection, then DirectionProvider is required - so your tests also need to have them, otherwise they're not correct tests.

from react-with-direction.

dac09 avatar dac09 commented on May 24, 2024

I'll give you an example of the tests that fail:

  it('Should remember state if its passed in', () => {
    const wrapper = mount(
      <MyComponent
        onSubmit={mockSubmit}
      />
    );

    expect(wrapper.state('answer')).toBe('option2');
  });

I understand that the Provider is required, but is the expectation in unit tests to write:

<DirectionProvider>
<ComponentToTest/>
</DirectionProvider>

?

Perhaps a utility function to provide context for tests would be helpful, e.g. for react-intl, a modified mount function can be used to provide context

function nodeWithIntlProp(node: ReactElement) {
  return React.cloneElement(node, { intl });
}

export function mountWithIntl(node: ReactElement) {
  return mount(nodeWithIntlProp(node), {
    context: { intl },
    childContextTypes: { intl: intlShape }
  });
}

from react-with-direction.

ljharb avatar ljharb commented on May 24, 2024

Yes, that's the requirement in unit tests, to include the DirectionProvider explicitly.

from react-with-direction.

dac09 avatar dac09 commented on May 24, 2024

Ok, great. Thanks 👍

from react-with-direction.

dac09 avatar dac09 commented on May 24, 2024

Thanks @yzimet. That's very useful!

from react-with-direction.

Related Issues (11)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.