Skip to content

Commit fa35bc9

Browse files
committed
refactor: pre-merge tweaks
1 parent c86b75c commit fa35bc9

17 files changed

+54
-46
lines changed

src/__tests__/config.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ test('resetToDefaults() resets config to defaults', () => {
2323
configure({
2424
asyncUtilTimeout: 5000,
2525
defaultIncludeHiddenElements: false,
26-
defaultHidden: false,
2726
});
2827
expect(getConfig().asyncUtilTimeout).toEqual(5000);
2928
expect(getConfig().defaultIncludeHiddenElements).toEqual(false);
@@ -35,6 +34,7 @@ test('resetToDefaults() resets config to defaults', () => {
3534

3635
test('configure handles alias option defaultHidden', () => {
3736
expect(getConfig().defaultIncludeHiddenElements).toEqual(true);
37+
3838
configure({ defaultHidden: false });
3939
expect(getConfig().defaultIncludeHiddenElements).toEqual(false);
4040
});

src/config.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,15 @@ export type Config = {
44
/** Default timeout, in ms, for `waitFor` and `findBy*` queries. */
55
asyncUtilTimeout: number;
66

7-
/** Default includeHiddenElements value for all queries */
7+
/** Default value for `includeHiddenElements` query option. */
88
defaultIncludeHiddenElements: boolean;
99

1010
/** Default options for `debug` helper. */
1111
defaultDebugOptions?: Partial<DebugOptions>;
1212
};
1313

1414
export type ConfigAliasOptions = {
15-
/** Default hidden value for all queries, alias to defaultIncludeHiddenElements to respect react-testing-library API
16-
* WARNING: if both defaultHidden and defaultIncludeHiddenElements values are defined,
17-
* then defaultIncludeHiddenElements will take precedence
18-
*/
15+
/** RTL-compatibility alias to `defaultIncludeHiddenElements` */
1916
defaultHidden: boolean;
2017
};
2118

@@ -34,14 +31,10 @@ export function configure(options: Partial<Config & ConfigAliasOptions>) {
3431
defaultHidden ??
3532
config.defaultIncludeHiddenElements;
3633

37-
const optionsToSet = {
38-
...restOptions,
39-
defaultIncludeHiddenElements,
40-
};
41-
4234
config = {
4335
...config,
44-
...optionsToSet,
36+
...restOptions,
37+
defaultIncludeHiddenElements,
4538
};
4639
}
4740

src/helpers/__tests__/includeHiddenElements.test.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
import { View } from 'react-native';
21
import React from 'react';
3-
4-
import render from '../../render';
5-
import { screen } from '../../screen';
6-
import { configure } from '../../config';
2+
import { View } from 'react-native';
3+
import { configure, render, screen } from '../..';
74

85
test('includeHiddenElements query option takes priority over hidden option and global config', () => {
96
configure({ defaultHidden: true, defaultIncludeHiddenElements: true });

src/helpers/findAll.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ export function findAll(
1515
) {
1616
const results = root.findAll(predicate);
1717

18-
const hidden =
18+
const includeHiddenElements =
1919
options?.includeHiddenElements ??
2020
options?.hidden ??
2121
getConfig()?.defaultIncludeHiddenElements;
2222

23-
if (hidden) {
23+
if (includeHiddenElements) {
2424
return results;
2525
}
2626

src/queries/__tests__/a11yState.test.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,5 +247,7 @@ test('byA11yState queries support hidden option', () => {
247247
).toBeFalsy();
248248
expect(() =>
249249
getByA11yState({ expanded: false }, { includeHiddenElements: false })
250-
).toThrow();
250+
).toThrowErrorMatchingInlineSnapshot(
251+
`"Unable to find an element with expanded state: false"`
252+
);
251253
});

src/queries/__tests__/a11yValue.test.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,5 +110,7 @@ test('byA11yValue queries support hidden option', () => {
110110
).toBeFalsy();
111111
expect(() =>
112112
getByA11yValue({ max: 10 }, { includeHiddenElements: false })
113-
).toThrow();
113+
).toThrowErrorMatchingInlineSnapshot(
114+
`"Unable to find an element with accessibilityValue: {"max":10}"`
115+
);
114116
});

src/queries/__tests__/displayValue.test.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,5 +115,7 @@ test('byDisplayValue queries support hidden option', () => {
115115
).toBeFalsy();
116116
expect(() =>
117117
getByDisplayValue('hidden', { includeHiddenElements: false })
118-
).toThrow();
118+
).toThrowErrorMatchingInlineSnapshot(
119+
`"Unable to find an element with displayValue: hidden"`
120+
);
119121
});

src/queries/__tests__/hintText.test.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,5 +122,7 @@ test('byHintText queries support hidden option', () => {
122122
).toBeFalsy();
123123
expect(() =>
124124
getByHintText('hidden', { includeHiddenElements: false })
125-
).toThrow();
125+
).toThrowErrorMatchingInlineSnapshot(
126+
`"Unable to find an element with accessibilityHint: hidden"`
127+
);
126128
});

src/queries/__tests__/labelText.test.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,5 +161,7 @@ test('byLabelText queries support hidden option', () => {
161161
).toBeFalsy();
162162
expect(() =>
163163
getByLabelText('hidden', { includeHiddenElements: false })
164-
).toThrow();
164+
).toThrowErrorMatchingInlineSnapshot(
165+
`"Unable to find an element with accessibilityLabel: hidden"`
166+
);
165167
});

src/queries/__tests__/placeholderText.test.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,7 @@ test('byPlaceholderText queries support hidden option', () => {
7474
).toBeFalsy();
7575
expect(() =>
7676
getByPlaceholderText('hidden', { includeHiddenElements: false })
77-
).toThrow();
77+
).toThrowErrorMatchingInlineSnapshot(
78+
`"Unable to find an element with placeholder: hidden"`
79+
);
7880
});

src/queries/__tests__/role.test.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -709,5 +709,9 @@ test('byRole queries support hidden option', () => {
709709
expect(getByRole('button', { includeHiddenElements: true })).toBeTruthy();
710710

711711
expect(queryByRole('button', { includeHiddenElements: false })).toBeFalsy();
712-
expect(() => getByRole('button', { includeHiddenElements: false })).toThrow();
712+
expect(() =>
713+
getByRole('button', { includeHiddenElements: false })
714+
).toThrowErrorMatchingInlineSnapshot(
715+
`"Unable to find an element with role: "button""`
716+
);
713717
});

src/queries/__tests__/testId.test.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,5 +146,7 @@ test('byTestId queries support hidden option', () => {
146146
expect(queryByTestId('hidden', { includeHiddenElements: false })).toBeFalsy();
147147
expect(() =>
148148
getByTestId('hidden', { includeHiddenElements: false })
149-
).toThrow();
149+
).toThrowErrorMatchingInlineSnapshot(
150+
`"Unable to find an element with testID: hidden"`
151+
);
150152
});

src/queries/__tests__/text.test.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,5 +478,7 @@ test('byText support hidden option', () => {
478478
expect(queryByText(/hidden/i, { includeHiddenElements: false })).toBeFalsy();
479479
expect(() =>
480480
getByText(/hidden/i, { includeHiddenElements: false })
481-
).toThrow();
481+
).toThrowErrorMatchingInlineSnapshot(
482+
`"Unable to find an element with text: /hidden/i"`
483+
);
482484
});

src/queries/options.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import { NormalizerFn } from '../matches';
22

3-
/**
4-
* hidden is an alias for includeHiddenElements that exists only to match react-testing-library API
5-
* if both hidden and includeHiddenElements values are defined, then includeHiddenElements will take precedence
6-
*/
73
export type CommonQueryOptions = {
8-
hidden?: boolean;
4+
/** Should query include elements hidden from accessibility. */
95
includeHiddenElements?: boolean;
6+
7+
/** RTL-compatibile alias to `includeHiddenElements`. */
8+
hidden?: boolean;
109
};
1110

1211
export type TextMatchOptions = {

typings/index.flow.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ type FindAllReturn = Promise<ReactTestInstance[]>;
1010

1111
type CommonQueryOptions = {
1212
includeHiddenElements?: boolean,
13-
/** Alias to `includeHiddenElements` for RTL compatibility */
1413
hidden?: boolean,
1514
};
1615
type TextMatch = string | RegExp;

website/docs/API.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -793,9 +793,11 @@ Default timeout, in ms, for async helper functions (`waitFor`, `waitForElementTo
793793

794794
#### `defaultIncludeHiddenElements` option
795795

796-
Default [includeHiddenElements](Queries.md#includehidden-option) query option for all queries. This default option will be overridden by the one you specify directly when using your query.
796+
Default value for [includeHiddenElements](Queries.md#includehidden-option) query option for all queries. Defaults to `true`, which means that queries will match [elements hidden from accessibility](#ishiddenfromaccessibility) by default.
797797

798-
There is an alias to this option named `defaultHidden` that exists only for compatibility with [react-testing-library](https://testing-library.com/docs/dom-testing-library/api-configuration/#defaulthidden).
798+
Currently this option is set to `true` which means that queries will also match hidden elements. This is done to avoid breaking changes. However, we plan to change the default behavior to exclude hidden elements in the next major release.
799+
800+
This option is also available as `defaultHidden` alias for compatibility with [React Testing Library](https://testing-library.com/docs/dom-testing-library/api-configuration/#defaulthidden).
799801

800802
#### `defaultDebugOptions` option
801803

website/docs/Queries.md

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -378,28 +378,26 @@ const element = screen.getByA11yValue({ min: 40 });
378378

379379
### `includeHiddenElements` option
380380

381-
All queries have the `includeHiddenElements` option which enables them to respect accessibility props on components when it is set to `false`. If you set `includeHiddenElements` to `true`, elements that are normally excluded from the accessibility tree are considered for the query as well. Currently `includeHiddenElements` option is set `true` by default, which means that elements includeHiddenElements from accessibility will be included by default. However, we plan to change the default value to `includeHiddenElements: false` in the next major release.
381+
All queries have the `includeHiddenElements` option which affects whether [elements hidden from accessibility](./API.md#ishiddenfromaccessibility) are matched by the query.
382382

383383
You can configure the default value with the [`configure` function](API.md#configure).
384384

385-
An element is considered to be hidden from accessibility based on [`isHiddenFromAccessibility()`](./API.md#ishiddenfromaccessibility) function.
386-
387-
There is an alias to this option named `hidden` that exists only for compatibility with [react-testing-library](https://testing-library.com/docs/dom-testing-library/api-configuration/#defaulthidden).
385+
This option is also available as `hidden` alias for compatibility with [React Testing Library](https://testing-library.com/docs/queries/byrole#hidden).
388386

389387
**Examples**
390388

391389
```tsx
392-
render(<Text style={{ display: 'none' }}>I am hidden from accessibility</Text>);
390+
render(<Text style={{ display: 'none' }}>Hidden from accessibility</Text>);
393391

394-
// Include hidden elements
392+
// Exclude hidden elements
395393
expect(
396-
screen.queryByText('I am hidden from accessibility', { includeHiddenElements: false })
394+
screen.queryByText('Hidden from accessibility', { includeHiddenElements: false })
397395
).toBeFalsy();
398396

399-
// Match hidden elements
400-
expect(screen.getByText('I am hidden from accessibility')).toBeTruthy(); // Defaults to includeHiddenElements: true for now
397+
// Include hidden elements
398+
expect(screen.getByText('Hidden from accessibility')).toBeTruthy();
401399
expect(
402-
screen.getByText('I am hidden from accessibility', { includeHiddenElements: true })
400+
screen.getByText('Hidden from accessibility', { includeHiddenElements: true })
403401
).toBeTruthy();
404402
```
405403

0 commit comments

Comments
 (0)