Files
iNaturalistReactNative/tests/unit/components/AddID/AddID.test.js
Amanda Bullington fa94ab5b42 ObsEdit - bottom sheets (#570)
* Add KebabMenu to shared components index

* Separate EvidenceList from PhotoCarousel component

* Remove unneeded props from PhotoCarousel

* Create notes bottom sheet

* Fix tests for delete obs sheet

* Add wild status sheet

* Show date from observations uploaded via website

* Move media viewer modal into evidence section

* Fix mock

* Move location fetching into its own hook

* Refactor ObsEdit header; move code into Header component

* Add discard changes sheet

* Styling updates and save changes button

* Add classes to buttons based on evidence/id missing

* Add imprecise location sheet

* Add jest.fn from provider to ObsEdit tests

* Remove fakeTimers from ObsEditWithoutProvider.test.js; minor cleanup

* Show discard obs sheet anytime a user tries to navigate back

* Fixes to bottom sheet backdrop, evidence, add discard changes sheet

* Switch to paper radio buttons instead of checkmarks

* Fix bottom sheet backdrop in android by wrapping with GestureHandlerRootView

* Update bottom sheet for add comment & notes

* Fix test

* Remove fake timer from ObsEdit test to get tests passing

* Force update github actions cache

---------

Co-authored-by: Ken-ichi Ueda <kenichi.ueda@gmail.com>
2023-04-20 17:02:19 -07:00

158 lines
4.8 KiB
JavaScript

import { BottomSheetModalProvider } from "@gorhom/bottom-sheet";
import { fireEvent, screen } from "@testing-library/react-native";
import AddID from "components/ObsEdit/AddID";
import initI18next from "i18n/initI18next";
import { t } from "i18next";
import inatjs from "inaturalistjs";
import INatPaperProvider from "providers/INatPaperProvider";
import React from "react";
import factory, { makeResponse } from "../../../factory";
import { renderComponent } from "../../../helpers/render";
// Mock inaturalistjs so we can make some fake responses
jest.mock( "inaturalistjs" );
jest.mock(
"components/SharedComponents/ViewWrapper",
() => function MockViewWrapper( props ) {
const MockName = "mock-view-no-footer";
return (
// eslint-disable-next-line react/jsx-props-no-spreading
<MockName {...props} testID={MockName}>
{props.children}
</MockName>
);
}
);
jest.mock(
"components/SharedComponents/Sheets/BottomSheetStandardBackdrop",
() => function MockBottomSheetStandardBackdrop( props ) {
const MockName = "mock-bottom-sheet-standard-backdrop";
return (
// eslint-disable-next-line react/jsx-props-no-spreading
<MockName {...props} testID={MockName}>
{props.children}
</MockName>
);
}
);
const mockTaxaList = [
factory( "RemoteTaxon" ),
factory( "RemoteTaxon" ),
factory( "RemoteTaxon" )
];
jest.mock( "sharedHooks/useAuthenticatedQuery", () => ( {
__esModule: true,
default: () => ( {
data: mockTaxaList
} )
} ) );
jest.mock( "react-native-vector-icons/MaterialIcons", () => {
const InnerReact = require( "react" );
class MaterialIcons extends InnerReact.Component {
static getImageSourceSync( _thing, _number, _color ) {
return { uri: "foo" };
}
render() {
// I have disabled the eslint rule here because it is about a mock and not the test
// eslint-disable-next-line testing-library/no-node-access
return InnerReact.createElement( "MaterialIcons", this.props, this.props.children );
}
}
return MaterialIcons;
} );
jest.mock( "@gorhom/bottom-sheet", () => {
const actualBottomSheet = jest.requireActual( "@gorhom/bottom-sheet" );
return {
...actualBottomSheet
};
} );
// react-native-paper's TextInput does a bunch of async stuff that's hard to
// control in a test, so we're just mocking it here.
jest.mock( "react-native-paper", () => {
const RealModule = jest.requireActual( "react-native-paper" );
const MockTextInput = props => {
const MockName = "mock-text-input";
// eslint-disable-next-line react/jsx-props-no-spreading
return <MockName {...props}>{props.children}</MockName>;
};
MockTextInput.Icon = RealModule.TextInput.Icon;
const MockedModule = {
...RealModule,
// eslint-disable-next-line react/jsx-props-no-spreading
// TextInput: props => <View {...props}>{props.children}</View>
TextInput: MockTextInput
};
return MockedModule;
} );
const mockRoute = { params: {} };
describe( "AddID", () => {
beforeAll( async () => {
await initI18next();
} );
test( "should not have accessibility errors", () => {
const addID = (
<BottomSheetModalProvider>
<INatPaperProvider>
<AddID route={mockRoute} />
</INatPaperProvider>
</BottomSheetModalProvider>
);
expect( addID ).toBeAccessible();
} );
it( "should render inside mocked container", () => {
renderComponent( <AddID route={mockRoute} /> );
expect( screen.getByTestId( "mock-view-no-footer" ) ).toBeTruthy();
} );
it( "show taxon search results", async () => {
inatjs.search.mockResolvedValue( makeResponse( mockTaxaList ) );
renderComponent( <AddID route={mockRoute} /> );
const input = screen.getByTestId( "SearchTaxon" );
const taxon = mockTaxaList[0];
fireEvent.changeText( input, "Some taxon" );
expect( await screen.findByTestId( `Search.taxa.${taxon.id}` ) ).toBeTruthy();
expect(
screen.getByTestId( `Search.taxa.${taxon.id}.photo` ).props.source
).toStrictEqual( { uri: taxon.default_photo.square_url } );
} );
it( "calls callback with a taxon that can be saved to Realm", async () => {
const mockCallback = jest.fn( ident => {
global.realm.write( () => {
global.realm.create( "Taxon", ident.taxon );
} );
} );
renderComponent(
<AddID
route={{
params: {
onIDAdded: mockCallback
}
}}
/>
);
const input = screen.getByTestId( "SearchTaxon" );
const taxon = mockTaxaList[0];
fireEvent.changeText( input, "Some taxon" );
expect( await screen.findByTestId( `Search.taxa.${taxon.id}` ) ).toBeTruthy();
const labelText = t( "Add-this-ID" );
const chooseButton = ( await screen.findAllByLabelText( labelText ) )[0];
fireEvent.press( chooseButton );
expect( mockCallback ).toHaveBeenCalled();
} );
} );