Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: added tsx tab to example #833

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 80 additions & 3 deletions docs/react-testing-library/example-intro.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ title: Example
sidebar_label: Example
---

import Tabs from '@theme/Tabs'
import TabItem from '@theme/TabItem'

## Full Example

See the following sections for a detailed breakdown of the test
Expand Down Expand Up @@ -167,9 +170,12 @@ expect(screen.getByRole('button')).not.toHaveAttribute('disabled')

### System Under Test

fetch.js
<Tabs groupId="test-utils" defaultValue="jsx" values={[ {label: 'Javascript',
value: 'jsx'}, {label: 'Typescript', value: 'tsx'}, ]}>

```jsx
<TabItem value="jsx">

```jsx title="fetch.jsx"
import React, { useState, useReducer } from 'react'
import axios from 'axios'

Expand Down Expand Up @@ -205,7 +211,7 @@ export default function Fetch({ url }) {
)
const [buttonClicked, setButtonClicked] = useState(false)

const fetchGreeting = async url =>
const fetchGreeting = async (url) =>
axios
.get(url)
.then((response) => {
Expand All @@ -218,6 +224,72 @@ export default function Fetch({ url }) {
dispatch({ type: 'ERROR', error })
})

const buttonText = buttonClicked ? 'Ok' : 'Load Greeting'

return (
<div>
<button onClick={() => fetchGreeting(url)} disabled={buttonClicked}>
{buttonText}
</button>
{greeting && <h1>{greeting}</h1>}
{error && <p role="alert">Oops, failed to fetch!</p>}
</div>
)
}
```

</TabItem>

<TabItem value="tsx">

```tsx title="fetch.tsx"
import React, { useState, useReducer } from 'react'
import axios from 'axios'

const initialState = {
error: null,
greeting: null,
}

function greetingReducer(state: any, action: any) {
switch (action.type) {
case 'SUCCESS': {
return {
error: null,
greeting: action.greeting,
}
}
case 'ERROR': {
return {
error: action.error,
greeting: null,
}
}
default: {
return state
}
}
}

export default function Fetch({ url }: { url: string }) {
const [{ error, greeting }, dispatch] = useReducer(
greetingReducer,
initialState
)
const [buttonClicked, setButtonClicked] = useState(false)

const fetchGreeting = async (url: string) =>
axios
.get(url)
.then((response) => {
const { data } = response
const { greeting } = data
dispatch({ type: 'SUCCESS', greeting })
setButtonClicked(true)
})
.catch((error) => {
dispatch({ type: 'ERROR', error })
})

const buttonText = buttonClicked ? 'Ok' : 'Load Greeting'

Expand All @@ -232,3 +304,8 @@ export default function Fetch({ url }) {
)
}
```

</TabItem>

</Tabs>