This repository was archived by the owner on Mar 5, 2022. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +45
-0
lines changed
cypress/integration/testing-lib-example Expand file tree Collapse file tree 3 files changed +45
-0
lines changed Original file line number Diff line number Diff line change 1+ Testing example used by https://testing-library.com/docs/react-testing-library/example-intro
Original file line number Diff line number Diff line change 1+ import React , { useState } from 'react'
2+ import axios from 'axios'
3+
4+ export default function Fetch ( { url } ) {
5+ const [ greeting , setGreeting ] = useState ( '' )
6+ const [ buttonClicked , setButtonClicked ] = useState ( false )
7+
8+ const fetchGreeting = ( ) => {
9+ axios . get ( url ) . then ( response => {
10+ const data = response . data
11+ const { greeting } = data
12+ setGreeting ( greeting )
13+ setButtonClicked ( true )
14+ } )
15+ }
16+
17+ const buttonText = buttonClicked ? 'Ok' : 'Load Greeting'
18+
19+ return (
20+ < div >
21+ < button onClick = { fetchGreeting } disabled = { buttonClicked } role = "button" >
22+ { buttonText }
23+ </ button >
24+ { greeting ? < h1 role = "heading" > { greeting } </ h1 > : null }
25+ </ div >
26+ )
27+ }
Original file line number Diff line number Diff line change 1+ /// <reference types="cypress" />
2+ import React from 'react'
3+ import { mount } from "cypress-react-unit-tests" ;
4+ import Fetch from './fetch'
5+
6+ it ( 'loads and displays greeting' , ( ) => {
7+ cy . server ( )
8+ cy . route ( '/greeting' , { greeting : 'Hello there' } ) . as ( 'greet' )
9+
10+ const url = '/greeting'
11+ mount ( < Fetch url = { url } /> )
12+
13+ cy . contains ( 'Load Greeting' ) . click ( )
14+ cy . get ( '[role=heading]' ) . should ( 'have.text' , 'Hello there' )
15+ cy . get ( '[role=button]' ) . and ( 'be.disabled' )
16+ cy . get ( '@greet' ) . its ( 'url' ) . should ( 'match' , / \/ g r e e t i n g $ / )
17+ } )
You can’t perform that action at this time.
0 commit comments