GithubHelp home page GithubHelp logo

Comments (12)

lelandrichardson avatar lelandrichardson commented on April 29, 2024 187

@kwhitaker wrapper.find('input').props().value should get you what you need

Though I wouldn't be opposed to adding a .val() shortcut...

from enzyme.

tshelburne avatar tshelburne commented on April 29, 2024 12

@lelandrichardson This works well when you want to test that the prop as an argument is correct, but doesn't work well when you are wanting to test the rendered output. In my case, I am auto-formatting a value as part of the render function as part of the implementation of the component. I might be missing something, but when I try to use a mounted component, <input>s never render the value:

const TextInput = ({value, autoformat}) =>
  <div>
    <input type="text" value={autoformat(value)} />
  </div>

const NumberInput = props => <TextInput autoformat={commas} {...props} />

mount(<NumberInput value={1000} />).find('input').html() // <input type="text"/> instead of <input type="text" value="1,000"/>
shallow(<NumberInput value={1000} />).find('input').html() // Error: This method is only meant to be run on single node. 0 found instead

In many cases, I'm able to use shallow rendering for many components and it is fine, but some of my components are extensions of lower-level components (as above), and thus mount appears to be required.

from enzyme.

tshelburne avatar tshelburne commented on April 29, 2024 10

@trusktr Can't disagree with your reasoning from a technical perspective, but when I'm asking my designers / new developers to update and write unit tests for components, it's a much more reasonable expectation for them to compare HTML strings than to consider the underpinnings of React (simple though it may seem).

We have a robust pattern library in it's own NPM module that is backed by React components, and it's not intended just to be a JS developer's playground, but something accessible to everyone on the team. I'm all about making systems that enable developers at every level, and being able to reliably check the rendered HTML has already been a meaningfully helpful tool.

That said, thanks for the clarification.

from enzyme.

kwhitaker avatar kwhitaker commented on April 29, 2024 2

@lelandrichardson thanks for the tip, but sadly that selector comes up as undefined.

Here's the full props from wrapper.find('select').props():

{
  "defaultValue": "en-UP",
  "id": "locale-selector",
  "children": ["  "" 
    {
      "type": "option",
      "key": "en-US",
      "ref": null,
      "props": {
        "value": "en-US",
        "children": "English (US)"
      },
      "_owner": null,
      "_store": {}
    },
    {
      "type": "option",
      "key": "en-UP",
      "ref": null,
      "props": {
        "value": "en-UP",
        "children": "English (Uppercase)"
      },
      "_owner": null,
      "_store": {}
    }
  ]
}

from enzyme.

tshelburne avatar tshelburne commented on April 29, 2024 2

@ljharb Thanks so much for the quick response.

I randomly decided to check what version I was on (2.2.0), and then to update to the latest version to run your request, and now this appears to be working. Sorry for the mistake.

from enzyme.

ndeen86 avatar ndeen86 commented on April 29, 2024 1

@trusktr My scenario to test, Am I given the right state property name to the input element's value attribute. like, i have my state property name as "firstName", but i specified as <input type="text" value={this.state.fName}. Will this make sense to test the above case

from enzyme.

kwhitaker avatar kwhitaker commented on April 29, 2024

Wait duh. I wasn't explicitly setting value in my component. Blah.

from enzyme.

tshelburne avatar tshelburne commented on April 29, 2024

@lelandrichardson Any thoughts on this?

from enzyme.

tshelburne avatar tshelburne commented on April 29, 2024

@ljharb I just wanted to ping you on my question above, since you are the number 2 contributor to the library - is this something we can expect to change?

from enzyme.

ljharb avatar ljharb commented on April 29, 2024

@tshelburne what does mount(<NumberInput value={1000} />).debug() and shallow(<NumberInput value={1000} />).debug() output?

from enzyme.

trusktr avatar trusktr commented on April 29, 2024

@tshelburne Checking the value prop of the input component as above is enough, because at that point you're relying on React to do it's job (if the value of an input component is 'foo', then it must be foo in the DOM too, otherwise it's a serious bug in React).

from enzyme.

abrorAbdullaev avatar abrorAbdullaev commented on April 29, 2024

@kwhitaker wrapper.find('input').props().value should get you what you need

Though I wouldn't be opposed to adding a .val() shortcut...

I think getting it with prop() also makes sense
wrappedContent.find('Form Input[id="client_edit_id"]').prop('value')

from enzyme.

Related Issues (20)

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.