question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

test form elements in isolation

See original GitHub issue

Hello,

I have an input on which I use Decorator, it works well in browser, however when I try to test it without wrapping it in Form element I get Cannot read property 'attachToForm' of undefined Is there a way to test form elements in isolation without having to wrap them in Form tag?

Issue Analytics

  • State:open
  • Created 8 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
arnifcommented, Oct 27, 2015

I solved this by creating a Formsy stub

var React = require('react');

export default class FormsyStub extends React.Component {
  static childContextTypes = {
    formsy: React.PropTypes.object.isRequired
  };

  getChildContext() {
      return {
          formsy: {
              attachToForm() { },
              detachFromForm() { },
              validate() { },
              isFormDisabled() { },
              isValidValue() { }
          }
      };
  }

    render() {
        return this.props.children();
    }
}

Then in my test:

import Stub from './FormsyStub';
const target = TestUtils.renderIntoDocument(<Stub>{ () => <Component stuff="stuff" /> }</Stub>);
0reactions
clayne11commented, Apr 20, 2016

Something like this: https://gist.github.com/clayne11/1ae19045adf31ddf51b9868288aefda5

With the way I have exported the TextField component, you can import it into a test file and test it independently of Formsy.

That being said, I don’t like the way Formsy uses functions rather than values. In certain situations props won’t update because the function hasn’t changed, even though the value has.

I use a different HOC than the one included with Formsy. This is mine:

import React, {PropTypes} from 'react';
import Formsy from 'formsy-react';
const {Mixin} = Formsy;

export default () => (Component) => {
  if (process.env.NODE_ENV !== 'production' && !Component) {
    console.error(`Must pass a component into FormsyDecorator`); // eslint-disable-line no-console
  }

  return (
    React.createClass({
      propTypes: {
        disabled: PropTypes.bool,
      },

      mixins: [Mixin],

      render() {
        const props = _.omit(this.props, 'value');
        return React.createElement(Component, {
          setValidations: this.setValidations,
          setValue: this.setValue,
          resetValue: this.resetValue,
          getValue: this.getValue,
          hasValue: this.hasValue,
          getErrorMessage: this.getErrorMessage,
          getErrorMessages: this.getErrorMessages,
          isFormDisabled: this.isFormDisabled,
          isValid: this.isValid,
          isPristine: this.isPristine,
          isFormSubmitted: this.isFormSubmitted,
          isRequired: this.isRequired,
          showRequired: this.showRequired,
          isValidValue: this.isValidValue,
          showError: this.showError(),
          ...props,
          onChange: this.setValue,
          value: this.getValue(),
          disabled: this.props.disabled || this.isFormDisabled(),
          errorMessage: this.getErrorMessage(),
        });
      }
    })
  );
};
Read more comments on GitHub >

github_iconTop Results From Across the Web

Developing and Testing UI Components in Isolation
In this article we have focused on developing and testing UI components in isolation and various benefits of the mentioned approach.
Read more >
7 Benefits of testing in isolation | CircleCI
Let's explore how isolation testing solves local dependency issues, improves accuracy, promotes clean production, enables continuous deployment, ...
Read more >
Electrical isolation test - Wikipedia
An electrical isolation test is a Direct current (DC) or Alternating current (AC) resistance test that is performed between sub-circuit common and subsystem ......
Read more >
Chapter 19. Test Isolation, and “Listening to Your Tests”
If we have a look through our form tests, we'll see that, actually, only item ➂ is tested explicitly. On items ➀ and...
Read more >
Hindsight lessons about automation: Test isolation principle
Test isolation means – to develop and maintain tests that are logically isolated, therefore independent of any other tests that are run in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found