Skip to content

Adding first iteration over TSX on new tab #700

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

Merged
merged 4 commits into from
Feb 1, 2021
Merged
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
52 changes: 50 additions & 2 deletions docs/react-testing-library/setup.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ title: Setup
sidebar_label: Setup
---

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

`React Testing Library` does not require any configuration to be used. However,
there are some things you can do when configuring your testing framework to
reduce some boilerplate. In these docs we'll demonstrate configuring Jest, but
Expand All @@ -28,12 +31,17 @@ make your test util file accessible without using relative paths.
The example below sets up data providers using the [`wrapper`](api.mdx#wrapper)
option to `render`.

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

<TabItem value="jsx">

```diff title="my-component.test.jsx"
- import { render, fireEvent } from '@testing-library/react';
+ import { render, fireEvent } from '../test-utils';
```

```js title="test-utils.js"
```jsx title="test-utils.jsx"
import React from 'react'
import { render } from '@testing-library/react'
import { ThemeProvider } from 'my-ui-lib'
Expand All @@ -60,6 +68,46 @@ export * from '@testing-library/react'
export { customRender as render }
```

</TabItem>

<TabItem value="tsx">

```diff title="my-component.test.tsx"
- import { render, fireEvent } from '@testing-library/react';
+ import { render, fireEvent } from '../test-utils';
```

```tsx title="test-utils.tsx"
import React, { FC } from 'react'
import { render } from '@testing-library/react'
import { ThemeProvider } from 'my-ui-lib'
import { TranslationProvider } from 'my-i18n-lib'
import defaultStrings from 'i18n/en-x-default'

const AllTheProviders: FC = ({ children }) => {
return (
<ThemeProvider theme="light">
<TranslationProvider messages={defaultStrings}>
{children}
</TranslationProvider>
</ThemeProvider>
)
}

const customRender = (
ui: ReactElement,
options?: Omit<RenderOptions, 'queries'>
) => render(ui, { wrapper: AllTheProviders, ...options })

export * from '@testing-library/react'

export { customRender as render }
```

</TabItem>

</Tabs>

> **Note**
>
> Babel versions lower than 7 throw an error when trying to override the named
Expand Down