Skip to content

feat: add asFragment return value from render #192

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

Merged
merged 2 commits into from
Oct 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ test('Fetch makes an API call and displays the greeting when load-greeting is cl
// Arrange
axiosMock.get.mockResolvedValueOnce({data: {greeting: 'hello there'}})
const url = '/greeting'
const {getByText, getByTestId, container} = render(<Fetch url={url} />)
const {getByText, getByTestId, container, asFragment} = render(
<Fetch url={url} />,
)

// Act
fireEvent.click(getByText('Load Greeting'))
Expand All @@ -119,6 +121,8 @@ test('Fetch makes an API call and displays the greeting when load-greeting is cl
expect(getByTestId('ok-button')).toHaveAttribute('disabled')
// snapshots work great with regular DOM nodes!
expect(container.firstChild).toMatchSnapshot()
// you can also use get a `DocumentFragment`, which is useful if you want to compare nodes across render
expect(asFragment()).toMatchSnapshot()
})
```

Expand Down Expand Up @@ -547,6 +551,38 @@ const usernameInputElement = getByTestId('username-input')
> about `data-testid`s from the blog post ["Making your UI tests resilient to
> change"][data-testid-blog-post]

#### `asFragment(): DocumentFragment`

Returns a `DocumentFragment` of your rendered component. This can be useful if
you need to avoid live bindings and see how your component reacts to events.

```javascript
import {render, fireEvent} from 'react-testing-library'

class TestComponent extends React.Component {
constructor() {
super()
this.state = {count: 0}
}

render() {
const {count} = this.state

return <button onClick={() => this.setState({count: count + 1})}>Click to increase: {count}</div>
}
}

const {getByText, asFragment} = render(<TestComponent />)
const firstRender = asFragment()

fireEvent.click(getByText(/Click to increase/))

// This will snapshot only the difference between the first render, and the
// state of the DOM after the click event.
// See https://github.com/jest-community/snapshot-diff
expect(firstRender).toMatchDiffSnapshot(asFragment())
```

### `cleanup`

Unmounts React trees that were mounted with [render](#render).
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"history": "^4.7.2",
"jest-dom": "^1.3.1",
"jest-in-case": "^1.0.2",
"kcd-scripts": "^0.39.1",
"kcd-scripts": "^0.42.0",
"react": "^16.4.1",
"react-dom": "^16.4.1",
"react-redux": "^5.0.7",
Expand Down
12 changes: 12 additions & 0 deletions src/__tests__/__snapshots__/render.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`supports fragments 1`] = `
<DocumentFragment>
<div>
<code>
DocumentFragment
</code>
is pretty cool!
</div>
</DocumentFragment>
`;
17 changes: 17 additions & 0 deletions src/__tests__/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,20 @@ it('cleansup document', () => {
expect(document.body.innerHTML).toBe('')
expect(spy).toHaveBeenCalledTimes(1)
})

it('supports fragments', () => {
class Test extends React.Component {
render() {
return (
<div>
<code>DocumentFragment</code> is pretty cool!
</div>
)
}
}

const {asFragment} = render(<Test />)
expect(asFragment()).toMatchSnapshot()
cleanup()
expect(document.body.innerHTML).toBe('')
})
11 changes: 11 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ function render(ui, {container, baseElement = container, queries} = {}) {
// Intentionally do not return anything to avoid unnecessarily complicating the API.
// folks can use all the same utilities we return in the first place that are bound to the container
},
asFragment: () => {
if (typeof document.createRange === 'function') {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might wanna add a /* istanbul ignore if */ here, and leave a comment pointing back to jsdom/jsdom#317

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I've just committed that 👍

return document
.createRange()
.createContextualFragment(container.innerHTML)
}

const template = document.createElement('template')
template.innerHTML = container.innerHTML
return template.content
},
...getQueriesForElement(baseElement, queries),
}
}
Expand Down
3 changes: 2 additions & 1 deletion typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ type GetsAndQueries = ReturnType<typeof getQueriesForElement>
export interface RenderResult extends GetsAndQueries {
container: HTMLElement
baseElement: HTMLElement
debug: (baseElement?: HTMLElement) => void
debug: (baseElement?: HTMLElement | DocumentFragment) => void
rerender: (ui: React.ReactElement<any>) => void
unmount: () => boolean
asFragment: () => DocumentFragment
}

/**
Expand Down