Files
iNaturalistReactNative/tests/unit/components/UserText/UserText.test.js
Johannes Klein 14d0239468 308 accessibility tests (#393)
* Regroup accessibility label strings at end of strings file
* Add accessibility test to AddID
* Add a11y labels to AddID
* Add a11y test to StandardCamera
* Add a11y props to StandardCamera
* Remove unit test with only todos
* Add a11y test to Messages
* Refactor PhotoScroll test into own file
* Add a11y test to ObsDetails
* Add a11y test to ObsList
* Add a11y matcher to PhotoGallery test
* Add a11y matcher to ProjectDetails test
* Add a11y matcher to ProjectObservations test
* Add a11y matcher to Projects test
* Add a11y props to ProjectList
* Add a11y props to ProjectTabs
* Add a11y matcher to Search
* Add a11y matcher to UserProfile test
* Add a11y test matcher to UserText test
* Update react-native-accessibility-engine
* Add a11y matcher to Tabs test
* Add a11y label to a selectable photo
* Refactor DataTab tests into separate file
* Refactor ActivityTab test out into own file
* Added a test how to check if a component uses a mocked container
* Add wrong a11y props to TextInput left icon
* Enable a11y test with actual BottomSheet because mock does not pass a11y props down
* Add a11y default props to Button
2023-01-27 12:30:57 -08:00

138 lines
4.5 KiB
JavaScript

import { getDefaultNormalizer, render } from "@testing-library/react-native";
import UserText from "components/SharedComponents/UserText";
import { trim } from "lodash";
import React from "react";
describe( "Sanitization", () => {
it( "does not render HTML tags we don't support", () => {
const paragraphTagContent = "Welcome to iNaturalist";
const quoteTagContent = "quote tag";
const scriptTagContent = "javascript tag";
const testText = `<div>
<p>${paragraphTagContent}</p>
<p>They said <q>${quoteTagContent}</q></p>
<script>${scriptTagContent}</script>
</div>`;
const {
queryByText
} = render(
<UserText text={testText} />
);
expect( queryByText( paragraphTagContent ) ).toBeTruthy();
expect( queryByText( quoteTagContent ) ).toBeFalsy();
expect( queryByText( scriptTagContent ) ).toBeFalsy();
} );
it( "only allows the HTML attributes we support on the web", () => {
const pTagText = "Welcome to iNaturalist";
const altText = "Girl in a jacket";
const testText = `<div>
<p style="font-size:100px">${pTagText}</p>
<img src="img_girl.jpg" alt=${altText} width="500" height="600">
<p>fontSize</p>
</div>`;
const { queryByText, findByLabelText } = render(
<UserText text={testText} />
);
// alt text renders as accessibilityLabel
expect( findByLabelText( altText ) ).toBeTruthy();
// default font size is 14, check if no change
expect( queryByText( pTagText ) ).toHaveProperty( "props.style.0.fontSize", 14 );
} );
it( "links all @ mentions", () => {
const testText = "@anglantis";
const {
queryByText
} = render(
<UserText text={testText} />
);
expect( queryByText( testText ) ).toHaveProperty( "props.accessibilityRole", "link" );
} );
it( "links all URLs", () => {
const testText = "https://www.inaturalist.org";
const { getByRole, queryByText } = render(
<UserText text={testText} />
);
expect( getByRole( "link" ) ).toBeTruthy();
expect( queryByText( testText ) ).toBeTruthy();
} );
it( "closes unclosed tags", () => {
const testText = "<p>Welcome to iNat";
const { queryByText } = render(
<UserText text={testText} />
);
expect( queryByText( "Welcome to iNat" ) ).toBeTruthy();
} );
it( "strips leading and trailing whitespace", () => {
const testText = " This is a single line with a lloooooot of whitespace \n\n\n\n\n\n\n ";
const { queryByText } = render(
<UserText text={testText} />
);
// By default, the ByText queries will strip the text used to find
// elements, so since we want to test that UserText is performing that
// stripping, we need to tell the query methods not to do that.
// https://callstack.github.io/react-native-testing-library/docs/api-queries/#normalization
const normalizer = getDefaultNormalizer( { trim: false } );
expect( queryByText( trim( testText ), { normalizer } ) ).toBeTruthy();
expect( queryByText( testText, { normalizer } ) ).toBeFalsy();
} );
} );
describe( "Basic Rendering", () => {
it( "should not have accessibility errors", () => {
const testText = "foo bar baz";
const userText = <UserText text={testText} />;
expect( userText ).toBeAccessible();
} );
it( "renders text", () => {
const testText = "foo bar baz";
const { queryByText, getByText } = render(
<UserText text={testText} />
);
expect( getByText( testText ) ).toBeTruthy();
expect( queryByText( "asdfgh" ) ).toBeFalsy();
} );
it( "renders markdown", () => {
const testText = "# This is Heading 1";
const { queryByText } = render(
<UserText text={testText} />
);
expect( queryByText( testText ) ).toBeFalsy();
expect( queryByText( "This is Heading 1" ) )
.toHaveProperty( "props.style.0.fontWeight", "bold" );
} );
it( "renders html", () => {
const testText = "<p>Welcome to <b>iNaturalist</b></p>";
const { queryByText } = render(
<UserText text={testText} />
);
expect( queryByText( testText ) ).toBeFalsy();
expect( queryByText( "Welcome to" ) ).not.toHaveProperty( "props.style.0.fontWeight", "bold" );
expect( queryByText( "iNaturalist" ) ).toHaveProperty( "props.style.0.fontWeight", "bold" );
} );
// Cannot test table and list rendering, at least using this type of test.
// Cannot tell if tables and lists are rendered successfully from component hierachy.
// Possibly because of the components being rendered with an internal renderer in native module.
} );