Skip to content
This repository was archived by the owner on Mar 5, 2022. It is now read-only.

Commit 2616a30

Browse files
committed
test equivalent to react-testing lib example
1 parent cbe2e77 commit 2616a30

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Testing example used by https://testing-library.com/docs/react-testing-library/example-intro
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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', /\/greeting$/)
17+
})

0 commit comments

Comments
 (0)