@@ -1733,3 +1733,153 @@ function MyComponent() {
17331733 </ div >
17341734 ) ;
17351735}
1736+
1737+
1738+
1739+ // DEBUGGING
1740+
1741+ // add debugger anywhere to add a breakpoint
1742+ debugger ;
1743+
1744+
1745+
1746+
1747+ // TESTING WITH JEST
1748+
1749+ // in package.json -> "scripts": {"test": "jest --watchAll"}
1750+ // run it with yarn test / npm run test
1751+
1752+ // TESTING WITH REACT TESTING LIBRARY
1753+ // npm install --save-dev @testing-library/react
1754+ // work with file.test.js
1755+ import { Component } from './Component' ;
1756+ import { render , fireEvent , wait } from '@testing-library/react' ;
1757+ const r = render ( < Component content = "Some content" title = "Some title" isImportant = { true } /> ) ;
1758+ r . getByTitle ( 'Some title' ) ;
1759+ r . getByText ( 'Some content' ) ;
1760+ // better to use describe
1761+ describe ( 'Component' , ( ) => {
1762+ describe ( 'render' , ( ) => {
1763+ it ( 'should return a container' , ( ) => {
1764+ const { container } = render (
1765+ < Component />
1766+ ) ;
1767+ expect ( container ) . toBeDefined ( ) ;
1768+ } ) ;
1769+ it ( 'should display the correct date' , ( ) => {
1770+ // needs the component to have a data-testid="title" defined (value can be anything you want)
1771+ const { getByTestId } = render (
1772+ < Component >
1773+ < h1 data-testid = "title" > Some title</ h1 >
1774+ </ Component >
1775+ ) ;
1776+ const title = getByTestId ( 'title' ) ;
1777+ expect ( title ) . toHaveTextContent ( 'Some title' ) ;
1778+ } ) ;
1779+ // expect().toHaveValue()
1780+
1781+ // SNAPSHOT TESTING
1782+ it ( 'should not change' , ( ) => {
1783+ const { getByTestId } = render (
1784+ < Component >
1785+ < h1 data-testid = "title" > Some title</ h1 >
1786+ </ Component >
1787+ ) ;
1788+ const title = getByTestId ( 'title' ) . toJSON ( ) ;
1789+ expect ( title ) . toMatchSnapshot ( ) ;
1790+ } ) ;
1791+
1792+ // EVENT TESTING
1793+ it ( 'should increment on click' , ( ) => {
1794+ const { queryByTestId } = render (
1795+ < Component >
1796+ < h1 data-testid = "title" > 0</ h1 >
1797+ </ Component >
1798+ ) ;
1799+ const title = getByTestId ( 'title' ) ;
1800+ fireEvent . click ( title ) ;
1801+ expect ( title . textContent ) . toBe ( '1' ) ;
1802+ fireEvent . click ( title ) ;
1803+ expect ( title . textContent ) . toBe ( '2' ) ;
1804+ } ) ;
1805+
1806+ // ASYNC TESTING
1807+ it ( 'should increment on click' , async ( ) => {
1808+ const { queryByTestId } = render (
1809+ < Component >
1810+ < h1 data-testid = "title" > 0</ h1 >
1811+ </ Component >
1812+ ) ;
1813+ const title = getByTestId ( 'title' ) ;
1814+ fireEvent . click ( title ) ;
1815+ await wait ( ( ) => {
1816+ expect ( title . textContent ) . toBe ( '1' ) ;
1817+ } ) ;
1818+ } ) ;
1819+ } ) ;
1820+
1821+ // EVENT MOCKS
1822+ describe ( 'change' , ( ) => {
1823+ it ( 'Should publish the typed text' , ( ) => {
1824+ const fn = jest . fn ( ) ;
1825+ const { getByTestId } = render (
1826+ < Input data-testid = "input" onChange = { fn } />
1827+ ) ;
1828+ const input = getByTestId ( "input" ) ;
1829+ fireEvent . change ( input , { target : { value : 'John' } } ) ;
1830+ expect ( fn . mock . calls ) . toEqual ( [ 'John' ] ) ;
1831+ } ) ;
1832+ } ) ;
1833+
1834+ // USEEFFECT HOOK TESTING (possible to mock a hook)
1835+ describe ( 'image from useEffect and async' , ( ) => {
1836+ it ( 'should render an image with a url' , async ( ) => {
1837+ const { queryByTestId } = render (
1838+ < Component >
1839+ < Image data-testid = "image" alt = "image" />
1840+ </ Component >
1841+ ) ;
1842+ await wait ( ( ) => {
1843+ const image = getByTestId ( 'image' ) ;
1844+ expect ( image . src ) . toMatch ( / h t t p / ) ;
1845+ } ) ;
1846+ } ) ;
1847+ } ) ;
1848+ } ) ;
1849+
1850+
1851+
1852+ // TESTING without external library
1853+ // very verbose...
1854+ // work with file.render.test.js
1855+ import { Component } from './Component' ;
1856+ describe ( 'Component' , ( ) => {
1857+ it ( 'should always render a message' , ( ) => {
1858+ const notImportantMessage = Message ( {
1859+ content : "I am text inside Component" ,
1860+ isImportant : false
1861+ } ) ;
1862+ expect ( notImportantMessage . props . children . props . children )
1863+ . toBe ( 'I am text inside Component' ) ;
1864+ const importantMessage = Message ( {
1865+ content : "I am text inside Component" ,
1866+ isImportant : true
1867+ } ) ;
1868+ expect ( importantMessage . props . children . props . children )
1869+ . toBe ( 'I am text inside Component' ) ;
1870+ } ) ;
1871+ it ( 'should make important message strong' , ( ) => {
1872+ const importantMessage = Message ( {
1873+ content : "I am text inside Component" ,
1874+ isImportant : true
1875+ } ) ;
1876+ expect ( importantMessage . props . children . type ) . toBe ( 'strong' ) ;
1877+ } ) ;
1878+ it ( 'should not make not important message strong' , ( ) => {
1879+ const notImportantMessage = Message ( {
1880+ content : "I am text inside Component" ,
1881+ isImportant : false
1882+ } ) ;
1883+ expect ( notImportantMessage . props . children . type ) . not . toBe ( 'strong' ) ;
1884+ } ) ;
1885+ } ) ;
0 commit comments