From 5c93709de876c2857a95b682a688a5f8d1a4be1f Mon Sep 17 00:00:00 2001 From: Gabriel <12038578+gabrielgrover@users.noreply.github.com> Date: Mon, 26 Jul 2021 15:06:50 -0700 Subject: [PATCH 01/16] feat: add Proxy trap for findAll method in ReactTestInstance --- src/render.js | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/src/render.js b/src/render.js index e6323d58d..6ac2a30df 100644 --- a/src/render.js +++ b/src/render.js @@ -13,6 +13,7 @@ import debugDeep from './helpers/debugDeep'; type Options = { wrapper?: React.ComponentType, createNodeMock?: (element: React.Element) => any, + respectAccessibilityProps?: boolean, }; type TestRendererOptions = { createNodeMock: (element: React.Element) => any, @@ -24,7 +25,7 @@ type TestRendererOptions = { */ export default function render( component: React.Element, - { wrapper: Wrapper, createNodeMock }: Options = {} + { wrapper: Wrapper, createNodeMock, respectAccessibilityProps }: Options = {} ): { ...FindByAPI, ...QueryByAPI, @@ -42,10 +43,11 @@ export default function render( const renderer = renderWithAct( wrap(component), - createNodeMock ? { createNodeMock } : undefined + createNodeMock ? { createNodeMock } : undefined, + // respectAccessibilityProps ); const update = updateWithAct(renderer, wrap); - const instance = renderer.root; + const instance = respectAccessibilityProps ? appendFindAllTrap(renderer) : renderer.root; const unmount = () => { act(() => { renderer.unmount(); @@ -70,7 +72,7 @@ export default function render( function renderWithAct( component: React.Element, - options?: TestRendererOptions + options?: TestRendererOptions, ): ReactTestRenderer { let renderer: ReactTestRenderer; @@ -107,3 +109,29 @@ function debug( debugImpl.shallow = (message) => debugShallow(instance, message); return debugImpl; } + +function appendFindAllTrap(renderer: ReactTestRenderer) { + return new Proxy(renderer.root, { + get(target, prop) { + const isFindAllProp = prop === "findAll"; + const newFindAll = (fn, node = target) => { + + if (node.props.accessibilityElementsHidden) { + return []; + } + + const initial = fn(node) ? [node] : []; + + return node.children.reduce((result, child) => { + if (typeof child === 'string') { + return result; + } + + return result.concat(newFindAll(fn, child)); + }, initial); + } + + return isFindAllProp ? newFindAll : Reflect.get(...arguments); + } + }); +} \ No newline at end of file From dcf7c146e00ef9c9e5ed09d7475f070ece9c5eda Mon Sep 17 00:00:00 2001 From: Gabriel <12038578+gabrielgrover@users.noreply.github.com> Date: Mon, 26 Jul 2021 17:01:58 -0700 Subject: [PATCH 02/16] test: add tests for new render option respectAccessibilityProps --- src/__tests__/byDisplayValue.test.js | 32 ++++++++++++ src/__tests__/byPlaceholderText.test.js | 32 ++++++++++++ src/__tests__/byTestId.test.js | 33 ++++++++++++ src/__tests__/byText.test.js | 67 +++++++++++++++++++++++++ src/render.js | 17 ++++--- 5 files changed, 173 insertions(+), 8 deletions(-) diff --git a/src/__tests__/byDisplayValue.test.js b/src/__tests__/byDisplayValue.test.js index 4bdc743d9..8fc472645 100644 --- a/src/__tests__/byDisplayValue.test.js +++ b/src/__tests__/byDisplayValue.test.js @@ -1,6 +1,7 @@ // @flow import * as React from 'react'; import { View, TextInput } from 'react-native'; +import { isExportDeclaration } from 'typescript'; import { render } from '..'; @@ -100,3 +101,34 @@ test('findBy queries work asynchronously', async () => { await expect(findByDisplayValue('Display Value')).resolves.toBeTruthy(); await expect(findAllByDisplayValue('Display Value')).resolves.toHaveLength(1); }, 20000); + +test('all queries should respect accessibility', async () => { + const Comp = () => ( + + + + + + + ); + + const { + findAllByDisplayValue, + findByDisplayValue, + getAllByDisplayValue, + queryAllByDisplayValue, + getByDisplayValue, + queryByDisplayValue + } = render(, { respectAccessibilityProps: true }); + + await expect(findAllByDisplayValue("test_01")).rejects.toBeTruthy(); + await expect(findByDisplayValue("test_01")).rejects.toBeTruthy(); + await expect(() => getAllByDisplayValue("test_01")).toThrow( + "Unable to find an element with displayValue: test_01" + ); + await expect(queryAllByDisplayValue("test_01")).toHaveLength(0); + await expect(() => getByDisplayValue("test_01")).toThrow( + "Unable to find an element with displayValue: test_01" + ); + await expect(queryByDisplayValue("test_01")).toBeNull(); +}); \ No newline at end of file diff --git a/src/__tests__/byPlaceholderText.test.js b/src/__tests__/byPlaceholderText.test.js index 6e4ee1c00..3fc18cd23 100644 --- a/src/__tests__/byPlaceholderText.test.js +++ b/src/__tests__/byPlaceholderText.test.js @@ -59,3 +59,35 @@ test('getAllByPlaceholderText, queryAllByPlaceholderText', () => { expect(queryAllByPlaceholderText(/fresh/i)).toEqual(inputs); expect(queryAllByPlaceholderText('no placeholder')).toHaveLength(0); }); + +test("queries should respect accessibility", async () => { + const Comp = () => ( + + + + + + ); + + const { + findAllByPlaceholderText, + findByPlaceholderText, + getAllByPlaceholderText, + getByPlaceholderText, + queryAllByPlaceholderText, + queryByPlaceholderText + } = render(, { + respectAccessibilityProps: true, + }); + + await expect(findAllByPlaceholderText("hidden")).rejects.toBeTruthy(); + await expect(findByPlaceholderText("hidden")).rejects.toBeTruthy(); + await expect(() => getAllByPlaceholderText("hidden")).toThrow( + "Unable to find an element with placeholder: hidden" + ); + await expect(() => getByPlaceholderText("hidden")).toThrow( + "Unable to find an element with placeholder: hidden" + ); + await expect(queryAllByPlaceholderText("hidden")).toHaveLength(0); + await expect(queryByPlaceholderText("hidden")).toBeNull(); +}); \ No newline at end of file diff --git a/src/__tests__/byTestId.test.js b/src/__tests__/byTestId.test.js index 3f8363e5a..d26eacb66 100644 --- a/src/__tests__/byTestId.test.js +++ b/src/__tests__/byTestId.test.js @@ -133,3 +133,36 @@ test('findByTestId and findAllByTestId work asynchronously', async () => { await expect(findByTestId('aTestId')).resolves.toBeTruthy(); await expect(findAllByTestId('aTestId')).resolves.toHaveLength(1); }, 20000); + +test("queries should respect accessibility", async () => { + const Comp = () => ( + + + hello world + + hello space + + ); + + const { + findAllByTestId, + findByTestId, + getAllByTestId, + getByTestId, + queryAllByTestId, + queryByTestId + } = render(, { + respectAccessibilityProps: true, + }); + + await expect(findAllByTestId("test")).rejects.toBeTruthy(); + await expect(findByTestId("test")).rejects.toBeTruthy(); + await expect(() => getAllByTestId("test")).toThrow( + "Unable to find an element with testID: test" + ); + await expect(() => getByTestId("test")).toThrow( + "Unable to find an element with testID: test" + ); + await expect(queryAllByTestId("test")).toHaveLength(0); + await expect(queryByTestId("test")).toBeNull(); +}); \ No newline at end of file diff --git a/src/__tests__/byText.test.js b/src/__tests__/byText.test.js index 5b4ff36dd..5c8ef2f83 100644 --- a/src/__tests__/byText.test.js +++ b/src/__tests__/byText.test.js @@ -85,6 +85,73 @@ test('getAllByText, queryAllByText', () => { expect(queryAllByText('InExistent')).toHaveLength(0); }); +test('queries should respect accessibility', async () => { + const Comp = () => ( + + + hello world + + hello space + + ); + + const { + findAllByText, + findByText, + getAllByText, + getByText, + queryAllByText, + queryByText + } = render(, { + respectAccessibilityProps: true, + }); + + await expect(findAllByText("hello world")).rejects.toBeTruthy(); + await expect(findByText("hello world")).rejects.toBeTruthy(); + await expect(() => getAllByText("hello world")).toThrow( + "Unable to find an element with text: hello world" + ); + await expect(() => getByText("hello world")).toThrow( + "Unable to find an element with text: hello world" + ); + await expect(queryAllByText("hello world")).toHaveLength(0); + await expect(queryByText("hello world")).toBeNull(); +}); + +test('queryAllByText returns an empty array if respectAccessibilityProps is true', () => { + const Comp = () => ( + + + hello world + + hello space + + ); + const { queryAllByText } = render(, { + respectAccessibilityProps: true, + }); + + expect(queryAllByText(/hello world/i)).toHaveLength(0); +}); + +test('getAllByText throws if respectAccessibilityProps is true', () => { + const Comp = () => ( + + + hello world + + hello space + + ); + const { getAllByText } = render(, { + respectAccessibilityProps: true, + }); + + expect(() => getAllByText(/hello world/i)).toThrow( + 'Unable to find an element with text: /hello world/i' + ); +}); + test('findByText queries work asynchronously', async () => { const options = { timeout: 10 }; // Short timeout so that this test runs quickly const { rerender, findByText, findAllByText } = render(); diff --git a/src/render.js b/src/render.js index 6ac2a30df..eda313a4c 100644 --- a/src/render.js +++ b/src/render.js @@ -43,11 +43,13 @@ export default function render( const renderer = renderWithAct( wrap(component), - createNodeMock ? { createNodeMock } : undefined, + createNodeMock ? { createNodeMock } : undefined // respectAccessibilityProps ); const update = updateWithAct(renderer, wrap); - const instance = respectAccessibilityProps ? appendFindAllTrap(renderer) : renderer.root; + const instance = respectAccessibilityProps + ? appendFindAllTrap(renderer) + : renderer.root; const unmount = () => { act(() => { renderer.unmount(); @@ -72,7 +74,7 @@ export default function render( function renderWithAct( component: React.Element, - options?: TestRendererOptions, + options?: TestRendererOptions ): ReactTestRenderer { let renderer: ReactTestRenderer; @@ -113,9 +115,8 @@ function debug( function appendFindAllTrap(renderer: ReactTestRenderer) { return new Proxy(renderer.root, { get(target, prop) { - const isFindAllProp = prop === "findAll"; + const isFindAllProp = prop === 'findAll'; const newFindAll = (fn, node = target) => { - if (node.props.accessibilityElementsHidden) { return []; } @@ -129,9 +130,9 @@ function appendFindAllTrap(renderer: ReactTestRenderer) { return result.concat(newFindAll(fn, child)); }, initial); - } + }; return isFindAllProp ? newFindAll : Reflect.get(...arguments); - } + }, }); -} \ No newline at end of file +} From 521a81c7d43d2ec389399d5f425cf58d7afd9bca Mon Sep 17 00:00:00 2001 From: Gabriel <12038578+gabrielgrover@users.noreply.github.com> Date: Mon, 26 Jul 2021 17:24:35 -0700 Subject: [PATCH 03/16] feat: rename prop --- src/render.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/render.js b/src/render.js index eda313a4c..fcb2cd3fd 100644 --- a/src/render.js +++ b/src/render.js @@ -13,7 +13,7 @@ import debugDeep from './helpers/debugDeep'; type Options = { wrapper?: React.ComponentType, createNodeMock?: (element: React.Element) => any, - respectAccessibilityProps?: boolean, + respectAccessibility?: boolean, }; type TestRendererOptions = { createNodeMock: (element: React.Element) => any, @@ -25,7 +25,7 @@ type TestRendererOptions = { */ export default function render( component: React.Element, - { wrapper: Wrapper, createNodeMock, respectAccessibilityProps }: Options = {} + { wrapper: Wrapper, createNodeMock, respectAccessibility }: Options = {} ): { ...FindByAPI, ...QueryByAPI, @@ -47,7 +47,7 @@ export default function render( // respectAccessibilityProps ); const update = updateWithAct(renderer, wrap); - const instance = respectAccessibilityProps + const instance = respectAccessibility ? appendFindAllTrap(renderer) : renderer.root; const unmount = () => { From f166a871918867a8d6b6d18233117411b4e46031 Mon Sep 17 00:00:00 2001 From: Gabriel <12038578+gabrielgrover@users.noreply.github.com> Date: Mon, 26 Jul 2021 17:47:37 -0700 Subject: [PATCH 04/16] fix: remove comment --- src/render.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/render.js b/src/render.js index fcb2cd3fd..448b410d1 100644 --- a/src/render.js +++ b/src/render.js @@ -44,7 +44,6 @@ export default function render( const renderer = renderWithAct( wrap(component), createNodeMock ? { createNodeMock } : undefined - // respectAccessibilityProps ); const update = updateWithAct(renderer, wrap); const instance = respectAccessibility From b627428e1e95a472cdc0005b7ef4b39ffe0d24c0 Mon Sep 17 00:00:00 2001 From: Gabriel <12038578+gabrielgrover@users.noreply.github.com> Date: Mon, 26 Jul 2021 19:02:27 -0700 Subject: [PATCH 05/16] test: update tests to account for option property rename --- src/__tests__/byDisplayValue.test.js | 2 +- src/__tests__/byPlaceholderText.test.js | 2 +- src/__tests__/byTestId.test.js | 2 +- src/__tests__/byText.test.js | 6 +++--- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/__tests__/byDisplayValue.test.js b/src/__tests__/byDisplayValue.test.js index 8fc472645..7b13e1520 100644 --- a/src/__tests__/byDisplayValue.test.js +++ b/src/__tests__/byDisplayValue.test.js @@ -119,7 +119,7 @@ test('all queries should respect accessibility', async () => { queryAllByDisplayValue, getByDisplayValue, queryByDisplayValue - } = render(, { respectAccessibilityProps: true }); + } = render(, { respectAccessibility: true }); await expect(findAllByDisplayValue("test_01")).rejects.toBeTruthy(); await expect(findByDisplayValue("test_01")).rejects.toBeTruthy(); diff --git a/src/__tests__/byPlaceholderText.test.js b/src/__tests__/byPlaceholderText.test.js index 3fc18cd23..d026f2648 100644 --- a/src/__tests__/byPlaceholderText.test.js +++ b/src/__tests__/byPlaceholderText.test.js @@ -77,7 +77,7 @@ test("queries should respect accessibility", async () => { queryAllByPlaceholderText, queryByPlaceholderText } = render(, { - respectAccessibilityProps: true, + respectAccessibility: true, }); await expect(findAllByPlaceholderText("hidden")).rejects.toBeTruthy(); diff --git a/src/__tests__/byTestId.test.js b/src/__tests__/byTestId.test.js index d26eacb66..0c459cb60 100644 --- a/src/__tests__/byTestId.test.js +++ b/src/__tests__/byTestId.test.js @@ -152,7 +152,7 @@ test("queries should respect accessibility", async () => { queryAllByTestId, queryByTestId } = render(, { - respectAccessibilityProps: true, + respectAccessibility: true, }); await expect(findAllByTestId("test")).rejects.toBeTruthy(); diff --git a/src/__tests__/byText.test.js b/src/__tests__/byText.test.js index 5c8ef2f83..37637a88c 100644 --- a/src/__tests__/byText.test.js +++ b/src/__tests__/byText.test.js @@ -103,7 +103,7 @@ test('queries should respect accessibility', async () => { queryAllByText, queryByText } = render(, { - respectAccessibilityProps: true, + respectAccessibility: true, }); await expect(findAllByText("hello world")).rejects.toBeTruthy(); @@ -128,7 +128,7 @@ test('queryAllByText returns an empty array if respectAccessibilityProps is true ); const { queryAllByText } = render(, { - respectAccessibilityProps: true, + respectAccessibility: true, }); expect(queryAllByText(/hello world/i)).toHaveLength(0); @@ -144,7 +144,7 @@ test('getAllByText throws if respectAccessibilityProps is true', () => { ); const { getAllByText } = render(, { - respectAccessibilityProps: true, + respectAccessibility: true, }); expect(() => getAllByText(/hello world/i)).toThrow( From d33da47c66c4f99ed3bef9266bde4e0557b35893 Mon Sep 17 00:00:00 2001 From: Gabriel <12038578+gabrielgrover@users.noreply.github.com> Date: Mon, 26 Jul 2021 21:13:03 -0700 Subject: [PATCH 06/16] fix: lint errors --- src/__tests__/byDisplayValue.test.js | 21 ++++++++++----------- src/__tests__/byPlaceholderText.test.js | 24 ++++++++++++------------ src/__tests__/byTestId.test.js | 24 ++++++++++++------------ src/__tests__/byText.test.js | 20 ++++++++++---------- 4 files changed, 44 insertions(+), 45 deletions(-) diff --git a/src/__tests__/byDisplayValue.test.js b/src/__tests__/byDisplayValue.test.js index 7b13e1520..c89374446 100644 --- a/src/__tests__/byDisplayValue.test.js +++ b/src/__tests__/byDisplayValue.test.js @@ -1,7 +1,6 @@ // @flow import * as React from 'react'; import { View, TextInput } from 'react-native'; -import { isExportDeclaration } from 'typescript'; import { render } from '..'; @@ -118,17 +117,17 @@ test('all queries should respect accessibility', async () => { getAllByDisplayValue, queryAllByDisplayValue, getByDisplayValue, - queryByDisplayValue + queryByDisplayValue, } = render(, { respectAccessibility: true }); - await expect(findAllByDisplayValue("test_01")).rejects.toBeTruthy(); - await expect(findByDisplayValue("test_01")).rejects.toBeTruthy(); - await expect(() => getAllByDisplayValue("test_01")).toThrow( - "Unable to find an element with displayValue: test_01" + await expect(findAllByDisplayValue('test_01')).rejects.toBeTruthy(); + await expect(findByDisplayValue('test_01')).rejects.toBeTruthy(); + await expect(() => getAllByDisplayValue('test_01')).toThrow( + 'Unable to find an element with displayValue: test_01' ); - await expect(queryAllByDisplayValue("test_01")).toHaveLength(0); - await expect(() => getByDisplayValue("test_01")).toThrow( - "Unable to find an element with displayValue: test_01" + await expect(queryAllByDisplayValue('test_01')).toHaveLength(0); + await expect(() => getByDisplayValue('test_01')).toThrow( + 'Unable to find an element with displayValue: test_01' ); - await expect(queryByDisplayValue("test_01")).toBeNull(); -}); \ No newline at end of file + await expect(queryByDisplayValue('test_01')).toBeNull(); +}); diff --git a/src/__tests__/byPlaceholderText.test.js b/src/__tests__/byPlaceholderText.test.js index d026f2648..910a733c4 100644 --- a/src/__tests__/byPlaceholderText.test.js +++ b/src/__tests__/byPlaceholderText.test.js @@ -60,7 +60,7 @@ test('getAllByPlaceholderText, queryAllByPlaceholderText', () => { expect(queryAllByPlaceholderText('no placeholder')).toHaveLength(0); }); -test("queries should respect accessibility", async () => { +test('queries should respect accessibility', async () => { const Comp = () => ( @@ -69,25 +69,25 @@ test("queries should respect accessibility", async () => { ); - const { + const { findAllByPlaceholderText, findByPlaceholderText, getAllByPlaceholderText, getByPlaceholderText, queryAllByPlaceholderText, - queryByPlaceholderText + queryByPlaceholderText, } = render(, { respectAccessibility: true, }); - await expect(findAllByPlaceholderText("hidden")).rejects.toBeTruthy(); - await expect(findByPlaceholderText("hidden")).rejects.toBeTruthy(); - await expect(() => getAllByPlaceholderText("hidden")).toThrow( - "Unable to find an element with placeholder: hidden" + await expect(findAllByPlaceholderText('hidden')).rejects.toBeTruthy(); + await expect(findByPlaceholderText('hidden')).rejects.toBeTruthy(); + await expect(() => getAllByPlaceholderText('hidden')).toThrow( + 'Unable to find an element with placeholder: hidden' ); - await expect(() => getByPlaceholderText("hidden")).toThrow( - "Unable to find an element with placeholder: hidden" + await expect(() => getByPlaceholderText('hidden')).toThrow( + 'Unable to find an element with placeholder: hidden' ); - await expect(queryAllByPlaceholderText("hidden")).toHaveLength(0); - await expect(queryByPlaceholderText("hidden")).toBeNull(); -}); \ No newline at end of file + await expect(queryAllByPlaceholderText('hidden')).toHaveLength(0); + await expect(queryByPlaceholderText('hidden')).toBeNull(); +}); diff --git a/src/__tests__/byTestId.test.js b/src/__tests__/byTestId.test.js index 0c459cb60..2cc154bce 100644 --- a/src/__tests__/byTestId.test.js +++ b/src/__tests__/byTestId.test.js @@ -134,7 +134,7 @@ test('findByTestId and findAllByTestId work asynchronously', async () => { await expect(findAllByTestId('aTestId')).resolves.toHaveLength(1); }, 20000); -test("queries should respect accessibility", async () => { +test('queries should respect accessibility', async () => { const Comp = () => ( @@ -144,25 +144,25 @@ test("queries should respect accessibility", async () => { ); - const { + const { findAllByTestId, findByTestId, getAllByTestId, getByTestId, queryAllByTestId, - queryByTestId + queryByTestId, } = render(, { respectAccessibility: true, }); - await expect(findAllByTestId("test")).rejects.toBeTruthy(); - await expect(findByTestId("test")).rejects.toBeTruthy(); - await expect(() => getAllByTestId("test")).toThrow( - "Unable to find an element with testID: test" + await expect(findAllByTestId('test')).rejects.toBeTruthy(); + await expect(findByTestId('test')).rejects.toBeTruthy(); + await expect(() => getAllByTestId('test')).toThrow( + 'Unable to find an element with testID: test' ); - await expect(() => getByTestId("test")).toThrow( - "Unable to find an element with testID: test" + await expect(() => getByTestId('test')).toThrow( + 'Unable to find an element with testID: test' ); - await expect(queryAllByTestId("test")).toHaveLength(0); - await expect(queryByTestId("test")).toBeNull(); -}); \ No newline at end of file + await expect(queryAllByTestId('test')).toHaveLength(0); + await expect(queryByTestId('test')).toBeNull(); +}); diff --git a/src/__tests__/byText.test.js b/src/__tests__/byText.test.js index 37637a88c..49663a6c3 100644 --- a/src/__tests__/byText.test.js +++ b/src/__tests__/byText.test.js @@ -95,27 +95,27 @@ test('queries should respect accessibility', async () => { ); - const { + const { findAllByText, findByText, getAllByText, getByText, queryAllByText, - queryByText + queryByText, } = render(, { respectAccessibility: true, }); - await expect(findAllByText("hello world")).rejects.toBeTruthy(); - await expect(findByText("hello world")).rejects.toBeTruthy(); - await expect(() => getAllByText("hello world")).toThrow( - "Unable to find an element with text: hello world" + await expect(findAllByText('hello world')).rejects.toBeTruthy(); + await expect(findByText('hello world')).rejects.toBeTruthy(); + await expect(() => getAllByText('hello world')).toThrow( + 'Unable to find an element with text: hello world' ); - await expect(() => getByText("hello world")).toThrow( - "Unable to find an element with text: hello world" + await expect(() => getByText('hello world')).toThrow( + 'Unable to find an element with text: hello world' ); - await expect(queryAllByText("hello world")).toHaveLength(0); - await expect(queryByText("hello world")).toBeNull(); + await expect(queryAllByText('hello world')).toHaveLength(0); + await expect(queryByText('hello world')).toBeNull(); }); test('queryAllByText returns an empty array if respectAccessibilityProps is true', () => { From d74b3e10bbb5603b94de0838c508f8f6c3a32e6b Mon Sep 17 00:00:00 2001 From: Gabriel <12038578+gabrielgrover@users.noreply.github.com> Date: Tue, 10 Aug 2021 11:23:20 -0700 Subject: [PATCH 07/16] refactor: reimplement findAll trap to use parent traversal --- src/render.js | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/src/render.js b/src/render.js index 448b410d1..86aed0fcb 100644 --- a/src/render.js +++ b/src/render.js @@ -115,23 +115,22 @@ function appendFindAllTrap(renderer: ReactTestRenderer) { return new Proxy(renderer.root, { get(target, prop) { const isFindAllProp = prop === 'findAll'; - const newFindAll = (fn, node = target) => { - if (node.props.accessibilityElementsHidden) { - return []; - } - const initial = fn(node) ? [node] : []; + return isFindAllProp ? newFindAll(target) : Reflect.get(...arguments); + }, + }); +} - return node.children.reduce((result, child) => { - if (typeof child === 'string') { - return result; - } +function newFindAll(instance: ReactTestInstance) { + return (originalPredicate: (instance: ReactTestInstance) => boolean) => { + const elements = instance.findAll(originalPredicate); - return result.concat(newFindAll(fn, child)); - }, initial); - }; + return elements.filter(isReactTestElementVisibleToAccessibility); + } +} - return isFindAllProp ? newFindAll : Reflect.get(...arguments); - }, - }); +function isReactTestElementVisibleToAccessibility(instance: ReactTestInstance) { + const isElementVisible = !instance.props.accessibilityElementsHidden; + const isParentVisible = !instance.parent || isReactTestElementVisibleToAccessibility(instance.parent); + return isParentVisible && isElementVisible; } From 4647edbfb09c868e32d1dcb14201a42eda89de49 Mon Sep 17 00:00:00 2001 From: Gabriel <12038578+gabrielgrover@users.noreply.github.com> Date: Tue, 10 Aug 2021 13:28:50 -0700 Subject: [PATCH 08/16] refactor: update tests --- src/__tests__/byDisplayValue.test.js | 39 +++++++++++-- src/__tests__/byPlaceholderText.test.js | 49 ++++++++++++---- src/__tests__/byTestId.test.js | 47 ++++++++++++---- src/__tests__/byText.test.js | 75 +++++++++++-------------- 4 files changed, 143 insertions(+), 67 deletions(-) diff --git a/src/__tests__/byDisplayValue.test.js b/src/__tests__/byDisplayValue.test.js index c89374446..a937aa997 100644 --- a/src/__tests__/byDisplayValue.test.js +++ b/src/__tests__/byDisplayValue.test.js @@ -102,12 +102,23 @@ test('findBy queries work asynchronously', async () => { }, 20000); test('all queries should respect accessibility', async () => { - const Comp = () => ( - - + const Input = () => ( + + + + ); + const Comp = () => ( + + + + + + + + ); @@ -120,14 +131,30 @@ test('all queries should respect accessibility', async () => { queryByDisplayValue, } = render(, { respectAccessibility: true }); - await expect(findAllByDisplayValue('test_01')).rejects.toBeTruthy(); - await expect(findByDisplayValue('test_01')).rejects.toBeTruthy(); + await expect(getAllByDisplayValue("test_02")).toHaveLength(2); await expect(() => getAllByDisplayValue('test_01')).toThrow( 'Unable to find an element with displayValue: test_01' ); - await expect(queryAllByDisplayValue('test_01')).toHaveLength(0); + await expect(() => getByDisplayValue("test_02")).toThrow( + "Found multiple elements with display value: test_02 " + ); await expect(() => getByDisplayValue('test_01')).toThrow( 'Unable to find an element with displayValue: test_01' ); + await expect(queryAllByDisplayValue('test_01')).toHaveLength(0); + await expect(queryAllByDisplayValue("test_02")).toHaveLength(2); await expect(queryByDisplayValue('test_01')).toBeNull(); + await expect(() => queryByDisplayValue("test_02")).toThrow( + "Found multiple elements with display value: test_02 " + ); + await expect(findAllByDisplayValue('test_01')).rejects.toEqual( + new Error("Unable to find an element with displayValue: test_01") + ); + await expect(findAllByDisplayValue('test_02')).resolves.toHaveLength(2); + await expect(findByDisplayValue('test_01')).rejects.toEqual( + new Error("Unable to find an element with displayValue: test_01") + ); + await expect(findByDisplayValue('test_02')).rejects.toEqual( + new Error("Found multiple elements with display value: test_02 ") + ); }); diff --git a/src/__tests__/byPlaceholderText.test.js b/src/__tests__/byPlaceholderText.test.js index 910a733c4..447694013 100644 --- a/src/__tests__/byPlaceholderText.test.js +++ b/src/__tests__/byPlaceholderText.test.js @@ -2,6 +2,7 @@ import * as React from 'react'; import { View, TextInput } from 'react-native'; import { render } from '..'; +import { queryAllByDisplayValue } from '../helpers/byDisplayValue'; const PLACEHOLDER_FRESHNESS = 'Add custom freshness'; const PLACEHOLDER_CHEF = 'Who inspected freshness?'; @@ -61,10 +62,22 @@ test('getAllByPlaceholderText, queryAllByPlaceholderText', () => { }); test('queries should respect accessibility', async () => { + const Input = () => ( + + + + + + ); const Comp = () => ( - - + + + + + + + ); @@ -80,14 +93,30 @@ test('queries should respect accessibility', async () => { respectAccessibility: true, }); - await expect(findAllByPlaceholderText('hidden')).rejects.toBeTruthy(); - await expect(findByPlaceholderText('hidden')).rejects.toBeTruthy(); - await expect(() => getAllByPlaceholderText('hidden')).toThrow( - 'Unable to find an element with placeholder: hidden' + await expect(() => getAllByPlaceholderText('test_01')).toThrow( + 'Unable to find an element with placeholder: test_01' + ); + await expect(getAllByPlaceholderText("test_02")).toHaveLength(2); + await expect(() => getByPlaceholderText('test_01')).toThrow( + 'Unable to find an element with placeholder: test_01' + ); + await expect(() => getByPlaceholderText("test_02")).toThrow( + "Found multiple elements with placeholder: test_02 " + ) + await expect(queryAllByPlaceholderText('test_01')).toHaveLength(0); + await expect(queryAllByPlaceholderText('test_02')).toHaveLength(2); + await expect(queryByPlaceholderText('test_01')).toBeNull(); + await expect(() => queryByPlaceholderText('test_02')).toThrow( + "Found multiple elements with placeholder: test_02 " + ); + await expect(findAllByPlaceholderText('test_01')).rejects.toEqual( + new Error('Unable to find an element with placeholder: test_01') + ); + await expect(findAllByPlaceholderText('test_02')).resolves.toHaveLength(2); + await expect(findByPlaceholderText('test_01')).rejects.toEqual( + new Error('Unable to find an element with placeholder: test_01') ); - await expect(() => getByPlaceholderText('hidden')).toThrow( - 'Unable to find an element with placeholder: hidden' + await expect(findByPlaceholderText('test_02')).rejects.toEqual( + new Error("Found multiple elements with placeholder: test_02 ") ); - await expect(queryAllByPlaceholderText('hidden')).toHaveLength(0); - await expect(queryByPlaceholderText('hidden')).toBeNull(); }); diff --git a/src/__tests__/byTestId.test.js b/src/__tests__/byTestId.test.js index 2cc154bce..8a31b27bf 100644 --- a/src/__tests__/byTestId.test.js +++ b/src/__tests__/byTestId.test.js @@ -135,12 +135,23 @@ test('findByTestId and findAllByTestId work asynchronously', async () => { }, 20000); test('queries should respect accessibility', async () => { + const Input = () => ( + + + + + + ); const Comp = () => ( - - hello world + + + + + + + - hello space ); @@ -155,14 +166,30 @@ test('queries should respect accessibility', async () => { respectAccessibility: true, }); - await expect(findAllByTestId('test')).rejects.toBeTruthy(); - await expect(findByTestId('test')).rejects.toBeTruthy(); - await expect(() => getAllByTestId('test')).toThrow( + await expect(() => getAllByTestId('test_01')).toThrow( 'Unable to find an element with testID: test' ); - await expect(() => getByTestId('test')).toThrow( - 'Unable to find an element with testID: test' + await expect(getAllByTestId('test_02')).toHaveLength(2); + await expect(() => getByTestId('test_01')).toThrow( + 'Unable to find an element with testID: test_01' + ); + await expect(() => getByTestId('test_02')).toThrow( + "Found multiple elements with testID: test_02" + ) + await expect(queryAllByTestId('test_01')).toHaveLength(0); + await expect(queryAllByTestId('test_02')).toHaveLength(2); + await expect(queryByTestId('test_01')).toBeNull(); + await expect(() => queryByTestId('test_02')).toThrow( + "Found multiple elements with testID: test_02" + ); + await expect(findAllByTestId('test_01')).rejects.toEqual( + new Error('Unable to find an element with testID: test_01') + ); + await expect(findAllByTestId('test_02')).resolves.toHaveLength(2); + await expect(findByTestId('test_01')).rejects.toEqual( + new Error('Unable to find an element with testID: test_01') + ); + await expect(findByTestId('test_02')).rejects.toEqual( + new Error("Found multiple elements with testID: test_02") ); - await expect(queryAllByTestId('test')).toHaveLength(0); - await expect(queryByTestId('test')).toBeNull(); }); diff --git a/src/__tests__/byText.test.js b/src/__tests__/byText.test.js index 49663a6c3..3e0ef22a6 100644 --- a/src/__tests__/byText.test.js +++ b/src/__tests__/byText.test.js @@ -86,12 +86,23 @@ test('getAllByText, queryAllByText', () => { }); test('queries should respect accessibility', async () => { + const Input = () => ( + + + test_01 + + + ); const Comp = () => ( + + test_02 + + test_02 + - hello world + test_01 - hello space ); @@ -106,49 +117,31 @@ test('queries should respect accessibility', async () => { respectAccessibility: true, }); - await expect(findAllByText('hello world')).rejects.toBeTruthy(); - await expect(findByText('hello world')).rejects.toBeTruthy(); - await expect(() => getAllByText('hello world')).toThrow( - 'Unable to find an element with text: hello world' + await expect(() => getAllByText('test_01')).toThrow( + 'Unable to find an element with text: test_01' ); - await expect(() => getByText('hello world')).toThrow( - 'Unable to find an element with text: hello world' + await expect(getAllByText('test_02')).toHaveLength(2); + await expect(() => getByText('test_01')).toThrow( + 'Unable to find an element with text: test_01' ); - await expect(queryAllByText('hello world')).toHaveLength(0); - await expect(queryByText('hello world')).toBeNull(); -}); - -test('queryAllByText returns an empty array if respectAccessibilityProps is true', () => { - const Comp = () => ( - - - hello world - - hello space - + await expect(() => getByText('test_02')).toThrow( + "Found multiple elements with text: test_02" + ) + await expect(queryAllByText('test_01')).toHaveLength(0); + await expect(queryAllByText('test_02')).toHaveLength(2); + await expect(queryByText('test_01')).toBeNull(); + await expect(() => queryByText('test_02')).toThrow( + "Found multiple elements with text: test_02" + ) + await expect(findAllByText('test_01')).rejects.toEqual( + new Error('Unable to find an element with text: test_01') ); - const { queryAllByText } = render(, { - respectAccessibility: true, - }); - - expect(queryAllByText(/hello world/i)).toHaveLength(0); -}); - -test('getAllByText throws if respectAccessibilityProps is true', () => { - const Comp = () => ( - - - hello world - - hello space - + await expect(findAllByText('test_02')).resolves.toHaveLength(2); + await expect(findByText('test_01')).rejects.toEqual( + new Error('Unable to find an element with text: test_01') ); - const { getAllByText } = render(, { - respectAccessibility: true, - }); - - expect(() => getAllByText(/hello world/i)).toThrow( - 'Unable to find an element with text: /hello world/i' + await expect(findByText('test_02')).rejects.toEqual( + new Error("Found multiple elements with text: test_02") ); }); From 0013643d0ba9d4095bd02cd6081e0d2556d983ad Mon Sep 17 00:00:00 2001 From: Gabriel <12038578+gabrielgrover@users.noreply.github.com> Date: Tue, 10 Aug 2021 13:29:41 -0700 Subject: [PATCH 09/16] feat: update options type --- typings/index.d.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/typings/index.d.ts b/typings/index.d.ts index c0188727d..6683e347f 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -296,6 +296,7 @@ export interface Thenable { export interface RenderOptions { wrapper?: React.ComponentType; createNodeMock?: (element: React.ReactElement) => any; + respectAccessibility?: boolean; } type Debug = { From cf8c6cb352369a7314ebaf734d609112d324508f Mon Sep 17 00:00:00 2001 From: Gabriel <12038578+gabrielgrover@users.noreply.github.com> Date: Tue, 10 Aug 2021 13:34:36 -0700 Subject: [PATCH 10/16] refactor: update newFindAll with new typings --- src/render.js | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/render.js b/src/render.js index 86aed0fcb..b53498bbb 100644 --- a/src/render.js +++ b/src/render.js @@ -116,21 +116,29 @@ function appendFindAllTrap(renderer: ReactTestRenderer) { get(target, prop) { const isFindAllProp = prop === 'findAll'; - return isFindAllProp ? newFindAll(target) : Reflect.get(...arguments); + return isFindAllProp ? newFindAll(target) : target[prop]; }, }); } function newFindAll(instance: ReactTestInstance) { - return (originalPredicate: (instance: ReactTestInstance) => boolean) => { - const elements = instance.findAll(originalPredicate); + return (predicate: (instance: ReactTestInstance) => boolean): ReactTestInstance[] => { + const elements = instance.findAll(predicate); return elements.filter(isReactTestElementVisibleToAccessibility); } } -function isReactTestElementVisibleToAccessibility(instance: ReactTestInstance) { - const isElementVisible = !instance.props.accessibilityElementsHidden; - const isParentVisible = !instance.parent || isReactTestElementVisibleToAccessibility(instance.parent); +function isReactTestElementVisibleToAccessibility(instance: ReactTestInstance): boolean { + const isElementVisible = + !instance.props.accessibilityElementsHidden; + //&& instance.props.importantForAccessibility !== "no-hide-descendants"; + + if (!instance.parent) { + return isElementVisible; + } + + const isParentVisible = isReactTestElementVisibleToAccessibility(instance.parent); + return isParentVisible && isElementVisible; } From 1d8b401386ceb1d62555bd6647a383686673e708 Mon Sep 17 00:00:00 2001 From: Gabriel <12038578+gabrielgrover@users.noreply.github.com> Date: Tue, 10 Aug 2021 13:42:45 -0700 Subject: [PATCH 11/16] feat: respect accessibility prop, importantForAccessibility="no-hide-descendants" --- src/__tests__/byDisplayValue.test.js | 4 ++-- src/__tests__/byPlaceholderText.test.js | 4 ++-- src/__tests__/byTestId.test.js | 4 ++-- src/__tests__/byText.test.js | 4 ++-- src/render.js | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/__tests__/byDisplayValue.test.js b/src/__tests__/byDisplayValue.test.js index a937aa997..05c8133e2 100644 --- a/src/__tests__/byDisplayValue.test.js +++ b/src/__tests__/byDisplayValue.test.js @@ -102,7 +102,7 @@ test('findBy queries work asynchronously', async () => { }, 20000); test('all queries should respect accessibility', async () => { - const Input = () => ( + const OtherComp = () => ( @@ -111,7 +111,7 @@ test('all queries should respect accessibility', async () => { ); const Comp = () => ( - + diff --git a/src/__tests__/byPlaceholderText.test.js b/src/__tests__/byPlaceholderText.test.js index 447694013..9f5946c3f 100644 --- a/src/__tests__/byPlaceholderText.test.js +++ b/src/__tests__/byPlaceholderText.test.js @@ -62,7 +62,7 @@ test('getAllByPlaceholderText, queryAllByPlaceholderText', () => { }); test('queries should respect accessibility', async () => { - const Input = () => ( + const OtherComp = () => ( @@ -71,7 +71,7 @@ test('queries should respect accessibility', async () => { ); const Comp = () => ( - + diff --git a/src/__tests__/byTestId.test.js b/src/__tests__/byTestId.test.js index 8a31b27bf..cde58236f 100644 --- a/src/__tests__/byTestId.test.js +++ b/src/__tests__/byTestId.test.js @@ -135,7 +135,7 @@ test('findByTestId and findAllByTestId work asynchronously', async () => { }, 20000); test('queries should respect accessibility', async () => { - const Input = () => ( + const OtherComp = () => ( @@ -144,7 +144,7 @@ test('queries should respect accessibility', async () => { ); const Comp = () => ( - + diff --git a/src/__tests__/byText.test.js b/src/__tests__/byText.test.js index 3e0ef22a6..3f085fc41 100644 --- a/src/__tests__/byText.test.js +++ b/src/__tests__/byText.test.js @@ -86,7 +86,7 @@ test('getAllByText, queryAllByText', () => { }); test('queries should respect accessibility', async () => { - const Input = () => ( + const OtherComp = () => ( test_01 @@ -95,7 +95,7 @@ test('queries should respect accessibility', async () => { ); const Comp = () => ( - + test_02 test_02 diff --git a/src/render.js b/src/render.js index b53498bbb..5fbda3fdd 100644 --- a/src/render.js +++ b/src/render.js @@ -131,8 +131,8 @@ function newFindAll(instance: ReactTestInstance) { function isReactTestElementVisibleToAccessibility(instance: ReactTestInstance): boolean { const isElementVisible = - !instance.props.accessibilityElementsHidden; - //&& instance.props.importantForAccessibility !== "no-hide-descendants"; + !instance.props.accessibilityElementsHidden + && instance.props.importantForAccessibility !== "no-hide-descendants"; if (!instance.parent) { return isElementVisible; From 897a2b25ac206151e4e7d8120748f8a7cdb55863 Mon Sep 17 00:00:00 2001 From: Gabriel <12038578+gabrielgrover@users.noreply.github.com> Date: Tue, 10 Aug 2021 13:50:39 -0700 Subject: [PATCH 12/16] refactor: update test --- src/__tests__/byDisplayValue.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/__tests__/byDisplayValue.test.js b/src/__tests__/byDisplayValue.test.js index 05c8133e2..be0d6b551 100644 --- a/src/__tests__/byDisplayValue.test.js +++ b/src/__tests__/byDisplayValue.test.js @@ -111,7 +111,7 @@ test('all queries should respect accessibility', async () => { ); const Comp = () => ( - + From fd8ff4d4c89cce8b5cfa1cdc9cbd4074ca635386 Mon Sep 17 00:00:00 2001 From: Gabriel <12038578+gabrielgrover@users.noreply.github.com> Date: Tue, 10 Aug 2021 21:43:59 -0700 Subject: [PATCH 13/16] feat: respect accessibilityViewIsModal --- src/__tests__/byDisplayValue.test.js | 1 + src/__tests__/byPlaceholderText.test.js | 1 + src/__tests__/byTestId.test.js | 1 + src/__tests__/byText.test.js | 8 ++++--- src/render.js | 30 ++++++++++++++++++++++--- 5 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/__tests__/byDisplayValue.test.js b/src/__tests__/byDisplayValue.test.js index be0d6b551..91d5a98c4 100644 --- a/src/__tests__/byDisplayValue.test.js +++ b/src/__tests__/byDisplayValue.test.js @@ -112,6 +112,7 @@ test('all queries should respect accessibility', async () => { const Comp = () => ( + diff --git a/src/__tests__/byPlaceholderText.test.js b/src/__tests__/byPlaceholderText.test.js index 9f5946c3f..e6c5cafd4 100644 --- a/src/__tests__/byPlaceholderText.test.js +++ b/src/__tests__/byPlaceholderText.test.js @@ -72,6 +72,7 @@ test('queries should respect accessibility', async () => { const Comp = () => ( + diff --git a/src/__tests__/byTestId.test.js b/src/__tests__/byTestId.test.js index cde58236f..df36f9c01 100644 --- a/src/__tests__/byTestId.test.js +++ b/src/__tests__/byTestId.test.js @@ -145,6 +145,7 @@ test('queries should respect accessibility', async () => { const Comp = () => ( + diff --git a/src/__tests__/byText.test.js b/src/__tests__/byText.test.js index 3f085fc41..abf0d67ca 100644 --- a/src/__tests__/byText.test.js +++ b/src/__tests__/byText.test.js @@ -91,15 +91,17 @@ test('queries should respect accessibility', async () => { test_01 + + test_02 + ); const Comp = () => ( + + test_02 - - test_02 - test_01 diff --git a/src/render.js b/src/render.js index 5fbda3fdd..e1b6c44f5 100644 --- a/src/render.js +++ b/src/render.js @@ -130,9 +130,10 @@ function newFindAll(instance: ReactTestInstance) { } function isReactTestElementVisibleToAccessibility(instance: ReactTestInstance): boolean { - const isElementVisible = - !instance.props.accessibilityElementsHidden - && instance.props.importantForAccessibility !== "no-hide-descendants"; + const isElementVisible = + !accessibilityHiddenIOS(instance) && + !accessibilityHiddenAndroid(instance) && + !hiddenByStyles(instance); if (!instance.parent) { return isElementVisible; @@ -142,3 +143,26 @@ function isReactTestElementVisibleToAccessibility(instance: ReactTestInstance): return isParentVisible && isElementVisible; } + +function accessibilityHiddenIOS(instance: ReactTestInstance): boolean { + const siblingHasAccessibilityViewIsModal = + instance.parent && + instance + .parent + .children + .some(c => ( + typeof c !== "string" && + c.props.accessibilityViewIsModal && + !Object.is(c, instance) + )); + + return instance.props.accessibilityElementsHidden || siblingHasAccessibilityViewIsModal; +} + +function accessibilityHiddenAndroid(instance: ReactTestInstance) { + return instance.props.importantForAccessibility === 'no-hide-descendants'; +} + +function hiddenByStyles(instance: ReactTestInstance) { + return instance.props.style && instance.props.style.display === "none"; +} \ No newline at end of file From c574ee9b9e190ac69f802e386caca380a7a98e41 Mon Sep 17 00:00:00 2001 From: Gabriel <12038578+gabrielgrover@users.noreply.github.com> Date: Tue, 10 Aug 2021 21:49:31 -0700 Subject: [PATCH 14/16] test: update test --- src/__tests__/byDisplayValue.test.js | 5 ++++- src/__tests__/byPlaceholderText.test.js | 5 ++++- src/__tests__/byTestId.test.js | 5 ++++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/__tests__/byDisplayValue.test.js b/src/__tests__/byDisplayValue.test.js index 91d5a98c4..9528e9a51 100644 --- a/src/__tests__/byDisplayValue.test.js +++ b/src/__tests__/byDisplayValue.test.js @@ -107,13 +107,16 @@ test('all queries should respect accessibility', async () => { + + + ); const Comp = () => ( - + diff --git a/src/__tests__/byPlaceholderText.test.js b/src/__tests__/byPlaceholderText.test.js index e6c5cafd4..888512a8a 100644 --- a/src/__tests__/byPlaceholderText.test.js +++ b/src/__tests__/byPlaceholderText.test.js @@ -67,13 +67,16 @@ test('queries should respect accessibility', async () => { + + + ); const Comp = () => ( - + diff --git a/src/__tests__/byTestId.test.js b/src/__tests__/byTestId.test.js index df36f9c01..e9707308f 100644 --- a/src/__tests__/byTestId.test.js +++ b/src/__tests__/byTestId.test.js @@ -140,13 +140,16 @@ test('queries should respect accessibility', async () => { + + + ); const Comp = () => ( - + From b11c463e94bf09e28d866e518abf09cbce028e61 Mon Sep 17 00:00:00 2001 From: Gabriel <12038578+gabrielgrover@users.noreply.github.com> Date: Tue, 10 Aug 2021 21:53:04 -0700 Subject: [PATCH 15/16] refactor: change condition --- src/render.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/render.js b/src/render.js index e1b6c44f5..6c1678ca0 100644 --- a/src/render.js +++ b/src/render.js @@ -151,7 +151,7 @@ function accessibilityHiddenIOS(instance: ReactTestInstance): boolean { .parent .children .some(c => ( - typeof c !== "string" && + c.props && c.props.accessibilityViewIsModal && !Object.is(c, instance) )); From 5b5587f959641a59f2e692ef34eda17172fc852c Mon Sep 17 00:00:00 2001 From: Gabriel <12038578+gabrielgrover@users.noreply.github.com> Date: Tue, 10 Aug 2021 22:00:45 -0700 Subject: [PATCH 16/16] refactor: lint fix --- src/__tests__/byDisplayValue.test.js | 22 +++++++-------- src/__tests__/byPlaceholderText.test.js | 17 ++++++------ src/__tests__/byTestId.test.js | 12 ++++---- src/__tests__/byText.test.js | 14 +++++----- src/render.js | 37 ++++++++++++++----------- 5 files changed, 53 insertions(+), 49 deletions(-) diff --git a/src/__tests__/byDisplayValue.test.js b/src/__tests__/byDisplayValue.test.js index 9528e9a51..953a3157e 100644 --- a/src/__tests__/byDisplayValue.test.js +++ b/src/__tests__/byDisplayValue.test.js @@ -103,7 +103,7 @@ test('findBy queries work asynchronously', async () => { test('all queries should respect accessibility', async () => { const OtherComp = () => ( - + @@ -115,7 +115,7 @@ test('all queries should respect accessibility', async () => { const Comp = () => ( - + @@ -135,30 +135,30 @@ test('all queries should respect accessibility', async () => { queryByDisplayValue, } = render(, { respectAccessibility: true }); - await expect(getAllByDisplayValue("test_02")).toHaveLength(2); + await expect(getAllByDisplayValue('test_02')).toHaveLength(2); await expect(() => getAllByDisplayValue('test_01')).toThrow( 'Unable to find an element with displayValue: test_01' ); - await expect(() => getByDisplayValue("test_02")).toThrow( - "Found multiple elements with display value: test_02 " + await expect(() => getByDisplayValue('test_02')).toThrow( + 'Found multiple elements with display value: test_02 ' ); await expect(() => getByDisplayValue('test_01')).toThrow( 'Unable to find an element with displayValue: test_01' ); await expect(queryAllByDisplayValue('test_01')).toHaveLength(0); - await expect(queryAllByDisplayValue("test_02")).toHaveLength(2); + await expect(queryAllByDisplayValue('test_02')).toHaveLength(2); await expect(queryByDisplayValue('test_01')).toBeNull(); - await expect(() => queryByDisplayValue("test_02")).toThrow( - "Found multiple elements with display value: test_02 " + await expect(() => queryByDisplayValue('test_02')).toThrow( + 'Found multiple elements with display value: test_02 ' ); await expect(findAllByDisplayValue('test_01')).rejects.toEqual( - new Error("Unable to find an element with displayValue: test_01") + new Error('Unable to find an element with displayValue: test_01') ); await expect(findAllByDisplayValue('test_02')).resolves.toHaveLength(2); await expect(findByDisplayValue('test_01')).rejects.toEqual( - new Error("Unable to find an element with displayValue: test_01") + new Error('Unable to find an element with displayValue: test_01') ); await expect(findByDisplayValue('test_02')).rejects.toEqual( - new Error("Found multiple elements with display value: test_02 ") + new Error('Found multiple elements with display value: test_02 ') ); }); diff --git a/src/__tests__/byPlaceholderText.test.js b/src/__tests__/byPlaceholderText.test.js index 888512a8a..120320bad 100644 --- a/src/__tests__/byPlaceholderText.test.js +++ b/src/__tests__/byPlaceholderText.test.js @@ -2,7 +2,6 @@ import * as React from 'react'; import { View, TextInput } from 'react-native'; import { render } from '..'; -import { queryAllByDisplayValue } from '../helpers/byDisplayValue'; const PLACEHOLDER_FRESHNESS = 'Add custom freshness'; const PLACEHOLDER_CHEF = 'Who inspected freshness?'; @@ -63,7 +62,7 @@ test('getAllByPlaceholderText, queryAllByPlaceholderText', () => { test('queries should respect accessibility', async () => { const OtherComp = () => ( - + @@ -75,7 +74,7 @@ test('queries should respect accessibility', async () => { const Comp = () => ( - + @@ -100,18 +99,18 @@ test('queries should respect accessibility', async () => { await expect(() => getAllByPlaceholderText('test_01')).toThrow( 'Unable to find an element with placeholder: test_01' ); - await expect(getAllByPlaceholderText("test_02")).toHaveLength(2); + await expect(getAllByPlaceholderText('test_02')).toHaveLength(2); await expect(() => getByPlaceholderText('test_01')).toThrow( 'Unable to find an element with placeholder: test_01' ); - await expect(() => getByPlaceholderText("test_02")).toThrow( - "Found multiple elements with placeholder: test_02 " - ) + await expect(() => getByPlaceholderText('test_02')).toThrow( + 'Found multiple elements with placeholder: test_02 ' + ); await expect(queryAllByPlaceholderText('test_01')).toHaveLength(0); await expect(queryAllByPlaceholderText('test_02')).toHaveLength(2); await expect(queryByPlaceholderText('test_01')).toBeNull(); await expect(() => queryByPlaceholderText('test_02')).toThrow( - "Found multiple elements with placeholder: test_02 " + 'Found multiple elements with placeholder: test_02 ' ); await expect(findAllByPlaceholderText('test_01')).rejects.toEqual( new Error('Unable to find an element with placeholder: test_01') @@ -121,6 +120,6 @@ test('queries should respect accessibility', async () => { new Error('Unable to find an element with placeholder: test_01') ); await expect(findByPlaceholderText('test_02')).rejects.toEqual( - new Error("Found multiple elements with placeholder: test_02 ") + new Error('Found multiple elements with placeholder: test_02 ') ); }); diff --git a/src/__tests__/byTestId.test.js b/src/__tests__/byTestId.test.js index e9707308f..c5d913af5 100644 --- a/src/__tests__/byTestId.test.js +++ b/src/__tests__/byTestId.test.js @@ -136,7 +136,7 @@ test('findByTestId and findAllByTestId work asynchronously', async () => { test('queries should respect accessibility', async () => { const OtherComp = () => ( - + @@ -148,7 +148,7 @@ test('queries should respect accessibility', async () => { const Comp = () => ( - + @@ -178,13 +178,13 @@ test('queries should respect accessibility', async () => { 'Unable to find an element with testID: test_01' ); await expect(() => getByTestId('test_02')).toThrow( - "Found multiple elements with testID: test_02" - ) + 'Found multiple elements with testID: test_02' + ); await expect(queryAllByTestId('test_01')).toHaveLength(0); await expect(queryAllByTestId('test_02')).toHaveLength(2); await expect(queryByTestId('test_01')).toBeNull(); await expect(() => queryByTestId('test_02')).toThrow( - "Found multiple elements with testID: test_02" + 'Found multiple elements with testID: test_02' ); await expect(findAllByTestId('test_01')).rejects.toEqual( new Error('Unable to find an element with testID: test_01') @@ -194,6 +194,6 @@ test('queries should respect accessibility', async () => { new Error('Unable to find an element with testID: test_01') ); await expect(findByTestId('test_02')).rejects.toEqual( - new Error("Found multiple elements with testID: test_02") + new Error('Found multiple elements with testID: test_02') ); }); diff --git a/src/__tests__/byText.test.js b/src/__tests__/byText.test.js index abf0d67ca..1de4ae702 100644 --- a/src/__tests__/byText.test.js +++ b/src/__tests__/byText.test.js @@ -87,7 +87,7 @@ test('getAllByText, queryAllByText', () => { test('queries should respect accessibility', async () => { const OtherComp = () => ( - + test_01 @@ -99,7 +99,7 @@ test('queries should respect accessibility', async () => { const Comp = () => ( - + test_02 @@ -127,14 +127,14 @@ test('queries should respect accessibility', async () => { 'Unable to find an element with text: test_01' ); await expect(() => getByText('test_02')).toThrow( - "Found multiple elements with text: test_02" - ) + 'Found multiple elements with text: test_02' + ); await expect(queryAllByText('test_01')).toHaveLength(0); await expect(queryAllByText('test_02')).toHaveLength(2); await expect(queryByText('test_01')).toBeNull(); await expect(() => queryByText('test_02')).toThrow( - "Found multiple elements with text: test_02" - ) + 'Found multiple elements with text: test_02' + ); await expect(findAllByText('test_01')).rejects.toEqual( new Error('Unable to find an element with text: test_01') ); @@ -143,7 +143,7 @@ test('queries should respect accessibility', async () => { new Error('Unable to find an element with text: test_01') ); await expect(findByText('test_02')).rejects.toEqual( - new Error("Found multiple elements with text: test_02") + new Error('Found multiple elements with text: test_02') ); }); diff --git a/src/render.js b/src/render.js index 6c1678ca0..b8e4a4e8f 100644 --- a/src/render.js +++ b/src/render.js @@ -122,14 +122,18 @@ function appendFindAllTrap(renderer: ReactTestRenderer) { } function newFindAll(instance: ReactTestInstance) { - return (predicate: (instance: ReactTestInstance) => boolean): ReactTestInstance[] => { + return ( + predicate: (instance: ReactTestInstance) => boolean + ): ReactTestInstance[] => { const elements = instance.findAll(predicate); return elements.filter(isReactTestElementVisibleToAccessibility); - } + }; } -function isReactTestElementVisibleToAccessibility(instance: ReactTestInstance): boolean { +function isReactTestElementVisibleToAccessibility( + instance: ReactTestInstance +): boolean { const isElementVisible = !accessibilityHiddenIOS(instance) && !accessibilityHiddenAndroid(instance) && @@ -139,7 +143,9 @@ function isReactTestElementVisibleToAccessibility(instance: ReactTestInstance): return isElementVisible; } - const isParentVisible = isReactTestElementVisibleToAccessibility(instance.parent); + const isParentVisible = isReactTestElementVisibleToAccessibility( + instance.parent + ); return isParentVisible && isElementVisible; } @@ -147,16 +153,15 @@ function isReactTestElementVisibleToAccessibility(instance: ReactTestInstance): function accessibilityHiddenIOS(instance: ReactTestInstance): boolean { const siblingHasAccessibilityViewIsModal = instance.parent && - instance - .parent - .children - .some(c => ( - c.props && - c.props.accessibilityViewIsModal && - !Object.is(c, instance) - )); - - return instance.props.accessibilityElementsHidden || siblingHasAccessibilityViewIsModal; + instance.parent.children.some( + (c) => + c.props && c.props.accessibilityViewIsModal && !Object.is(c, instance) + ); + + return ( + instance.props.accessibilityElementsHidden || + siblingHasAccessibilityViewIsModal + ); } function accessibilityHiddenAndroid(instance: ReactTestInstance) { @@ -164,5 +169,5 @@ function accessibilityHiddenAndroid(instance: ReactTestInstance) { } function hiddenByStyles(instance: ReactTestInstance) { - return instance.props.style && instance.props.style.display === "none"; -} \ No newline at end of file + return instance.props.style && instance.props.style.display === 'none'; +}