-
-
Notifications
You must be signed in to change notification settings - Fork 72
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
text(str) assertion doesn't support custom component #134
Comments
How did you create |
beforeEach(() => {
props = {
number: {
name: 'hello',
number: 1234
}
}
component = shallow(<Something {...props} />)
}) |
Ok, so it's shallow-rendered - that explicitly means that custom components inside it aren't rendered. Try |
I am getting this following error: Method “shallow” is only meant to be run on a single node. 3 found instead. |
Then you'll have to disambiguate your |
We just want to validate content of a custom component which we can easily access (as you can see in the example)... The thing is that the component is working perfectly fine and we do not want to start testing it by rendering it... |
If there are 3 CardText components, you can't validate them. Add an assertion in your test that you've only found 1 item, and then you'll be able to assert things on it. |
I only have a CardTitle and a CardText... |
I'd recommend using .debug() to clarify exactly what gets rendered. Perhaps you could also share the implementation of |
JSX: Test: beforeEach(() => {
props = {
testNumber: {
name: 'random name',
number: 1234
}
}
component = shallow(<Something {...props} />)
}) it('should have a CardText displaying A Number', () => {
const card = component.find('Card[data-id="aDataId"]').shallow()
const cardTextNumber = card.find('CardText')
expect(cardTextNumber).to.have.text('A Number: 1234')
}) |
Ok, then CardText doesn't have any text - it has a children prop which is the text you're looking for. (Text is something a rendered component has) Try asserting on .props().children instead. |
Yeah that was my initial solution but I have to fragment the test like this to make it work: it('should have a CardText displaying A Number', () => {
const card = component.find('Card[data-id="aDataId"]').shallow()
const cardTextNumber = card.find('CardText').get(0).props
expect(cardTextNumber.children[0]).to.deep.equal('A Number: ')
expect(cardTextNumber.children[1]).to.deep.equal(1234)
}) I wasn't sure if it was the best way to do it thats why I opened this issue... |
Yes, that's currently the best way to do it (no You can also do This is effectively an "issue" with enzyme itself, not with chai-enzyme; we should move this conversation to enzymejs/enzyme#692 |
For example:
A Number: {card.number}
If I write the test for the previous JSX code:
Sadly, it seems like custom components are not supported. I am getting the following error:
expected the node in to have text 'A Number: 1234', but it has ''
The text was updated successfully, but these errors were encountered: