Skip to content

Commit

Permalink
Support an array of nodes in contain
Browse files Browse the repository at this point in the history
  • Loading branch information
rylanc authored and ayrton committed Aug 28, 2017
1 parent 6cc8e38 commit c985924
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 10 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
1. [Assertions](#assertions)
1. [`checked()`](#checked)
1. [`className(str)`](#classnamestr)
1. [`contain(node)`](#containnode)
1. [`contain(nodeOrNodes)`](#containnodeornodes)
1. [`containMatchingElement(node)`](#containmatchingelementnode)
1. [`descendants(selector)`](#descendantsselector)
1. [`exactly()`](#exactly)
Expand Down Expand Up @@ -173,14 +173,14 @@ expect(wrapper.find('span')).to.have.className('child')
expect(wrapper.find('span')).to.not.have.className('root')
```

#### `contain(node)`
#### `contain(nodeOrNodes)`

| render | mount | shallow |
| -------|-------|-------- |
| no | yes | yes |


Assert that the wrapper contains a given node:
Assert that the wrapper contains a given node or array of nodes:

```js
import React from 'react'
Expand All @@ -205,7 +205,10 @@ class Fixture extends React.Component {
<div>
<ul>
<li><User index={1} /></li>
<li><User index={2} /></li>
<li>
<User index={2} />
<User index={3} />
</li>
</ul>
</div>
)
Expand All @@ -215,6 +218,7 @@ class Fixture extends React.Component {
const wrapper = mount(<Fixture />) // mount/render/shallow when applicable

expect(wrapper).to.contain(<User index={1} />)
expect(wrapper).to.contain([<User index={2} />, <User index={3} />])
expect(wrapper).to.not.contain(<User index={3} />)
```

Expand Down
4 changes: 2 additions & 2 deletions src/assertions/contain.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import reactElementToJSXString from 'react-element-to-jsx-string'
import reactNodeToString from '../reactNodeToString'

export default function contain ({ wrapper, markup, arg1, sig }) {
const arg1JSXString = reactElementToJSXString(arg1)
const arg1JSXString = reactNodeToString(arg1)

this.assert(
wrapper.hasNode(arg1),
Expand Down
20 changes: 20 additions & 0 deletions src/reactNodeToString.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import reactElementToJSXString from 'react-element-to-jsx-string'

function reactArrayToJSXString (nodes) {
let jsxString = '['
nodes.forEach((node, idx) => {
jsxString += `\n ${reactElementToJSXString(node)}`
if (idx < nodes.length - 1) {
jsxString += ','
}
})
return `${jsxString}\n]`
}

export default function reactNodeToString (node) {
if (Array.isArray(node)) {
return reactArrayToJSXString(node)
} else {
return reactElementToJSXString(node)
}
}
37 changes: 33 additions & 4 deletions test/contain.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ class Fixture extends React.Component {
<div>
<ul>
<li><User index={1} /></li>
<li><User index={2} /></li>
<li>
<User index={2} />
<User index={3} />
</li>
</ul>
</div>
)
Expand All @@ -35,13 +38,13 @@ describe('#contain', () => {
}, { render: false })

it('passes negated when the actual does not match the expected', (wrapper) => {
expect(wrapper).to.not.contain(<User index={3} />)
expect(wrapper).to.not.contain(<User index={4} />)
}, { render: false })

it('fails when the actual does not match the expected', (wrapper) => {
expect(() => {
expect(wrapper).to.contain(<User index={3} />)
}).to.throw('to contain <User index={3} />')
expect(wrapper).to.contain(<User index={4} />)
}).to.throw('to contain <User index={4} />')

expect(() => {
expect(wrapper).to.not.contain(<User index={2} />)
Expand All @@ -54,4 +57,30 @@ describe('#contain', () => {
}).to.throw()
})
})

describe('(nodes)', () => {
it('passes when the actual matches the expected', (wrapper) => {
expect(wrapper).to.contain([<User index={2} />, <User index={3} />])
}, { render: false })

it('passes negated when the actual does not match the expected', (wrapper) => {
expect(wrapper).to.not.contain([<User index={3} />, <User index={4} />])
}, { render: false })

it('fails when the actual does not match the expected', (wrapper) => {
expect(() => {
expect(wrapper).to.contain([<User index={3} />, <User index={4} />])
}).to.throw('to contain [\n <User index={3} />,\n <User index={4} />\n]')

expect(() => {
expect(wrapper).to.not.contain([<User index={2} />, <User index={3} />])
}).to.throw('not to contain [\n <User index={2} />,\n <User index={3} />\n]')
}, { render: false })

it('fails when the actual is undefined', () => {
expect(() => {
expect(undefined).to.contain([<User index={2} />, <User index={3} />])
}).to.throw()
})
})
})

0 comments on commit c985924

Please sign in to comment.