Skip to content

Commit c335c34

Browse files
committed
docs
1 parent 858416f commit c335c34

30 files changed

Lines changed: 178 additions & 169 deletions

skills/react-native-testing/references/api-reference-v14.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ Use when `userEvent` doesn't support the event or when triggering events on comp
326326

327327
```ts
328328
async function fireEvent(
329-
element: TestInstance,
329+
instance: TestInstance,
330330
eventName: string,
331331
...data: unknown[]
332332
): Promise<void>;
@@ -361,7 +361,7 @@ Available automatically with any `@testing-library/react-native` import. No setu
361361
| Matcher | Signature | Description |
362362
| --------------------- | ------------------------------------------------------------- | --------------------------- |
363363
| `toHaveTextContent()` | `(text: string \| RegExp, options?: { exact?, normalizer? })` | Text content match |
364-
| `toContainElement()` | `(element: TestInstance \| null)` | Contains child element |
364+
| `toContainElement()` | `(instance: TestInstance \| null)` | Contains child element |
365365
| `toBeEmptyElement()` || No children or text content |
366366

367367
### Element State
@@ -430,7 +430,7 @@ Waits until the queried element is removed. Element must be initially present.
430430
### `within`
431431

432432
```ts
433-
function within(element: TestInstance): Queries;
433+
function within(instance: TestInstance): Queries;
434434
```
435435

436436
Scoped queries on a subtree. Useful for querying within a single `FlatList` item or a specific screen.
@@ -533,7 +533,7 @@ Note: `concurrentRoot` option is removed (always on). `unstable_validateStringsR
533533
### `isHiddenFromAccessibility`
534534

535535
```ts
536-
function isHiddenFromAccessibility(element: TestInstance | null): boolean;
536+
function isHiddenFromAccessibility(instance: TestInstance | null): boolean;
537537
```
538538

539539
Also available as `isInaccessible()` alias.

src/__tests__/fire-event.test.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ describe('fireEvent.changeText', () => {
161161
const input = screen.getByTestId('input');
162162
await fireEvent.changeText(input, 'new text');
163163
expect(onChangeText).toHaveBeenCalledWith('new text');
164-
expect(nativeState.valueForElement.get(input)).toBe('new text');
164+
expect(nativeState.valueForInstance.get(input)).toBe('new text');
165165
});
166166

167167
test('does not fire on non-editable TextInput', async () => {
@@ -170,7 +170,7 @@ describe('fireEvent.changeText', () => {
170170
const input = screen.getByTestId('input');
171171
await fireEvent.changeText(input, 'new text');
172172
expect(onChangeText).not.toHaveBeenCalled();
173-
expect(nativeState.valueForElement.get(input)).toBeUndefined();
173+
expect(nativeState.valueForInstance.get(input)).toBeUndefined();
174174
});
175175
});
176176

@@ -286,7 +286,7 @@ describe('fireEvent.scroll', () => {
286286
const scrollView = screen.getByTestId('scroll');
287287
await fireEvent.scroll(scrollView, verticalScrollEvent);
288288
expect(onScroll.mock.calls[0][0]).toMatchObject(verticalScrollEvent);
289-
expect(nativeState.contentOffsetForElement.get(scrollView)).toEqual({ x: 0, y: 200 });
289+
expect(nativeState.contentOffsetForInstance.get(scrollView)).toEqual({ x: 0, y: 200 });
290290
});
291291

292292
test.each([
@@ -301,7 +301,7 @@ describe('fireEvent.scroll', () => {
301301
const scrollView = screen.getByTestId('scroll');
302302
await fireEvent(scrollView, eventName, verticalScrollEvent);
303303
expect(handler).toHaveBeenCalledWith(verticalScrollEvent);
304-
expect(nativeState.contentOffsetForElement.get(scrollView)).toEqual({ x: 0, y: 200 });
304+
expect(nativeState.contentOffsetForInstance.get(scrollView)).toEqual({ x: 0, y: 200 });
305305
});
306306

307307
test('without contentOffset scrolls to (0, 0)', async () => {
@@ -316,7 +316,7 @@ describe('fireEvent.scroll', () => {
316316
expect(onScroll.mock.calls[0][0]).toMatchObject({
317317
nativeEvent: { contentOffset: { x: 0, y: 0 } },
318318
});
319-
expect(nativeState.contentOffsetForElement.get(scrollView)).toEqual({ x: 0, y: 0 });
319+
expect(nativeState.contentOffsetForInstance.get(scrollView)).toEqual({ x: 0, y: 0 });
320320
});
321321

322322
test('with non-finite contentOffset values uses 0', async () => {
@@ -331,7 +331,7 @@ describe('fireEvent.scroll', () => {
331331
nativeEvent: { contentOffset: { y: Infinity } },
332332
});
333333
expect(onScroll).toHaveBeenCalled();
334-
expect(nativeState.contentOffsetForElement.get(scrollView)).toEqual({ x: 0, y: 0 });
334+
expect(nativeState.contentOffsetForInstance.get(scrollView)).toEqual({ x: 0, y: 0 });
335335
});
336336

337337
test('with horizontal scroll updates native state', async () => {
@@ -344,7 +344,7 @@ describe('fireEvent.scroll', () => {
344344
const scrollView = screen.getByTestId('scroll');
345345
await fireEvent.scroll(scrollView, horizontalScrollEvent);
346346
expect(onScroll.mock.calls[0][0]).toMatchObject(horizontalScrollEvent);
347-
expect(nativeState.contentOffsetForElement.get(scrollView)).toEqual({ x: 50, y: 0 });
347+
expect(nativeState.contentOffsetForInstance.get(scrollView)).toEqual({ x: 50, y: 0 });
348348
});
349349

350350
test('without contentOffset via fireEvent() does not update native state', async () => {
@@ -357,7 +357,7 @@ describe('fireEvent.scroll', () => {
357357
const scrollView = screen.getByTestId('scroll');
358358
await fireEvent(scrollView, 'scroll', { nativeEvent: {} });
359359
expect(onScroll).toHaveBeenCalled();
360-
expect(nativeState.contentOffsetForElement.get(scrollView)).toBeUndefined();
360+
expect(nativeState.contentOffsetForInstance.get(scrollView)).toBeUndefined();
361361
});
362362

363363
test('with non-finite x contentOffset value uses 0', async () => {
@@ -372,7 +372,7 @@ describe('fireEvent.scroll', () => {
372372
nativeEvent: { contentOffset: { x: Infinity } },
373373
});
374374
expect(onScroll).toHaveBeenCalled();
375-
expect(nativeState.contentOffsetForElement.get(scrollView)).toEqual({ x: 0, y: 0 });
375+
expect(nativeState.contentOffsetForInstance.get(scrollView)).toEqual({ x: 0, y: 0 });
376376
});
377377
});
378378

src/helpers/accessibility.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ export function computeAriaModal(instance: TestInstance): boolean | undefined {
150150
}
151151

152152
export function computeAriaLabel(instance: TestInstance): string | undefined {
153-
const labelElementId = instance.props['aria-labelledby'] ?? instance.props.accessibilityLabelledBy;
153+
const labelElementId =
154+
instance.props['aria-labelledby'] ?? instance.props.accessibilityLabelledBy;
154155
if (labelElementId) {
155156
const container = getContainerInstance(instance);
156157
const labelInstance = findAll(

src/helpers/matchers/match-accessibility-state.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ export interface AccessibilityStateMatcher {
2020
expanded?: boolean;
2121
}
2222

23-
export function matchAccessibilityState(instance: TestInstance, matcher: AccessibilityStateMatcher) {
23+
export function matchAccessibilityState(
24+
instance: TestInstance,
25+
matcher: AccessibilityStateMatcher,
26+
) {
2427
if (matcher.busy !== undefined && matcher.busy !== computeAriaBusy(instance)) {
2528
return false;
2629
}

src/matchers/to-be-busy.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@ import { computeAriaBusy } from '../helpers/accessibility';
66
import { formatElement } from '../helpers/format-element';
77
import { checkHostElement } from './utils';
88

9-
export function toBeBusy(this: jest.MatcherContext, element: TestInstance) {
10-
checkHostElement(element, toBeBusy, this);
9+
export function toBeBusy(this: jest.MatcherContext, instance: TestInstance) {
10+
checkHostElement(instance, toBeBusy, this);
1111

1212
return {
13-
pass: computeAriaBusy(element),
13+
pass: computeAriaBusy(instance),
1414
message: () => {
15-
const matcher = matcherHint(`${this.isNot ? '.not' : ''}.toBeBusy`, 'element', '');
15+
const matcher = matcherHint(`${this.isNot ? '.not' : ''}.toBeBusy`, 'instance', '');
1616
return [
1717
matcher,
1818
'',
19-
`Received element is ${this.isNot ? '' : 'not '}busy:`,
20-
redent(formatElement(element), 2),
19+
`Received instance is ${this.isNot ? '' : 'not '}busy:`,
20+
redent(formatElement(instance), 2),
2121
].join('\n');
2222
},
2323
};

src/matchers/to-be-checked.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,35 @@ import { formatElement } from '../helpers/format-element';
1313
import { isHostSwitch } from '../helpers/host-component-names';
1414
import { checkHostElement } from './utils';
1515

16-
export function toBeChecked(this: jest.MatcherContext, element: TestInstance) {
17-
checkHostElement(element, toBeChecked, this);
16+
export function toBeChecked(this: jest.MatcherContext, instance: TestInstance) {
17+
checkHostElement(instance, toBeChecked, this);
1818

19-
if (!isHostSwitch(element) && !isSupportedAccessibilityElement(element)) {
19+
if (!isHostSwitch(instance) && !isSupportedAccessibilityElement(instance)) {
2020
throw new ErrorWithStack(
2121
`toBeChecked() works only on host "Switch" elements or accessibility elements with "checkbox", "radio" or "switch" role.`,
2222
toBeChecked,
2323
);
2424
}
2525

2626
return {
27-
pass: computeAriaChecked(element) === true,
27+
pass: computeAriaChecked(instance) === true,
2828
message: () => {
2929
const is = this.isNot ? 'is' : 'is not';
3030
return [
31-
matcherHint(`${this.isNot ? '.not' : ''}.toBeChecked`, 'element', ''),
31+
matcherHint(`${this.isNot ? '.not' : ''}.toBeChecked`, 'instance', ''),
3232
'',
33-
`Received element ${is} checked:`,
34-
redent(formatElement(element), 2),
33+
`Received instance ${is} checked:`,
34+
redent(formatElement(instance), 2),
3535
].join('\n');
3636
},
3737
};
3838
}
3939

40-
function isSupportedAccessibilityElement(element: TestInstance) {
41-
if (!isAccessibilityElement(element)) {
40+
function isSupportedAccessibilityElement(instance: TestInstance) {
41+
if (!isAccessibilityElement(instance)) {
4242
return false;
4343
}
4444

45-
const role = getRole(element);
45+
const role = getRole(instance);
4646
return rolesSupportingCheckedState[role];
4747
}

src/matchers/to-be-disabled.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,46 @@ import { computeAriaDisabled } from '../helpers/accessibility';
66
import { formatElement } from '../helpers/format-element';
77
import { checkHostElement } from './utils';
88

9-
export function toBeDisabled(this: jest.MatcherContext, element: TestInstance) {
10-
checkHostElement(element, toBeDisabled, this);
9+
export function toBeDisabled(this: jest.MatcherContext, instance: TestInstance) {
10+
checkHostElement(instance, toBeDisabled, this);
1111

12-
const isDisabled = computeAriaDisabled(element) || isAncestorDisabled(element);
12+
const isDisabled = computeAriaDisabled(instance) || isAncestorDisabled(instance);
1313

1414
return {
1515
pass: isDisabled,
1616
message: () => {
1717
const is = this.isNot ? 'is' : 'is not';
1818
return [
19-
matcherHint(`${this.isNot ? '.not' : ''}.toBeDisabled`, 'element', ''),
19+
matcherHint(`${this.isNot ? '.not' : ''}.toBeDisabled`, 'instance', ''),
2020
'',
21-
`Received element ${is} disabled:`,
22-
redent(formatElement(element), 2),
21+
`Received instance ${is} disabled:`,
22+
redent(formatElement(instance), 2),
2323
].join('\n');
2424
},
2525
};
2626
}
2727

28-
export function toBeEnabled(this: jest.MatcherContext, element: TestInstance) {
29-
checkHostElement(element, toBeEnabled, this);
28+
export function toBeEnabled(this: jest.MatcherContext, instance: TestInstance) {
29+
checkHostElement(instance, toBeEnabled, this);
3030

31-
const isEnabled = !computeAriaDisabled(element) && !isAncestorDisabled(element);
31+
const isEnabled = !computeAriaDisabled(instance) && !isAncestorDisabled(instance);
3232

3333
return {
3434
pass: isEnabled,
3535
message: () => {
3636
const is = this.isNot ? 'is' : 'is not';
3737
return [
38-
matcherHint(`${this.isNot ? '.not' : ''}.toBeEnabled`, 'element', ''),
38+
matcherHint(`${this.isNot ? '.not' : ''}.toBeEnabled`, 'instance', ''),
3939
'',
40-
`Received element ${is} enabled:`,
41-
redent(formatElement(element), 2),
40+
`Received instance ${is} enabled:`,
41+
redent(formatElement(instance), 2),
4242
].join('\n');
4343
},
4444
};
4545
}
4646

47-
function isAncestorDisabled(element: TestInstance): boolean {
48-
const parent = element.parent;
47+
function isAncestorDisabled(instance: TestInstance): boolean {
48+
const parent = instance.parent;
4949
if (parent == null) {
5050
return false;
5151
}

src/matchers/to-be-empty-element.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ import { isTestInstance } from '../helpers/component-tree';
66
import { formatElementList } from '../helpers/format-element';
77
import { checkHostElement } from './utils';
88

9-
export function toBeEmptyElement(this: jest.MatcherContext, element: TestInstance) {
10-
checkHostElement(element, toBeEmptyElement, this);
9+
export function toBeEmptyElement(this: jest.MatcherContext, instance: TestInstance) {
10+
checkHostElement(instance, toBeEmptyElement, this);
1111

1212
// TODO check
13-
const children = element.children.filter((child) => isTestInstance(child));
13+
const children = instance.children.filter((child) => isTestInstance(child));
1414

1515
return {
1616
pass: children.length === 0,
1717
message: () => {
1818
return [
19-
matcherHint(`${this.isNot ? '.not' : ''}.toBeEmptyElement`, 'element', ''),
19+
matcherHint(`${this.isNot ? '.not' : ''}.toBeEmptyElement`, 'instance', ''),
2020
'',
2121
'Received:',
2222
`${RECEIVED_COLOR(redent(formatElementList(children), 2))}`,

src/matchers/to-be-expanded.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,35 @@ import { computeAriaExpanded } from '../helpers/accessibility';
66
import { formatElement } from '../helpers/format-element';
77
import { checkHostElement } from './utils';
88

9-
export function toBeExpanded(this: jest.MatcherContext, element: TestInstance) {
10-
checkHostElement(element, toBeExpanded, this);
9+
export function toBeExpanded(this: jest.MatcherContext, instance: TestInstance) {
10+
checkHostElement(instance, toBeExpanded, this);
1111

1212
return {
13-
pass: computeAriaExpanded(element) === true,
13+
pass: computeAriaExpanded(instance) === true,
1414
message: () => {
15-
const matcher = matcherHint(`${this.isNot ? '.not' : ''}.toBeExpanded`, 'element', '');
15+
const matcher = matcherHint(`${this.isNot ? '.not' : ''}.toBeExpanded`, 'instance', '');
1616
return [
1717
matcher,
1818
'',
19-
`Received element is ${this.isNot ? '' : 'not '}expanded:`,
20-
redent(formatElement(element), 2),
19+
`Received instance is ${this.isNot ? '' : 'not '}expanded:`,
20+
redent(formatElement(instance), 2),
2121
].join('\n');
2222
},
2323
};
2424
}
2525

26-
export function toBeCollapsed(this: jest.MatcherContext, element: TestInstance) {
27-
checkHostElement(element, toBeCollapsed, this);
26+
export function toBeCollapsed(this: jest.MatcherContext, instance: TestInstance) {
27+
checkHostElement(instance, toBeCollapsed, this);
2828

2929
return {
30-
pass: computeAriaExpanded(element) === false,
30+
pass: computeAriaExpanded(instance) === false,
3131
message: () => {
32-
const matcher = matcherHint(`${this.isNot ? '.not' : ''}.toBeCollapsed`, 'element', '');
32+
const matcher = matcherHint(`${this.isNot ? '.not' : ''}.toBeCollapsed`, 'instance', '');
3333
return [
3434
matcher,
3535
'',
36-
`Received element is ${this.isNot ? '' : 'not '}collapsed:`,
37-
redent(formatElement(element), 2),
36+
`Received instance is ${this.isNot ? '' : 'not '}collapsed:`,
37+
redent(formatElement(instance), 2),
3838
].join('\n');
3939
},
4040
};

src/matchers/to-be-on-the-screen.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,34 @@ import { matcherHint, RECEIVED_COLOR } from 'jest-matcher-utils';
22
import redent from 'redent';
33
import type { TestInstance } from 'test-renderer';
44

5-
import { getContainerElement } from '../helpers/component-tree';
5+
import { getContainerInstance } from '../helpers/component-tree';
66
import { formatElement } from '../helpers/format-element';
77
import { screen } from '../screen';
88
import { checkHostElement } from './utils';
99

10-
export function toBeOnTheScreen(this: jest.MatcherContext, element: TestInstance) {
11-
if (element !== null || !this.isNot) {
12-
checkHostElement(element, toBeOnTheScreen, this);
10+
export function toBeOnTheScreen(this: jest.MatcherContext, instance: TestInstance) {
11+
if (instance !== null || !this.isNot) {
12+
checkHostElement(instance, toBeOnTheScreen, this);
1313
}
1414

15-
const pass = element === null ? false : screen.container === getContainerElement(element);
15+
const pass = instance === null ? false : screen.container === getContainerInstance(instance);
1616

1717
const errorFound = () => {
18-
return `expected element tree not to contain element, but found\n${redent(
19-
formatElement(element),
18+
return `expected instance tree not to contain instance, but found\n${redent(
19+
formatElement(instance),
2020
2,
2121
)}`;
2222
};
2323

2424
const errorNotFound = () => {
25-
return `element could not be found in the element tree`;
25+
return `instance could not be found in the instance tree`;
2626
};
2727

2828
return {
2929
pass,
3030
message: () => {
3131
return [
32-
matcherHint(`${this.isNot ? '.not' : ''}.toBeOnTheScreen`, 'element', ''),
32+
matcherHint(`${this.isNot ? '.not' : ''}.toBeOnTheScreen`, 'instance', ''),
3333
'',
3434
RECEIVED_COLOR(this.isNot ? errorFound() : errorNotFound()),
3535
].join('\n');

0 commit comments

Comments
 (0)