Skip to content

Commit b2a79d4

Browse files
committed
configure
1 parent f8b703f commit b2a79d4

File tree

2 files changed

+64
-7
lines changed

2 files changed

+64
-7
lines changed

src/__tests__/config.test.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import { configure, getConfig, resetToDefaults } from '../config';
2+
import { _console } from '../helpers/logger';
23

34
beforeEach(() => {
45
resetToDefaults();
6+
jest.spyOn(_console, 'warn').mockImplementation(() => {});
7+
});
8+
9+
afterEach(() => {
10+
jest.restoreAllMocks();
511
});
612

713
test('getConfig() returns existing configuration', () => {
@@ -46,3 +52,45 @@ test('configure handles alias option defaultHidden', () => {
4652
configure({ defaultHidden: true });
4753
expect(getConfig().defaultIncludeHiddenElements).toEqual(true);
4854
});
55+
56+
test('does not warn when no options are passed', () => {
57+
configure({});
58+
59+
expect(_console.warn).not.toHaveBeenCalled();
60+
});
61+
62+
test('does not warn when only valid options are passed', () => {
63+
configure({
64+
asyncUtilTimeout: 2000,
65+
defaultIncludeHiddenElements: true,
66+
defaultDebugOptions: { message: 'test' },
67+
defaultHidden: false,
68+
});
69+
70+
expect(_console.warn).not.toHaveBeenCalled();
71+
});
72+
73+
test('warns when unknown option is passed', () => {
74+
configure({ unknownOption: 'value' } as any);
75+
76+
expect(_console.warn).toHaveBeenCalledTimes(1);
77+
const warningMessage = jest.mocked(_console.warn).mock.calls[0][0];
78+
expect(warningMessage).toContain('Unknown option(s) passed to configure: unknownOption');
79+
expect(warningMessage).toContain('config.test.ts');
80+
});
81+
82+
test('warns when multiple unknown options are passed', () => {
83+
configure({ asyncUtilTimeout: 1000, unknown1: 'value1', unknown2: 'value2' } as any);
84+
85+
expect(_console.warn).toHaveBeenCalledTimes(1);
86+
const warningMessage = jest.mocked(_console.warn).mock.calls[0][0];
87+
expect(warningMessage).toContain('Unknown option(s) passed to configure: unknown1, unknown2');
88+
expect(warningMessage).toContain('config.test.ts');
89+
});
90+
91+
test('still configures correctly when unknown options are passed', () => {
92+
configure({ asyncUtilTimeout: 3000, unknownOption: 'value' } as any);
93+
94+
expect(_console.warn).toHaveBeenCalledTimes(1);
95+
expect(getConfig().asyncUtilTimeout).toBe(3000);
96+
});

src/config.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { DebugOptions } from './helpers/debug';
2+
import { validateOptions } from './helpers/validate-options';
23

34
/**
45
* Global configuration options for React Native Testing Library.
@@ -31,17 +32,25 @@ let config = { ...defaultConfig };
3132
* Configure global options for React Native Testing Library.
3233
*/
3334
export function configure(options: Partial<Config & ConfigAliasOptions>) {
34-
const { defaultHidden, ...restOptions } = options;
35+
const {
36+
defaultHidden,
37+
asyncUtilTimeout,
38+
defaultIncludeHiddenElements,
39+
defaultDebugOptions,
40+
...rest
41+
} = options;
42+
43+
validateOptions('configure', rest, configure);
3544

36-
const defaultIncludeHiddenElements =
37-
restOptions.defaultIncludeHiddenElements ??
38-
defaultHidden ??
39-
config.defaultIncludeHiddenElements;
45+
const resolvedDefaultIncludeHiddenElements =
46+
defaultIncludeHiddenElements ?? defaultHidden ?? config.defaultIncludeHiddenElements;
4047

4148
config = {
4249
...config,
43-
...restOptions,
44-
defaultIncludeHiddenElements,
50+
...(asyncUtilTimeout !== undefined && { asyncUtilTimeout }),
51+
...(defaultIncludeHiddenElements !== undefined && { defaultIncludeHiddenElements }),
52+
...(defaultDebugOptions !== undefined && { defaultDebugOptions }),
53+
defaultIncludeHiddenElements: resolvedDefaultIncludeHiddenElements,
4554
};
4655
}
4756

0 commit comments

Comments
 (0)