Test Renderer
Importazione
import TestRenderer from 'react-test-renderer'; // ES6
const TestRenderer = require('react-test-renderer'); // ES5 with npm
Panoramica
Questo package fornisce un renderizzatore React che può essere usato per renderizzare componenti React come oggetti Javascript puri, senza dipendere dal DOM o da un ambiente mobile nativo.
Fondamentalmente, questo package permette di catturare facilmente uno snapshot della vista gerarchica della piattaforma (simile ad un albero DOM) renderizzata dal DOM di React o da un componente di React Native senza utilizzare un browser o jsdom.
Esempio:
import TestRenderer from 'react-test-renderer';
function Link(props) {
return <a href={props.page}>{props.children}</a>;
}
const testRenderer = TestRenderer.create(
<Link page="https://www.facebook.com/">Facebook</Link>
);
console.log(testRenderer.toJSON());
// { type: 'a',
// props: { href: 'https://www.facebook.com/' },
// children: [ 'Facebook' ] }
Puoi utilizzare la feature di snapshot testing di Jest per salvare automaticamente una copia dellâalbero JSON in un file e verificare nei tuoi test che non sia cambiato: Per saperne di piĂš.
Puoi anche navigare lâoutput per trovare nodi specifici e fare asserzioni su di essi.
import TestRenderer from 'react-test-renderer';
function MyComponent() {
return (
<div>
<SubComponent foo="bar" />
<p className="my">Hello</p>
</div>
)
}
function SubComponent() {
return (
<p className="sub">Sub</p>
);
}
const testRenderer = TestRenderer.create(<MyComponent />);
const testInstance = testRenderer.root;
expect(testInstance.findByType(SubComponent).props.foo).toBe('bar');
expect(testInstance.findByProps({className: "sub"}).children).toEqual(['Sub']);
TestRenderer
Istanza di TestRenderer
testRenderer.toJSON()
testRenderer.toTree()
testRenderer.update()
testRenderer.unmount()
testRenderer.getInstance()
testRenderer.root
TestInstance
testInstance.find()
testInstance.findByType()
testInstance.findByProps()
testInstance.findAll()
testInstance.findAllByType()
testInstance.findAllByProps()
testInstance.instance
testInstance.type
testInstance.props
testInstance.parent
testInstance.children
Riferimento
TestRenderer.create()
TestRenderer.create(element, options);
Crea unâistanza di TestRenderer
tramite lâelemento React passato in input. Non utilizza il DOM reale, ma renderizza comunque in maniera completa lâalberatura del componente in memoria cosĂŹ da poter fare asserzioni su di essa. Ritorna una istanza di TestRenderer.
TestRenderer.act()
TestRenderer.act(callback);
In modo simile allâhelper act()
di react-dom/test-utils
, TestRenderer.act
prepara un componente per le asserzioni. Usa questa versione di act()
per il wrapping di chiamate a TestRenderer.create
e testRenderer.update
.
import {create, act} from 'react-test-renderer';
import App from './app.js'; // Il componente in test
// renderizzazione del componente
let root;
act(() => {
root = create(<App value={1}/>)
});
// asserzioni sulla root
expect(root.toJSON()).toMatchSnapshot();
// aggiornamenti qualche props
act(() => {
root.update(<App value={2}/>);
})
// asserzioni sulla root
expect(root.toJSON()).toMatchSnapshot();
testRenderer.toJSON()
testRenderer.toJSON()
Ritorna un oggetto che raffigura lâalbero renderizzato. Questo albero contiene solo nodi specifici della piattaforma come <div>
o <View>
e le loro props, ma non contiene nessun componente scritto dallâutente. Questo risulta utile per lo snapshot testing.
testRenderer.toTree()
testRenderer.toTree()
Ritorna un oggetto che raffigura lâalbero renderizzato. La rappresentazione è piĂš dettagliata di quella fornita da toJSON()
, e include componenti scritti dallâutente. Probabilmente non hai bisogno di questo metodo a meno che non stia scrivendo una tua libreria di asserzioni sul test renderer.
testRenderer.update()
testRenderer.update(element)
Ri-renderizza lâalbero in memoria con un nuovo elemento root. Questo simula un aggiornamento di React a partire dalla root. Se il nuovo elemento ha lo stesso tipo e chiave del precedente elemento, lâalbero sarĂ aggiornato; altrimenti, verrĂ ri-montato un nuovo albero.
testRenderer.unmount()
testRenderer.unmount()
Smonta lâalbero in memoria, scatenando gli eventi di lifecycle appropriati.
testRenderer.getInstance()
testRenderer.getInstance()
Ritorna lâistanza corrispondente allâelemento root, se disponibile. Questo non funzionerĂ se lâelemento root è un componente funzione perchĂŠ non avrĂ istanza.
testRenderer.root
testRenderer.root
Ritorna lâoggetto root âistanza di testâ che è utile per fare asserzioni su specifici nodi nellâalbero. Puoi usarlo per trovare altre âistanze di testâ maggiormente in profonditĂ .
testInstance.find()
testInstance.find(test)
Trova una singola istanza di test discendente per la quale test(testInstance)
ritorna true
. Se test(testInstance)
non ritorna true
per quella specifica istanza di test, lancerĂ un errore.
testInstance.findByType()
testInstance.findByType(type)
Trova una singola istanza di test discendente usando il type
fornito in input. Se non câè esattamente unâistanza di test con il type
fornito, lancerĂ un errore.
testInstance.findByProps()
testInstance.findByProps(props)
Trova una singola istanza di test discendente usando le props
fornite in input. Se non câè esattamente unâistanza di test con le props
fornite, lancerĂ un errore.
testInstance.findAll()
testInstance.findAll(test)
Trova tutte le istanze di test discendenti per le quali test(testInstance)
ritorna true
.
testInstance.findAllByType()
testInstance.findAllByType(type)
Trova tutte le istanze di test discendenti con il type
fornito.
testInstance.findAllByProps()
testInstance.findAllByProps(props)
Trova tutte le istanze di test discendenti con le props
fornite.
testInstance.instance
testInstance.instance
Lâistanza del componente corrispondente a questa istanza di test. Eâ disponibile solo per componenti classe, in quanto i componenti funzione non hanno istanze. Corrisponde al valore this
dentro il dato componente.
testInstance.type
testInstance.type
Il tipo del componente corrispondente a questa istanza di test. Per esempio, un componente <Button />
ha come tipo Button
.
testInstance.props
testInstance.props
Le props corrispondenti a questa istanza di test. Per esempio, un componente <Button size="small" />
ha come props {size: 'small'}
.
testInstance.parent
testInstance.parent
Lâistanza padre di questa istanza di test.
testInstance.children
testInstance.children
Le istanze figlio di questa istanza di test.
Idee
Puoi passare una funzione createNodeMock
a TestRenderer.create
come opzione, che consente riferimenti a mock personalizzati.
createNodeMock
accetta lâelemento corrente e dovrebbe ritornare un oggetto riferimento mock.
Questo metodo è utile quando testi un componente che si affida ai riferimenti.
import TestRenderer from 'react-test-renderer';
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.input = null;
}
componentDidMount() {
this.input.focus();
}
render() {
return <input type="text" ref={el => this.input = el} />
}
}
let focused = false;
TestRenderer.create(
<MyComponent />,
{
createNodeMock: (element) => {
if (element.type === 'input') {
// mock a focus function
return {
focus: () => {
focused = true;
}
};
}
return null;
}
}
);
expect(focused).toBe(true);