createSnapshot

Renders Fela components into a string snapshot including both rendered HTML and CSS to achieve predictable component tests.

It formats and optimises the output for maximum readability.

Arguments

ArgumentTypeDefaultDescription
componentComponent(new tab)A valid React component that is rendered.
themeObject?
{}
A theme object that is automatically used for rendering.
rendererRenderer?
createRenderer()
A Fela renderer with custom configuration
ProviderProvider?bindings specific ProviderA custom Provider component
ThemeProviderThemeProvider?bindings specific ThemeProviderA custom ThemeProvider component

Returns

(string): Formatted string including CSS and HTML.

Imports

import { createSnapshot } from 'jest-react-fela'
import { createSnapshot } from 'jest-preact-fela'
import { createSnapshot } from 'jest-inferno-fela'

Example

describe('Rendering a Fela component', () => {
it('should render correctly', () => {
const snapshot = createSnapshot(
<FelaComponent style={{ color: 'blue', backgroundColor: 'black' }} />
)
expect(snapshot).toMatchSnapshot()
})
})

Snapshot

.a {
color: blue
}
.b {
background-color: black
}
<div className=a b />

Passing a Theme

describe('Rendering a Fela component', () => {
it('should render correctly', () => {
const rule = ({ theme }) => ({
backgroundColor: theme.primary,
color: theme.secondary,
})
const theme = {
primary: 'blue',
secondary: 'red',
}
const snapshot = createSnapshot(<FelaComponent rule={rule} />, theme)
expect(snapshot).toMatchSnapshot()
})
})

Snapshot

.a {
background-color: blue
}
.b {
color: red
}
<div className=a b />