Skip to content

feat: config option to allow breaking changes #1241

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
Dec 2, 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
19 changes: 18 additions & 1 deletion src/__tests__/config.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { getConfig, configure, resetToDefaults } from '../config';
import {
getConfig,
configure,
resetToDefaults,
configureInternal,
} from '../config';

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

test('getConfig() returns existing configuration', () => {
expect(getConfig().useBreakingChanges).toEqual(false);
expect(getConfig().asyncUtilTimeout).toEqual(1000);
expect(getConfig().defaultIncludeHiddenElements).toEqual(true);
});
Expand All @@ -16,6 +22,7 @@ test('configure() overrides existing config values', () => {
asyncUtilTimeout: 5000,
defaultDebugOptions: { message: 'debug message' },
defaultIncludeHiddenElements: true,
useBreakingChanges: false,
});
});

Expand All @@ -32,6 +39,16 @@ test('resetToDefaults() resets config to defaults', () => {
expect(getConfig().defaultIncludeHiddenElements).toEqual(true);
});

test('resetToDefaults() resets internal config to defaults', () => {
configureInternal({
useBreakingChanges: true,
});
expect(getConfig().useBreakingChanges).toEqual(true);

resetToDefaults();
expect(getConfig().useBreakingChanges).toEqual(false);
});

test('configure handles alias option defaultHidden', () => {
expect(getConfig().defaultIncludeHiddenElements).toEqual(true);

Expand Down
21 changes: 20 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { DebugOptions } from './helpers/debugDeep';

/**
* Global configuration options for React Native Testing Library.
*/
export type Config = {
/** Default timeout, in ms, for `waitFor` and `findBy*` queries. */
asyncUtilTimeout: number;
Expand All @@ -16,13 +19,22 @@ export type ConfigAliasOptions = {
defaultHidden: boolean;
};

const defaultConfig: Config = {
export type InternalConfig = Config & {
/** Whether to use breaking changes intended for next major version release. */
useBreakingChanges: boolean;
};

const defaultConfig: InternalConfig = {
useBreakingChanges: false,
asyncUtilTimeout: 1000,
defaultIncludeHiddenElements: true,
};

let config = { ...defaultConfig };

/**
* Configure global options for React Native Testing Library.
*/
export function configure(options: Partial<Config & ConfigAliasOptions>) {
const { defaultHidden, ...restOptions } = options;

Expand All @@ -38,6 +50,13 @@ export function configure(options: Partial<Config & ConfigAliasOptions>) {
};
}

export function configureInternal(option: Partial<InternalConfig>) {
config = {
...config,
...option,
};
}

export function resetToDefaults() {
config = { ...defaultConfig };
}
Expand Down