Skip to content

feature: configure API from DTL #1141

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 9 commits into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
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
22 changes: 22 additions & 0 deletions src/__tests__/config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { getConfig, configure, resetToDefaults } from '../config';

beforeEach(() => {
resetToDefaults();
});

test('getConfig() returns existing configuration', () => {
expect(getConfig().asyncUtilTimeout).toEqual(1000);
});

test('configure() overrides existing config values', () => {
configure({ asyncUtilTimeout: 5000 });
expect(getConfig().asyncUtilTimeout).toEqual(5000);
});

test('resetToDefaults() resets config to defaults', () => {
configure({ asyncUtilTimeout: 5000 });
expect(getConfig().asyncUtilTimeout).toEqual(5000);

resetToDefaults();
expect(getConfig().asyncUtilTimeout).toEqual(1000);
});
32 changes: 31 additions & 1 deletion src/__tests__/waitFor.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import { Text, TouchableOpacity, View, Pressable } from 'react-native';
import { fireEvent, render, waitFor } from '..';
import { fireEvent, render, waitFor, configure, resetToDefaults } from '..';

class Banana extends React.Component<any> {
changeFresh = () => {
Expand All @@ -19,6 +19,10 @@ class Banana extends React.Component<any> {
}
}

beforeEach(() => {
resetToDefaults();
});

class BananaContainer extends React.Component<{}, any> {
state = { fresh: false };

Expand Down Expand Up @@ -64,6 +68,32 @@ test('waits for element until timeout is met', async () => {
await waitFor(() => getByText('Fresh'));
});

test('waitFor defaults to asyncWaitTimeout config option', async () => {
configure({ asyncUtilTimeout: 100 });
const { getByText } = render(<BananaContainer />);

fireEvent.press(getByText('Change freshness!'));
await expect(waitFor(() => getByText('Fresh'))).rejects.toThrow();

// Async action ends after 300ms and we only waited 100ms, so we need to wait
// for the remaining async actions to finish
await waitFor(() => getByText('Fresh'), { timeout: 1000 });
});

test('waitFor timeout option takes precendence over `asyncWaitTimeout` config option', async () => {
configure({ asyncUtilTimeout: 2000 });
const { getByText } = render(<BananaContainer />);

fireEvent.press(getByText('Change freshness!'));
await expect(
waitFor(() => getByText('Fresh'), { timeout: 100 })
).rejects.toThrow();

// Async action ends after 300ms and we only waited 100ms, so we need to wait
// for the remaining async actions to finish
await waitFor(() => getByText('Fresh'));
});

test('waits for element with custom interval', async () => {
const mockFn = jest.fn(() => {
throw Error('test');
Expand Down
27 changes: 27 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export type Config = {
/** Default timeout, in ms, for `waitFor` and `findBy*` queries. */
asyncUtilTimeout: number;
};

const defaultConfig: Config = {
asyncUtilTimeout: 1000,
};

let config = {
...defaultConfig,
};

export function configure(options: Partial<Config>) {
config = {
...config,
...options,
};
}

export function resetToDefaults() {
config = defaultConfig;
}

export function getConfig() {
return config;
}
2 changes: 2 additions & 0 deletions src/pure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ export type {
RenderResult as RenderAPI,
} from './render';
export type { RenderHookOptions, RenderHookResult } from './renderHook';
export type { Config } from './config';

export { act };
export { cleanup };
export { configure, resetToDefaults } from './config';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is resetToDefaults exported from DTL? Couldn't find it on their docs

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No it's not, only configure is, but I think it's good to have that for testinging convenience. Wdyt?

export { fireEvent };
export { render };
export { waitFor };
Expand Down
4 changes: 2 additions & 2 deletions src/waitFor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* globals jest */
import act, { setReactActEnvironment, getIsReactActEnvironment } from './act';
import { getConfig } from './config';
import { ErrorWithStack, copyStackTrace } from './helpers/errors';
import {
setTimeout,
Expand All @@ -9,7 +10,6 @@ import {
} from './helpers/timers';
import { checkReactVersionAtLeast } from './react-versions';

const DEFAULT_TIMEOUT = 1000;
const DEFAULT_INTERVAL = 50;

export type WaitForOptions = {
Expand All @@ -22,7 +22,7 @@ export type WaitForOptions = {
function waitForInternal<T>(
expectation: () => T,
{
timeout = DEFAULT_TIMEOUT,
timeout = getConfig().asyncUtilTimeout,
interval = DEFAULT_INTERVAL,
stackTraceError,
onTimeout,
Expand Down
7 changes: 7 additions & 0 deletions typings/index.flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,13 @@ declare module '@testing-library/react-native' {

declare export var waitForElementToBeRemoved: WaitForElementToBeRemovedFunction;

declare interface Config {
asyncUtilTimeout: number;
}

declare export var configure: (options: $Shape<Config>) => void;
declare export var resetToDefaults: () => void;

declare export var act: (callback: () => void) => Thenable;
declare export var within: (instance: ReactTestInstance) => Queries;
declare export var getQueriesForElement: (
Expand Down
28 changes: 28 additions & 0 deletions website/docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ title: API
- [Examples](#examples)
- [With `initialProps`](#with-initialprops)
- [With `wrapper`](#with-wrapper)
- [Configuration](#configuration)
- [`configure`](#configure)
- [`asyncUtilTimeout` option](#asyncutiltimeout-option)
- [`resetToDefaults()`](#resettodefaults)
- [Accessibility](#accessibility)
- [`isInaccessible`](#isinaccessible)

Expand Down Expand Up @@ -714,6 +718,30 @@ it('should use context value', () => {
});
```


## Configuration

### `configure`

```ts
type Config = {
asyncUtilTimeout: number;
};

function configure(options: Partial<Config>) {}
```

#### `asyncUtilTimeout` option

Default timeout, in ms, for async helper functions (`waitFor`, `waitForElementToBeRemoved`) and `findBy*` queries. Defaults to 1000 ms.


### `resetToDefaults()`

```ts
function resetToDefaults() {}
```

## Accessibility

### `isInaccessible`
Expand Down