Files
iNaturalistReactNative/tests/unit/components/Observations/ObsCard.test.js
Amanda Bullington 5456758f27 Move hooks into sharedHooks folder; reorganize Observations and models/ (#194)
* Rearrange hooks and observation component code
* Rename models/ to realmModels/ and use babel aliases

Closes #155

Co-authored-by: Ken-ichi Ueda <kenichi.ueda@gmail.com>
2022-10-20 16:59:19 -07:00

66 lines
2.0 KiB
JavaScript

import { fireEvent, render } from "@testing-library/react-native";
import ObsCard from "components/Observations/ObsCard";
import { t } from "i18next";
import React from "react";
import factory from "../../../factory";
const testObservation = factory( "LocalObservation" );
const qualityGradeText = t( "RG" );
// this probably isn't the right approach, but it does allow the test to pass
jest.mock( "../../../../src/realmModels/index", ( ) => {
const originalModule = jest.requireActual( "../../../../src/realmModels/index" );
// Mock the default export and named export 'foo'
return {
__esModule: true,
...originalModule,
default: {
inMemory: true
}
};
} );
test( "renders text passed into observation card", ( ) => {
const { getByTestId, getByText } = render(
<ObsCard
item={testObservation}
/>
);
expect( getByTestId( `ObsList.obsCard.${testObservation.uuid}` ) ).toBeTruthy( );
expect( getByTestId( "ObsList.photo" ).props.source )
.toStrictEqual( { uri: testObservation.observationPhotos[0].photo.url } );
expect( getByText( testObservation.taxon.preferredCommonName ) ).toBeTruthy( );
expect( getByText( testObservation.placeGuess ) ).toBeTruthy( );
expect( getByText( testObservation.comments.length.toString( ) ) ).toBeTruthy( );
expect( getByText( testObservation.identifications.length.toString( ) ) ).toBeTruthy( );
expect( getByText( qualityGradeText ) ).toBeTruthy( );
} );
test( "navigates to ObsDetails on button press", ( ) => {
const fakeNavigation = {
navigate: jest.fn( )
};
const { getByTestId } = render(
<ObsCard
item={testObservation}
handlePress={( ) => fakeNavigation.navigate( "ObsDetails" )}
/>
);
const button = getByTestId( `ObsList.obsCard.${testObservation.uuid}` );
fireEvent.press( button );
expect( fakeNavigation.navigate ).toBeCalledWith( "ObsDetails" );
} );
test( "should not have accessibility errors", ( ) => {
const obsCard = <ObsCard item={testObservation} />;
expect( obsCard ).toBeAccessible( );
} );