diff --git a/src/helpers/accessiblity.ts b/src/helpers/accessiblity.ts index f3ac4de34..ee440bc37 100644 --- a/src/helpers/accessiblity.ts +++ b/src/helpers/accessiblity.ts @@ -176,13 +176,6 @@ export function getAccessibilityCheckedState( return ariaChecked ?? accessibilityState?.checked; } -export function getAccessibilitySelectedState( - element: ReactTestInstance -): NonNullable { - const { accessibilityState, 'aria-selected': ariaSelected } = element.props; - return ariaSelected ?? accessibilityState?.selected ?? false; -} - export function getAccessibilityValue( element: ReactTestInstance ): AccessibilityValue | undefined { @@ -212,3 +205,17 @@ export function getAccessibilityValue( text: ariaValueText ?? accessibilityValue?.text, }; } + +export function isElementBusy( + element: ReactTestInstance +): NonNullable { + const { accessibilityState, 'aria-busy': ariaBusy } = element.props; + return ariaBusy ?? accessibilityState?.busy ?? false; +} + +export function isElementSelected( + element: ReactTestInstance +): NonNullable { + const { accessibilityState, 'aria-selected': ariaSelected } = element.props; + return ariaSelected ?? accessibilityState?.selected ?? false; +} diff --git a/src/matchers/__tests__/to-be-busy.test.tsx b/src/matchers/__tests__/to-be-busy.test.tsx new file mode 100644 index 000000000..235558fba --- /dev/null +++ b/src/matchers/__tests__/to-be-busy.test.tsx @@ -0,0 +1,96 @@ +import * as React from 'react'; +import { View } from 'react-native'; +import { render, screen } from '../..'; +import '../extend-expect'; + +test('toBeBusy() basic case', () => { + render( + <> + + + + + + + ); + + expect(screen.getByTestId('busy')).toBeBusy(); + expect(screen.getByTestId('busy-aria')).toBeBusy(); + expect(screen.getByTestId('not-busy')).not.toBeBusy(); + expect(screen.getByTestId('not-busy-aria')).not.toBeBusy(); + expect(screen.getByTestId('default')).not.toBeBusy(); +}); + +test('toBeBusy() error messages', () => { + render( + <> + + + + + + + ); + + expect(() => expect(screen.getByTestId('busy')).not.toBeBusy()) + .toThrowErrorMatchingInlineSnapshot(` + "expect(element).not.toBeBusy() + + Received element is busy: + " + `); + + expect(() => expect(screen.getByTestId('busy-aria')).not.toBeBusy()) + .toThrowErrorMatchingInlineSnapshot(` + "expect(element).not.toBeBusy() + + Received element is busy: + " + `); + + expect(() => expect(screen.getByTestId('not-busy')).toBeBusy()) + .toThrowErrorMatchingInlineSnapshot(` + "expect(element).toBeBusy() + + Received element is not busy: + " + `); + + expect(() => expect(screen.getByTestId('not-busy-aria')).toBeBusy()) + .toThrowErrorMatchingInlineSnapshot(` + "expect(element).toBeBusy() + + Received element is not busy: + " + `); + + expect(() => expect(screen.getByTestId('default')).toBeBusy()) + .toThrowErrorMatchingInlineSnapshot(` + "expect(element).toBeBusy() + + Received element is not busy: + " + `); +}); diff --git a/src/matchers/__tests__/to-be-selected.test.tsx b/src/matchers/__tests__/to-be-selected.test.tsx index cbf1f46cc..ba68e4936 100644 --- a/src/matchers/__tests__/to-be-selected.test.tsx +++ b/src/matchers/__tests__/to-be-selected.test.tsx @@ -3,7 +3,7 @@ import { View } from 'react-native'; import { render, screen } from '../..'; import '../extend-expect'; -test('.toBeSelected() basic case', () => { +test('toBeSelected() basic case', () => { render( <> @@ -21,7 +21,7 @@ test('.toBeSelected() basic case', () => { expect(screen.getByTestId('default')).not.toBeSelected(); }); -test('.toBeSelected() error messages', () => { +test('toBeSelected() error messages', () => { render( <> diff --git a/src/matchers/extend-expect.d.ts b/src/matchers/extend-expect.d.ts index 6bc63beaf..acdb16d1d 100644 --- a/src/matchers/extend-expect.d.ts +++ b/src/matchers/extend-expect.d.ts @@ -4,6 +4,7 @@ export interface JestNativeMatchers { toBeOnTheScreen(): R; toBeChecked(): R; toBeDisabled(): R; + toBeBusy(): R; toBeEmptyElement(): R; toBeEnabled(): R; toBePartiallyChecked(): R; diff --git a/src/matchers/extend-expect.ts b/src/matchers/extend-expect.ts index 87988a4a6..730e59cbe 100644 --- a/src/matchers/extend-expect.ts +++ b/src/matchers/extend-expect.ts @@ -3,6 +3,7 @@ import { toBeOnTheScreen } from './to-be-on-the-screen'; import { toBeChecked } from './to-be-checked'; import { toBeDisabled, toBeEnabled } from './to-be-disabled'; +import { toBeBusy } from './to-be-busy'; import { toBeEmptyElement } from './to-be-empty-element'; import { toBePartiallyChecked } from './to-be-partially-checked'; import { toBeSelected } from './to-be-selected'; @@ -15,6 +16,7 @@ expect.extend({ toBeOnTheScreen, toBeChecked, toBeDisabled, + toBeBusy, toBeEmptyElement, toBeEnabled, toBePartiallyChecked, diff --git a/src/matchers/index.tsx b/src/matchers/index.tsx index 16f5a6d9a..49014cc2a 100644 --- a/src/matchers/index.tsx +++ b/src/matchers/index.tsx @@ -1,6 +1,7 @@ export { toBeOnTheScreen } from './to-be-on-the-screen'; export { toBeChecked } from './to-be-checked'; export { toBeDisabled, toBeEnabled } from './to-be-disabled'; +export { toBeBusy } from './to-be-busy'; export { toBeEmptyElement } from './to-be-empty-element'; export { toBePartiallyChecked } from './to-be-partially-checked'; export { toBeVisible } from './to-be-visible'; diff --git a/src/matchers/to-be-busy.tsx b/src/matchers/to-be-busy.tsx new file mode 100644 index 000000000..e8b31f554 --- /dev/null +++ b/src/matchers/to-be-busy.tsx @@ -0,0 +1,28 @@ +import { ReactTestInstance } from 'react-test-renderer'; +import { matcherHint } from 'jest-matcher-utils'; +import { isElementBusy } from '../helpers/accessiblity'; +import { checkHostElement, formatElement } from './utils'; + +export function toBeBusy( + this: jest.MatcherContext, + element: ReactTestInstance +) { + checkHostElement(element, toBeBusy, this); + + return { + pass: isElementBusy(element), + message: () => { + const matcher = matcherHint( + `${this.isNot ? '.not' : ''}.toBeBusy`, + 'element', + '' + ); + return [ + matcher, + '', + `Received element is ${this.isNot ? '' : 'not '}busy:`, + formatElement(element), + ].join('\n'); + }, + }; +} diff --git a/src/matchers/to-be-selected.ts b/src/matchers/to-be-selected.ts index 3e35c4dd0..46502cbe9 100644 --- a/src/matchers/to-be-selected.ts +++ b/src/matchers/to-be-selected.ts @@ -1,6 +1,6 @@ import { ReactTestInstance } from 'react-test-renderer'; import { matcherHint } from 'jest-matcher-utils'; -import { getAccessibilitySelectedState } from '../helpers/accessiblity'; +import { isElementSelected } from '../helpers/accessiblity'; import { checkHostElement, formatElement } from './utils'; export function toBeSelected( @@ -10,7 +10,7 @@ export function toBeSelected( checkHostElement(element, toBeSelected, this); return { - pass: getAccessibilitySelectedState(element), + pass: isElementSelected(element), message: () => { const is = this.isNot ? 'is' : 'is not'; return [